Skip to content

Commit

Permalink
Pull request: AG-32410-imp-tests
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit e6f2c22
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Fri May 31 13:46:21 2024 +0400

    scripts: imp code

commit 8870312
Merge: 9a452ed fe43e10
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Fri May 31 13:36:21 2024 +0400

    Merge remote-tracking branch 'origin/master' into AG-32410-imp-tests

commit 9a452ed
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Fri May 31 10:06:42 2024 +0400

    all: imp code

commit 9d11c99
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Thu May 30 11:41:19 2024 +0400

    upstream: tests race

commit 031fb5c
Merge: d2db301 17b2ad2
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Thu May 30 11:23:18 2024 +0400

    Merge remote-tracking branch 'origin/master' into AG-32410-imp-tests

commit d2db301
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Thu May 30 11:20:48 2024 +0400

    upstream: imp tests

commit e1c031e
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Thu May 30 10:44:49 2024 +0400

    proxy: imp tests

commit 1ff1d6d
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Thu May 30 10:38:28 2024 +0400

    proxy: imp tests

commit 95adbf8
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Thu May 30 10:26:14 2024 +0400

    proxy: imp tests
  • Loading branch information
Mizzick committed May 31, 2024
1 parent fe43e10 commit 5607a43
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 62 deletions.
19 changes: 16 additions & 3 deletions proxy/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ func TestServeCached(t *testing.T) {

// Create a DNS-over-UDP client connection.
addr := dnsProxy.Addr(ProtoUDP)
client := &dns.Client{Net: "udp", Timeout: 500 * time.Millisecond}
client := &dns.Client{
Net: string(ProtoUDP),
Timeout: testTimeout,
}

// Create a DNS request.
request := (&dns.Msg{}).SetQuestion("google.com.", dns.TypeA)
Expand Down Expand Up @@ -270,7 +273,17 @@ func TestCache_concurrent(t *testing.T) {
g.Wait()
}

const (
// cacheTick is a cache check period.
cacheTick = 50 * time.Millisecond

// cacheTimeout is the timeout of cache check.
cacheTimeout = 20 * cacheTick
)

func TestCacheExpiration(t *testing.T) {
t.Parallel()

dnsProxy := mustNew(t, &Config{
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(localhostAnyPort)},
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(localhostAnyPort)},
Expand Down Expand Up @@ -322,7 +335,7 @@ func TestCacheExpiration(t *testing.T) {
}

return true
}, 1100*time.Millisecond, 100*time.Millisecond)
}, cacheTimeout, cacheTick)
}

func TestCacheExpirationWithTTLOverride(t *testing.T) {
Expand Down Expand Up @@ -607,7 +620,7 @@ func setAndGetCache(t *testing.T, c *cache, g *sync.WaitGroup, host, ip string)
ci, _, _ := c.get(dnsMsg)

return ci == nil
}, 1100*time.Millisecond, 100*time.Millisecond, "cache for %s should already be removed", host)
}, cacheTimeout, cacheTick, "cache for %s should already be removed", host)
}

