Add and test IsSubDomain function
This commit is contained in:
parent
d89f439689
commit
63639e33f8
|
@ -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
|
||||
|
|
23
dns_test.go
23
dns_test.go
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue