2019-07-14 11:29:58 +00:00
|
|
|
package trie
|
|
|
|
|
|
|
|
import (
|
2022-04-05 20:25:53 +00:00
|
|
|
"net/netip"
|
2019-07-14 11:29:58 +00:00
|
|
|
"testing"
|
2020-04-08 07:45:59 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-07-14 11:29:58 +00:00
|
|
|
)
|
|
|
|
|
2022-04-05 20:25:53 +00:00
|
|
|
var localIP = netip.AddrFrom4([4]byte{127, 0, 0, 1})
|
2019-07-15 10:00:51 +00:00
|
|
|
|
2019-07-14 11:29:58 +00:00
|
|
|
func TestTrie_Basic(t *testing.T) {
|
2022-04-05 20:25:53 +00:00
|
|
|
tree := New[netip.Addr]()
|
2019-07-14 11:29:58 +00:00
|
|
|
domains := []string{
|
|
|
|
"example.com",
|
|
|
|
"google.com",
|
2020-06-07 09:25:51 +00:00
|
|
|
"localhost",
|
2019-07-14 11:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, domain := range domains {
|
2019-07-15 10:00:51 +00:00
|
|
|
tree.Insert(domain, localIP)
|
2019-07-14 11:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
node := tree.Search("example.com")
|
2020-04-08 07:45:59 +00:00
|
|
|
assert.NotNil(t, node)
|
2022-11-02 14:28:18 +00:00
|
|
|
assert.True(t, node.Data() == localIP)
|
2020-04-08 07:45:59 +00:00
|
|
|
assert.NotNil(t, tree.Insert("", localIP))
|
2020-06-07 09:25:51 +00:00
|
|
|
assert.Nil(t, tree.Search(""))
|
|
|
|
assert.NotNil(t, tree.Search("localhost"))
|
2020-06-24 11:46:37 +00:00
|
|
|
assert.Nil(t, tree.Search("www.google.com"))
|
2019-07-14 11:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrie_Wildcard(t *testing.T) {
|
2022-04-05 20:25:53 +00:00
|
|
|
tree := New[netip.Addr]()
|
2019-07-14 11:29:58 +00:00
|
|
|
domains := []string{
|
|
|
|
"*.example.com",
|
|
|
|
"sub.*.example.com",
|
|
|
|
"*.dev",
|
2020-04-08 07:45:59 +00:00
|
|
|
".org",
|
|
|
|
".example.net",
|
2020-04-24 15:49:19 +00:00
|
|
|
".apple.*",
|
2020-05-28 04:13:05 +00:00
|
|
|
"+.foo.com",
|
2020-06-24 10:41:23 +00:00
|
|
|
"+.stun.*.*",
|
|
|
|
"+.stun.*.*.*",
|
|
|
|
"+.stun.*.*.*.*",
|
|
|
|
"stun.l.google.com",
|
2019-07-14 11:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, domain := range domains {
|
2019-07-15 10:00:51 +00:00
|
|
|
tree.Insert(domain, localIP)
|
2019-07-14 11:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 07:45:59 +00: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 15:49:19 +00:00
|
|
|
assert.NotNil(t, tree.Search("test.apple.com"))
|
2020-05-28 04:13:05 +00:00
|
|
|
assert.NotNil(t, tree.Search("test.foo.com"))
|
|
|
|
assert.NotNil(t, tree.Search("foo.com"))
|
2020-06-24 10:41:23 +00:00
|
|
|
assert.NotNil(t, tree.Search("global.stun.website.com"))
|
2020-04-08 07:45:59 +00:00
|
|
|
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 11:29:58 +00:00
|
|
|
|
2020-04-08 07:45:59 +00:00
|
|
|
func TestTrie_Priority(t *testing.T) {
|
2022-04-05 20:25:53 +00:00
|
|
|
tree := New[int]()
|
2020-04-08 07:45:59 +00:00
|
|
|
domains := []string{
|
|
|
|
".dev",
|
|
|
|
"example.dev",
|
|
|
|
"*.example.dev",
|
|
|
|
"test.example.dev",
|
2019-07-14 11:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 07:45:59 +00:00
|
|
|
assertFn := func(domain string, data int) {
|
|
|
|
node := tree.Search(domain)
|
|
|
|
assert.NotNil(t, node)
|
2022-11-02 14:28:18 +00:00
|
|
|
assert.Equal(t, data, node.Data())
|
2019-07-14 11:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 07:45:59 +00:00
|
|
|
for idx, domain := range domains {
|
2022-04-05 20:25:53 +00:00
|
|
|
tree.Insert(domain, idx+1)
|
2019-07-14 11:29:58 +00:00
|
|
|
}
|
2019-07-15 10:00:51 +00:00
|
|
|
|
2022-04-05 20:25:53 +00: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 11:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrie_Boundary(t *testing.T) {
|
2022-04-05 20:25:53 +00:00
|
|
|
tree := New[netip.Addr]()
|
2019-07-15 10:00:51 +00:00
|
|
|
tree.Insert("*.dev", localIP)
|
2019-07-14 11:29:58 +00:00
|
|
|
|
2020-04-08 07:45:59 +00:00
|
|
|
assert.NotNil(t, tree.Insert(".", localIP))
|
|
|
|
assert.NotNil(t, tree.Insert("..dev", localIP))
|
|
|
|
assert.Nil(t, tree.Search("dev"))
|
2019-07-14 11:29:58 +00:00
|
|
|
}
|
2022-01-26 14:28:13 +00:00
|
|
|
|
|
|
|
func TestTrie_WildcardBoundary(t *testing.T) {
|
2022-04-05 20:25:53 +00:00
|
|
|
tree := New[netip.Addr]()
|
2022-01-26 14:28:13 +00:00
|
|
|
tree.Insert("+.*", localIP)
|
|
|
|
tree.Insert("stun.*.*.*", localIP)
|
|
|
|
|
|
|
|
assert.NotNil(t, tree.Search("example.com"))
|
|
|
|
}
|