All checks were successful
check / check (push) Successful in 2m3s
Replace .golangci.yml with the canonical v2-schema config (default: all minus six disabled linters, lll 88, tests included) and bump every golangci-lint pin to v2.12.2: - Dockerfile: golangci/golangci-lint:v2.12.2-alpine (hash-pinned) - script/bootstrap: GOLANGCI_LINT_VERSION 2.12.2 with new linux-amd64/arm64 release-archive sha256 pins Fix all 747 findings the stricter config surfaces, with no behavior changes: t.Parallel() throughout the test suite, static sentinel errors and errors.Is comparisons, checked error returns, context propagation (contextcheck/noctx), 88-column wrapping, extracted constants and helpers for goconst/dupl/funlen/cyclop, exhaustive switch cases replicating existing defaults, and white-box test files renamed to *_internal_test.go for testpackage. Three nolint:tagliatelle directives preserve the existing snake_case JSON wire and on-disk metadata formats.
226 lines
4.3 KiB
Go
226 lines
4.3 KiB
Go
package allowlist_test
|
|
|
|
import (
|
|
"net/url"
|
|
"testing"
|
|
|
|
"sneak.berlin/go/pixa/internal/allowlist"
|
|
)
|
|
|
|
const (
|
|
testExactHost = "cdn.example.com"
|
|
testImageURL = "https://cdn.example.com/image.jpg"
|
|
testSuffix = ".example.com"
|
|
)
|
|
|
|
type isAllowedCase struct {
|
|
name string
|
|
patterns []string
|
|
testURL string
|
|
want bool
|
|
}
|
|
|
|
func runIsAllowedCases(t *testing.T, tests []isAllowedCase) {
|
|
t.Helper()
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
w := allowlist.New(tt.patterns)
|
|
|
|
var u *url.URL
|
|
|
|
if tt.testURL != "" {
|
|
parsed, err := url.Parse(tt.testURL)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse test URL: %v", err)
|
|
}
|
|
|
|
u = parsed
|
|
}
|
|
|
|
got := w.IsAllowed(u)
|
|
if got != tt.want {
|
|
t.Errorf("IsAllowed() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHostAllowList_IsAllowed_ExactMatch(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
runIsAllowedCases(t, []isAllowedCase{
|
|
{
|
|
name: "exact match",
|
|
patterns: []string{testExactHost},
|
|
testURL: testImageURL,
|
|
want: true,
|
|
},
|
|
{
|
|
name: "exact match case insensitive",
|
|
patterns: []string{"CDN.Example.COM"},
|
|
testURL: testImageURL,
|
|
want: true,
|
|
},
|
|
{
|
|
name: "exact match not found",
|
|
patterns: []string{testExactHost},
|
|
testURL: "https://other.example.com/image.jpg",
|
|
want: false,
|
|
},
|
|
{
|
|
name: "multiple patterns",
|
|
patterns: []string{testExactHost, ".images.org", "static.test.net"},
|
|
testURL: "https://photos.images.org/image.jpg",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "empty allow list",
|
|
patterns: []string{},
|
|
testURL: testImageURL,
|
|
want: false,
|
|
},
|
|
{
|
|
name: "nil url",
|
|
patterns: []string{testExactHost},
|
|
testURL: "",
|
|
want: false,
|
|
},
|
|
{
|
|
name: "url with port",
|
|
patterns: []string{testExactHost},
|
|
testURL: "https://cdn.example.com:443/image.jpg",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "whitespace in patterns",
|
|
patterns: []string{" cdn.example.com ", " .other.com "},
|
|
testURL: testImageURL,
|
|
want: true,
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestHostAllowList_IsAllowed_SuffixMatch(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
runIsAllowedCases(t, []isAllowedCase{
|
|
{
|
|
name: "suffix match",
|
|
patterns: []string{testSuffix},
|
|
testURL: testImageURL,
|
|
want: true,
|
|
},
|
|
{
|
|
name: "suffix match deep subdomain",
|
|
patterns: []string{testSuffix},
|
|
testURL: "https://cdn.images.example.com/image.jpg",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "suffix match apex domain",
|
|
patterns: []string{testSuffix},
|
|
testURL: "https://example.com/image.jpg",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "suffix match not found",
|
|
patterns: []string{testSuffix},
|
|
testURL: "https://notexample.com/image.jpg",
|
|
want: false,
|
|
},
|
|
{
|
|
name: "suffix match partial not allowed",
|
|
patterns: []string{testSuffix},
|
|
testURL: "https://fakeexample.com/image.jpg",
|
|
want: false,
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestHostAllowList_IsEmpty(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
patterns []string
|
|
want bool
|
|
}{
|
|
{
|
|
name: "empty",
|
|
patterns: []string{},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "nil",
|
|
patterns: nil,
|
|
want: true,
|
|
},
|
|
{
|
|
name: "whitespace only",
|
|
patterns: []string{" ", ""},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "has entries",
|
|
patterns: []string{"example.com"},
|
|
want: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
w := allowlist.New(tt.patterns)
|
|
if got := w.IsEmpty(); got != tt.want {
|
|
t.Errorf("IsEmpty() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHostAllowList_Count(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
patterns []string
|
|
want int
|
|
}{
|
|
{
|
|
name: "empty",
|
|
patterns: []string{},
|
|
want: 0,
|
|
},
|
|
{
|
|
name: "exact hosts only",
|
|
patterns: []string{"a.com", "b.com", "c.com"},
|
|
want: 3,
|
|
},
|
|
{
|
|
name: "suffix hosts only",
|
|
patterns: []string{".a.com", ".b.com"},
|
|
want: 2,
|
|
},
|
|
{
|
|
name: "mixed",
|
|
patterns: []string{"exact.com", ".suffix.com"},
|
|
want: 2,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
w := allowlist.New(tt.patterns)
|
|
if got := w.Count(); got != tt.want {
|
|
t.Errorf("Count() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|