dnsfilter_test.go (AdGuardHome-0.104.1) | : | dnsfilter_test.go (AdGuardHome-0.104.3) | ||
---|---|---|---|---|
package dnsfilter | package dnsfilter | |||
import ( | import ( | |||
"bytes" | ||||
"fmt" | "fmt" | |||
"net" | "net" | |||
"strings" | ||||
"testing" | "testing" | |||
"github.com/AdguardTeam/AdGuardHome/internal/testutil" | ||||
"github.com/AdguardTeam/golibs/log" | ||||
"github.com/AdguardTeam/urlfilter/rules" | "github.com/AdguardTeam/urlfilter/rules" | |||
"github.com/miekg/dns" | "github.com/miekg/dns" | |||
"github.com/stretchr/testify/assert" | "github.com/stretchr/testify/assert" | |||
) | ) | |||
func TestMain(m *testing.M) { | ||||
testutil.DiscardLogOutput(m) | ||||
} | ||||
var setts RequestFilteringSettings | var setts RequestFilteringSettings | |||
// HELPERS | // HELPERS | |||
// SAFE BROWSING | // SAFE BROWSING | |||
// SAFE SEARCH | // SAFE SEARCH | |||
// PARENTAL | // PARENTAL | |||
// FILTERING | // FILTERING | |||
// BENCHMARKS | // BENCHMARKS | |||
// HELPERS | // HELPERS | |||
skipping to change at line 140 | skipping to change at line 148 | |||
// ...and 1 IPv6 address | // ...and 1 IPv6 address | |||
ret, err = d.CheckHost("host2", dns.TypeAAAA, &setts) | ret, err = d.CheckHost("host2", dns.TypeAAAA, &setts) | |||
assert.True(t, err == nil && ret.IsFiltered) | assert.True(t, err == nil && ret.IsFiltered) | |||
assert.True(t, ret.IP != nil && ret.IP.Equal(net.ParseIP("::1"))) | assert.True(t, ret.IP != nil && ret.IP.Equal(net.ParseIP("::1"))) | |||
} | } | |||
// SAFE BROWSING | // SAFE BROWSING | |||
func TestSafeBrowsing(t *testing.T) { | func TestSafeBrowsing(t *testing.T) { | |||
logOutput := &bytes.Buffer{} | ||||
testutil.ReplaceLogWriter(t, logOutput) | ||||
testutil.ReplaceLogLevel(t, log.DEBUG) | ||||
d := NewForTest(&Config{SafeBrowsingEnabled: true}, nil) | d := NewForTest(&Config{SafeBrowsingEnabled: true}, nil) | |||
defer d.Close() | defer d.Close() | |||
gctx.stats.Safebrowsing.Requests = 0 | gctx.stats.Safebrowsing.Requests = 0 | |||
d.checkMatch(t, "wmconvirus.narod.ru") | d.checkMatch(t, "wmconvirus.narod.ru") | |||
assert.True(t, strings.Contains(logOutput.String(), "SafeBrowsing lookup | ||||
for wmconvirus.narod.ru")) | ||||
d.checkMatch(t, "test.wmconvirus.narod.ru") | d.checkMatch(t, "test.wmconvirus.narod.ru") | |||
d.checkMatchEmpty(t, "yandex.ru") | d.checkMatchEmpty(t, "yandex.ru") | |||
d.checkMatchEmpty(t, "pornhub.com") | d.checkMatchEmpty(t, "pornhub.com") | |||
// test cached result | // test cached result | |||
d.safeBrowsingServer = "127.0.0.1" | d.safeBrowsingServer = "127.0.0.1" | |||
d.checkMatch(t, "wmconvirus.narod.ru") | d.checkMatch(t, "wmconvirus.narod.ru") | |||
d.checkMatchEmpty(t, "pornhub.com") | d.checkMatchEmpty(t, "pornhub.com") | |||
d.safeBrowsingServer = defaultSafebrowsingServer | d.safeBrowsingServer = defaultSafebrowsingServer | |||
} | } | |||
skipping to change at line 293 | skipping to change at line 308 | |||
safeDomain, ok := d.SafeSearchDomain(domain) | safeDomain, ok := d.SafeSearchDomain(domain) | |||
if !ok { | if !ok { | |||
t.Fatalf("Failed to get safesearch domain for %s", domain) | t.Fatalf("Failed to get safesearch domain for %s", domain) | |||
} | } | |||
ips, err := net.LookupIP(safeDomain) | ips, err := net.LookupIP(safeDomain) | |||
if err != nil { | if err != nil { | |||
t.Fatalf("Failed to lookup for %s", safeDomain) | t.Fatalf("Failed to lookup for %s", safeDomain) | |||
} | } | |||
t.Logf("IP addresses: %v", ips) | ||||
ip := ips[0] | ip := ips[0] | |||
for _, i := range ips { | for _, i := range ips { | |||
if i.To4() != nil { | if i.To4() != nil { | |||
ip = i | ip = i | |||
break | break | |||
} | } | |||
} | } | |||
result, err = d.CheckHost(domain, dns.TypeA, &setts) | result, err = d.CheckHost(domain, dns.TypeA, &setts) | |||
if err != nil { | if err != nil { | |||
skipping to change at line 327 | skipping to change at line 341 | |||
} | } | |||
if cachedValue.IP.String() != ip.String() { | if cachedValue.IP.String() != ip.String() { | |||
t.Fatalf("Wrong IP in cache for %s safesearch: %s", domain, cache dValue.IP.String()) | t.Fatalf("Wrong IP in cache for %s safesearch: %s", domain, cache dValue.IP.String()) | |||
} | } | |||
} | } | |||
// PARENTAL | // PARENTAL | |||
func TestParentalControl(t *testing.T) { | func TestParentalControl(t *testing.T) { | |||
logOutput := &bytes.Buffer{} | ||||
testutil.ReplaceLogWriter(t, logOutput) | ||||
testutil.ReplaceLogLevel(t, log.DEBUG) | ||||
d := NewForTest(&Config{ParentalEnabled: true}, nil) | d := NewForTest(&Config{ParentalEnabled: true}, nil) | |||
defer d.Close() | defer d.Close() | |||
d.checkMatch(t, "pornhub.com") | d.checkMatch(t, "pornhub.com") | |||
assert.True(t, strings.Contains(logOutput.String(), "Parental lookup for pornhub.com")) | ||||
d.checkMatch(t, "www.pornhub.com") | d.checkMatch(t, "www.pornhub.com") | |||
d.checkMatchEmpty(t, "www.yandex.ru") | d.checkMatchEmpty(t, "www.yandex.ru") | |||
d.checkMatchEmpty(t, "yandex.ru") | d.checkMatchEmpty(t, "yandex.ru") | |||
d.checkMatchEmpty(t, "api.jquery.com") | d.checkMatchEmpty(t, "api.jquery.com") | |||
// test cached result | // test cached result | |||
d.parentalServer = "127.0.0.1" | d.parentalServer = "127.0.0.1" | |||
d.checkMatch(t, "pornhub.com") | d.checkMatch(t, "pornhub.com") | |||
d.checkMatchEmpty(t, "yandex.ru") | d.checkMatchEmpty(t, "yandex.ru") | |||
d.parentalServer = defaultParentalServer | d.parentalServer = defaultParentalServer | |||
End of changes. 9 change blocks. | ||||
1 lines changed or deleted | 21 lines changed or added |