Skip to content

Commit

Permalink
dnsforward: imp code, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Oct 26, 2022
1 parent 8fca6de commit b442ca9
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 29 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/AdguardTeam/dnsproxy v0.46.2
github.com/AdguardTeam/golibs v0.11.0
github.com/AdguardTeam/golibs v0.11.1
github.com/AdguardTeam/urlfilter v0.16.0
github.com/NYTimes/gziphandler v1.1.1
github.com/ameshkov/dnscrypt/v2 v2.2.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -2,8 +2,8 @@ github.com/AdguardTeam/dnsproxy v0.46.2 h1:ZUKM713Ts5meYQqk6cJkUBMCFSWqFPXTgjXkN
github.com/AdguardTeam/dnsproxy v0.46.2/go.mod h1:PAmRzFqls0E92XTglyY2ESAqMAzZJhHKErG1ZpRnpjA=
github.com/AdguardTeam/golibs v0.4.0/go.mod h1:skKsDKIBB7kkFflLJBpfGX+G8QFTx0WKUzB6TIgtUj4=
github.com/AdguardTeam/golibs v0.10.4/go.mod h1:rSfQRGHIdgfxriDDNgNJ7HmE5zRoURq8R+VdR81Zuzw=
github.com/AdguardTeam/golibs v0.11.0 h1:fWp5bRLL7N806HWeNiRM7vHJH+wwWQ3Z6kpGPeu2onM=
github.com/AdguardTeam/golibs v0.11.0/go.mod h1:87bN2x4VsTritptE3XZg9l8T6gznWsIxHBcQ1DeRIXA=
github.com/AdguardTeam/golibs v0.11.1 h1:XcIiekD8lVQQOsWoAyWVn0R0ih3BoU/N8GGUDWRRgzY=
github.com/AdguardTeam/golibs v0.11.1/go.mod h1:87bN2x4VsTritptE3XZg9l8T6gznWsIxHBcQ1DeRIXA=
github.com/AdguardTeam/gomitmproxy v0.2.0/go.mod h1:Qdv0Mktnzer5zpdpi5rAwixNJzW2FN91LjKJCkVbYGU=
github.com/AdguardTeam/urlfilter v0.16.0 h1:IO29m+ZyQuuOnPLTzHuXj35V1DZOp1Dcryl576P2syg=
github.com/AdguardTeam/urlfilter v0.16.0/go.mod h1:46YZDOV1+qtdRDuhZKVPSSp7JWWes0KayqHrKAFBdEI=
Expand Down
16 changes: 2 additions & 14 deletions internal/aghtest/aghtest.go
Expand Up @@ -7,10 +7,11 @@ import (
"testing"

"github.com/AdguardTeam/golibs/log"
"github.com/stretchr/testify/require"
)

// DiscardLogOutput runs tests with discarded logger output.
//
// TODO(a.garipov): Replace with testutil.
func DiscardLogOutput(m *testing.M) {
// TODO(e.burkov): Refactor code and tests to not use the global mutable
// logger.
Expand Down Expand Up @@ -45,16 +46,3 @@ func ReplaceLogLevel(t testing.TB, l log.Level) {
t.Cleanup(func() { log.SetLevel(prev) })
log.SetLevel(l)
}

// RequireTypeAssert is a helper that first requires the desired type and then,
// if the type is correct, converts and returns the value.
//
// TODO(a.garipov): Use everywhere.
func RequireTypeAssert[T any](t testing.TB, v any) (res T) {
t.Helper()

var zero T
require.IsType(t, zero, v)

return v.(T)
}
8 changes: 0 additions & 8 deletions internal/dnsforward/dns.go
Expand Up @@ -369,10 +369,6 @@ func (s *Server) dhcpHostToIP(host string) (ip netip.Addr, ok bool) {
s.tableHostToIPLock.Lock()
defer s.tableHostToIPLock.Unlock()

if s.tableHostToIP == nil {
return netip.Addr{}, false
}

ip, ok = s.tableHostToIP[host]

return ip, ok
Expand Down Expand Up @@ -492,10 +488,6 @@ func (s *Server) ipToDHCPHost(ip netip.Addr) (host string, ok bool) {
s.tableIPToHostLock.Lock()
defer s.tableIPToHostLock.Unlock()

if s.tableIPToHost == nil {
return "", false
}

host, ok = s.tableIPToHost[ip]

return host, ok
Expand Down
5 changes: 3 additions & 2 deletions internal/dnsforward/dns_test.go
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/netutil"
"github.com/AdguardTeam/golibs/testutil"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -313,7 +314,7 @@ func TestServer_ProcessDHCPHosts_localRestriction(t *testing.T) {
ans := pctx.Res.Answer
require.Len(t, ans, 1)

a := aghtest.RequireTypeAssert[*dns.A](t, ans[0])
a := testutil.RequireTypeAssert[*dns.A](t, ans[0])

ip := aghalg.Must(netutil.IPToAddr(a.A, netutil.AddrFamilyIPv4))
assert.Equal(t, tc.wantIP, ip)
Expand Down Expand Up @@ -435,7 +436,7 @@ func TestServer_ProcessDHCPHosts(t *testing.T) {
ans := pctx.Res.Answer
require.Len(t, ans, 1)

a := aghtest.RequireTypeAssert[*dns.A](t, ans[0])
a := testutil.RequireTypeAssert[*dns.A](t, ans[0])

ip := aghalg.Must(netutil.IPToAddr(a.A, netutil.AddrFamilyIPv4))
assert.Equal(t, tc.wantIP, ip)
Expand Down
4 changes: 2 additions & 2 deletions internal/dnsforward/dnsforward_test.go
Expand Up @@ -33,7 +33,7 @@ import (
)

func TestMain(m *testing.M) {
aghtest.DiscardLogOutput(m)
testutil.DiscardLogOutput(m)
}

const (
Expand Down Expand Up @@ -1065,7 +1065,7 @@ func TestPTRResponseFromDHCPLeases(t *testing.T) {
assert.Equal(t, dns.TypePTR, ans.Header().Rrtype)
assert.Equal(t, "34.12.168.192.in-addr.arpa.", ans.Header().Name)

ptr := aghtest.RequireTypeAssert[*dns.PTR](t, ans)
ptr := testutil.RequireTypeAssert[*dns.PTR](t, ans)

assert.Equal(t, dns.Fqdn("myhost."+localDomain), ptr.Ptr)
}
Expand Down

0 comments on commit b442ca9

Please sign in to comment.