2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-04-14 21:15:54 +00:00
|
|
|
|
|
|
|
package publicdns
|
|
|
|
|
|
|
|
import (
|
2022-07-26 03:55:44 +00:00
|
|
|
"net/netip"
|
2022-09-06 18:15:30 +00:00
|
|
|
"reflect"
|
2022-04-14 21:15:54 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInit(t *testing.T) {
|
2022-09-06 18:15:30 +00:00
|
|
|
for _, baseKey := range KnownDoHPrefixes() {
|
|
|
|
baseSet := DoHIPsOfBase(baseKey)
|
2022-04-14 21:15:54 +00:00
|
|
|
for _, addr := range baseSet {
|
2022-09-06 18:15:30 +00:00
|
|
|
back, only, ok := DoHEndpointFromIP(addr)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("DoHEndpointFromIP(%v) not mapped back to %v", addr, baseKey)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if only {
|
|
|
|
t.Errorf("unexpected DoH only bit set for %v", addr)
|
|
|
|
}
|
|
|
|
if back != baseKey {
|
|
|
|
t.Errorf("Expected %v to map to %s, got %s", addr, baseKey, back)
|
2022-04-14 21:15:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 18:15:30 +00:00
|
|
|
func TestDoHV6(t *testing.T) {
|
2022-04-14 21:15:54 +00:00
|
|
|
tests := []struct {
|
|
|
|
in string
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
firstIP netip.Addr
|
2022-04-14 21:15:54 +00:00
|
|
|
want bool
|
|
|
|
}{
|
2022-07-26 03:55:44 +00:00
|
|
|
{"https://cloudflare-dns.com/dns-query", netip.MustParseAddr("2606:4700:4700::1111"), true},
|
|
|
|
{"https://dns.google/dns-query", netip.MustParseAddr("2001:4860:4860::8888"), true},
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
{"bogus", netip.Addr{}, false},
|
2022-04-14 21:15:54 +00:00
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.in, func(t *testing.T) {
|
|
|
|
ip, ok := DoHV6(test.in)
|
|
|
|
if ok != test.want || ip != test.firstIP {
|
|
|
|
t.Errorf("DohV6 got (%v: IPv6 %v) for %v, want (%v: IPv6 %v)", ip, ok, test.in, test.firstIP, test.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-09-06 18:15:30 +00:00
|
|
|
|
|
|
|
func TestDoHIPsOfBase(t *testing.T) {
|
|
|
|
ips := func(s ...string) (ret []netip.Addr) {
|
|
|
|
for _, ip := range s {
|
|
|
|
ret = append(ret, netip.MustParseAddr(ip))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
base string
|
|
|
|
want []netip.Addr
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
base: "https://cloudflare-dns.com/dns-query",
|
|
|
|
want: ips("1.1.1.1", "1.0.0.1", "2606:4700:4700::1111", "2606:4700:4700::1001"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
base: "https://dns.nextdns.io/",
|
|
|
|
want: ips(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
base: "https://dns.nextdns.io/ff",
|
|
|
|
want: ips(
|
|
|
|
"45.90.28.0",
|
|
|
|
"45.90.30.0",
|
|
|
|
"2a07:a8c0::ff",
|
|
|
|
"2a07:a8c1::ff",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
base: "https://dns.nextdns.io/c3a884",
|
|
|
|
want: ips(
|
|
|
|
"45.90.28.0",
|
|
|
|
"45.90.30.0",
|
|
|
|
"2a07:a8c0::c3:a884",
|
|
|
|
"2a07:a8c1::c3:a884",
|
|
|
|
),
|
|
|
|
},
|
2022-09-29 00:19:30 +00:00
|
|
|
{
|
|
|
|
base: "https://dns.nextdns.io/112233445566778899aabbcc",
|
|
|
|
want: ips(
|
|
|
|
"45.90.28.0",
|
|
|
|
"45.90.30.0",
|
|
|
|
"2a07:a8c0:1122:3344:5566:7788:99aa:bbcc",
|
|
|
|
"2a07:a8c1:1122:3344:5566:7788:99aa:bbcc",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
base: "https://dns.nextdns.io/112233445566778899aabbccdd",
|
|
|
|
want: ips(), // nothing; profile length is over 12 bytes
|
|
|
|
},
|
2022-09-09 21:06:06 +00:00
|
|
|
{
|
|
|
|
base: "https://dns.nextdns.io/c3a884/with/more/stuff",
|
|
|
|
want: ips(
|
|
|
|
"45.90.28.0",
|
|
|
|
"45.90.30.0",
|
|
|
|
"2a07:a8c0::c3:a884",
|
|
|
|
"2a07:a8c1::c3:a884",
|
|
|
|
),
|
|
|
|
},
|
2022-09-11 15:07:45 +00:00
|
|
|
{
|
|
|
|
base: "https://dns.nextdns.io/c3a884?with=query¶ms",
|
|
|
|
want: ips(
|
|
|
|
"45.90.28.0",
|
|
|
|
"45.90.30.0",
|
|
|
|
"2a07:a8c0::c3:a884",
|
|
|
|
"2a07:a8c1::c3:a884",
|
|
|
|
),
|
|
|
|
},
|
2023-06-23 00:47:36 +00:00
|
|
|
{
|
|
|
|
base: "https://dns.controld.com/hyq3ipr2ct",
|
|
|
|
want: ips(
|
|
|
|
"76.76.2.22",
|
|
|
|
"76.76.10.22",
|
|
|
|
"2a07:a8c0:0:6:7b5b:5949:35ad:0",
|
|
|
|
"2a07:a8c1:0:6:7b5b:5949:35ad:0",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
base: "https://dns.controld.com/112233445566778899aabbcc",
|
|
|
|
want: ips(
|
|
|
|
"76.76.2.22",
|
|
|
|
"76.76.10.22",
|
|
|
|
"2a07:a8c0:0:ffff:ffff:ffff:ffff:0",
|
|
|
|
"2a07:a8c1:0:ffff:ffff:ffff:ffff:0",
|
|
|
|
),
|
|
|
|
},
|
2022-09-06 18:15:30 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
got := DoHIPsOfBase(tt.base)
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("DoHIPsOfBase(%q) = %v; want %v", tt.base, got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|