Classify DNS names as apex domains or hostnames using the Public Suffix List (golang.org/x/net/publicsuffix). Correctly handles multi-level TLDs like .co.uk and .com.au.
85 lines
1.9 KiB
Go
85 lines
1.9 KiB
Go
package config
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestClassifyTarget(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expected string
|
|
wantErr bool
|
|
}{
|
|
{"apex .com", "example.com", "domain", false},
|
|
{"apex .org", "example.org", "domain", false},
|
|
{"apex .co.uk", "example.co.uk", "domain", false},
|
|
{"apex .com.au", "example.com.au", "domain", false},
|
|
{"subdomain www", "www.example.com", "hostname", false},
|
|
{"subdomain api", "api.example.com", "hostname", false},
|
|
{"deep subdomain", "a.b.c.example.com", "hostname", false},
|
|
{"subdomain .co.uk", "www.example.co.uk", "hostname", false},
|
|
{"trailing dot", "example.com.", "domain", false},
|
|
{"trailing dot sub", "www.example.com.", "hostname", false},
|
|
{"uppercase", "EXAMPLE.COM", "domain", false},
|
|
{"mixed case", "Www.Example.Com", "hostname", false},
|
|
{"public suffix", "co.uk", "", true},
|
|
{"tld only", "com", "", true},
|
|
{"empty", "", "", true},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
result, err := ClassifyTarget(tc.input)
|
|
if tc.wantErr {
|
|
assert.Error(t, err)
|
|
|
|
return
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
assert.Equal(t, tc.expected, result)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestClassifyTargets(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
targets := []string{
|
|
"example.com",
|
|
"www.example.com",
|
|
"api.example.com",
|
|
"example.co.uk",
|
|
"blog.example.co.uk",
|
|
}
|
|
|
|
domains, hostnames, err := classifyTargets(targets)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(
|
|
t,
|
|
[]string{"example.com", "example.co.uk"},
|
|
domains,
|
|
)
|
|
assert.Equal(
|
|
t,
|
|
[]string{"www.example.com", "api.example.com", "blog.example.co.uk"},
|
|
hostnames,
|
|
)
|
|
}
|
|
|
|
func TestClassifyTargets_RejectsPublicSuffix(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, _, err := classifyTargets([]string{"example.com", "co.uk"})
|
|
assert.Error(t, err)
|
|
}
|