Add and test IsSubDomain function

This commit is contained in:
Miek Gieben 2012-02-15 10:22:52 +01:00
parent d89f439689
commit 63639e33f8
2 changed files with 27 additions and 4 deletions

View File

@ -262,16 +262,16 @@ func IsDomainName(s string) (uint8, uint8, bool) { // copied from net package.
return labels, uint8(l - longer), ok
}
// IsSubDomain checks if child is indeed a child of parent.
// In the DNS this is called in bailiwick.
// IsSubDomain checks if child is indeed a child of the parent.
// In the DNS this is called "the subdomain is in bailiwick".
func IsSubDomain(parent, child string) bool {
// If the number of labels both domain name have
// in common equals the number of labels of parent,
// child is a subdomain of parent.
plabs := SplitLabels(parent)
clabs := SplitLabels(child)
if len(plabs) < len(clabs) {
// parent is smaller than child
if len(clabs) < len(plabs) {
// child is smaller than parent, reversed arguments?
return false
}
// Copied from CompareLabels to prevent another SplitLabels

View File

@ -78,3 +78,26 @@ func TestEDNS_RR(t *testing.T) {
edns.Option[0].Data = "lalalala"
//t..Logf("%v\n", edns)
}
func TestBailiwick(t *testing.T) {
yes := map[string]string{
"miek.nl": "ns.miek.nl",
".": "miek.nl",
}
for parent, child := range yes {
if !IsSubDomain(parent, child) {
t.Logf("%s should be child of %s\n", child, parent)
t.Fail()
}
}
no := map[string]string{
"www.miek.nl": "ns.miek.nl",
"miek.nl": ".",
}
for parent, child := range no {
if IsSubDomain(parent, child) {
t.Logf("%s should not be child of %s\n", child, parent)
t.Fail()
}
}
}