make sure invalid hosts aren't compared

Comparing 2 invalid hosts would erroneously return equal, because they
would compare the invalid host string.
This commit is contained in:
James Bardin 2017-11-20 18:09:24 -05:00
parent 8091bd627d
commit 9034fdb050
2 changed files with 34 additions and 1 deletions

View File

@ -126,5 +126,15 @@ func (h *FriendlyHost) Equal(other *FriendlyHost) bool {
return false return false
} }
return h.Normalized() == other.Normalized() otherHost, err := svchost.ForComparison(other.Raw)
if err != nil {
return false
}
host, err := svchost.ForComparison(h.Raw)
if err != nil {
return false
}
return otherHost == host
} }

View File

@ -116,3 +116,26 @@ func TestFriendlyHost(t *testing.T) {
} }
} }
} }
func TestInvalidHostEquals(t *testing.T) {
invalid := NewFriendlyHost("NOT_A_HOST_NAME")
valid := PublicRegistryHost
// invalid hosts are not comparable
if invalid.Equal(invalid) {
t.Fatal("invalid host names are not comparable")
}
if valid.Equal(invalid) {
t.Fatalf("%q is not equal to %q", valid, invalid)
}
puny := NewFriendlyHost("xn--s-fka0wmm0zea7g8b.xn--o-8ta85a3b1dwcda1k.io")
display := NewFriendlyHost("ʎɹʇsıƃǝɹ.ɯɹoɟɐɹɹǝʇ.io")
// The pre-normalized host is not a valid source, and therefore not
// comparable to the display version.
if display.Equal(puny) {
t.Fatalf("invalid host %q should not be comparable", puny)
}
}