mihomo/component/domain-trie/trie_test.go

84 lines
1.4 KiB
Go
Raw Normal View History

2019-07-14 19:29:58 +08:00
package trie
import (
"net"
"testing"
)
2019-07-15 18:00:51 +08:00
var localIP = net.IP{127, 0, 0, 1}
2019-07-14 19:29:58 +08:00
func TestTrie_Basic(t *testing.T) {
tree := New()
domains := []string{
"example.com",
"google.com",
}
for _, domain := range domains {
2019-07-15 18:00:51 +08:00
tree.Insert(domain, localIP)
2019-07-14 19:29:58 +08:00
}
node := tree.Search("example.com")
if node == nil {
t.Error("should not recv nil")
}
2019-07-15 18:00:51 +08:00
if !node.Data.(net.IP).Equal(localIP) {
2019-07-14 19:29:58 +08:00
t.Error("should equal 127.0.0.1")
}
}
func TestTrie_Wildcard(t *testing.T) {
tree := New()
domains := []string{
"*.example.com",
"sub.*.example.com",
"*.dev",
}
for _, domain := range domains {
2019-07-15 18:00:51 +08:00
tree.Insert(domain, localIP)
2019-07-14 19:29:58 +08:00
}
if tree.Search("sub.example.com") == nil {
t.Error("should not recv nil")
}
if tree.Search("sub.foo.example.com") == nil {
t.Error("should not recv nil")
}
if tree.Search("foo.sub.example.com") != nil {
t.Error("should recv nil")
}
if tree.Search("foo.example.dev") != nil {
t.Error("should recv nil")
}
2019-07-15 18:00:51 +08:00
if tree.Search("example.com") != nil {
t.Error("should recv nil")
}
2019-07-14 19:29:58 +08:00
}
func TestTrie_Boundary(t *testing.T) {
tree := New()
2019-07-15 18:00:51 +08:00
tree.Insert("*.dev", localIP)
2019-07-14 19:29:58 +08:00
if err := tree.Insert(".", localIP); err == nil {
t.Error("should recv err")
}
if err := tree.Insert(".com", localIP); err == nil {
2019-07-14 19:29:58 +08:00
t.Error("should recv err")
}
if tree.Search("dev") != nil {
t.Error("should recv nil")
}
if tree.Search(".dev") != nil {
t.Error("should recv nil")
}
2019-07-14 19:29:58 +08:00
}