github.com/kelleygo/clashcore@v1.0.2/component/trie/domain_set_test.go (about)

     1  package trie_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kelleygo/clashcore/component/trie"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestDomainSet(t *testing.T) {
    11  	tree := trie.New[struct{}]()
    12  	domainSet := []string{
    13  		"baidu.com",
    14  		"google.com",
    15  		"www.google.com",
    16  		"test.a.net",
    17  		"test.a.oc",
    18  		"Mijia Cloud",
    19  		".qq.com",
    20  		"+.cn",
    21  	}
    22  
    23  	for _, domain := range domainSet {
    24  		assert.NoError(t, tree.Insert(domain, struct{}{}))
    25  	}
    26  	set := tree.NewDomainSet()
    27  	assert.NotNil(t, set)
    28  	assert.True(t, set.Has("test.cn"))
    29  	assert.True(t, set.Has("cn"))
    30  	assert.True(t, set.Has("Mijia Cloud"))
    31  	assert.True(t, set.Has("test.a.net"))
    32  	assert.True(t, set.Has("www.qq.com"))
    33  	assert.True(t, set.Has("google.com"))
    34  	assert.False(t, set.Has("qq.com"))
    35  	assert.False(t, set.Has("www.baidu.com"))
    36  }
    37  
    38  func TestDomainSetComplexWildcard(t *testing.T) {
    39  	tree := trie.New[struct{}]()
    40  	domainSet := []string{
    41  		"+.baidu.com",
    42  		"+.a.baidu.com",
    43  		"www.baidu.com",
    44  		"+.bb.baidu.com",
    45  		"test.a.net",
    46  		"test.a.oc",
    47  		"www.qq.com",
    48  	}
    49  
    50  	for _, domain := range domainSet {
    51  		assert.NoError(t, tree.Insert(domain, struct{}{}))
    52  	}
    53  	set := tree.NewDomainSet()
    54  	assert.NotNil(t, set)
    55  	assert.False(t, set.Has("google.com"))
    56  	assert.True(t, set.Has("www.baidu.com"))
    57  	assert.True(t, set.Has("test.test.baidu.com"))
    58  }
    59  
    60  func TestDomainSetWildcard(t *testing.T) {
    61  	tree := trie.New[struct{}]()
    62  	domainSet := []string{
    63  		"*.*.*.baidu.com",
    64  		"www.baidu.*",
    65  		"stun.*.*",
    66  		"*.*.qq.com",
    67  		"test.*.baidu.com",
    68  		"*.apple.com",
    69  	}
    70  
    71  	for _, domain := range domainSet {
    72  		assert.NoError(t, tree.Insert(domain, struct{}{}))
    73  	}
    74  	set := tree.NewDomainSet()
    75  	assert.NotNil(t, set)
    76  	assert.True(t, set.Has("www.baidu.com"))
    77  	assert.True(t, set.Has("test.test.baidu.com"))
    78  	assert.True(t, set.Has("test.test.qq.com"))
    79  	assert.True(t, set.Has("stun.ab.cd"))
    80  	assert.False(t, set.Has("test.baidu.com"))
    81  	assert.False(t, set.Has("www.google.com"))
    82  	assert.False(t, set.Has("a.www.google.com"))
    83  	assert.False(t, set.Has("test.qq.com"))
    84  	assert.False(t, set.Has("test.test.test.qq.com"))
    85  }