Pull request: all: custom autohost tlds
Updates #2393. Squashed commit of the following: commit 87034134e240480938cdeec14d6b44294bf6442c Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Mar 25 15:48:46 2021 +0300 dnsforward: fix commit abf3a1ce8ed7a148d1cc631007fb0422f6da4ae6 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Mar 25 15:21:11 2021 +0300 dnsforward: imp code, validation commit fac389bdafc093ce17a7e0831166b89293b550be Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Mar 25 14:54:45 2021 +0300 all: add validation, imp docs, tests commit 21b4532afe59f3b89383cb330c9a7d49ec124b6e Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed Mar 24 19:09:43 2021 +0300 all: custom autohost tlds
This commit is contained in:
107
internal/dnsforward/dns_test.go
Normal file
107
internal/dnsforward/dns_test.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package dnsforward
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/AdguardTeam/dnsproxy/proxy"
|
||||
"github.com/miekg/dns"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestServer_ProcessInternalHosts(t *testing.T) {
|
||||
knownIP := net.IP{1, 2, 3, 4}
|
||||
testCases := []struct {
|
||||
name string
|
||||
host string
|
||||
suffix string
|
||||
wantErrMsg string
|
||||
wantIP net.IP
|
||||
qtyp uint16
|
||||
wantRes resultCode
|
||||
}{{
|
||||
name: "success_external",
|
||||
host: "example.com",
|
||||
suffix: defaultAutohostSuffix,
|
||||
wantErrMsg: "",
|
||||
wantIP: nil,
|
||||
qtyp: dns.TypeA,
|
||||
wantRes: resultCodeSuccess,
|
||||
}, {
|
||||
name: "success_external_non_a",
|
||||
host: "example.com",
|
||||
suffix: defaultAutohostSuffix,
|
||||
wantErrMsg: "",
|
||||
wantIP: nil,
|
||||
qtyp: dns.TypeCNAME,
|
||||
wantRes: resultCodeSuccess,
|
||||
}, {
|
||||
name: "success_internal",
|
||||
host: "example.lan",
|
||||
suffix: defaultAutohostSuffix,
|
||||
wantErrMsg: "",
|
||||
wantIP: knownIP,
|
||||
qtyp: dns.TypeA,
|
||||
wantRes: resultCodeSuccess,
|
||||
}, {
|
||||
name: "success_internal_unknown",
|
||||
host: "example-new.lan",
|
||||
suffix: defaultAutohostSuffix,
|
||||
wantErrMsg: "",
|
||||
wantIP: nil,
|
||||
qtyp: dns.TypeA,
|
||||
wantRes: resultCodeSuccess,
|
||||
}}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
s := &Server{
|
||||
autohostSuffix: tc.suffix,
|
||||
tableHostToIP: map[string]net.IP{
|
||||
"example": knownIP,
|
||||
},
|
||||
}
|
||||
|
||||
req := &dns.Msg{
|
||||
MsgHdr: dns.MsgHdr{
|
||||
Id: 1234,
|
||||
},
|
||||
Question: []dns.Question{{
|
||||
Name: dns.Fqdn(tc.host),
|
||||
Qtype: tc.qtyp,
|
||||
Qclass: dns.ClassINET,
|
||||
}},
|
||||
}
|
||||
|
||||
dctx := &dnsContext{
|
||||
proxyCtx: &proxy.DNSContext{
|
||||
Req: req,
|
||||
},
|
||||
}
|
||||
|
||||
res := s.processInternalHosts(dctx)
|
||||
assert.Equal(t, tc.wantRes, res)
|
||||
|
||||
if tc.wantErrMsg == "" {
|
||||
assert.NoError(t, dctx.err)
|
||||
} else {
|
||||
require.Error(t, dctx.err)
|
||||
|
||||
assert.Equal(t, tc.wantErrMsg, dctx.err.Error())
|
||||
}
|
||||
|
||||
pctx := dctx.proxyCtx
|
||||
if tc.wantIP == nil {
|
||||
assert.Nil(t, pctx.Res)
|
||||
} else {
|
||||
require.NotNil(t, pctx.Res)
|
||||
|
||||
ans := pctx.Res.Answer
|
||||
require.Len(t, ans, 1)
|
||||
|
||||
assert.Equal(t, tc.wantIP, ans[0].(*dns.A).A)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user