mihomo/component/trie/domain_test.go

129 lines
3 KiB
Go
Raw Permalink Normal View History

package trie_test
2019-07-14 19:29:58 +08:00
import (
2022-04-06 04:25:53 +08:00
"net/netip"
2019-07-14 19:29:58 +08:00
"testing"
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/component/trie"
"github.com/stretchr/testify/assert"
2019-07-14 19:29:58 +08:00
)
2022-04-06 04:25:53 +08:00
var localIP = netip.AddrFrom4([4]byte{127, 0, 0, 1})
2019-07-15 18:00:51 +08:00
2019-07-14 19:29:58 +08:00
func TestTrie_Basic(t *testing.T) {
tree := trie.New[netip.Addr]()
2019-07-14 19:29:58 +08:00
domains := []string{
"example.com",
"google.com",
2020-06-07 17:25:51 +08:00
"localhost",
2019-07-14 19:29:58 +08:00
}
for _, domain := range domains {
assert.NoError(t, tree.Insert(domain, localIP))
2019-07-14 19:29:58 +08:00
}
node := tree.Search("example.com")
assert.NotNil(t, node)
assert.True(t, node.Data() == localIP)
assert.NotNil(t, tree.Insert("", localIP))
2020-06-07 17:25:51 +08:00
assert.Nil(t, tree.Search(""))
assert.NotNil(t, tree.Search("localhost"))
assert.Nil(t, tree.Search("www.google.com"))
2019-07-14 19:29:58 +08:00
}
func TestTrie_Wildcard(t *testing.T) {
tree := trie.New[netip.Addr]()
2019-07-14 19:29:58 +08:00
domains := []string{
"*.example.com",
"sub.*.example.com",
"*.dev",
".org",
".example.net",
2020-04-24 23:49:19 +08:00
".apple.*",
"+.foo.com",
"+.stun.*.*",
"+.stun.*.*.*",
"+.stun.*.*.*.*",
"stun.l.google.com",
2019-07-14 19:29:58 +08:00
}
for _, domain := range domains {
assert.NoError(t, tree.Insert(domain, localIP))
2019-07-14 19:29:58 +08:00
}
assert.NotNil(t, tree.Search("sub.example.com"))
assert.NotNil(t, tree.Search("sub.foo.example.com"))
assert.NotNil(t, tree.Search("test.org"))
assert.NotNil(t, tree.Search("test.example.net"))
2020-04-24 23:49:19 +08:00
assert.NotNil(t, tree.Search("test.apple.com"))
assert.NotNil(t, tree.Search("test.foo.com"))
assert.NotNil(t, tree.Search("foo.com"))
assert.NotNil(t, tree.Search("global.stun.website.com"))
assert.Nil(t, tree.Search("foo.sub.example.com"))
assert.Nil(t, tree.Search("foo.example.dev"))
assert.Nil(t, tree.Search("example.com"))
}
2019-07-14 19:29:58 +08:00
func TestTrie_Priority(t *testing.T) {
tree := trie.New[int]()
domains := []string{
".dev",
"example.dev",
"*.example.dev",
"test.example.dev",
2019-07-14 19:29:58 +08:00
}
assertFn := func(domain string, data int) {
node := tree.Search(domain)
assert.NotNil(t, node)
assert.Equal(t, data, node.Data())
2019-07-14 19:29:58 +08:00
}
for idx, domain := range domains {
assert.NoError(t, tree.Insert(domain, idx+1))
2019-07-14 19:29:58 +08:00
}
2019-07-15 18:00:51 +08:00
2022-04-06 04:25:53 +08:00
assertFn("test.dev", 1)
assertFn("foo.bar.dev", 1)
assertFn("example.dev", 2)
assertFn("foo.example.dev", 3)
assertFn("test.example.dev", 4)
2019-07-14 19:29:58 +08:00
}
func TestTrie_Boundary(t *testing.T) {
tree := trie.New[netip.Addr]()
assert.NoError(t, tree.Insert("*.dev", localIP))
2019-07-14 19:29:58 +08:00
assert.NotNil(t, tree.Insert(".", localIP))
assert.NotNil(t, tree.Insert("..dev", localIP))
assert.Nil(t, tree.Search("dev"))
2019-07-14 19:29:58 +08:00
}
2022-01-26 22:28:13 +08:00
func TestTrie_WildcardBoundary(t *testing.T) {
tree := trie.New[netip.Addr]()
assert.NoError(t, tree.Insert("+.*", localIP))
assert.NoError(t, tree.Insert("stun.*.*.*", localIP))
2022-01-26 22:28:13 +08:00
assert.NotNil(t, tree.Search("example.com"))
}
func TestTrie_Foreach(t *testing.T) {
tree := trie.New[netip.Addr]()
domainList := []string{
"google.com",
"stun.*.*.*",
"test.*.google.com",
"+.baidu.com",
"*.baidu.com",
"*.*.baidu.com",
}
for _, domain := range domainList {
assert.NoError(t, tree.Insert(domain, localIP))
}
count := 0
tree.Foreach(func(domain string, data netip.Addr) {
count++
})
assert.Equal(t, 7, count)
}