Better trie implementations

This commit is contained in:
2025-10-08 20:57:13 +02:00
parent 5f0f4aa96b
commit 582163d4be
26 changed files with 2482 additions and 122 deletions

View File

@@ -0,0 +1,42 @@
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)
}
})
}
}