github.com/kelleygo/clashcore@v1.0.2/component/trie/domain_test.go (about) 1 package trie_test 2 3 import ( 4 "net/netip" 5 "testing" 6 7 "github.com/kelleygo/clashcore/component/trie" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 var localIP = netip.AddrFrom4([4]byte{127, 0, 0, 1}) 12 13 func TestTrie_Basic(t *testing.T) { 14 tree := trie.New[netip.Addr]() 15 domains := []string{ 16 "example.com", 17 "google.com", 18 "localhost", 19 } 20 21 for _, domain := range domains { 22 assert.NoError(t, tree.Insert(domain, localIP)) 23 } 24 25 node := tree.Search("example.com") 26 assert.NotNil(t, node) 27 assert.True(t, node.Data() == localIP) 28 assert.NotNil(t, tree.Insert("", localIP)) 29 assert.Nil(t, tree.Search("")) 30 assert.NotNil(t, tree.Search("localhost")) 31 assert.Nil(t, tree.Search("www.google.com")) 32 } 33 34 func TestTrie_Wildcard(t *testing.T) { 35 tree := trie.New[netip.Addr]() 36 domains := []string{ 37 "*.example.com", 38 "sub.*.example.com", 39 "*.dev", 40 ".org", 41 ".example.net", 42 ".apple.*", 43 "+.foo.com", 44 "+.stun.*.*", 45 "+.stun.*.*.*", 46 "+.stun.*.*.*.*", 47 "stun.l.google.com", 48 } 49 50 for _, domain := range domains { 51 assert.NoError(t, tree.Insert(domain, localIP)) 52 } 53 54 assert.NotNil(t, tree.Search("sub.example.com")) 55 assert.NotNil(t, tree.Search("sub.foo.example.com")) 56 assert.NotNil(t, tree.Search("test.org")) 57 assert.NotNil(t, tree.Search("test.example.net")) 58 assert.NotNil(t, tree.Search("test.apple.com")) 59 assert.NotNil(t, tree.Search("test.foo.com")) 60 assert.NotNil(t, tree.Search("foo.com")) 61 assert.NotNil(t, tree.Search("global.stun.website.com")) 62 assert.Nil(t, tree.Search("foo.sub.example.com")) 63 assert.Nil(t, tree.Search("foo.example.dev")) 64 assert.Nil(t, tree.Search("example.com")) 65 } 66 67 func TestTrie_Priority(t *testing.T) { 68 tree := trie.New[int]() 69 domains := []string{ 70 ".dev", 71 "example.dev", 72 "*.example.dev", 73 "test.example.dev", 74 } 75 76 assertFn := func(domain string, data int) { 77 node := tree.Search(domain) 78 assert.NotNil(t, node) 79 assert.Equal(t, data, node.Data()) 80 } 81 82 for idx, domain := range domains { 83 assert.NoError(t, tree.Insert(domain, idx+1)) 84 } 85 86 assertFn("test.dev", 1) 87 assertFn("foo.bar.dev", 1) 88 assertFn("example.dev", 2) 89 assertFn("foo.example.dev", 3) 90 assertFn("test.example.dev", 4) 91 } 92 93 func TestTrie_Boundary(t *testing.T) { 94 tree := trie.New[netip.Addr]() 95 assert.NoError(t, tree.Insert("*.dev", localIP)) 96 97 assert.NotNil(t, tree.Insert(".", localIP)) 98 assert.NotNil(t, tree.Insert("..dev", localIP)) 99 assert.Nil(t, tree.Search("dev")) 100 } 101 102 func TestTrie_WildcardBoundary(t *testing.T) { 103 tree := trie.New[netip.Addr]() 104 assert.NoError(t, tree.Insert("+.*", localIP)) 105 assert.NoError(t, tree.Insert("stun.*.*.*", localIP)) 106 107 assert.NotNil(t, tree.Search("example.com")) 108 } 109 110 func TestTrie_Foreach(t *testing.T) { 111 tree := trie.New[netip.Addr]() 112 domainList := []string{ 113 "google.com", 114 "stun.*.*.*", 115 "test.*.google.com", 116 "+.baidu.com", 117 "*.baidu.com", 118 "*.*.baidu.com", 119 } 120 for _, domain := range domainList { 121 assert.NoError(t, tree.Insert(domain, localIP)) 122 } 123 count := 0 124 tree.Foreach(func(domain string, data netip.Addr) { 125 count++ 126 }) 127 assert.Equal(t, 7, count) 128 }