43 lines
1.5 KiB
Go
43 lines
1.5 KiB
Go
package dnstrie
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestIsValidDomainName(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
host string
|
|
expected bool
|
|
}{
|
|
{"Valid Hostname", "example.com", true},
|
|
{"Valid with Subdomain", "sub.domain.co.uk", true},
|
|
{"Valid with Hyphen", "my-host-name.org", true},
|
|
{"Valid with Digits", "app1.server2.net", true},
|
|
{"Valid FQDN", "example.com.", true},
|
|
{"Valid Single Label", "localhost", true},
|
|
{"Valid: Long but within limits", strings.Repeat("a", 63) + "." + strings.Repeat("b", 63) + ".com", true},
|
|
{"Invalid: Label Too Long", strings.Repeat("a", 64) + ".com", false},
|
|
{"Invalid: Total Length Too Long", strings.Repeat("a", 60) + "." + strings.Repeat("b", 60) + "." + strings.Repeat("c", 60) + "." + strings.Repeat("d", 60) + "." + strings.Repeat("e", 60) + ".com", false},
|
|
{"Invalid: Starts with Hyphen", "-invalid.com", false},
|
|
{"Invalid: Ends with Hyphen", "invalid-.com", false},
|
|
{"Invalid: Contains Underscore", "my_host.com", false},
|
|
{"Invalid: Contains Space", "my host.com", false},
|
|
{"Invalid: Double Dot", "example..com", false},
|
|
{"Invalid: Starts with Dot", ".example.com", false},
|
|
{"Invalid: Empty Label", "sub..domain.com", false},
|
|
{"Invalid: Just a Dot", ".", false},
|
|
{"Invalid: Empty String", "", false},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
got := isValidDomainName(test.host)
|
|
if got != test.expected {
|
|
t.Errorf("isValidDomainName(%q) = %v; want %v", test.host, got, test.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|