Skip to content

Commit

Permalink
all: imp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Nov 29, 2023
1 parent e753bb5 commit 9e6e8e7
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 40 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -3,7 +3,7 @@ module github.com/AdguardTeam/AdGuardHome
go 1.20

require (
// TODO(a.garipov): Upgrade to v0.60.
// TODO(a.garipov): !! Upgrade to v0.60.
github.com/AdguardTeam/dnsproxy v0.59.2-0.20231129094552-f661fdcf9edc
github.com/AdguardTeam/golibs v0.17.2
github.com/AdguardTeam/urlfilter v0.17.3
Expand Down
4 changes: 2 additions & 2 deletions internal/dnsforward/dnsforward_test.go
Expand Up @@ -54,10 +54,10 @@ const (
testMessagesCount = 10
)

// testClientAddr is the common net.Addr for tests.
// testClientAddrPort is the common net.Addr for tests.
//
// TODO(a.garipov): Use more.
var testClientAddr = netip.MustParseAddrPort("1.2.3.4:12345")
var testClientAddrPort = netip.MustParseAddrPort("1.2.3.4:12345")

func startDeferStop(t *testing.T, s *Server) {
t.Helper()
Expand Down
4 changes: 2 additions & 2 deletions internal/dnsforward/filter_test.go
Expand Up @@ -187,7 +187,7 @@ func TestHandleDNSRequest_handleDNSRequest(t *testing.T) {
dctx := &proxy.DNSContext{
Proto: proxy.ProtoUDP,
Req: tc.req,
Addr: netip.MustParseAddrPort("127.0.0.1:1"),
Addr: testClientAddrPort,
}

t.Run(tc.name, func(t *testing.T) {
Expand Down Expand Up @@ -326,7 +326,7 @@ func TestHandleDNSRequest_filterDNSResponse(t *testing.T) {
Proto: proxy.ProtoUDP,
Req: tc.req,
Res: resp,
Addr: netip.MustParseAddrPort("127.0.0.1:1"),
Addr: testClientAddrPort,
}

dctx := &dnsContext{
Expand Down
26 changes: 1 addition & 25 deletions internal/dnsforward/http.go
Expand Up @@ -129,10 +129,7 @@ func (s *Server) getDNSConfig() (c *jsonDNSConfig) {
ratelimit := s.conf.Ratelimit
ratelimitSubnetLenIPv4 := s.conf.RatelimitSubnetLenIPv4
ratelimitSubnetLenIPv6 := s.conf.RatelimitSubnetLenIPv6
ratelimitWhitelist := slices.Clone(s.conf.RatelimitWhitelist)
if ratelimitWhitelist == nil {
ratelimitWhitelist = []netip.Addr{}
}
ratelimitWhitelist := append([]netip.Addr{}, s.conf.RatelimitWhitelist...)

customIP := s.conf.EDNSClientSubnet.CustomIP
enableEDNSClientSubnet := s.conf.EDNSClientSubnet.Enabled
Expand Down Expand Up @@ -294,12 +291,6 @@ func (req *jsonDNSConfig) validate(privateNets netutil.SubnetSet) (err error) {
return err
}

err = req.checkRatelimitWhitelist()
if err != nil {
// Don't wrap the error since it's informative enough as is.
return err
}

err = req.checkBlockingMode()
if err != nil {
// Don't wrap the error since it's informative enough as is.
Expand Down Expand Up @@ -408,21 +399,6 @@ func checkInclusion(ptr *int, minN, maxN int) (err error) {
return nil
}

// checkRatelimitWhitelist returns an error if any of IP addresses is invalid.
func (req *jsonDNSConfig) checkRatelimitWhitelist() (err error) {
if req.RatelimitWhitelist == nil {
return nil
}

for i, ip := range *req.RatelimitWhitelist {
if !ip.IsValid() {
return fmt.Errorf("ratelimit whitelist: at index %d: invalid ip", i)
}
}

return nil
}

// handleSetConfig handles requests to the POST /control/dns_config endpoint.
func (s *Server) handleSetConfig(w http.ResponseWriter, r *http.Request) {
req := &jsonDNSConfig{}
Expand Down
12 changes: 4 additions & 8 deletions internal/dnsforward/process_internal_test.go
Expand Up @@ -97,14 +97,14 @@ func TestServer_ProcessInitial(t *testing.T) {
dctx := &dnsContext{
proxyCtx: &proxy.DNSContext{
Req: createTestMessageWithType(tc.target, tc.qType),
Addr: testClientAddr,
Addr: testClientAddrPort,
RequestID: 1234,
},
}

gotRC := s.processInitial(dctx)
assert.Equal(t, tc.wantRC, gotRC)
assert.Equal(t, testClientAddr.Addr(), gotAddr)
assert.Equal(t, testClientAddrPort.Addr(), gotAddr)

if tc.wantRCode > 0 {
gotResp := dctx.proxyCtx.Res
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestServer_ProcessFilteringAfterResponse(t *testing.T) {
Proto: proxy.ProtoUDP,
Req: tc.req,
Res: resp,
Addr: testClientAddr,
Addr: testClientAddrPort,
},
}

Expand Down Expand Up @@ -412,10 +412,6 @@ func TestServer_ProcessDetermineLocal(t *testing.T) {
want: assert.False,
name: "invalid",
cliAddr: netip.AddrPort{},
}, {
want: assert.False,
name: "nil",
cliAddr: netip.AddrPort{},
}}

for _, tc := range testCases {
Expand Down Expand Up @@ -790,7 +786,7 @@ func TestServer_ProcessLocalPTR_usingResolvers(t *testing.T) {
var dnsCtx *dnsContext
setup := func(use bool) {
proxyCtx = &proxy.DNSContext{
Addr: testClientAddr,
Addr: testClientAddrPort,
Req: createTestMessageWithType(reqAddr, dns.TypePTR),
}
dnsCtx = &dnsContext{
Expand Down
17 changes: 15 additions & 2 deletions internal/dnsforward/stats_test.go
@@ -1,6 +1,7 @@
package dnsforward

import (
"net/netip"
"testing"
"time"

Expand Down Expand Up @@ -64,6 +65,7 @@ func TestServer_ProcessQueryLogsAndStats(t *testing.T) {
name string
domain string
proto proxy.Proto
addr netip.AddrPort
clientID string
wantLogProto querylog.ClientProto
wantStatClient string
Expand All @@ -74,6 +76,7 @@ func TestServer_ProcessQueryLogsAndStats(t *testing.T) {
name: "success_udp",
domain: domain,
proto: proxy.ProtoUDP,
addr: testClientAddrPort,
clientID: "",
wantLogProto: "",
wantStatClient: "1.2.3.4",
Expand All @@ -84,6 +87,7 @@ func TestServer_ProcessQueryLogsAndStats(t *testing.T) {
name: "success_tls_clientid",
domain: domain,
proto: proxy.ProtoTLS,
addr: testClientAddrPort,
clientID: "cli42",
wantLogProto: querylog.ClientProtoDoT,
wantStatClient: "cli42",
Expand All @@ -94,6 +98,7 @@ func TestServer_ProcessQueryLogsAndStats(t *testing.T) {
name: "success_tls",
domain: domain,
proto: proxy.ProtoTLS,
addr: testClientAddrPort,
clientID: "",
wantLogProto: querylog.ClientProtoDoT,
wantStatClient: "1.2.3.4",
Expand All @@ -104,6 +109,7 @@ func TestServer_ProcessQueryLogsAndStats(t *testing.T) {
name: "success_quic",
domain: domain,
proto: proxy.ProtoQUIC,
addr: testClientAddrPort,
clientID: "",
wantLogProto: querylog.ClientProtoDoQ,
wantStatClient: "1.2.3.4",
Expand All @@ -114,6 +120,7 @@ func TestServer_ProcessQueryLogsAndStats(t *testing.T) {
name: "success_https",
domain: domain,
proto: proxy.ProtoHTTPS,
addr: testClientAddrPort,
clientID: "",
wantLogProto: querylog.ClientProtoDoH,
wantStatClient: "1.2.3.4",
Expand All @@ -124,6 +131,7 @@ func TestServer_ProcessQueryLogsAndStats(t *testing.T) {
name: "success_dnscrypt",
domain: domain,
proto: proxy.ProtoDNSCrypt,
addr: testClientAddrPort,
clientID: "",
wantLogProto: querylog.ClientProtoDNSCrypt,
wantStatClient: "1.2.3.4",
Expand All @@ -134,6 +142,7 @@ func TestServer_ProcessQueryLogsAndStats(t *testing.T) {
name: "success_udp_filtered",
domain: domain,
proto: proxy.ProtoUDP,
addr: testClientAddrPort,
clientID: "",
wantLogProto: "",
wantStatClient: "1.2.3.4",
Expand All @@ -144,6 +153,7 @@ func TestServer_ProcessQueryLogsAndStats(t *testing.T) {
name: "success_udp_sb",
domain: domain,
proto: proxy.ProtoUDP,
addr: testClientAddrPort,
clientID: "",
wantLogProto: "",
wantStatClient: "1.2.3.4",
Expand All @@ -154,6 +164,7 @@ func TestServer_ProcessQueryLogsAndStats(t *testing.T) {
name: "success_udp_ss",
domain: domain,
proto: proxy.ProtoUDP,
addr: testClientAddrPort,
clientID: "",
wantLogProto: "",
wantStatClient: "1.2.3.4",
Expand All @@ -164,6 +175,7 @@ func TestServer_ProcessQueryLogsAndStats(t *testing.T) {
name: "success_udp_pc",
domain: domain,
proto: proxy.ProtoUDP,
addr: testClientAddrPort,
clientID: "",
wantLogProto: "",
wantStatClient: "1.2.3.4",
Expand All @@ -174,9 +186,10 @@ func TestServer_ProcessQueryLogsAndStats(t *testing.T) {
name: "success_udp_pc_empty_fqdn",
domain: ".",
proto: proxy.ProtoUDP,
addr: netip.MustParseAddrPort("4.3.2.1:1234"),
clientID: "",
wantLogProto: "",
wantStatClient: "1.2.3.4",
wantStatClient: "4.3.2.1",
wantCode: resultCodeSuccess,
reason: filtering.FilteredParental,
wantStatResult: stats.RParental,
Expand All @@ -203,7 +216,7 @@ func TestServer_ProcessQueryLogsAndStats(t *testing.T) {
Proto: tc.proto,
Req: req,
Res: &dns.Msg{},
Addr: testClientAddr,
Addr: tc.addr,
Upstream: ups,
}
dctx := &dnsContext{
Expand Down

0 comments on commit 9e6e8e7

Please sign in to comment.