"Fossies" - the Fresh Open Source Software Archive

Member "AdGuardHome-0.104.3/internal/dnsfilter/sb_pc_test.go" (19 Nov 2020, 3791 Bytes) of package /linux/misc/dns/AdGuardHome-0.104.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "sb_pc_test.go": 0.104.1_vs_0.104.3.

    1 package dnsfilter
    2 
    3 import (
    4     "crypto/sha256"
    5     "strings"
    6     "testing"
    7 
    8     "github.com/AdguardTeam/AdGuardHome/internal/agherr"
    9     "github.com/AdguardTeam/golibs/cache"
   10     "github.com/miekg/dns"
   11     "github.com/stretchr/testify/assert"
   12 )
   13 
   14 func TestSafeBrowsingHash(t *testing.T) {
   15     // test hostnameToHashes()
   16     hashes := hostnameToHashes("1.2.3.sub.host.com")
   17     assert.Equal(t, 3, len(hashes))
   18     _, ok := hashes[sha256.Sum256([]byte("3.sub.host.com"))]
   19     assert.True(t, ok)
   20     _, ok = hashes[sha256.Sum256([]byte("sub.host.com"))]
   21     assert.True(t, ok)
   22     _, ok = hashes[sha256.Sum256([]byte("host.com"))]
   23     assert.True(t, ok)
   24     _, ok = hashes[sha256.Sum256([]byte("com"))]
   25     assert.False(t, ok)
   26 
   27     c := &sbCtx{
   28         svc:        "SafeBrowsing",
   29         hashToHost: hashes,
   30     }
   31 
   32     q := c.getQuestion()
   33 
   34     assert.True(t, strings.Contains(q, "7a1b."))
   35     assert.True(t, strings.Contains(q, "af5a."))
   36     assert.True(t, strings.Contains(q, "eb11."))
   37     assert.True(t, strings.HasSuffix(q, "sb.dns.adguard.com."))
   38 }
   39 
   40 func TestSafeBrowsingCache(t *testing.T) {
   41     c := &sbCtx{
   42         svc:       "SafeBrowsing",
   43         cacheTime: 100,
   44     }
   45     conf := cache.Config{}
   46     c.cache = cache.New(conf)
   47 
   48     // store in cache hashes for "3.sub.host.com" and "host.com"
   49     //  and empty data for hash-prefix for "sub.host.com"
   50     hash := sha256.Sum256([]byte("sub.host.com"))
   51     c.hashToHost = make(map[[32]byte]string)
   52     c.hashToHost[hash] = "sub.host.com"
   53     var hashesArray [][]byte
   54     hash4 := sha256.Sum256([]byte("3.sub.host.com"))
   55     hashesArray = append(hashesArray, hash4[:])
   56     hash2 := sha256.Sum256([]byte("host.com"))
   57     hashesArray = append(hashesArray, hash2[:])
   58     c.storeCache(hashesArray)
   59 
   60     // match "3.sub.host.com" or "host.com" from cache
   61     c.hashToHost = make(map[[32]byte]string)
   62     hash = sha256.Sum256([]byte("3.sub.host.com"))
   63     c.hashToHost[hash] = "3.sub.host.com"
   64     hash = sha256.Sum256([]byte("sub.host.com"))
   65     c.hashToHost[hash] = "sub.host.com"
   66     hash = sha256.Sum256([]byte("host.com"))
   67     c.hashToHost[hash] = "host.com"
   68     assert.Equal(t, 1, c.getCached())
   69 
   70     // match "sub.host.com" from cache
   71     c.hashToHost = make(map[[32]byte]string)
   72     hash = sha256.Sum256([]byte("sub.host.com"))
   73     c.hashToHost[hash] = "sub.host.com"
   74     assert.Equal(t, -1, c.getCached())
   75 
   76     // match "sub.host.com" from cache,
   77     //  but another hash for "nonexisting.com" is not in cache
   78     //  which means that we must get data from server for it
   79     c.hashToHost = make(map[[32]byte]string)
   80     hash = sha256.Sum256([]byte("sub.host.com"))
   81     c.hashToHost[hash] = "sub.host.com"
   82     hash = sha256.Sum256([]byte("nonexisting.com"))
   83     c.hashToHost[hash] = "nonexisting.com"
   84     assert.Equal(t, 0, c.getCached())
   85 
   86     hash = sha256.Sum256([]byte("sub.host.com"))
   87     _, ok := c.hashToHost[hash]
   88     assert.False(t, ok)
   89 
   90     hash = sha256.Sum256([]byte("nonexisting.com"))
   91     _, ok = c.hashToHost[hash]
   92     assert.True(t, ok)
   93 
   94     c = &sbCtx{
   95         svc:       "SafeBrowsing",
   96         cacheTime: 100,
   97     }
   98     conf = cache.Config{}
   99     c.cache = cache.New(conf)
  100 
  101     hash = sha256.Sum256([]byte("sub.host.com"))
  102     c.hashToHost = make(map[[32]byte]string)
  103     c.hashToHost[hash] = "sub.host.com"
  104 
  105     c.cache.Set(hash[0:2], make([]byte, 32))
  106     assert.Equal(t, 0, c.getCached())
  107 }
  108 
  109 // testErrUpstream implements upstream.Upstream interface for replacing real
  110 // upstream in tests.
  111 type testErrUpstream struct{}
  112 
  113 // Exchange always returns nil Msg and non-nil error.
  114 func (teu *testErrUpstream) Exchange(*dns.Msg) (*dns.Msg, error) {
  115     return nil, agherr.Error("bad")
  116 }
  117 
  118 func (teu *testErrUpstream) Address() string {
  119     return ""
  120 }
  121 
  122 func TestSBPC_checkErrorUpstream(t *testing.T) {
  123     d := NewForTest(&Config{SafeBrowsingEnabled: true}, nil)
  124     defer d.Close()
  125 
  126     ups := &testErrUpstream{}
  127 
  128     d.safeBrowsingUpstream = ups
  129     d.parentalUpstream = ups
  130 
  131     _, err := d.checkSafeBrowsing("smthng.com")
  132     assert.NotNil(t, err)
  133 
  134     _, err = d.checkParental("smthng.com")
  135     assert.NotNil(t, err)
  136 }