Merge: * dnsfilter: fix tests: pass config object to NewForTest()

* commit '64f66cfb5d71e34d59977925fd9453a21fe2cd1a':
  * dnsfilter: fix tests: pass config object to NewForTest()
This commit is contained in:
Simon Zolin 2019-07-01 19:24:53 +03:00
commit 8e3f05e538
1 changed files with 47 additions and 62 deletions

View File

@ -24,11 +24,14 @@ import (
// HELPERS // HELPERS
func purgeCaches() { func purgeCaches() {
if safebrowsingCache != nil { if gctx.safebrowsingCache != nil {
safebrowsingCache.Purge() gctx.safebrowsingCache.Purge()
} }
if parentalCache != nil { if gctx.parentalCache != nil {
parentalCache.Purge() gctx.parentalCache.Purge()
}
if gctx.safeSearchCache != nil {
gctx.safeSearchCache.Purge()
} }
} }
@ -39,14 +42,8 @@ func _Func() string {
return path.Base(f.Name()) return path.Base(f.Name())
} }
func NewForTest() *Dnsfilter { func NewForTest(c *Config, filters map[int]string) *Dnsfilter {
d := New(nil, nil) d := New(c, filters)
purgeCaches()
return d
}
func NewForTestFilters(filters map[int]string) *Dnsfilter {
d := New(nil, filters)
purgeCaches() purgeCaches()
return d return d
} }
@ -94,7 +91,7 @@ func TestEtcHostsMatching(t *testing.T) {
addr, addr6) addr, addr6)
filters := make(map[int]string) filters := make(map[int]string)
filters[0] = text filters[0] = text
d := NewForTestFilters(filters) d := NewForTest(nil, filters)
defer d.Destroy() defer d.Destroy()
d.checkMatchIP(t, "google.com", addr, dns.TypeA) d.checkMatchIP(t, "google.com", addr, dns.TypeA)
@ -119,37 +116,35 @@ func TestSafeBrowsing(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
t.Run(fmt.Sprintf("%s in %s", tc, _Func()), func(t *testing.T) { t.Run(fmt.Sprintf("%s in %s", tc, _Func()), func(t *testing.T) {
d := NewForTest() d := NewForTest(&Config{SafeBrowsingEnabled: true}, nil)
defer d.Destroy() defer d.Destroy()
d.SafeBrowsingEnabled = true gctx.stats.Safebrowsing.Requests = 0
stats.Safebrowsing.Requests = 0
d.checkMatch(t, "wmconvirus.narod.ru") d.checkMatch(t, "wmconvirus.narod.ru")
d.checkMatch(t, "wmconvirus.narod.ru") d.checkMatch(t, "wmconvirus.narod.ru")
if stats.Safebrowsing.Requests != 1 { if gctx.stats.Safebrowsing.Requests != 1 {
t.Errorf("Safebrowsing lookup positive cache is not working: %v", stats.Safebrowsing.Requests) t.Errorf("Safebrowsing lookup positive cache is not working: %v", gctx.stats.Safebrowsing.Requests)
} }
d.checkMatch(t, "WMconvirus.narod.ru") d.checkMatch(t, "WMconvirus.narod.ru")
if stats.Safebrowsing.Requests != 1 { if gctx.stats.Safebrowsing.Requests != 1 {
t.Errorf("Safebrowsing lookup positive cache is not working: %v", stats.Safebrowsing.Requests) t.Errorf("Safebrowsing lookup positive cache is not working: %v", gctx.stats.Safebrowsing.Requests)
} }
d.checkMatch(t, "wmconvirus.narod.ru.") d.checkMatch(t, "wmconvirus.narod.ru.")
d.checkMatch(t, "test.wmconvirus.narod.ru") d.checkMatch(t, "test.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")
l := stats.Safebrowsing.Requests l := gctx.stats.Safebrowsing.Requests
d.checkMatchEmpty(t, "pornhub.com") d.checkMatchEmpty(t, "pornhub.com")
if stats.Safebrowsing.Requests != l { if gctx.stats.Safebrowsing.Requests != l {
t.Errorf("Safebrowsing lookup negative cache is not working: %v", stats.Safebrowsing.Requests) t.Errorf("Safebrowsing lookup negative cache is not working: %v", gctx.stats.Safebrowsing.Requests)
} }
}) })
} }
} }
func TestParallelSB(t *testing.T) { func TestParallelSB(t *testing.T) {
d := NewForTest() d := NewForTest(&Config{SafeBrowsingEnabled: true}, nil)
defer d.Destroy() defer d.Destroy()
d.SafeBrowsingEnabled = true
t.Run("group", func(t *testing.T) { t.Run("group", func(t *testing.T) {
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
t.Run(fmt.Sprintf("aaa%d", i), func(t *testing.T) { t.Run(fmt.Sprintf("aaa%d", i), func(t *testing.T) {
@ -167,7 +162,7 @@ func TestParallelSB(t *testing.T) {
// the only way to verify that custom server option is working is to point it at a server that does serve safebrowsing // the only way to verify that custom server option is working is to point it at a server that does serve safebrowsing
func TestSafeBrowsingCustomServerFail(t *testing.T) { func TestSafeBrowsingCustomServerFail(t *testing.T) {
d := NewForTest() d := NewForTest(&Config{SafeBrowsingEnabled: true}, nil)
defer d.Destroy() defer d.Destroy()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// w.Write("Hello, client") // w.Write("Hello, client")
@ -176,7 +171,6 @@ func TestSafeBrowsingCustomServerFail(t *testing.T) {
defer ts.Close() defer ts.Close()
address := ts.Listener.Addr().String() address := ts.Listener.Addr().String()
d.SafeBrowsingEnabled = true
d.SetHTTPTimeout(time.Second * 5) d.SetHTTPTimeout(time.Second * 5)
d.SetSafeBrowsingServer(address) // this will ensure that test fails d.SetSafeBrowsingServer(address) // this will ensure that test fails
d.checkMatchEmpty(t, "wmconvirus.narod.ru") d.checkMatchEmpty(t, "wmconvirus.narod.ru")
@ -185,13 +179,15 @@ func TestSafeBrowsingCustomServerFail(t *testing.T) {
// SAFE SEARCH // SAFE SEARCH
func TestSafeSearch(t *testing.T) { func TestSafeSearch(t *testing.T) {
d := NewForTest() d := NewForTest(nil, nil)
defer d.Destroy() defer d.Destroy()
_, ok := d.SafeSearchDomain("www.google.com") _, ok := d.SafeSearchDomain("www.google.com")
if ok { if ok {
t.Errorf("Expected safesearch to error when disabled") t.Errorf("Expected safesearch to error when disabled")
} }
d.SafeSearchEnabled = true
d = NewForTest(&Config{SafeSearchEnabled: true}, nil)
defer d.Destroy()
val, ok := d.SafeSearchDomain("www.google.com") val, ok := d.SafeSearchDomain("www.google.com")
if !ok { if !ok {
t.Errorf("Expected safesearch to find result for www.google.com") t.Errorf("Expected safesearch to find result for www.google.com")
@ -202,12 +198,9 @@ func TestSafeSearch(t *testing.T) {
} }
func TestCheckHostSafeSearchYandex(t *testing.T) { func TestCheckHostSafeSearchYandex(t *testing.T) {
d := NewForTest() d := NewForTest(&Config{SafeSearchEnabled: true}, nil)
defer d.Destroy() defer d.Destroy()
// Enable safesearch
d.SafeSearchEnabled = true
// Slice of yandex domains // Slice of yandex domains
yandex := []string{"yAndeX.ru", "YANdex.COM", "yandex.ua", "yandex.by", "yandex.kz", "www.yandex.com"} yandex := []string{"yAndeX.ru", "YANdex.COM", "yandex.ua", "yandex.by", "yandex.kz", "www.yandex.com"}
@ -225,12 +218,9 @@ func TestCheckHostSafeSearchYandex(t *testing.T) {
} }
func TestCheckHostSafeSearchGoogle(t *testing.T) { func TestCheckHostSafeSearchGoogle(t *testing.T) {
d := NewForTest() d := NewForTest(&Config{SafeSearchEnabled: true}, nil)
defer d.Destroy() defer d.Destroy()
// Enable safesearch
d.SafeSearchEnabled = true
// Slice of google domains // Slice of google domains
googleDomains := []string{"www.google.com", "www.google.im", "www.google.co.in", "www.google.iq", "www.google.is", "www.google.it", "www.google.je"} googleDomains := []string{"www.google.com", "www.google.im", "www.google.co.in", "www.google.iq", "www.google.is", "www.google.it", "www.google.je"}
@ -248,7 +238,7 @@ func TestCheckHostSafeSearchGoogle(t *testing.T) {
} }
func TestSafeSearchCacheYandex(t *testing.T) { func TestSafeSearchCacheYandex(t *testing.T) {
d := NewForTest() d := NewForTest(nil, nil)
defer d.Destroy() defer d.Destroy()
domain := "yandex.ru" domain := "yandex.ru"
@ -264,8 +254,9 @@ func TestSafeSearchCacheYandex(t *testing.T) {
t.Fatalf("SafeSearch is not enabled but there is an answer for `%s` !", domain) t.Fatalf("SafeSearch is not enabled but there is an answer for `%s` !", domain)
} }
// Enable safesearch d = NewForTest(&Config{SafeSearchEnabled: true}, nil)
d.SafeSearchEnabled = true defer d.Destroy()
result, err = d.CheckHost(domain, dns.TypeA, "") result, err = d.CheckHost(domain, dns.TypeA, "")
if err != nil { if err != nil {
t.Fatalf("CheckHost for safesearh domain %s failed cause %s", domain, err) t.Fatalf("CheckHost for safesearh domain %s failed cause %s", domain, err)
@ -277,7 +268,7 @@ func TestSafeSearchCacheYandex(t *testing.T) {
} }
// Check cache // Check cache
cachedValue, isFound, err := getCachedReason(safeSearchCache, domain) cachedValue, isFound, err := getCachedReason(gctx.safeSearchCache, domain)
if err != nil { if err != nil {
t.Fatalf("An error occured during cache search for %s: %s", domain, err) t.Fatalf("An error occured during cache search for %s: %s", domain, err)
@ -293,7 +284,7 @@ func TestSafeSearchCacheYandex(t *testing.T) {
} }
func TestSafeSearchCacheGoogle(t *testing.T) { func TestSafeSearchCacheGoogle(t *testing.T) {
d := NewForTest() d := NewForTest(nil, nil)
defer d.Destroy() defer d.Destroy()
domain := "www.google.ru" domain := "www.google.ru"
result, err := d.CheckHost(domain, dns.TypeA, "") result, err := d.CheckHost(domain, dns.TypeA, "")
@ -304,8 +295,8 @@ func TestSafeSearchCacheGoogle(t *testing.T) {
t.Fatalf("SafeSearch is not enabled but there is an answer!") t.Fatalf("SafeSearch is not enabled but there is an answer!")
} }
// Enable safesearch and check host d = NewForTest(&Config{SafeSearchEnabled: true}, nil)
d.SafeSearchEnabled = true defer d.Destroy()
// Let's lookup for safesearch domain // Let's lookup for safesearch domain
safeDomain, ok := d.SafeSearchDomain(domain) safeDomain, ok := d.SafeSearchDomain(domain)
@ -338,7 +329,7 @@ func TestSafeSearchCacheGoogle(t *testing.T) {
} }
// Check cache // Check cache
cachedValue, isFound, err := getCachedReason(safeSearchCache, domain) cachedValue, isFound, err := getCachedReason(gctx.safeSearchCache, domain)
if err != nil { if err != nil {
t.Fatalf("An error occured during cache search for %s: %s", domain, err) t.Fatalf("An error occured during cache search for %s: %s", domain, err)
@ -356,17 +347,16 @@ func TestSafeSearchCacheGoogle(t *testing.T) {
// PARENTAL // PARENTAL
func TestParentalControl(t *testing.T) { func TestParentalControl(t *testing.T) {
d := NewForTest() d := NewForTest(&Config{ParentalEnabled: true}, nil)
defer d.Destroy() defer d.Destroy()
d.ParentalEnabled = true
d.ParentalSensitivity = 3 d.ParentalSensitivity = 3
d.checkMatch(t, "pornhub.com") d.checkMatch(t, "pornhub.com")
d.checkMatch(t, "pornhub.com") d.checkMatch(t, "pornhub.com")
if stats.Parental.Requests != 1 { if gctx.stats.Parental.Requests != 1 {
t.Errorf("Parental lookup positive cache is not working") t.Errorf("Parental lookup positive cache is not working")
} }
d.checkMatch(t, "PORNhub.com") d.checkMatch(t, "PORNhub.com")
if stats.Parental.Requests != 1 { if gctx.stats.Parental.Requests != 1 {
t.Errorf("Parental lookup positive cache is not working") t.Errorf("Parental lookup positive cache is not working")
} }
d.checkMatch(t, "www.pornhub.com") d.checkMatch(t, "www.pornhub.com")
@ -374,9 +364,9 @@ func TestParentalControl(t *testing.T) {
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")
l := stats.Parental.Requests l := gctx.stats.Parental.Requests
d.checkMatchEmpty(t, "yandex.ru") d.checkMatchEmpty(t, "yandex.ru")
if stats.Parental.Requests != l { if gctx.stats.Parental.Requests != l {
t.Errorf("Parental lookup negative cache is not working") t.Errorf("Parental lookup negative cache is not working")
} }
@ -442,7 +432,7 @@ func TestMatching(t *testing.T) {
t.Run(fmt.Sprintf("%s-%s", test.testname, test.hostname), func(t *testing.T) { t.Run(fmt.Sprintf("%s-%s", test.testname, test.hostname), func(t *testing.T) {
filters := make(map[int]string) filters := make(map[int]string)
filters[0] = test.rules filters[0] = test.rules
d := NewForTestFilters(filters) d := NewForTest(nil, filters)
defer d.Destroy() defer d.Destroy()
ret, err := d.CheckHost(test.hostname, dns.TypeA, "") ret, err := d.CheckHost(test.hostname, dns.TypeA, "")
@ -470,9 +460,8 @@ func TestClientSettings(t *testing.T) {
var r Result var r Result
filters := make(map[int]string) filters := make(map[int]string)
filters[0] = "||example.org^\n" filters[0] = "||example.org^\n"
d := NewForTestFilters(filters) d := NewForTest(&Config{ParentalEnabled: true}, filters)
defer d.Destroy() defer d.Destroy()
d.ParentalEnabled = true
d.ParentalSensitivity = 3 d.ParentalSensitivity = 3
// no client settings: // no client settings:
@ -508,9 +497,8 @@ func TestClientSettings(t *testing.T) {
// BENCHMARKS // BENCHMARKS
func BenchmarkSafeBrowsing(b *testing.B) { func BenchmarkSafeBrowsing(b *testing.B) {
d := NewForTest() d := NewForTest(&Config{SafeBrowsingEnabled: true}, nil)
defer d.Destroy() defer d.Destroy()
d.SafeBrowsingEnabled = true
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
hostname := "wmconvirus.narod.ru" hostname := "wmconvirus.narod.ru"
ret, err := d.CheckHost(hostname, dns.TypeA, "") ret, err := d.CheckHost(hostname, dns.TypeA, "")
@ -524,9 +512,8 @@ func BenchmarkSafeBrowsing(b *testing.B) {
} }
func BenchmarkSafeBrowsingParallel(b *testing.B) { func BenchmarkSafeBrowsingParallel(b *testing.B) {
d := NewForTest() d := NewForTest(&Config{SafeBrowsingEnabled: true}, nil)
defer d.Destroy() defer d.Destroy()
d.SafeBrowsingEnabled = true
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
hostname := "wmconvirus.narod.ru" hostname := "wmconvirus.narod.ru"
@ -542,9 +529,8 @@ func BenchmarkSafeBrowsingParallel(b *testing.B) {
} }
func BenchmarkSafeSearch(b *testing.B) { func BenchmarkSafeSearch(b *testing.B) {
d := NewForTest() d := NewForTest(&Config{SafeSearchEnabled: true}, nil)
defer d.Destroy() defer d.Destroy()
d.SafeSearchEnabled = true
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
val, ok := d.SafeSearchDomain("www.google.com") val, ok := d.SafeSearchDomain("www.google.com")
if !ok { if !ok {
@ -557,9 +543,8 @@ func BenchmarkSafeSearch(b *testing.B) {
} }
func BenchmarkSafeSearchParallel(b *testing.B) { func BenchmarkSafeSearchParallel(b *testing.B) {
d := NewForTest() d := NewForTest(&Config{SafeSearchEnabled: true}, nil)
defer d.Destroy() defer d.Destroy()
d.SafeSearchEnabled = true
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
val, ok := d.SafeSearchDomain("www.google.com") val, ok := d.SafeSearchDomain("www.google.com")