-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie_test.go
More file actions
57 lines (50 loc) · 1.69 KB
/
trie_test.go
File metadata and controls
57 lines (50 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package refuser
import (
"testing"
)
func TestTrieLookup_SuffixMatching(t *testing.T) {
// 1. 创建 Trie 并插入规则
trie := NewTrie()
if trie.Root == nil {
t.Fatalf("NewTrie() 应该返回一个非空的 Root 节点")
}
rulesToInsert := []string{
"example.com",
"sub.example.net.", // 包含末尾点
"GOOgle.cn", // 包含大写
"a.b.c.d",
}
for _, rule := range rulesToInsert {
trie.Insert(rule)
}
// 2. 查找测试 (Lookup)
testCases := []struct {
name string
fqdn string
expected bool // 预期是否命中
}{
// --- 命中测试 (Expected TRUE) ---
{"T01_精确匹配", "example.com", true},
{"T02_子域名匹配", "www.example.com", true},
{"T03_多级子域名匹配", "mail.www.example.com.", true},
{"T04_末尾点子域名", "mail.sub.example.net", true},
{"T05_多级精确匹配", "a.b.c.d.", true},
{"T06_大小写不敏感", "QUERY.GOOgle.cn", true},
{"T07_子域名匹配多级规则", "x.a.b.c.d", true},
{"T08_大小写混合子域名", "x.y.Example.com", true},
// --- 未命中测试 (Expected FALSE) ---
{"T10_完全不匹配", "baidu.com", false},
{"T11_部分匹配-长", "ex.example.co", false}, // 规则中没有 example.co
{"T12_非子域名", "com.example", false}, // 确保不是简单的字符串包含
{"T13_更深层级但不匹配", "x.x.y.z.w", false},
{"T14_规则的一部分", "example", false}, // 确保只插入了 example.com,查询 example 不命中
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := trie.Lookup(tc.fqdn)
if result != tc.expected {
t.Errorf("Lookup(%q) 期望结果: %t, 实际结果: %t", tc.fqdn, tc.expected, result)
}
})
}
}