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:
parent
388f6eea29
commit
7e7e8dcb22
|
@ -4,13 +4,17 @@ go:
|
||||||
- 1.9.x
|
- 1.9.x
|
||||||
- tip
|
- tip
|
||||||
|
|
||||||
|
env:
|
||||||
|
- TESTS="-race -v -bench=. -coverprofile=coverage.txt -covermode=atomic"
|
||||||
|
- TESTS="-race -v ./..."
|
||||||
|
|
||||||
before_install:
|
before_install:
|
||||||
# don't use the miekg/dns when testing forks
|
# don't use the miekg/dns when testing forks
|
||||||
- mkdir -p $GOPATH/src/github.com/miekg
|
- mkdir -p $GOPATH/src/github.com/miekg
|
||||||
- ln -s $TRAVIS_BUILD_DIR $GOPATH/src/github.com/miekg/ || true
|
- ln -s $TRAVIS_BUILD_DIR $GOPATH/src/github.com/miekg/ || true
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- go test -race -v -bench=. -coverprofile=coverage.txt -covermode=atomic
|
- go test $TESTS
|
||||||
|
|
||||||
after_success:
|
after_success:
|
||||||
- bash <(curl -s https://codecov.io/bash)
|
- bash <(curl -s https://codecov.io/bash)
|
||||||
|
|
|
@ -20,7 +20,9 @@ import (
|
||||||
func AddOrigin(s, origin string) string {
|
func AddOrigin(s, origin string) string {
|
||||||
// ("foo.", "origin.") -> "foo." (already a FQDN)
|
// ("foo.", "origin.") -> "foo." (already a FQDN)
|
||||||
// ("foo", "origin.") -> "foo.origin."
|
// ("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." (@ represents the apex (bare) domain)
|
||||||
// ("", "origin.") -> "origin." (not obvious)
|
// ("", "origin.") -> "origin." (not obvious)
|
||||||
// ("foo", "") -> "foo" (not obvious)
|
// ("foo", "") -> "foo" (not obvious)
|
||||||
|
@ -34,9 +36,8 @@ func AddOrigin(s, origin string) string {
|
||||||
if s == "@" || len(s) == 0 {
|
if s == "@" || len(s) == 0 {
|
||||||
return origin // Expand apex.
|
return origin // Expand apex.
|
||||||
}
|
}
|
||||||
|
|
||||||
if origin == "." {
|
if origin == "." {
|
||||||
return s + origin // AddOrigin(s, ".") is an expensive way to add a ".".
|
return dns.Fqdn(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s + "." + origin // The simple case.
|
return s + "." + origin // The simple case.
|
||||||
|
@ -57,8 +58,12 @@ func TrimDomainName(s, origin string) string {
|
||||||
return strings.TrimSuffix(s, origin)
|
return strings.TrimSuffix(s, origin)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
original := s
|
||||||
|
s = dns.Fqdn(s)
|
||||||
|
origin = dns.Fqdn(origin)
|
||||||
|
|
||||||
if !dns.IsSubDomain(origin, s) {
|
if !dns.IsSubDomain(origin, s) {
|
||||||
return s
|
return original
|
||||||
}
|
}
|
||||||
|
|
||||||
slabels := dns.Split(s)
|
slabels := dns.Split(s)
|
||||||
|
|
|
@ -10,6 +10,8 @@ func TestAddOrigin(t *testing.T) {
|
||||||
{"@", "example.com.", "example.com."},
|
{"@", "example.com.", "example.com."},
|
||||||
{"foo", "example.com.", "foo.example.com."},
|
{"foo", "example.com.", "foo.example.com."},
|
||||||
{"foo.", "example.com.", "foo."},
|
{"foo.", "example.com.", "foo."},
|
||||||
|
{"example.com", ".", "example.com."},
|
||||||
|
{"example.com.", ".", "example.com."},
|
||||||
// Oddball tests:
|
// Oddball tests:
|
||||||
// In general origin should not be "" or "." but at least
|
// In general origin should not be "" or "." but at least
|
||||||
// these tests verify we don't crash and will keep results
|
// these tests verify we don't crash and will keep results
|
||||||
|
@ -26,7 +28,7 @@ func TestAddOrigin(t *testing.T) {
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
actual := AddOrigin(test.e1, test.e2)
|
actual := AddOrigin(test.e1, test.e2)
|
||||||
if test.expected != actual {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue