sb_pc_test.go (AdGuardHome-0.104.1) | : | sb_pc_test.go (AdGuardHome-0.104.3) | ||
---|---|---|---|---|
package dnsfilter | package dnsfilter | |||
import ( | import ( | |||
"crypto/sha256" | "crypto/sha256" | |||
"strings" | "strings" | |||
"testing" | "testing" | |||
"github.com/AdguardTeam/AdGuardHome/internal/agherr" | ||||
"github.com/AdguardTeam/golibs/cache" | "github.com/AdguardTeam/golibs/cache" | |||
"github.com/miekg/dns" | ||||
"github.com/stretchr/testify/assert" | "github.com/stretchr/testify/assert" | |||
) | ) | |||
func TestSafeBrowsingHash(t *testing.T) { | func TestSafeBrowsingHash(t *testing.T) { | |||
// test hostnameToHashes() | // test hostnameToHashes() | |||
hashes := hostnameToHashes("1.2.3.sub.host.com") | hashes := hostnameToHashes("1.2.3.sub.host.com") | |||
assert.Equal(t, 3, len(hashes)) | assert.Equal(t, 3, len(hashes)) | |||
_, ok := hashes[sha256.Sum256([]byte("3.sub.host.com"))] | _, ok := hashes[sha256.Sum256([]byte("3.sub.host.com"))] | |||
assert.True(t, ok) | assert.True(t, ok) | |||
_, ok = hashes[sha256.Sum256([]byte("sub.host.com"))] | _, ok = hashes[sha256.Sum256([]byte("sub.host.com"))] | |||
skipping to change at line 91 | skipping to change at line 93 | |||
c.hashToHost[hash] = "nonexisting.com" | c.hashToHost[hash] = "nonexisting.com" | |||
assert.Equal(t, 0, c.getCached()) | assert.Equal(t, 0, c.getCached()) | |||
hash = sha256.Sum256([]byte("sub.host.com")) | hash = sha256.Sum256([]byte("sub.host.com")) | |||
_, ok := c.hashToHost[hash] | _, ok := c.hashToHost[hash] | |||
assert.False(t, ok) | assert.False(t, ok) | |||
hash = sha256.Sum256([]byte("nonexisting.com")) | hash = sha256.Sum256([]byte("nonexisting.com")) | |||
_, ok = c.hashToHost[hash] | _, ok = c.hashToHost[hash] | |||
assert.True(t, ok) | assert.True(t, ok) | |||
c = &sbCtx{ | ||||
svc: "SafeBrowsing", | ||||
cacheTime: 100, | ||||
} | ||||
conf = cache.Config{} | ||||
c.cache = cache.New(conf) | ||||
hash = sha256.Sum256([]byte("sub.host.com")) | ||||
c.hashToHost = make(map[[32]byte]string) | ||||
c.hashToHost[hash] = "sub.host.com" | ||||
c.cache.Set(hash[0:2], make([]byte, 32)) | ||||
assert.Equal(t, 0, c.getCached()) | ||||
} | ||||
// testErrUpstream implements upstream.Upstream interface for replacing real | ||||
// upstream in tests. | ||||
type testErrUpstream struct{} | ||||
// Exchange always returns nil Msg and non-nil error. | ||||
func (teu *testErrUpstream) Exchange(*dns.Msg) (*dns.Msg, error) { | ||||
return nil, agherr.Error("bad") | ||||
} | ||||
func (teu *testErrUpstream) Address() string { | ||||
return "" | ||||
} | ||||
func TestSBPC_checkErrorUpstream(t *testing.T) { | ||||
d := NewForTest(&Config{SafeBrowsingEnabled: true}, nil) | ||||
defer d.Close() | ||||
ups := &testErrUpstream{} | ||||
d.safeBrowsingUpstream = ups | ||||
d.parentalUpstream = ups | ||||
_, err := d.checkSafeBrowsing("smthng.com") | ||||
assert.NotNil(t, err) | ||||
_, err = d.checkParental("smthng.com") | ||||
assert.NotNil(t, err) | ||||
} | } | |||
End of changes. 3 change blocks. | ||||
0 lines changed or deleted | 45 lines changed or added |