mirror of
https://github.com/AdguardTeam/dnsproxy.git
synced 2026-10-12 02:01:26 +00:00
79 lines
1.9 KiB
Go
79 lines
1.9 KiB
Go
package proxy
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/AdguardTeam/dnsproxy/internal/dnsproxytest"
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
|
"github.com/AdguardTeam/golibs/testutil/servicetest"
|
|
"github.com/miekg/dns"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestFilteringHandler(t *testing.T) {
|
|
// Initializing the test middleware
|
|
m := &sync.RWMutex{}
|
|
blockResponse := false
|
|
|
|
reqHandler := &testHandler{
|
|
OnHandle: func(ctx context.Context, p *Proxy, d *DNSContext) (err error) {
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
|
|
if !blockResponse {
|
|
// Use the default Resolve method if response is not blocked.
|
|
return p.Resolve(ctx, d)
|
|
}
|
|
|
|
resp := dns.Msg{}
|
|
resp.SetRcode(d.Req, dns.RcodeNotImplemented)
|
|
resp.RecursionAvailable = true
|
|
|
|
// Set the response right away
|
|
d.Res = &resp
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
// Prepare the proxy server.
|
|
dnsProxy := mustNew(t, &Config{
|
|
Logger: testLogger,
|
|
TrustedProxies: dnsproxytest.DefaultTrustedProxies,
|
|
UpstreamConfig: &UpstreamConfig{
|
|
Upstreams: []upstream.Upstream{newTestUpstream(t)},
|
|
},
|
|
RequestHandler: reqHandler,
|
|
UDPListenAddr: []*net.UDPAddr{dnsproxytest.LocalhostAnyPortUDP},
|
|
TCPListenAddr: []*net.TCPAddr{dnsproxytest.LocalhostAnyPortTCP},
|
|
})
|
|
|
|
servicetest.RequireRun(t, dnsProxy, dnsproxytest.Timeout)
|
|
|
|
addr := dnsProxy.Addr(ProtoTCP)
|
|
client := &dns.Client{
|
|
Net: string(ProtoTCP),
|
|
Timeout: dnsproxytest.Timeout,
|
|
}
|
|
|
|
// Send the first message (not blocked)
|
|
req := dnsproxytest.NewTestRequest()
|
|
|
|
r, _, err := client.Exchange(req, addr.String())
|
|
require.NoError(t, err)
|
|
dnsproxytest.RequireResponse(t, req, r)
|
|
|
|
// Now send the second and make sure it is blocked
|
|
m.Lock()
|
|
blockResponse = true
|
|
m.Unlock()
|
|
|
|
r, _, err = client.Exchange(req, addr.String())
|
|
require.NoError(t, err)
|
|
assert.Equal(t, dns.RcodeNotImplemented, r.Rcode)
|
|
}
|