godns/resolver/sfx_tree_test.go
Tyler d8079551c9
Some checks failed
continuous-integration/drone/push Build is failing
Add testing, cleanup, rework suffix tree to use nameservers. Parse nameservers from yaml.
2021-04-15 01:04:58 -04:00

54 lines
1.7 KiB
Go

package resolver
import (
"strings"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_Suffix_Tree(t *testing.T) {
root := newSuffixTreeRoot()
Convey("Google should not be found", t, func() {
root.insert("cn", &Nameserver{address: "114.114.114.114"})
root.sinsert([]string{"baidu", "cn"}, &Nameserver{address: "166.111.8.28"})
root.sinsert([]string{"sina", "cn"}, &Nameserver{address: "114.114.114.114"})
v, found := root.search(strings.Split("google.com", "."))
So(found, ShouldEqual, false)
v, found = root.search(strings.Split("baidu.cn", "."))
So(found, ShouldEqual, true)
So(v.address, ShouldEqual, "166.111.8.28")
})
Convey("Google should be found", t, func() {
root.sinsert(strings.Split("com", "."), &Nameserver{address: ""})
root.sinsert(strings.Split("google.com", "."), &Nameserver{address: "8.8.8.8"})
root.sinsert(strings.Split("twitter.com", "."), &Nameserver{address: "8.8.8.8"})
root.sinsert(strings.Split("scholar.google.com", "."), &Nameserver{address: "208.67.222.222"})
v, found := root.search(strings.Split("google.com", "."))
So(found, ShouldEqual, true)
So(v.address, ShouldEqual, "8.8.8.8")
v, found = root.search(strings.Split("www.google.com", "."))
So(found, ShouldEqual, true)
So(v.address, ShouldEqual, "8.8.8.8")
v, found = root.search(strings.Split("scholar.google.com", "."))
So(found, ShouldEqual, true)
So(v.address, ShouldEqual, "208.67.222.222")
v, found = root.search(strings.Split("twitter.com", "."))
So(found, ShouldEqual, true)
So(v.address, ShouldEqual, "8.8.8.8")
v, found = root.search(strings.Split("baidu.cn", "."))
So(found, ShouldEqual, true)
So(v.address, ShouldEqual, "166.111.8.28")
})
}