util.TrimDomainName() fails when origin doesn't end in dot (#559)

* Fix https://github.com/miekg/dns/issues/555 dnsutil.TrimDomainName tests fail

* Remove comment

* Clean up comments and code.

* Clean up comments, use dns.Fqdn() where we can, lint.
This commit is contained in:
Tom Limoncelli 2017-11-08 10:19:10 -08:00 committed by Miek Gieben
parent 388f6eea29
commit 7e7e8dcb22
3 changed files with 17 additions and 6 deletions

View File

@ -4,13 +4,17 @@ go:
- 1.9.x
- tip
env:
- TESTS="-race -v -bench=. -coverprofile=coverage.txt -covermode=atomic"
- TESTS="-race -v ./..."
before_install:
# don't use the miekg/dns when testing forks
- mkdir -p $GOPATH/src/github.com/miekg
- ln -s $TRAVIS_BUILD_DIR $GOPATH/src/github.com/miekg/ || true
script:
- go test -race -v -bench=. -coverprofile=coverage.txt -covermode=atomic
- go test $TESTS
after_success:
- bash <(curl -s https://codecov.io/bash)

View File

@ -20,7 +20,9 @@ import (
func AddOrigin(s, origin string) string {
// ("foo.", "origin.") -> "foo." (already a FQDN)
// ("foo", "origin.") -> "foo.origin."
// ("foo"), "origin" -> "foo.origin"
// ("foo", "origin") -> "foo.origin"
// ("foo", ".") -> "foo." (Same as dns.Fqdn())
// ("foo.", ".") -> "foo." (Same as dns.Fqdn())
// ("@", "origin.") -> "origin." (@ represents the apex (bare) domain)
// ("", "origin.") -> "origin." (not obvious)
// ("foo", "") -> "foo" (not obvious)
@ -34,9 +36,8 @@ func AddOrigin(s, origin string) string {
if s == "@" || len(s) == 0 {
return origin // Expand apex.
}
if origin == "." {
return s + origin // AddOrigin(s, ".") is an expensive way to add a ".".
return dns.Fqdn(s)
}
return s + "." + origin // The simple case.
@ -57,8 +58,12 @@ func TrimDomainName(s, origin string) string {
return strings.TrimSuffix(s, origin)
}
original := s
s = dns.Fqdn(s)
origin = dns.Fqdn(origin)
if !dns.IsSubDomain(origin, s) {
return s
return original
}
slabels := dns.Split(s)

View File

@ -10,6 +10,8 @@ func TestAddOrigin(t *testing.T) {
{"@", "example.com.", "example.com."},
{"foo", "example.com.", "foo.example.com."},
{"foo.", "example.com.", "foo."},
{"example.com", ".", "example.com."},
{"example.com.", ".", "example.com."},
// Oddball tests:
// In general origin should not be "" or "." but at least
// these tests verify we don't crash and will keep results
@ -26,7 +28,7 @@ func TestAddOrigin(t *testing.T) {
for _, test := range tests {
actual := AddOrigin(test.e1, test.e2)
if test.expected != actual {
t.Errorf("AddOrigin(%#v, %#v) expected %#v, go %#v\n", test.e1, test.e2, test.expected, actual)
t.Errorf("AddOrigin(%#v, %#v) expected %#v, got %#v\n", test.e1, test.e2, test.expected, actual)
}
}
}