filtering: imp code

This commit is contained in:
Dimitry Kolyshev
2022-12-26 22:44:19 +07:00
parent 5480bed1f7
commit d76834f843

View File

@@ -417,274 +417,275 @@ func TestMatching(t *testing.T) {
host string host string
wantReason Reason wantReason Reason
wantIsFiltered bool wantIsFiltered bool
wantDNSType uint16 qtype uint16
}{{ }{{
name: "sanity", name: "sanity",
rules: "||doubleclick.net^", rules: "||doubleclick.net^",
host: "www.doubleclick.net", host: "www.doubleclick.net",
wantIsFiltered: true, wantIsFiltered: true,
wantReason: FilteredBlockList, wantReason: FilteredBlockList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "sanity", name: "sanity",
rules: "||doubleclick.net^", rules: "||doubleclick.net^",
host: "nodoubleclick.net", host: "nodoubleclick.net",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredNotFound, wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "sanity", name: "sanity",
rules: "||doubleclick.net^", rules: "||doubleclick.net^",
host: "doubleclick.net.ru", host: "doubleclick.net.ru",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredNotFound, wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "sanity", name: "sanity",
rules: "||doubleclick.net^", rules: "||doubleclick.net^",
host: sbBlocked, host: sbBlocked,
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredNotFound, wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "blocking", name: "blocking",
rules: blockingRules, rules: blockingRules,
host: "example.org", host: "example.org",
wantIsFiltered: true, wantIsFiltered: true,
wantReason: FilteredBlockList, wantReason: FilteredBlockList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "blocking", name: "blocking",
rules: blockingRules, rules: blockingRules,
host: "test.example.org", host: "test.example.org",
wantIsFiltered: true, wantIsFiltered: true,
wantReason: FilteredBlockList, wantReason: FilteredBlockList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "blocking", name: "blocking",
rules: blockingRules, rules: blockingRules,
host: "test.test.example.org", host: "test.test.example.org",
wantIsFiltered: true, wantIsFiltered: true,
wantReason: FilteredBlockList, wantReason: FilteredBlockList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "blocking", name: "blocking",
rules: blockingRules, rules: blockingRules,
host: "testexample.org", host: "testexample.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredNotFound, wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "blocking", name: "blocking",
rules: blockingRules, rules: blockingRules,
host: "onemoreexample.org", host: "onemoreexample.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredNotFound, wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "allowlist", name: "allowlist",
rules: allowlistRules, rules: allowlistRules,
host: "example.org", host: "example.org",
wantIsFiltered: true, wantIsFiltered: true,
wantReason: FilteredBlockList, wantReason: FilteredBlockList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "allowlist", name: "allowlist",
rules: allowlistRules, rules: allowlistRules,
host: "test.example.org", host: "test.example.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredAllowList, wantReason: NotFilteredAllowList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "allowlist", name: "allowlist",
rules: allowlistRules, rules: allowlistRules,
host: "test.test.example.org", host: "test.test.example.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredAllowList, wantReason: NotFilteredAllowList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "allowlist", name: "allowlist",
rules: allowlistRules, rules: allowlistRules,
host: "testexample.org", host: "testexample.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredNotFound, wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "allowlist", name: "allowlist",
rules: allowlistRules, rules: allowlistRules,
host: "onemoreexample.org", host: "onemoreexample.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredNotFound, wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "important", name: "important",
rules: importantRules, rules: importantRules,
host: "example.org", host: "example.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredAllowList, wantReason: NotFilteredAllowList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "important", name: "important",
rules: importantRules, rules: importantRules,
host: "test.example.org", host: "test.example.org",
wantIsFiltered: true, wantIsFiltered: true,
wantReason: FilteredBlockList, wantReason: FilteredBlockList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "important", name: "important",
rules: importantRules, rules: importantRules,
host: "test.test.example.org", host: "test.test.example.org",
wantIsFiltered: true, wantIsFiltered: true,
wantReason: FilteredBlockList, wantReason: FilteredBlockList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "important", name: "important",
rules: importantRules, rules: importantRules,
host: "testexample.org", host: "testexample.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredNotFound, wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "important", name: "important",
rules: importantRules, rules: importantRules,
host: "onemoreexample.org", host: "onemoreexample.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredNotFound, wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "regex", name: "regex",
rules: regexRules, rules: regexRules,
host: "example.org", host: "example.org",
wantIsFiltered: true, wantIsFiltered: true,
wantReason: FilteredBlockList, wantReason: FilteredBlockList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "regex", name: "regex",
rules: regexRules, rules: regexRules,
host: "test.example.org", host: "test.example.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredAllowList, wantReason: NotFilteredAllowList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "regex", name: "regex",
rules: regexRules, rules: regexRules,
host: "test.test.example.org", host: "test.test.example.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredAllowList, wantReason: NotFilteredAllowList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "regex", name: "regex",
rules: regexRules, rules: regexRules,
host: "testexample.org", host: "testexample.org",
wantIsFiltered: true, wantIsFiltered: true,
wantReason: FilteredBlockList, wantReason: FilteredBlockList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "regex", name: "regex",
rules: regexRules, rules: regexRules,
host: "onemoreexample.org", host: "onemoreexample.org",
wantIsFiltered: true, wantIsFiltered: true,
wantReason: FilteredBlockList, wantReason: FilteredBlockList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "mask", name: "mask",
rules: maskRules, rules: maskRules,
host: "test.example.org", host: "test.example.org",
wantIsFiltered: true, wantIsFiltered: true,
wantReason: FilteredBlockList, wantReason: FilteredBlockList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "mask", name: "mask",
rules: maskRules, rules: maskRules,
host: "test2.example.org", host: "test2.example.org",
wantIsFiltered: true, wantIsFiltered: true,
wantReason: FilteredBlockList, wantReason: FilteredBlockList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "mask", name: "mask",
rules: maskRules, rules: maskRules,
host: "example.com", host: "example.com",
wantIsFiltered: true, wantIsFiltered: true,
wantReason: FilteredBlockList, wantReason: FilteredBlockList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "mask", name: "mask",
rules: maskRules, rules: maskRules,
host: "exampleeee.com", host: "exampleeee.com",
wantIsFiltered: true, wantIsFiltered: true,
wantReason: FilteredBlockList, wantReason: FilteredBlockList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "mask", name: "mask",
rules: maskRules, rules: maskRules,
host: "onemoreexamsite.com", host: "onemoreexamsite.com",
wantIsFiltered: true, wantIsFiltered: true,
wantReason: FilteredBlockList, wantReason: FilteredBlockList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "mask", name: "mask",
rules: maskRules, rules: maskRules,
host: "example.org", host: "example.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredNotFound, wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "mask", name: "mask",
rules: maskRules, rules: maskRules,
host: "testexample.org", host: "testexample.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredNotFound, wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "mask", name: "mask",
rules: maskRules, rules: maskRules,
host: "example.co.uk", host: "example.co.uk",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredNotFound, wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "dnstype", name: "dnstype",
rules: dnstypeRules, rules: dnstypeRules,
host: "onemoreexample.org", host: "onemoreexample.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredNotFound, wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "dnstype", name: "dnstype",
rules: dnstypeRules, rules: dnstypeRules,
host: "example.org", host: "example.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredNotFound, wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "dnstype", name: "dnstype",
rules: dnstypeRules, rules: dnstypeRules,
host: "example.org", host: "example.org",
wantIsFiltered: true, wantIsFiltered: true,
wantReason: FilteredBlockList, wantReason: FilteredBlockList,
wantDNSType: dns.TypeAAAA, qtype: dns.TypeAAAA,
}, { }, {
name: "dnstype", name: "dnstype",
rules: dnstypeRules, rules: dnstypeRules,
host: "test.example.org", host: "test.example.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredAllowList, wantReason: NotFilteredAllowList,
wantDNSType: dns.TypeA, qtype: dns.TypeA,
}, { }, {
name: "dnstype", name: "dnstype",
rules: dnstypeRules, rules: dnstypeRules,
host: "test.example.org", host: "test.example.org",
wantIsFiltered: false, wantIsFiltered: false,
wantReason: NotFilteredAllowList, wantReason: NotFilteredAllowList,
wantDNSType: dns.TypeAAAA, qtype: dns.TypeAAAA,
}} }}
for _, tc := range testCases { for _, tc := range testCases {
t.Run(fmt.Sprintf("%s-%s", tc.name, tc.host), func(t *testing.T) { t.Run(fmt.Sprintf("%s-%s", tc.name, tc.host), func(t *testing.T) {
filters := []Filter{{ID: 0, Data: []byte(tc.rules)}} filters := []Filter{{ID: 0, Data: []byte(tc.rules)}}
d, setts := newForTest(t, nil, filters) d, setts := newForTest(t, nil, filters)
t.Cleanup(d.Close) t.Cleanup(d.Close)
res, err := d.CheckHost(tc.host, tc.wantDNSType, setts) res, err := d.CheckHost(tc.host, tc.qtype, setts)
require.NoError(t, err) require.NoError(t, err)
assert.Equalf(t, tc.wantIsFiltered, res.IsFiltered, "Hostname %s has wrong result (%v must be %v)", tc.host, res.IsFiltered, tc.wantIsFiltered) assert.Equalf(t, tc.wantIsFiltered, res.IsFiltered, "Hostname %s has wrong result (%v must be %v)", tc.host, res.IsFiltered, tc.wantIsFiltered)