增加web功能
This commit is contained in:
62
shield/rule_test.go
Normal file
62
shield/rule_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package shield
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"dns-server/config"
|
||||
)
|
||||
|
||||
func TestRuleParsing(t *testing.T) {
|
||||
// 创建一个简单的配置
|
||||
cfg := &config.ShieldConfig{
|
||||
LocalRulesFile: "",
|
||||
RemoteRulesCacheDir: ".",
|
||||
UpdateInterval: 3600,
|
||||
StatsFile: "",
|
||||
StatsSaveInterval: 300,
|
||||
HostsFile: "",
|
||||
Blacklists: []config.BlacklistEntry{},
|
||||
}
|
||||
|
||||
// 测试规则
|
||||
testCases := []struct {
|
||||
rule string
|
||||
domain string
|
||||
blocked bool
|
||||
desc string
|
||||
}{
|
||||
// 测试关键字匹配规则
|
||||
{"/ad.qq.com/", "ad.qq.com", true, "精确匹配"},
|
||||
{"/ad.qq.com/", "sub.ad.qq.com", true, "子域名包含匹配"},
|
||||
{"/ad/", "ad.example.com", true, "开头匹配"},
|
||||
{"/ad/", "example.ad.com", true, "中间匹配"},
|
||||
{"/ad/", "example.com.ad", true, "结尾匹配"},
|
||||
{"/AD/", "ad.example.com", true, "不区分大小写匹配"},
|
||||
{"/example.com/", "example.com", true, "特殊字符转义匹配"},
|
||||
{"/ad/", "example.com", false, "不包含关键字,不应匹配"},
|
||||
{"/test/", "example.com", false, "不同关键字,不应匹配"},
|
||||
|
||||
// 测试排除规则
|
||||
{"@@/ad/", "ad.example.com", false, "排除规则,不应匹配"},
|
||||
}
|
||||
|
||||
// 运行测试
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
// 为每个测试用例创建一个新的屏蔽管理器实例
|
||||
manager := NewShieldManager(cfg)
|
||||
|
||||
// 添加规则
|
||||
manager.AddRule(tc.rule)
|
||||
|
||||
// 检查域名是否被屏蔽
|
||||
result := manager.CheckDomainBlockDetails(tc.domain)
|
||||
blocked := result["blocked"].(bool)
|
||||
|
||||
// 验证结果
|
||||
if blocked != tc.blocked {
|
||||
t.Errorf("Rule %q: Domain %q expected %t, got %t", tc.rule, tc.domain, tc.blocked, blocked)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user