AdGuardHome/internal/filtering/filtering_test.go

697 lines
18 KiB
Go
Raw Normal View History

package filtering
2018-08-30 15:25:33 +01:00
import (
Pull request: cover with tests Merge in DNS/adguard-home from 2271-cover-with-tests to master Updates #2271. Squashed commit of the following: commit db6440efe05171bc15367a2996521848ca348053 Merge: db7fa726b bf4c256c7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:23:09 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit db7fa726bb91b08ec7aaa6c0c818c88b5feb87cd Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:26:51 2020 +0300 all: clean dependencies sum commit b8dc6078c4bcc0de1b7e9073832de122f6fe38a4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:46:00 2020 +0300 testutil: improve code quality commit 001b7194682b1f00aa54dc5a28236faed5a5b02d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:29:49 2020 +0300 testutil: enhance functionality commit f6ccd91a4df6c56778eab8ae50e88e3818b20dd3 Merge: 43fa2eefb 6358240e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:57:37 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit 43fa2eefbc10ef361603cacc1ca12092b12a057a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 14:55:15 2020 +0300 querylog: replace fake log with real in tests commit b95bee7565a14a02c80c78131b3ced224663dd8a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:38:59 2020 +0300 dnsfilter: replace thoughtless declaration with idiomatic one commit a210b1586092e7ae91a9e67c972fa2d2f6baded6 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 19:00:25 2020 +0300 all: refresh golibs dependencies commit 4ff97bd1ade6c80e274ff5716e44df4eba55bdd9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 18:38:47 2020 +0300 all: remove std log commit 542dbda10fefce9f46d15489712b163d919b1291 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 13:46:39 2020 +0300 querylog: improve test logic and readability commit 796d402385925e8e62a1b4c7bf56e4ceec22418c Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 19:06:42 2020 +0300 all: improve code quality commit e81894c11ef15b0453e8e5297f1349936a32f9dd Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 18:32:30 2020 +0300 all: cover with tests commit 252d81fc8a50a91b02cf0f6f35cc22178a2a4d90 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 17:32:01 2020 +0300 all: cover with tests
2020-11-16 16:45:31 +00:00
"bytes"
"fmt"
2023-09-07 15:13:48 +01:00
"net/netip"
2018-08-30 15:25:33 +01:00
"testing"
2019-05-22 16:30:27 +01:00
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
2023-06-07 18:04:01 +01:00
"github.com/AdguardTeam/AdGuardHome/internal/filtering/hashprefix"
Pull request: cover with tests Merge in DNS/adguard-home from 2271-cover-with-tests to master Updates #2271. Squashed commit of the following: commit db6440efe05171bc15367a2996521848ca348053 Merge: db7fa726b bf4c256c7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:23:09 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit db7fa726bb91b08ec7aaa6c0c818c88b5feb87cd Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:26:51 2020 +0300 all: clean dependencies sum commit b8dc6078c4bcc0de1b7e9073832de122f6fe38a4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:46:00 2020 +0300 testutil: improve code quality commit 001b7194682b1f00aa54dc5a28236faed5a5b02d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:29:49 2020 +0300 testutil: enhance functionality commit f6ccd91a4df6c56778eab8ae50e88e3818b20dd3 Merge: 43fa2eefb 6358240e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:57:37 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit 43fa2eefbc10ef361603cacc1ca12092b12a057a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 14:55:15 2020 +0300 querylog: replace fake log with real in tests commit b95bee7565a14a02c80c78131b3ced224663dd8a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:38:59 2020 +0300 dnsfilter: replace thoughtless declaration with idiomatic one commit a210b1586092e7ae91a9e67c972fa2d2f6baded6 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 19:00:25 2020 +0300 all: refresh golibs dependencies commit 4ff97bd1ade6c80e274ff5716e44df4eba55bdd9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 18:38:47 2020 +0300 all: remove std log commit 542dbda10fefce9f46d15489712b163d919b1291 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 13:46:39 2020 +0300 querylog: improve test logic and readability commit 796d402385925e8e62a1b4c7bf56e4ceec22418c Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 19:06:42 2020 +0300 all: improve code quality commit e81894c11ef15b0453e8e5297f1349936a32f9dd Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 18:32:30 2020 +0300 all: cover with tests commit 252d81fc8a50a91b02cf0f6f35cc22178a2a4d90 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 17:32:01 2020 +0300 all: cover with tests
2020-11-16 16:45:31 +00:00
"github.com/AdguardTeam/golibs/log"
2023-09-07 15:13:48 +01:00
"github.com/AdguardTeam/golibs/netutil"
2022-11-02 13:18:02 +00:00
"github.com/AdguardTeam/golibs/testutil"
"github.com/AdguardTeam/urlfilter/rules"
2019-05-22 16:30:27 +01:00
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2018-08-30 15:25:33 +01:00
)
Pull request: 2273 clean tests output Merge in DNS/adguard-home from 2273-clean-tests-output to master Closes #2273. Squashed commit of the following: commit 7571a33fc1f76300bd256578b3afa95338e399c4 Merge: f17df0f9c a19523b25 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:45:30 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit f17df0f9ce2a3ed25db33fbc6a2e7cabd33f657b Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:35:42 2020 +0300 home: move build constraint on top line commit 3717c8ef5a51f9dcaa7e524bfa7b0f1d90bec93d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:24:50 2020 +0300 all: add improvements to changelog commit de6d5afe39d74a3c3d3e0bbe6d0e09aea0214d56 Merge: 43d4f7acf 394fc5a9d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:04:38 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit 43d4f7acf188e810aa7277cb6479110c9842e8be Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 13:38:13 2020 +0300 dnsfilter: remove redundant test logging commit 7194c1413006b8f52fc454e89ab052bf52e4e517 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:19:14 2020 +0300 testutil: improve comments commit 9f17ab08e287fa69ce30d9e7eec8ea8880f87716 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:22:08 2020 +0300 nclient4: fix wrong function name commit f355749149b2a4485792ba2bdcbc0bb4b629d726 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:07:22 2020 +0300 testutil: fix comments and naming commit f8c50a260bfae08d594a7f37d603941d3680a45e Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 14:09:50 2020 +0300 testutil: create a package and include it commit f169cdc4084b719de65aa0cdc65200b48785322e Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:15:58 2020 +0300 agherr: discard loggers output commit 360e736b5a2a30f2c5350448234f14b841e3ea27 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:09:55 2020 +0300 all: replace default log writer with ioutil.Discard Closes #2273.
2020-11-16 12:52:05 +00:00
func TestMain(m *testing.M) {
2022-11-02 13:18:02 +00:00
testutil.DiscardLogOutput(m)
Pull request: 2273 clean tests output Merge in DNS/adguard-home from 2273-clean-tests-output to master Closes #2273. Squashed commit of the following: commit 7571a33fc1f76300bd256578b3afa95338e399c4 Merge: f17df0f9c a19523b25 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:45:30 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit f17df0f9ce2a3ed25db33fbc6a2e7cabd33f657b Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:35:42 2020 +0300 home: move build constraint on top line commit 3717c8ef5a51f9dcaa7e524bfa7b0f1d90bec93d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:24:50 2020 +0300 all: add improvements to changelog commit de6d5afe39d74a3c3d3e0bbe6d0e09aea0214d56 Merge: 43d4f7acf 394fc5a9d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:04:38 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit 43d4f7acf188e810aa7277cb6479110c9842e8be Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 13:38:13 2020 +0300 dnsfilter: remove redundant test logging commit 7194c1413006b8f52fc454e89ab052bf52e4e517 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:19:14 2020 +0300 testutil: improve comments commit 9f17ab08e287fa69ce30d9e7eec8ea8880f87716 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:22:08 2020 +0300 nclient4: fix wrong function name commit f355749149b2a4485792ba2bdcbc0bb4b629d726 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:07:22 2020 +0300 testutil: fix comments and naming commit f8c50a260bfae08d594a7f37d603941d3680a45e Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 14:09:50 2020 +0300 testutil: create a package and include it commit f169cdc4084b719de65aa0cdc65200b48785322e Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:15:58 2020 +0300 agherr: discard loggers output commit 360e736b5a2a30f2c5350448234f14b841e3ea27 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:09:55 2020 +0300 all: replace default log writer with ioutil.Discard Closes #2273.
2020-11-16 12:52:05 +00:00
}
2022-08-17 16:23:30 +01:00
const (
sbBlocked = "wmconvirus.narod.ru"
pcBlocked = "pornhub.com"
)
// Helpers.
2022-09-29 15:36:01 +01:00
func newForTest(t testing.TB, c *Config, filters []Filter) (f *DNSFilter, setts *Settings) {
setts = &Settings{
ProtectionEnabled: true,
FilteringEnabled: true,
}
2019-07-23 18:01:50 +01:00
if c != nil {
* SB/PC: use 4-character hash in request * use hash prefix as the cache key Squashed commit of the following: commit d719a84ee9b9cf43aaab4f53d07451645ea836db Merge: d9d6d443 97df1989 Author: Simon Zolin <s.zolin@adguard.com> Date: Thu Aug 13 11:41:28 2020 +0300 Merge remote-tracking branch 'origin/master' into sbpc commit d9d6d44376c44959f2216b08e577d8e5c5f65bff Merge: 0a8b2483 de92c852 Author: Simon Zolin <s.zolin@adguard.com> Date: Thu Aug 13 11:23:14 2020 +0300 Merge remote-tracking branch 'origin/master' into sbpc commit 0a8b24839683683a9d327ecf57a7d182b3996b1d Merge: 0255a24a 9b9902f0 Author: Simon Zolin <s.zolin@adguard.com> Date: Wed Aug 12 20:21:22 2020 +0300 Merge remote-tracking branch 'origin/master' into sbpc commit 0255a24a191efd2e4ef23d6a00a7a9fed8831730 Author: Simon Zolin <s.zolin@adguard.com> Date: Wed Aug 12 17:16:57 2020 +0300 - TestServerCustomClientUpstream(): fix commit d2311902f887be9621a9d9312c73f899dd269440 Author: Simon Zolin <s.zolin@adguard.com> Date: Wed Aug 12 17:07:12 2020 +0300 * SB/PC: hard-code Family server IP addresses to prevent from requesting them at runtime commit ee340108f11f98d49a7af2a7e8a228c25ab1537a Author: Simon Zolin <s.zolin@adguard.com> Date: Wed Aug 12 17:05:57 2020 +0300 * dnsproxy v0.30.1 commit f5f53ba7116ad525204d00b80352202eee88b78c Author: Simon Zolin <s.zolin@adguard.com> Date: Wed Aug 12 14:53:23 2020 +0300 minor commit fb4631e2cd570b0fd5ae26ec2b1890361275a5a8 Author: Simon Zolin <s.zolin@adguard.com> Date: Mon Aug 10 20:07:27 2020 +0300 * SB/PC: implement new cache commit f9f58461a6efbcfacd798f7640a4645cf1971cb2 Author: Simon Zolin <s.zolin@adguard.com> Date: Fri Aug 7 19:31:05 2020 +0300 doc commit ed69626a6c119ab1a3b187f5afbd4cef708c3159 Author: Simon Zolin <s.zolin@adguard.com> Date: Fri Aug 7 18:25:57 2020 +0300 * SB/PC: use hostname prefix for cache commit afa8040c8c0836c7e59e6fb9aaf1caccd132ea8f Author: Simon Zolin <s.zolin@adguard.com> Date: Fri Jul 31 11:19:49 2020 +0300 * SB/PC: use 4-character hash in request
2020-08-13 09:49:42 +01:00
c.SafeBrowsingCacheSize = 10000
c.ParentalCacheSize = 10000
c.SafeSearchCacheSize = 1000
c.CacheTime = 30
2023-04-12 12:48:42 +01:00
setts.SafeSearchEnabled = c.SafeSearchConf.Enabled
2019-07-25 14:37:06 +01:00
setts.SafeBrowsingEnabled = c.SafeBrowsingEnabled
setts.ParentalEnabled = c.ParentalEnabled
2022-09-29 15:36:01 +01:00
} else {
// It must not be nil.
c = &Config{}
2019-07-23 18:01:50 +01:00
}
2022-09-29 15:36:01 +01:00
f, err := New(c, filters)
require.NoError(t, err)
return f, setts
2018-08-30 15:25:33 +01:00
}
2023-06-07 18:04:01 +01:00
func newChecker(host string) Checker {
return hashprefix.New(&hashprefix.Config{
CacheTime: 10,
CacheSize: 100000,
Upstream: aghtest.NewBlockUpstream(host, true),
})
}
2022-09-29 15:36:01 +01:00
func (d *DNSFilter) checkMatch(t *testing.T, hostname string, setts *Settings) {
2018-08-30 15:25:33 +01:00
t.Helper()
2022-09-29 15:36:01 +01:00
res, err := d.CheckHost(hostname, dns.TypeA, setts)
require.NoErrorf(t, err, "host %q", hostname)
assert.Truef(t, res.IsFiltered, "host %q", hostname)
2018-08-30 15:25:33 +01:00
}
2022-09-29 15:36:01 +01:00
func (d *DNSFilter) checkMatchIP(t *testing.T, hostname, ip string, qtype uint16, setts *Settings) {
t.Helper()
2022-09-29 15:36:01 +01:00
res, err := d.CheckHost(hostname, qtype, setts)
require.NoErrorf(t, err, "host %q", hostname, err)
require.NotEmpty(t, res.Rules, "host %q", hostname)
assert.Truef(t, res.IsFiltered, "host %q", hostname)
r := res.Rules[0]
require.NotNilf(t, r.IP, "Expected ip %s to match, actual: %v", ip, r.IP)
assert.Equalf(t, ip, r.IP.String(), "host %q", hostname)
}
2022-09-29 15:36:01 +01:00
func (d *DNSFilter) checkMatchEmpty(t *testing.T, hostname string, setts *Settings) {
2018-08-30 15:25:33 +01:00
t.Helper()
2022-09-29 15:36:01 +01:00
res, err := d.CheckHost(hostname, dns.TypeA, setts)
require.NoErrorf(t, err, "host %q", hostname)
assert.Falsef(t, res.IsFiltered, "host %q", hostname)
2018-08-30 15:25:33 +01:00
}
2023-09-07 15:13:48 +01:00
func TestDNSFilter_CheckHost_hostRules(t *testing.T) {
addr := "216.239.38.120"
2019-05-22 16:30:27 +01:00
addr6 := "::1"
text := fmt.Sprintf(` %s google.com www.google.com # enforce google's safesearch
%s ipv6.com
0.0.0.0 block.com
0.0.0.1 host2
0.0.0.2 host2
::1 host2
`,
2019-05-22 16:30:27 +01:00
addr, addr6)
filters := []Filter{{
ID: 0, Data: []byte(text),
}}
2022-09-29 15:36:01 +01:00
d, setts := newForTest(t, nil, filters)
t.Cleanup(d.Close)
2022-09-29 15:36:01 +01:00
d.checkMatchIP(t, "google.com", addr, dns.TypeA, setts)
d.checkMatchIP(t, "www.google.com", addr, dns.TypeA, setts)
d.checkMatchEmpty(t, "subdomain.google.com", setts)
d.checkMatchEmpty(t, "example.org", setts)
2019-05-22 16:30:27 +01:00
// IPv4 match.
2022-09-29 15:36:01 +01:00
d.checkMatchIP(t, "block.com", "0.0.0.0", dns.TypeA, setts)
// Empty IPv6.
2022-09-29 15:36:01 +01:00
res, err := d.CheckHost("block.com", dns.TypeAAAA, setts)
require.NoError(t, err)
assert.True(t, res.IsFiltered)
require.Len(t, res.Rules, 1)
assert.Equal(t, "0.0.0.0 block.com", res.Rules[0].Text)
assert.Empty(t, res.Rules[0].IP)
// IPv6 match.
2022-09-29 15:36:01 +01:00
d.checkMatchIP(t, "ipv6.com", addr6, dns.TypeAAAA, setts)
// Empty IPv4.
2022-09-29 15:36:01 +01:00
res, err = d.CheckHost("ipv6.com", dns.TypeA, setts)
require.NoError(t, err)
assert.True(t, res.IsFiltered)
require.Len(t, res.Rules, 1)
assert.Equal(t, "::1 ipv6.com", res.Rules[0].Text)
assert.Empty(t, res.Rules[0].IP)
// Two IPv4, both must be returned.
2022-09-29 15:36:01 +01:00
res, err = d.CheckHost("host2", dns.TypeA, setts)
require.NoError(t, err)
assert.True(t, res.IsFiltered)
require.Len(t, res.Rules, 2)
2023-09-07 15:13:48 +01:00
assert.Equal(t, res.Rules[0].IP, netip.AddrFrom4([4]byte{0, 0, 0, 1}))
assert.Equal(t, res.Rules[1].IP, netip.AddrFrom4([4]byte{0, 0, 0, 2}))
// One IPv6 address.
2022-09-29 15:36:01 +01:00
res, err = d.CheckHost("host2", dns.TypeAAAA, setts)
require.NoError(t, err)
assert.True(t, res.IsFiltered)
require.Len(t, res.Rules, 1)
2023-09-07 15:13:48 +01:00
assert.Equal(t, res.Rules[0].IP, netutil.IPv6Localhost())
}
// Safe Browsing.
2018-08-30 15:25:33 +01:00
func TestSafeBrowsing(t *testing.T) {
Pull request: cover with tests Merge in DNS/adguard-home from 2271-cover-with-tests to master Updates #2271. Squashed commit of the following: commit db6440efe05171bc15367a2996521848ca348053 Merge: db7fa726b bf4c256c7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:23:09 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit db7fa726bb91b08ec7aaa6c0c818c88b5feb87cd Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:26:51 2020 +0300 all: clean dependencies sum commit b8dc6078c4bcc0de1b7e9073832de122f6fe38a4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:46:00 2020 +0300 testutil: improve code quality commit 001b7194682b1f00aa54dc5a28236faed5a5b02d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:29:49 2020 +0300 testutil: enhance functionality commit f6ccd91a4df6c56778eab8ae50e88e3818b20dd3 Merge: 43fa2eefb 6358240e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:57:37 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit 43fa2eefbc10ef361603cacc1ca12092b12a057a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 14:55:15 2020 +0300 querylog: replace fake log with real in tests commit b95bee7565a14a02c80c78131b3ced224663dd8a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:38:59 2020 +0300 dnsfilter: replace thoughtless declaration with idiomatic one commit a210b1586092e7ae91a9e67c972fa2d2f6baded6 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 19:00:25 2020 +0300 all: refresh golibs dependencies commit 4ff97bd1ade6c80e274ff5716e44df4eba55bdd9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 18:38:47 2020 +0300 all: remove std log commit 542dbda10fefce9f46d15489712b163d919b1291 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 13:46:39 2020 +0300 querylog: improve test logic and readability commit 796d402385925e8e62a1b4c7bf56e4ceec22418c Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 19:06:42 2020 +0300 all: improve code quality commit e81894c11ef15b0453e8e5297f1349936a32f9dd Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 18:32:30 2020 +0300 all: cover with tests commit 252d81fc8a50a91b02cf0f6f35cc22178a2a4d90 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 17:32:01 2020 +0300 all: cover with tests
2020-11-16 16:45:31 +00:00
logOutput := &bytes.Buffer{}
aghtest.ReplaceLogWriter(t, logOutput)
aghtest.ReplaceLogLevel(t, log.DEBUG)
Pull request: cover with tests Merge in DNS/adguard-home from 2271-cover-with-tests to master Updates #2271. Squashed commit of the following: commit db6440efe05171bc15367a2996521848ca348053 Merge: db7fa726b bf4c256c7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:23:09 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit db7fa726bb91b08ec7aaa6c0c818c88b5feb87cd Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:26:51 2020 +0300 all: clean dependencies sum commit b8dc6078c4bcc0de1b7e9073832de122f6fe38a4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:46:00 2020 +0300 testutil: improve code quality commit 001b7194682b1f00aa54dc5a28236faed5a5b02d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:29:49 2020 +0300 testutil: enhance functionality commit f6ccd91a4df6c56778eab8ae50e88e3818b20dd3 Merge: 43fa2eefb 6358240e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:57:37 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit 43fa2eefbc10ef361603cacc1ca12092b12a057a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 14:55:15 2020 +0300 querylog: replace fake log with real in tests commit b95bee7565a14a02c80c78131b3ced224663dd8a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:38:59 2020 +0300 dnsfilter: replace thoughtless declaration with idiomatic one commit a210b1586092e7ae91a9e67c972fa2d2f6baded6 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 19:00:25 2020 +0300 all: refresh golibs dependencies commit 4ff97bd1ade6c80e274ff5716e44df4eba55bdd9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 18:38:47 2020 +0300 all: remove std log commit 542dbda10fefce9f46d15489712b163d919b1291 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 13:46:39 2020 +0300 querylog: improve test logic and readability commit 796d402385925e8e62a1b4c7bf56e4ceec22418c Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 19:06:42 2020 +0300 all: improve code quality commit e81894c11ef15b0453e8e5297f1349936a32f9dd Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 18:32:30 2020 +0300 all: cover with tests commit 252d81fc8a50a91b02cf0f6f35cc22178a2a4d90 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 17:32:01 2020 +0300 all: cover with tests
2020-11-16 16:45:31 +00:00
2023-06-07 18:04:01 +01:00
sbChecker := newChecker(sbBlocked)
d, setts := newForTest(t, &Config{
SafeBrowsingEnabled: true,
SafeBrowsingChecker: sbChecker,
}, nil)
t.Cleanup(d.Close)
Pull request: cover with tests Merge in DNS/adguard-home from 2271-cover-with-tests to master Updates #2271. Squashed commit of the following: commit db6440efe05171bc15367a2996521848ca348053 Merge: db7fa726b bf4c256c7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:23:09 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit db7fa726bb91b08ec7aaa6c0c818c88b5feb87cd Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:26:51 2020 +0300 all: clean dependencies sum commit b8dc6078c4bcc0de1b7e9073832de122f6fe38a4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:46:00 2020 +0300 testutil: improve code quality commit 001b7194682b1f00aa54dc5a28236faed5a5b02d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:29:49 2020 +0300 testutil: enhance functionality commit f6ccd91a4df6c56778eab8ae50e88e3818b20dd3 Merge: 43fa2eefb 6358240e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:57:37 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit 43fa2eefbc10ef361603cacc1ca12092b12a057a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 14:55:15 2020 +0300 querylog: replace fake log with real in tests commit b95bee7565a14a02c80c78131b3ced224663dd8a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:38:59 2020 +0300 dnsfilter: replace thoughtless declaration with idiomatic one commit a210b1586092e7ae91a9e67c972fa2d2f6baded6 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 19:00:25 2020 +0300 all: refresh golibs dependencies commit 4ff97bd1ade6c80e274ff5716e44df4eba55bdd9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 18:38:47 2020 +0300 all: remove std log commit 542dbda10fefce9f46d15489712b163d919b1291 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 13:46:39 2020 +0300 querylog: improve test logic and readability commit 796d402385925e8e62a1b4c7bf56e4ceec22418c Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 19:06:42 2020 +0300 all: improve code quality commit e81894c11ef15b0453e8e5297f1349936a32f9dd Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 18:32:30 2020 +0300 all: cover with tests commit 252d81fc8a50a91b02cf0f6f35cc22178a2a4d90 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 17:32:01 2020 +0300 all: cover with tests
2020-11-16 16:45:31 +00:00
2022-09-29 15:36:01 +01:00
d.checkMatch(t, sbBlocked, setts)
2022-08-17 16:23:30 +01:00
require.Contains(t, logOutput.String(), fmt.Sprintf("safebrowsing lookup for %q", sbBlocked))
Pull request: cover with tests Merge in DNS/adguard-home from 2271-cover-with-tests to master Updates #2271. Squashed commit of the following: commit db6440efe05171bc15367a2996521848ca348053 Merge: db7fa726b bf4c256c7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:23:09 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit db7fa726bb91b08ec7aaa6c0c818c88b5feb87cd Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:26:51 2020 +0300 all: clean dependencies sum commit b8dc6078c4bcc0de1b7e9073832de122f6fe38a4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:46:00 2020 +0300 testutil: improve code quality commit 001b7194682b1f00aa54dc5a28236faed5a5b02d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:29:49 2020 +0300 testutil: enhance functionality commit f6ccd91a4df6c56778eab8ae50e88e3818b20dd3 Merge: 43fa2eefb 6358240e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:57:37 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit 43fa2eefbc10ef361603cacc1ca12092b12a057a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 14:55:15 2020 +0300 querylog: replace fake log with real in tests commit b95bee7565a14a02c80c78131b3ced224663dd8a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:38:59 2020 +0300 dnsfilter: replace thoughtless declaration with idiomatic one commit a210b1586092e7ae91a9e67c972fa2d2f6baded6 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 19:00:25 2020 +0300 all: refresh golibs dependencies commit 4ff97bd1ade6c80e274ff5716e44df4eba55bdd9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 18:38:47 2020 +0300 all: remove std log commit 542dbda10fefce9f46d15489712b163d919b1291 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 13:46:39 2020 +0300 querylog: improve test logic and readability commit 796d402385925e8e62a1b4c7bf56e4ceec22418c Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 19:06:42 2020 +0300 all: improve code quality commit e81894c11ef15b0453e8e5297f1349936a32f9dd Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 18:32:30 2020 +0300 all: cover with tests commit 252d81fc8a50a91b02cf0f6f35cc22178a2a4d90 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 17:32:01 2020 +0300 all: cover with tests
2020-11-16 16:45:31 +00:00
2022-09-29 15:36:01 +01:00
d.checkMatch(t, "test."+sbBlocked, setts)
d.checkMatchEmpty(t, "yandex.ru", setts)
d.checkMatchEmpty(t, pcBlocked, setts)
// Cached result.
2022-09-29 15:36:01 +01:00
d.checkMatch(t, sbBlocked, setts)
d.checkMatchEmpty(t, pcBlocked, setts)
2018-08-30 15:25:33 +01:00
}
func TestParallelSB(t *testing.T) {
2023-06-07 18:04:01 +01:00
d, setts := newForTest(t, &Config{
SafeBrowsingEnabled: true,
SafeBrowsingChecker: newChecker(sbBlocked),
}, nil)
t.Cleanup(d.Close)
2022-08-17 16:23:30 +01:00
2018-08-30 15:25:33 +01:00
t.Run("group", func(t *testing.T) {
2024-05-15 11:34:12 +01:00
for i := range 100 {
2018-08-30 15:25:33 +01:00
t.Run(fmt.Sprintf("aaa%d", i), func(t *testing.T) {
t.Parallel()
2022-09-29 15:36:01 +01:00
d.checkMatch(t, sbBlocked, setts)
d.checkMatch(t, "test."+sbBlocked, setts)
d.checkMatchEmpty(t, "yandex.ru", setts)
d.checkMatchEmpty(t, pcBlocked, setts)
2018-08-30 15:25:33 +01:00
})
}
})
}
// Parental.
2018-08-30 15:25:33 +01:00
func TestParentalControl(t *testing.T) {
Pull request: cover with tests Merge in DNS/adguard-home from 2271-cover-with-tests to master Updates #2271. Squashed commit of the following: commit db6440efe05171bc15367a2996521848ca348053 Merge: db7fa726b bf4c256c7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:23:09 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit db7fa726bb91b08ec7aaa6c0c818c88b5feb87cd Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:26:51 2020 +0300 all: clean dependencies sum commit b8dc6078c4bcc0de1b7e9073832de122f6fe38a4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:46:00 2020 +0300 testutil: improve code quality commit 001b7194682b1f00aa54dc5a28236faed5a5b02d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:29:49 2020 +0300 testutil: enhance functionality commit f6ccd91a4df6c56778eab8ae50e88e3818b20dd3 Merge: 43fa2eefb 6358240e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:57:37 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit 43fa2eefbc10ef361603cacc1ca12092b12a057a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 14:55:15 2020 +0300 querylog: replace fake log with real in tests commit b95bee7565a14a02c80c78131b3ced224663dd8a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:38:59 2020 +0300 dnsfilter: replace thoughtless declaration with idiomatic one commit a210b1586092e7ae91a9e67c972fa2d2f6baded6 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 19:00:25 2020 +0300 all: refresh golibs dependencies commit 4ff97bd1ade6c80e274ff5716e44df4eba55bdd9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 18:38:47 2020 +0300 all: remove std log commit 542dbda10fefce9f46d15489712b163d919b1291 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 13:46:39 2020 +0300 querylog: improve test logic and readability commit 796d402385925e8e62a1b4c7bf56e4ceec22418c Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 19:06:42 2020 +0300 all: improve code quality commit e81894c11ef15b0453e8e5297f1349936a32f9dd Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 18:32:30 2020 +0300 all: cover with tests commit 252d81fc8a50a91b02cf0f6f35cc22178a2a4d90 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 17:32:01 2020 +0300 all: cover with tests
2020-11-16 16:45:31 +00:00
logOutput := &bytes.Buffer{}
aghtest.ReplaceLogWriter(t, logOutput)
aghtest.ReplaceLogLevel(t, log.DEBUG)
Pull request: cover with tests Merge in DNS/adguard-home from 2271-cover-with-tests to master Updates #2271. Squashed commit of the following: commit db6440efe05171bc15367a2996521848ca348053 Merge: db7fa726b bf4c256c7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:23:09 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit db7fa726bb91b08ec7aaa6c0c818c88b5feb87cd Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:26:51 2020 +0300 all: clean dependencies sum commit b8dc6078c4bcc0de1b7e9073832de122f6fe38a4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:46:00 2020 +0300 testutil: improve code quality commit 001b7194682b1f00aa54dc5a28236faed5a5b02d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:29:49 2020 +0300 testutil: enhance functionality commit f6ccd91a4df6c56778eab8ae50e88e3818b20dd3 Merge: 43fa2eefb 6358240e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:57:37 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit 43fa2eefbc10ef361603cacc1ca12092b12a057a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 14:55:15 2020 +0300 querylog: replace fake log with real in tests commit b95bee7565a14a02c80c78131b3ced224663dd8a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:38:59 2020 +0300 dnsfilter: replace thoughtless declaration with idiomatic one commit a210b1586092e7ae91a9e67c972fa2d2f6baded6 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 19:00:25 2020 +0300 all: refresh golibs dependencies commit 4ff97bd1ade6c80e274ff5716e44df4eba55bdd9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 18:38:47 2020 +0300 all: remove std log commit 542dbda10fefce9f46d15489712b163d919b1291 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 13:46:39 2020 +0300 querylog: improve test logic and readability commit 796d402385925e8e62a1b4c7bf56e4ceec22418c Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 19:06:42 2020 +0300 all: improve code quality commit e81894c11ef15b0453e8e5297f1349936a32f9dd Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 18:32:30 2020 +0300 all: cover with tests commit 252d81fc8a50a91b02cf0f6f35cc22178a2a4d90 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 17:32:01 2020 +0300 all: cover with tests
2020-11-16 16:45:31 +00:00
2023-06-07 18:04:01 +01:00
d, setts := newForTest(t, &Config{
ParentalEnabled: true,
ParentalControlChecker: newChecker(pcBlocked),
}, nil)
t.Cleanup(d.Close)
2022-09-29 15:36:01 +01:00
d.checkMatch(t, pcBlocked, setts)
2022-08-17 16:23:30 +01:00
require.Contains(t, logOutput.String(), fmt.Sprintf("parental lookup for %q", pcBlocked))
2022-09-29 15:36:01 +01:00
d.checkMatch(t, "www."+pcBlocked, setts)
d.checkMatchEmpty(t, "www.yandex.ru", setts)
d.checkMatchEmpty(t, "yandex.ru", setts)
d.checkMatchEmpty(t, "api.jquery.com", setts)
// Test cached result.
2022-09-29 15:36:01 +01:00
d.checkMatch(t, pcBlocked, setts)
d.checkMatchEmpty(t, "yandex.ru", setts)
2018-08-30 15:25:33 +01:00
}
// Filtering.
2018-08-30 15:25:33 +01:00
func TestMatching(t *testing.T) {
const nl = "\n"
const (
blockingRules = `||example.org^` + nl
allowlistRules = `||example.org^` + nl + `@@||test.example.org` + nl
importantRules = `@@||example.org^` + nl + `||test.example.org^$important` + nl
regexRules = `/example\.org/` + nl + `@@||test.example.org^` + nl
maskRules = `test*.example.org^` + nl + `exam*.com` + nl
dnstypeRules = `||example.org^$dnstype=AAAA` + nl + `@@||test.example.org^` + nl
)
testCases := []struct {
name string
rules string
host string
wantReason Reason
wantIsFiltered bool
wantDNSType uint16
}{{
name: "sanity",
rules: "||doubleclick.net^",
host: "www.doubleclick.net",
wantIsFiltered: true,
wantReason: FilteredBlockList,
wantDNSType: dns.TypeA,
}, {
name: "sanity",
rules: "||doubleclick.net^",
host: "nodoubleclick.net",
wantIsFiltered: false,
wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA,
}, {
name: "sanity",
rules: "||doubleclick.net^",
host: "doubleclick.net.ru",
wantIsFiltered: false,
wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA,
}, {
name: "sanity",
rules: "||doubleclick.net^",
2022-08-17 16:23:30 +01:00
host: sbBlocked,
wantIsFiltered: false,
wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA,
}, {
name: "blocking",
rules: blockingRules,
host: "example.org",
wantIsFiltered: true,
wantReason: FilteredBlockList,
wantDNSType: dns.TypeA,
}, {
name: "blocking",
rules: blockingRules,
host: "test.example.org",
wantIsFiltered: true,
wantReason: FilteredBlockList,
wantDNSType: dns.TypeA,
}, {
name: "blocking",
rules: blockingRules,
host: "test.test.example.org",
wantIsFiltered: true,
wantReason: FilteredBlockList,
wantDNSType: dns.TypeA,
}, {
name: "blocking",
rules: blockingRules,
host: "testexample.org",
wantIsFiltered: false,
wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA,
}, {
name: "blocking",
rules: blockingRules,
host: "onemoreexample.org",
wantIsFiltered: false,
wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA,
}, {
name: "allowlist",
rules: allowlistRules,
host: "example.org",
wantIsFiltered: true,
wantReason: FilteredBlockList,
wantDNSType: dns.TypeA,
}, {
name: "allowlist",
rules: allowlistRules,
host: "test.example.org",
wantIsFiltered: false,
wantReason: NotFilteredAllowList,
wantDNSType: dns.TypeA,
}, {
name: "allowlist",
rules: allowlistRules,
host: "test.test.example.org",
wantIsFiltered: false,
wantReason: NotFilteredAllowList,
wantDNSType: dns.TypeA,
}, {
name: "allowlist",
rules: allowlistRules,
host: "testexample.org",
wantIsFiltered: false,
wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA,
}, {
name: "allowlist",
rules: allowlistRules,
host: "onemoreexample.org",
wantIsFiltered: false,
wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA,
}, {
name: "important",
rules: importantRules,
host: "example.org",
wantIsFiltered: false,
wantReason: NotFilteredAllowList,
wantDNSType: dns.TypeA,
}, {
name: "important",
rules: importantRules,
host: "test.example.org",
wantIsFiltered: true,
wantReason: FilteredBlockList,
wantDNSType: dns.TypeA,
}, {
name: "important",
rules: importantRules,
host: "test.test.example.org",
wantIsFiltered: true,
wantReason: FilteredBlockList,
wantDNSType: dns.TypeA,
}, {
name: "important",
rules: importantRules,
host: "testexample.org",
wantIsFiltered: false,
wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA,
}, {
name: "important",
rules: importantRules,
host: "onemoreexample.org",
wantIsFiltered: false,
wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA,
}, {
name: "regex",
rules: regexRules,
host: "example.org",
wantIsFiltered: true,
wantReason: FilteredBlockList,
wantDNSType: dns.TypeA,
}, {
name: "regex",
rules: regexRules,
host: "test.example.org",
wantIsFiltered: false,
wantReason: NotFilteredAllowList,
wantDNSType: dns.TypeA,
}, {
name: "regex",
rules: regexRules,
host: "test.test.example.org",
wantIsFiltered: false,
wantReason: NotFilteredAllowList,
wantDNSType: dns.TypeA,
}, {
name: "regex",
rules: regexRules,
host: "testexample.org",
wantIsFiltered: true,
wantReason: FilteredBlockList,
wantDNSType: dns.TypeA,
}, {
name: "regex",
rules: regexRules,
host: "onemoreexample.org",
wantIsFiltered: true,
wantReason: FilteredBlockList,
wantDNSType: dns.TypeA,
}, {
name: "mask",
rules: maskRules,
host: "test.example.org",
wantIsFiltered: true,
wantReason: FilteredBlockList,
wantDNSType: dns.TypeA,
}, {
name: "mask",
rules: maskRules,
host: "test2.example.org",
wantIsFiltered: true,
wantReason: FilteredBlockList,
wantDNSType: dns.TypeA,
}, {
name: "mask",
rules: maskRules,
host: "example.com",
wantIsFiltered: true,
wantReason: FilteredBlockList,
wantDNSType: dns.TypeA,
}, {
name: "mask",
rules: maskRules,
host: "exampleeee.com",
wantIsFiltered: true,
wantReason: FilteredBlockList,
wantDNSType: dns.TypeA,
}, {
name: "mask",
rules: maskRules,
host: "onemoreexamsite.com",
wantIsFiltered: true,
wantReason: FilteredBlockList,
wantDNSType: dns.TypeA,
}, {
name: "mask",
rules: maskRules,
host: "example.org",
wantIsFiltered: false,
wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA,
}, {
name: "mask",
rules: maskRules,
host: "testexample.org",
wantIsFiltered: false,
wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA,
}, {
name: "mask",
rules: maskRules,
host: "example.co.uk",
wantIsFiltered: false,
wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA,
}, {
name: "dnstype",
rules: dnstypeRules,
host: "onemoreexample.org",
wantIsFiltered: false,
wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA,
}, {
name: "dnstype",
rules: dnstypeRules,
host: "example.org",
wantIsFiltered: false,
wantReason: NotFilteredNotFound,
wantDNSType: dns.TypeA,
}, {
name: "dnstype",
rules: dnstypeRules,
host: "example.org",
wantIsFiltered: true,
wantReason: FilteredBlockList,
wantDNSType: dns.TypeAAAA,
}, {
name: "dnstype",
rules: dnstypeRules,
host: "test.example.org",
wantIsFiltered: false,
wantReason: NotFilteredAllowList,
wantDNSType: dns.TypeA,
}, {
name: "dnstype",
rules: dnstypeRules,
host: "test.example.org",
wantIsFiltered: false,
wantReason: NotFilteredAllowList,
wantDNSType: dns.TypeAAAA,
}}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%s-%s", tc.name, tc.host), func(t *testing.T) {
filters := []Filter{{ID: 0, Data: []byte(tc.rules)}}
2022-09-29 15:36:01 +01:00
d, setts := newForTest(t, nil, filters)
t.Cleanup(d.Close)
2022-09-29 15:36:01 +01:00
res, err := d.CheckHost(tc.host, tc.wantDNSType, setts)
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.wantReason, res.Reason, "Hostname %s has wrong reason (%v must be %v)", tc.host, res.Reason, tc.wantReason)
2018-08-30 15:25:33 +01:00
})
}
}
func TestWhitelist(t *testing.T) {
rules := `||host1^
||host2^
`
filters := []Filter{{
ID: 0, Data: []byte(rules),
}}
whiteRules := `||host1^
||host3^
`
whiteFilters := []Filter{{
ID: 0, Data: []byte(whiteRules),
}}
2022-09-29 15:36:01 +01:00
d, setts := newForTest(t, nil, filters)
2023-07-12 13:13:31 +01:00
err := d.setFilters(filters, whiteFilters, false)
require.NoError(t, err)
t.Cleanup(d.Close)
// Matched by white filter.
2022-09-29 15:36:01 +01:00
res, err := d.CheckHost("host1", dns.TypeA, setts)
require.NoError(t, err)
Pull request: 2508 ip conversion vol.1 Merge in DNS/adguard-home from 2508-ip-conversion to master Updates #2508. Squashed commit of the following: commit 3f64709fbc73ef74c11b910997be1e9bc337193c Merge: 5ac7faaaa 0d67aa251 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 16:21:34 2021 +0300 Merge branch 'master' into 2508-ip-conversion commit 5ac7faaaa9dda570fdb872acad5d13d078f46b64 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 12:00:11 2021 +0300 all: replace conditions with appropriate functions in tests commit 9e3fa9a115ed23024c57dd5192d5173477ddbf71 Merge: db992a42a bba74859e Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 10:47:10 2021 +0300 Merge branch 'master' into 2508-ip-conversion commit db992a42a2c6f315421e78a6a0492e2bfb3ce89d Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 18:55:53 2021 +0300 sysutil: fix linux tests commit f629b15d62349323ce2da05e68dc9cc0b5f6e194 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 18:41:20 2021 +0300 all: improve code quality commit 3bf03a75524040738562298bd1de6db536af130f Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 17:33:26 2021 +0300 sysutil: fix linux net.IP conversion commit 5d5b6994916923636e635588631b63b7e7b74e5f Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 14:57:26 2021 +0300 dnsforward: remove redundant net.IP <-> string conversion commit 0b955d99b7fad40942f21d1dd8734adb99126195 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Jan 11 18:04:25 2021 +0300 dhcpd: remove net.IP <-> string conversion
2021-01-13 13:56:05 +00:00
assert.False(t, res.IsFiltered)
assert.Equal(t, res.Reason, NotFilteredAllowList)
require.Len(t, res.Rules, 1)
assert.Equal(t, "||host1^", res.Rules[0].Text)
// Not matched by white filter, but matched by block filter.
2022-09-29 15:36:01 +01:00
res, err = d.CheckHost("host2", dns.TypeA, setts)
require.NoError(t, err)
Pull request: 2508 ip conversion vol.1 Merge in DNS/adguard-home from 2508-ip-conversion to master Updates #2508. Squashed commit of the following: commit 3f64709fbc73ef74c11b910997be1e9bc337193c Merge: 5ac7faaaa 0d67aa251 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 16:21:34 2021 +0300 Merge branch 'master' into 2508-ip-conversion commit 5ac7faaaa9dda570fdb872acad5d13d078f46b64 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 12:00:11 2021 +0300 all: replace conditions with appropriate functions in tests commit 9e3fa9a115ed23024c57dd5192d5173477ddbf71 Merge: db992a42a bba74859e Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 10:47:10 2021 +0300 Merge branch 'master' into 2508-ip-conversion commit db992a42a2c6f315421e78a6a0492e2bfb3ce89d Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 18:55:53 2021 +0300 sysutil: fix linux tests commit f629b15d62349323ce2da05e68dc9cc0b5f6e194 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 18:41:20 2021 +0300 all: improve code quality commit 3bf03a75524040738562298bd1de6db536af130f Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 17:33:26 2021 +0300 sysutil: fix linux net.IP conversion commit 5d5b6994916923636e635588631b63b7e7b74e5f Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 14:57:26 2021 +0300 dnsforward: remove redundant net.IP <-> string conversion commit 0b955d99b7fad40942f21d1dd8734adb99126195 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Jan 11 18:04:25 2021 +0300 dhcpd: remove net.IP <-> string conversion
2021-01-13 13:56:05 +00:00
assert.True(t, res.IsFiltered)
assert.Equal(t, res.Reason, FilteredBlockList)
require.Len(t, res.Rules, 1)
assert.Equal(t, "||host2^", res.Rules[0].Text)
}
// Client Settings.
func applyClientSettings(setts *Settings) {
setts.FilteringEnabled = false
setts.ParentalEnabled = false
setts.SafeBrowsingEnabled = true
rule, _ := rules.NewNetworkRule("||facebook.com^", 0)
s := ServiceEntry{}
s.Name = "facebook"
s.Rules = []*rules.NetworkRule{rule}
setts.ServicesRules = append(setts.ServicesRules, s)
}
func TestClientSettings(t *testing.T) {
2022-09-29 15:36:01 +01:00
d, setts := newForTest(t,
&Config{
2023-06-07 18:04:01 +01:00
ParentalEnabled: true,
SafeBrowsingEnabled: false,
SafeBrowsingChecker: newChecker(sbBlocked),
ParentalControlChecker: newChecker(pcBlocked),
},
[]Filter{{
ID: 0, Data: []byte("||example.org^\n"),
}},
)
t.Cleanup(d.Close)
2022-08-17 16:23:30 +01:00
type testCase struct {
name string
host string
before bool
wantReason Reason
}
testCases := []testCase{{
name: "filters",
host: "example.org",
before: true,
wantReason: FilteredBlockList,
}, {
name: "parental",
2022-08-17 16:23:30 +01:00
host: pcBlocked,
before: true,
wantReason: FilteredParental,
}, {
name: "safebrowsing",
2022-08-17 16:23:30 +01:00
host: sbBlocked,
before: false,
wantReason: FilteredSafeBrowsing,
}, {
name: "additional_rules",
host: "facebook.com",
before: false,
wantReason: FilteredBlockedService,
}}
makeTester := func(tc testCase, before bool) func(t *testing.T) {
return func(t *testing.T) {
t.Helper()
2022-09-29 15:36:01 +01:00
r, err := d.CheckHost(tc.host, dns.TypeA, setts)
require.NoError(t, err)
if before {
assert.True(t, r.IsFiltered)
assert.Equal(t, tc.wantReason, r.Reason)
} else {
assert.False(t, r.IsFiltered)
}
}
}
// Check behaviour without any per-client settings, then apply per-client
// settings and check behavior once again.
for _, tc := range testCases {
t.Run(tc.name, makeTester(tc, tc.before))
}
2022-09-29 15:36:01 +01:00
applyClientSettings(setts)
for _, tc := range testCases {
t.Run(tc.name, makeTester(tc, !tc.before))
}
}
// Benchmarks.
2018-08-30 15:25:33 +01:00
func BenchmarkSafeBrowsing(b *testing.B) {
2023-06-07 18:04:01 +01:00
d, setts := newForTest(b, &Config{
SafeBrowsingEnabled: true,
SafeBrowsingChecker: newChecker(sbBlocked),
}, nil)
b.Cleanup(d.Close)
2022-08-17 16:23:30 +01:00
2024-05-15 11:34:12 +01:00
for range b.N {
2022-09-29 15:36:01 +01:00
res, err := d.CheckHost(sbBlocked, dns.TypeA, setts)
require.NoError(b, err)
2022-08-17 16:23:30 +01:00
assert.Truef(b, res.IsFiltered, "expected hostname %q to match", sbBlocked)
2018-08-30 15:25:33 +01:00
}
}
func BenchmarkSafeBrowsingParallel(b *testing.B) {
2023-06-07 18:04:01 +01:00
d, setts := newForTest(b, &Config{
SafeBrowsingEnabled: true,
SafeBrowsingChecker: newChecker(sbBlocked),
}, nil)
b.Cleanup(d.Close)
2022-08-17 16:23:30 +01:00
2018-08-30 15:25:33 +01:00
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
2022-09-29 15:36:01 +01:00
res, err := d.CheckHost(sbBlocked, dns.TypeA, setts)
require.NoError(b, err)
2022-08-17 16:23:30 +01:00
assert.Truef(b, res.IsFiltered, "expected hostname %q to match", sbBlocked)
2018-08-30 15:25:33 +01:00
}
})
}