package server import ( "strings" "testing" ) func TestValidateDNSLabel(t *testing.T) { tests := []struct { name string label string wantErr string }{ {"valid short", "a", ""}, {"valid typical", "my-site", ""}, {"valid numeric", "123", ""}, {"valid max length", strings.Repeat("a", 63), ""}, {"too long", strings.Repeat("a", 64), "63-character limit"}, {"way too long", strings.Repeat("a", 200), "63-character limit"}, {"uppercase", "MySite", "lowercase letters"}, {"underscore", "my_site", "lowercase letters"}, {"starts with hyphen", "-mysite", "lowercase letters"}, {"ends with hyphen", "mysite-", "lowercase letters"}, {"empty", "", "lowercase letters"}, {"spaces", "my site", "lowercase letters"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := validateDNSLabel(tt.label) if tt.wantErr == "" { if got != "" { t.Errorf("validateDNSLabel(%q) = %q, want no error", tt.label, got) } } else { if got == "" { t.Errorf("validateDNSLabel(%q) = no error, want error containing %q", tt.label, tt.wantErr) } else if !strings.Contains(got, tt.wantErr) { t.Errorf("validateDNSLabel(%q) = %q, want error containing %q", tt.label, got, tt.wantErr) } } }) } } func TestValidateFQDN(t *testing.T) { tests := []struct { name string label string farmDomain string wantErr bool }{ {"typical", "mysite", "wiki.cafe", false}, {"max label with short domain", strings.Repeat("a", 63), "w.co", false}, // 63-char label + "." + 189-char domain = 253 chars exactly {"exactly 253", strings.Repeat("a", 63), strings.Repeat("b", 189), false}, // 63-char label + "." + 190-char domain = 254 chars {"exceeds 253", strings.Repeat("a", 63), strings.Repeat("b", 190), true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := validateFQDN(tt.label, tt.farmDomain) if tt.wantErr && got == "" { t.Errorf("validateFQDN(%q, %q) = no error, want error", tt.label, tt.farmDomain) } if !tt.wantErr && got != "" { t.Errorf("validateFQDN(%q, %q) = %q, want no error", tt.label, tt.farmDomain, got) } }) } }