2023-04-01 11:53:39 +08:00
|
|
|
package trie_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/component/trie"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2023-04-01 12:15:03 +08:00
|
|
|
func TestDomainSet(t *testing.T) {
|
|
|
|
tree := trie.New[struct{}]()
|
2023-04-01 11:53:39 +08:00
|
|
|
domainSet := []string{
|
|
|
|
"baidu.com",
|
|
|
|
"google.com",
|
|
|
|
"www.google.com",
|
|
|
|
"test.a.net",
|
|
|
|
"test.a.oc",
|
2023-04-20 13:45:22 +08:00
|
|
|
"Mijia Cloud",
|
|
|
|
".qq.com",
|
|
|
|
"+.cn",
|
2023-04-01 11:53:39 +08:00
|
|
|
}
|
2023-04-01 12:15:03 +08:00
|
|
|
|
|
|
|
for _, domain := range domainSet {
|
|
|
|
assert.NoError(t, tree.Insert(domain, struct{}{}))
|
|
|
|
}
|
|
|
|
set := tree.NewDomainSet()
|
2023-04-01 11:53:39 +08:00
|
|
|
assert.NotNil(t, set)
|
2023-04-20 13:45:22 +08:00
|
|
|
assert.True(t, set.Has("test.cn"))
|
|
|
|
assert.True(t, set.Has("cn"))
|
|
|
|
assert.True(t, set.Has("Mijia Cloud"))
|
2023-04-01 11:53:39 +08:00
|
|
|
assert.True(t, set.Has("test.a.net"))
|
2023-04-20 13:45:22 +08:00
|
|
|
assert.True(t, set.Has("www.qq.com"))
|
2023-04-01 11:53:39 +08:00
|
|
|
assert.True(t, set.Has("google.com"))
|
2023-04-20 13:45:22 +08:00
|
|
|
assert.False(t, set.Has("qq.com"))
|
2023-04-01 11:53:39 +08:00
|
|
|
assert.False(t, set.Has("www.baidu.com"))
|
|
|
|
}
|
|
|
|
|
2023-04-01 12:15:03 +08:00
|
|
|
func TestDomainSetComplexWildcard(t *testing.T) {
|
|
|
|
tree := trie.New[struct{}]()
|
2023-04-01 11:53:39 +08:00
|
|
|
domainSet := []string{
|
|
|
|
"+.baidu.com",
|
|
|
|
"+.a.baidu.com",
|
|
|
|
"www.baidu.com",
|
|
|
|
"+.bb.baidu.com",
|
|
|
|
"test.a.net",
|
|
|
|
"test.a.oc",
|
|
|
|
"www.qq.com",
|
|
|
|
}
|
2023-04-01 12:15:03 +08:00
|
|
|
|
|
|
|
for _, domain := range domainSet {
|
|
|
|
assert.NoError(t, tree.Insert(domain, struct{}{}))
|
|
|
|
}
|
|
|
|
set := tree.NewDomainSet()
|
2023-04-01 11:53:39 +08:00
|
|
|
assert.NotNil(t, set)
|
|
|
|
assert.False(t, set.Has("google.com"))
|
|
|
|
assert.True(t, set.Has("www.baidu.com"))
|
|
|
|
assert.True(t, set.Has("test.test.baidu.com"))
|
|
|
|
}
|
|
|
|
|
2023-04-01 12:15:03 +08:00
|
|
|
func TestDomainSetWildcard(t *testing.T) {
|
|
|
|
tree := trie.New[struct{}]()
|
2023-04-01 11:53:39 +08:00
|
|
|
domainSet := []string{
|
|
|
|
"*.*.*.baidu.com",
|
|
|
|
"www.baidu.*",
|
|
|
|
"stun.*.*",
|
|
|
|
"*.*.qq.com",
|
|
|
|
"test.*.baidu.com",
|
2023-04-26 10:49:16 +08:00
|
|
|
"*.apple.com",
|
2023-04-01 11:53:39 +08:00
|
|
|
}
|
2023-04-01 12:15:03 +08:00
|
|
|
|
|
|
|
for _, domain := range domainSet {
|
|
|
|
assert.NoError(t, tree.Insert(domain, struct{}{}))
|
|
|
|
}
|
|
|
|
set := tree.NewDomainSet()
|
2023-04-01 11:53:39 +08:00
|
|
|
assert.NotNil(t, set)
|
|
|
|
assert.True(t, set.Has("www.baidu.com"))
|
|
|
|
assert.True(t, set.Has("test.test.baidu.com"))
|
|
|
|
assert.True(t, set.Has("test.test.qq.com"))
|
2023-04-01 12:15:03 +08:00
|
|
|
assert.True(t, set.Has("stun.ab.cd"))
|
2023-04-01 11:53:39 +08:00
|
|
|
assert.False(t, set.Has("test.baidu.com"))
|
2023-04-01 12:15:03 +08:00
|
|
|
assert.False(t, set.Has("www.google.com"))
|
2023-04-26 10:49:16 +08:00
|
|
|
assert.False(t, set.Has("a.www.google.com"))
|
2023-04-01 11:53:39 +08:00
|
|
|
assert.False(t, set.Has("test.qq.com"))
|
|
|
|
assert.False(t, set.Has("test.test.test.qq.com"))
|
|
|
|
}
|