mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-12-02 13:16:47 +00:00
DomainSet emits at most one exact entry and one suffix-only entry for each domain. Sorting without the leading dot makes matching pairs adjacent, so they can be combined in place. Replace per-entry binary searches and the separate rule slice with an adjacent pairing pass. Merge pairs into +.domain, remove consumed entries from the original slice, and sort the resulting rules by their full text. Add regression coverage for multiple pairs and wildcard ordering. Output semantics and early termination remain unchanged.
95 lines
2.7 KiB
Go
95 lines
2.7 KiB
Go
package provider_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/metacubex/mihomo/constant"
|
|
"github.com/metacubex/mihomo/rules/provider"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestDomainStrategyDumpMrs(test *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
domains []string
|
|
expected []string
|
|
queries map[string]bool
|
|
}{
|
|
{
|
|
"suffix-only",
|
|
[]string{".example.com"},
|
|
[]string{".example.com"},
|
|
map[string]bool{"example.com": false, "www.example.com": true},
|
|
},
|
|
{
|
|
"exact-only",
|
|
[]string{"example.com"},
|
|
[]string{"example.com"},
|
|
map[string]bool{"example.com": true, "www.example.com": false},
|
|
},
|
|
{
|
|
"merge-terminals",
|
|
[]string{"example.com", ".example.com"},
|
|
[]string{"+.example.com"},
|
|
map[string]bool{"example.com": true, "www.example.com": true},
|
|
},
|
|
{
|
|
"deduplicate",
|
|
[]string{"+.example.com", "+.example.com", "example.com", ".example.com"},
|
|
[]string{"+.example.com"},
|
|
map[string]bool{"example.com": true, "www.example.com": true},
|
|
},
|
|
{
|
|
"mixed",
|
|
[]string{".example.com", "example.net", "example.org", ".example.org"},
|
|
[]string{"+.example.org", ".example.com", "example.net"},
|
|
map[string]bool{
|
|
"example.com": false,
|
|
"www.example.com": true,
|
|
"example.net": true,
|
|
"www.example.net": false,
|
|
"example.org": true,
|
|
"www.example.org": true,
|
|
},
|
|
},
|
|
{
|
|
"wildcards",
|
|
[]string{"*.example.com", "+.example.org", "test.*.google.com", ".test.*.google.com", ".example.net"},
|
|
[]string{"*.example.com", "+.example.org", "+.test.*.google.com", ".example.net"},
|
|
map[string]bool{
|
|
"example.com": false,
|
|
"www.example.com": true,
|
|
"example.org": true,
|
|
"www.example.org": true,
|
|
"test.www.google.com": true,
|
|
"www.test.www.google.com": true,
|
|
"example.net": false,
|
|
"www.example.net": true,
|
|
},
|
|
},
|
|
}
|
|
for _, testCase := range tests {
|
|
test.Run(testCase.name, func(test *testing.T) {
|
|
strategy := provider.NewDomainStrategy()
|
|
for _, domain := range testCase.domains {
|
|
strategy.Insert(domain)
|
|
}
|
|
strategy.FinishInsert()
|
|
rebuilt := provider.NewDomainStrategy()
|
|
var domains []string
|
|
strategy.DumpMrs(func(domain string) bool {
|
|
domains = append(domains, domain)
|
|
rebuilt.Insert(domain)
|
|
return true
|
|
})
|
|
rebuilt.FinishInsert()
|
|
assert.Equal(test, testCase.expected, domains)
|
|
for domain, expected := range testCase.queries {
|
|
metadata := &constant.Metadata{Host: domain}
|
|
assert.Equal(test, expected, strategy.Match(metadata, constant.RuleMatchHelper{}), domain)
|
|
assert.Equal(test, expected, rebuilt.Match(metadata, constant.RuleMatchHelper{}), domain)
|
|
}
|
|
})
|
|
}
|
|
}
|