Skip to content

Commit

Permalink
safesearch: imp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Jun 20, 2023
1 parent 3f9056d commit 0a6f490
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ and this project adheres to
<!--
## [v0.108.0] - TBA
## [v0.107.32] - 2023-06-28 (APPROX.)
## [v0.107.33] - 2023-06-28 (APPROX.)
See also the [v0.107.32 GitHub milestone][ms-v0.107.32].
See also the [v0.107.33 GitHub milestone][ms-v0.107.33].
[ms-v0.107.32]: https://github.com/AdguardTeam/AdGuardHome/milestone/68?closed=1
[ms-v0.107.33]: https://github.com/AdguardTeam/AdGuardHome/milestone/68?closed=1
NOTE: Add new changes BELOW THIS COMMENT.
-->
Expand Down Expand Up @@ -82,7 +82,8 @@ In this release, the schema version has changed from 20 to 21.

### Fixed

- Safe Search not working with `AAAA` queries for Yandex domains ([#5913]).
- Safe Search not working with `AAAA` queries for domains that don't have `AAAA`
records ([#5913]).

[#951]: https://github.com/AdguardTeam/AdGuardHome/issues/951
[#1577]: https://github.com/AdguardTeam/AdGuardHome/issues/1577
Expand Down
2 changes: 1 addition & 1 deletion internal/filtering/safesearch/safesearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (ss *Default) newResult(

ips, err := ss.resolver.LookupIP(context.Background(), qtypeToProto(qtype), host)
if err != nil {
return nil, err
return nil, fmt.Errorf("resolving cname: %w", err)
}

ss.log(log.DEBUG, "resolved %s", ips)
Expand Down
59 changes: 59 additions & 0 deletions internal/filtering/safesearch/safesearch_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package safesearch_test

import (
"context"
"net"
"testing"
"time"
Expand Down Expand Up @@ -80,6 +81,14 @@ func TestDefault_CheckHost_yandexAAAA(t *testing.T) {
require.NoError(t, err)

assert.True(t, res.IsFiltered)

// TODO(a.garipov): Currently, the safe-search filter returns a single rule
// with a nil IP address. This isn't really necessary and should be changed
// once the TODO in [safesearch.Default.newResult] is resolved.
require.Len(t, res.Rules, 1)

assert.Nil(t, res.Rules[0].IP)
assert.EqualValues(t, filtering.SafeSearchListID, res.Rules[0].FilterListID)
}

func TestDefault_CheckHost_google(t *testing.T) {
Expand Down Expand Up @@ -116,6 +125,56 @@ func TestDefault_CheckHost_google(t *testing.T) {
}
}

// testResolver is a [filtering.Resolver] for tests.
//
// TODO(a.garipov): Move to aghtest and use everywhere.
type testResolver struct {
OnLookupIP func(ctx context.Context, network, host string) (ips []net.IP, err error)
}

// type check
var _ filtering.Resolver = (*testResolver)(nil)

// LookupIP implements the [filtering.Resolver] interface for *testResolver.
func (r *testResolver) LookupIP(
ctx context.Context,
network string,
host string,
) (ips []net.IP, err error) {
return r.OnLookupIP(ctx, network, host)
}

func TestDefault_CheckHost_duckduckgoAAAA(t *testing.T) {
conf := testConf
conf.CustomResolver = &testResolver{
OnLookupIP: func(_ context.Context, network, host string) (ips []net.IP, err error) {
assert.Equal(t, "ip6", network)
assert.Equal(t, "safe.duckduckgo.com", host)

return nil, nil
},
}

ss, err := safesearch.NewDefault(conf, "", testCacheSize, testCacheTTL)
require.NoError(t, err)

// The DuckDuckGo safe-search addresses are resolved through CNAMEs, but
// DuckDuckGo doesn't have a safe-search IPv6 address. The result should be
// the same as the one for Yandex IPv6. That is, a NODATA response.
res, err := ss.CheckHost("www.duckduckgo.com", dns.TypeAAAA)
require.NoError(t, err)

assert.True(t, res.IsFiltered)

// TODO(a.garipov): Currently, the safe-search filter returns a single rule
// with a nil IP address. This isn't really necessary and should be changed
// once the TODO in [safesearch.Default.newResult] is resolved.
require.Len(t, res.Rules, 1)

assert.Nil(t, res.Rules[0].IP)
assert.EqualValues(t, filtering.SafeSearchListID, res.Rules[0].FilterListID)
}

func TestDefault_Update(t *testing.T) {
conf := testConf
ss, err := safesearch.NewDefault(conf, "", testCacheSize, testCacheTTL)
Expand Down

0 comments on commit 0a6f490

Please sign in to comment.