func TestCache_getWithSubnet(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions proxy/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net"
"sync"
"testing"
"time"

"github.com/AdguardTeam/golibs/testutil"
"github.com/miekg/dns"
Expand Down Expand Up @@ -53,7 +52,10 @@ func TestFilteringHandler(t *testing.T) {

// Create a DNS-over-UDP client connection
addr := dnsProxy.Addr(ProtoUDP)
client := &dns.Client{Net: "udp", Timeout: 500 * time.Millisecond}
client := &dns.Client{
Net: string(ProtoUDP),
Timeout: testTimeout,
}

// Send the first message (not blocked)
req := newTestMessage()
Expand Down
53 changes: 35 additions & 18 deletions proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const (

// defaultTestTTL used to guarantee caching.
defaultTestTTL = 1000

// testTimeout is the default timeout for tests.
testTimeout = 500 * time.Millisecond
)

// localhostAnyPort is a [netip.AddrPort] having a value of 127.0.0.1:0.
Expand Down Expand Up @@ -579,12 +582,14 @@ func TestProxy_Resolve_dnssecCache(t *testing.T) {
}

func TestExchangeWithReservedDomains(t *testing.T) {
t.Parallel()

dnsProxy := mustNew(t, &Config{
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(localhostAnyPort)},
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(localhostAnyPort)},
UpstreamConfig: newTestUpstreamConfigWithBoot(
t,
1*time.Second,
testTimeout,
"[/adguard.com/]1.2.3.4",
"[/google.ru/]2.3.4.5",
"[/maps.google.ru/]#",
Expand Down Expand Up @@ -646,7 +651,7 @@ func TestExchangeWithReservedDomains(t *testing.T) {
// TestOneByOneUpstreamsExchange tries to resolve DNS request
// with one valid and two invalid upstreams
func TestOneByOneUpstreamsExchange(t *testing.T) {
const testTimeout = 1 * time.Second
t.Parallel()

dnsProxy := mustNew(t, &Config{
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(localhostAnyPort)},
Expand Down Expand Up @@ -712,18 +717,18 @@ func newLocalUpstreamListener(t *testing.T, port uint16, h dns.Handler) (real ne
}

func TestFallback(t *testing.T) {
t.Parallel()

responseCh := make(chan uint16)
failCh := make(chan uint16)

const timeout = 1 * time.Second

successHandler := dns.HandlerFunc(func(w dns.ResponseWriter, r *dns.Msg) {
testutil.RequireSend(testutil.PanicT{}, responseCh, r.Id, timeout)
testutil.RequireSend(testutil.PanicT{}, responseCh, r.Id, testTimeout)

require.NoError(testutil.PanicT{}, w.WriteMsg((&dns.Msg{}).SetReply(r)))
})
failHandler := dns.HandlerFunc(func(w dns.ResponseWriter, r *dns.Msg) {
testutil.RequireSend(testutil.PanicT{}, failCh, r.Id, timeout)
testutil.RequireSend(testutil.PanicT{}, failCh, r.Id, testTimeout)

require.NoError(testutil.PanicT{}, w.WriteMsg(&dns.Msg{}))
})
Expand All @@ -746,7 +751,7 @@ func TestFallback(t *testing.T) {
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(localhostAnyPort)},
UpstreamConfig: newTestUpstreamConfig(
t,
timeout,
testTimeout,
failAddr,
"[/specific.example/]"+alsoSuccessAddr,
// almost.failing.example will fall here first.
Expand All @@ -755,7 +760,7 @@ func TestFallback(t *testing.T) {
TrustedProxies: defaultTrustedProxies,
Fallbacks: newTestUpstreamConfig(
t,
timeout,
testTimeout,
failAddr,
successAddr,
"[/failing.example/]"+failAddr,
Expand Down Expand Up @@ -810,7 +815,7 @@ func TestFallback(t *testing.T) {
require.NoError(t, err)

for _, ch := range tc.wantSignals {
reqID, ok := testutil.RequireReceive(testutil.PanicT{}, ch, timeout)
reqID, ok := testutil.RequireReceive(testutil.PanicT{}, ch, testTimeout)
require.True(t, ok)

assert.Equal(t, req.Id, reqID)
Expand All @@ -823,17 +828,17 @@ func TestFallback(t *testing.T) {
}

func TestFallbackFromInvalidBootstrap(t *testing.T) {
timeout := 1 * time.Second
t.Parallel()

invalidRslv, err := upstream.NewUpstreamResolver("8.8.8.8:555", &upstream.Options{
Timeout: timeout,
Timeout: testTimeout,
})
require.NoError(t, err)

// Prepare the proxy server
upsConf, err := ParseUpstreamsConfig(
[]string{"tls://dns.adguard.com"},
&upstream.Options{Bootstrap: invalidRslv, Timeout: timeout},
&upstream.Options{Bootstrap: invalidRslv, Timeout: testTimeout},
)
require.NoError(t, err)

Expand All @@ -844,7 +849,7 @@ func TestFallbackFromInvalidBootstrap(t *testing.T) {
TrustedProxies: defaultTrustedProxies,
Fallbacks: newTestUpstreamConfig(
t,
timeout,
testTimeout,
"1.0.0.1",
"8.8.8.8",
),
Expand Down Expand Up @@ -874,7 +879,7 @@ func TestFallbackFromInvalidBootstrap(t *testing.T) {
requireResponse(t, req, res)

elapsed := time.Since(start)
assert.Greater(t, 3*timeout, elapsed)
assert.Greater(t, 3*testTimeout, elapsed)
}

func TestRefuseAny(t *testing.T) {
Expand All @@ -896,7 +901,10 @@ func TestRefuseAny(t *testing.T) {

// Create a DNS-over-UDP client connection
addr := dnsProxy.Addr(ProtoUDP)
client := &dns.Client{Net: "udp", Timeout: 500 * time.Millisecond}
client := &dns.Client{
Net: string(ProtoUDP),
Timeout: testTimeout,
}

// Create a DNS request
request := (&dns.Msg{
Expand Down Expand Up @@ -930,7 +938,10 @@ func TestInvalidDNSRequest(t *testing.T) {
testutil.CleanupAndRequireSuccess(t, func() (err error) { return dnsProxy.Shutdown(ctx) })

// Create a DNS-over-UDP client connection
client := &dns.Client{Net: "udp", Timeout: 500 * time.Millisecond}
client := &dns.Client{
Net: string(ProtoUDP),
Timeout: testTimeout,
}

// Create a DNS request
request := &dns.Msg{
Expand All @@ -950,7 +961,10 @@ func TestResponseInRequest(t *testing.T) {
dnsProxy := mustStartDefaultProxy(t)

addr := dnsProxy.Addr(ProtoUDP)
client := &dns.Client{Net: "udp", Timeout: 500 * time.Millisecond}
client := &dns.Client{
Net: string(ProtoUDP),
Timeout: testTimeout,
}

req := newTestMessage()
req.Response = true
Expand All @@ -968,7 +982,10 @@ func TestNoQuestion(t *testing.T) {
dnsProxy := mustStartDefaultProxy(t)

addr := dnsProxy.Addr(ProtoUDP)
client := &dns.Client{Net: "udp", Timeout: 500 * time.Millisecond}
client := &dns.Client{
Net: string(ProtoUDP),
Timeout: testTimeout,
}

req := newTestMessage()
req.Question = nil
Expand Down
6 changes: 4 additions & 2 deletions proxy/ratelimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net"
"net/netip"
"testing"
"time"

"github.com/AdguardTeam/golibs/testutil"
"github.com/miekg/dns"
Expand All @@ -31,7 +30,10 @@ func TestRatelimitingProxy(t *testing.T) {

// Create a DNS-over-UDP client connection
addr := dnsProxy.Addr(ProtoUDP)
client := &dns.Client{Net: "udp", Timeout: 500 * time.Millisecond}
client := &dns.Client{
Net: string(ProtoUDP),
Timeout: testTimeout,
}

// Send the first message (not blocked)
req := newTestMessage()
Expand Down
5 changes: 2 additions & 3 deletions proxy/upstreams_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package proxy

import (
"testing"
"time"

"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/errors"
Expand Down Expand Up @@ -278,7 +277,7 @@ func TestGetUpstreamsForDomainWithoutDuplicates(t *testing.T) {
&upstream.Options{
InsecureSkipVerify: false,
Bootstrap: nil,
Timeout: 1 * time.Second,
Timeout: testTimeout,
},
)
assert.NoError(t, err)
Expand Down Expand Up @@ -455,7 +454,7 @@ func BenchmarkGetUpstreamsForDomain(b *testing.B) {
&upstream.Options{
InsecureSkipVerify: false,
Bootstrap: nil,
Timeout: 1 * time.Second,
Timeout: testTimeout,
},
)

Expand Down
24 changes: 3 additions & 21 deletions scripts/make/go-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,9 @@ run_linter "${GO:-go}" vet ./...

run_linter govulncheck ./...

run_linter gocyclo --over 10\
./internal/bootstrap/\
./internal/netutil/\
./internal/version/\
./fastip/\
./main.go\
./proxy/\
./proxyutil/\
./upstream/\
;

run_linter gocognit --over 10\
./internal/bootstrap/\
./internal/netutil/\
./internal/version/\
./fastip/\
./main.go\
./proxy/\
./proxyutil/\
./upstream/\
;
run_linter gocyclo --over 10 .

run_linter gocognit --over 10 .

run_linter ineffassign ./...

Expand Down
8 changes: 4 additions & 4 deletions upstream/dnscrypt_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"github.com/stretchr/testify/require"
)

// Helpers

// dnsCryptHandlerFunc is a function-based implementation of the
// [dnscrypt.Handler] interface.
type dnsCryptHandlerFunc func(w dnscrypt.ResponseWriter, r *dns.Msg) (err error)
Expand Down Expand Up @@ -89,9 +87,9 @@ func startTestDNSCryptServer(
return stamp
}

// Tests

func TestUpstreamDNSCrypt(t *testing.T) {
t.Parallel()

// AdGuard DNS (DNSCrypt)
address := "sdns://AQMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20"
u, err := AddressToUpstream(address, &Options{Timeout: dialTimeout})
Expand Down Expand Up @@ -153,6 +151,8 @@ func TestDNSCrypt_Exchange_truncated(t *testing.T) {
}

func TestDNSCrypt_Exchange_deadline(t *testing.T) {
t.Parallel()

// Prepare the test DNSCrypt server config
rc, err := dnscrypt.GenerateResolverConfig("example.org", nil)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 5607a43

Please sign in to comment.