2020-12-23 12:35:07 +03:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2023-08-30 18:26:02 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2020-12-23 12:35:07 +03:00
|
|
|
"github.com/AdguardTeam/urlfilter/rules"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenAnswerHTTPS_andSVCB(t *testing.T) {
|
|
|
|
// Preconditions.
|
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
s := createTestServer(t, &filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
}, ServerConfig{
|
|
|
|
Config: Config{
|
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
|
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2023-09-04 17:18:43 +03:00
|
|
|
}, nil)
|
2020-12-23 12:35:07 +03:00
|
|
|
|
|
|
|
req := &dns.Msg{
|
|
|
|
Question: []dns.Question{{
|
|
|
|
Name: "abcd",
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constants and helper values.
|
|
|
|
|
|
|
|
const host = "example.com"
|
|
|
|
const prio = 32
|
|
|
|
|
|
|
|
ip4 := net.IPv4(127, 0, 0, 1)
|
|
|
|
ip6 := net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
|
|
|
|
|
|
|
|
// Helper functions.
|
|
|
|
|
|
|
|
dnssvcb := func(key, value string) (svcb *rules.DNSSVCB) {
|
|
|
|
svcb = &rules.DNSSVCB{
|
|
|
|
Target: host,
|
|
|
|
Priority: prio,
|
|
|
|
}
|
|
|
|
|
|
|
|
if key == "" {
|
|
|
|
return svcb
|
|
|
|
}
|
|
|
|
|
|
|
|
svcb.Params = map[string]string{
|
|
|
|
key: value,
|
|
|
|
}
|
|
|
|
|
|
|
|
return svcb
|
|
|
|
}
|
|
|
|
|
|
|
|
wantsvcb := func(kv dns.SVCBKeyValue) (want *dns.SVCB) {
|
|
|
|
want = &dns.SVCB{
|
|
|
|
Hdr: s.hdr(req, dns.TypeSVCB),
|
|
|
|
Priority: prio,
|
2021-03-15 17:17:16 +03:00
|
|
|
Target: dns.Fqdn(host),
|
2020-12-23 12:35:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if kv == nil {
|
|
|
|
return want
|
|
|
|
}
|
|
|
|
|
|
|
|
want.Value = []dns.SVCBKeyValue{kv}
|
|
|
|
|
|
|
|
return want
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests.
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
svcb *rules.DNSSVCB
|
|
|
|
want *dns.SVCB
|
|
|
|
name string
|
|
|
|
}{{
|
|
|
|
svcb: dnssvcb("", ""),
|
|
|
|
want: wantsvcb(nil),
|
|
|
|
name: "no_params",
|
|
|
|
}, {
|
|
|
|
svcb: dnssvcb("foo", "bar"),
|
|
|
|
want: wantsvcb(nil),
|
|
|
|
name: "invalid",
|
|
|
|
}, {
|
|
|
|
svcb: dnssvcb("alpn", "h3"),
|
|
|
|
want: wantsvcb(&dns.SVCBAlpn{Alpn: []string{"h3"}}),
|
|
|
|
name: "alpn",
|
2022-04-07 18:08:39 +03:00
|
|
|
}, {
|
|
|
|
svcb: dnssvcb("ech", "AAAA"),
|
|
|
|
want: wantsvcb(&dns.SVCBECHConfig{ECH: []byte{0, 0, 0}}),
|
|
|
|
name: "ech",
|
2020-12-23 12:35:07 +03:00
|
|
|
}, {
|
|
|
|
svcb: dnssvcb("echconfig", "AAAA"),
|
|
|
|
want: wantsvcb(&dns.SVCBECHConfig{ECH: []byte{0, 0, 0}}),
|
2022-04-07 18:08:39 +03:00
|
|
|
name: "ech_deprecated",
|
2020-12-23 12:35:07 +03:00
|
|
|
}, {
|
|
|
|
svcb: dnssvcb("echconfig", "%BAD%"),
|
|
|
|
want: wantsvcb(nil),
|
2022-04-07 18:08:39 +03:00
|
|
|
name: "ech_invalid",
|
2020-12-23 12:35:07 +03:00
|
|
|
}, {
|
|
|
|
svcb: dnssvcb("ipv4hint", "127.0.0.1"),
|
|
|
|
want: wantsvcb(&dns.SVCBIPv4Hint{Hint: []net.IP{ip4}}),
|
|
|
|
name: "ipv4hint",
|
|
|
|
}, {
|
|
|
|
svcb: dnssvcb("ipv4hint", "127.0.01"),
|
|
|
|
want: wantsvcb(nil),
|
|
|
|
name: "ipv4hint_invalid",
|
|
|
|
}, {
|
|
|
|
svcb: dnssvcb("ipv6hint", "::1"),
|
|
|
|
want: wantsvcb(&dns.SVCBIPv6Hint{Hint: []net.IP{ip6}}),
|
|
|
|
name: "ipv6hint",
|
|
|
|
}, {
|
|
|
|
svcb: dnssvcb("ipv6hint", ":::1"),
|
|
|
|
want: wantsvcb(nil),
|
|
|
|
name: "ipv6hint_invalid",
|
|
|
|
}, {
|
|
|
|
svcb: dnssvcb("mandatory", "alpn"),
|
|
|
|
want: wantsvcb(&dns.SVCBMandatory{Code: []dns.SVCBKey{dns.SVCB_ALPN}}),
|
|
|
|
name: "mandatory",
|
|
|
|
}, {
|
|
|
|
svcb: dnssvcb("mandatory", "alpnn"),
|
|
|
|
want: wantsvcb(nil),
|
|
|
|
name: "mandatory_invalid",
|
|
|
|
}, {
|
|
|
|
svcb: dnssvcb("no-default-alpn", ""),
|
|
|
|
want: wantsvcb(&dns.SVCBNoDefaultAlpn{}),
|
2021-03-15 17:17:16 +03:00
|
|
|
name: "no_default_alpn",
|
2022-04-12 15:45:18 +03:00
|
|
|
}, {
|
|
|
|
svcb: dnssvcb("dohpath", "/dns-query"),
|
|
|
|
want: wantsvcb(&dns.SVCBDoHPath{Template: "/dns-query"}),
|
|
|
|
name: "dohpath",
|
2020-12-23 12:35:07 +03:00
|
|
|
}, {
|
|
|
|
svcb: dnssvcb("port", "8080"),
|
|
|
|
want: wantsvcb(&dns.SVCBPort{Port: 8080}),
|
|
|
|
name: "port",
|
|
|
|
}, {
|
|
|
|
svcb: dnssvcb("port", "1005008080"),
|
|
|
|
want: wantsvcb(nil),
|
2021-03-15 17:17:16 +03:00
|
|
|
name: "bad_port",
|
2020-12-23 12:35:07 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run("https", func(t *testing.T) {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
want := &dns.HTTPS{SVCB: *tc.want}
|
|
|
|
want.Hdr.Rrtype = dns.TypeHTTPS
|
|
|
|
|
|
|
|
got := s.genAnswerHTTPS(req, tc.svcb)
|
|
|
|
assert.Equal(t, want, got)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("svcb", func(t *testing.T) {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
got := s.genAnswerSVCB(req, tc.svcb)
|
|
|
|
assert.Equal(t, tc.want, got)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|