AdGuardHome/upstream/upstream_test.go

100 lines
1.9 KiB
Go
Raw Normal View History

2018-11-01 11:45:32 +00:00
package upstream
import (
"github.com/miekg/dns"
"net"
"testing"
)
func TestDnsUpstream(t *testing.T) {
2018-11-05 17:40:10 +00:00
u, err := NewDnsUpstream("8.8.8.8:53", "udp", "")
2018-11-01 11:45:32 +00:00
if err != nil {
t.Errorf("cannot create a DNS upstream")
}
testUpstream(t, u)
}
func TestHttpsUpstream(t *testing.T) {
testCases := []string{
"https://cloudflare-dns.com/dns-query",
"https://dns.google.com/experimental",
"https://doh.cleanbrowsing.org/doh/security-filter/",
}
for _, url := range testCases {
u, err := NewHttpsUpstream(url)
if err != nil {
t.Errorf("cannot create a DNS-over-HTTPS upstream")
}
testUpstream(t, u)
}
}
func TestDnsOverTlsUpstream(t *testing.T) {
var tests = []struct {
endpoint string
tlsServerName string
}{
{"1.1.1.1:853", ""},
2018-11-05 17:40:10 +00:00
{"9.9.9.9:853", ""},
2018-11-01 11:45:32 +00:00
{"185.228.168.10:853", "security-filter-dns.cleanbrowsing.org"},
}
for _, test := range tests {
2018-11-05 17:40:10 +00:00
u, err := NewDnsUpstream(test.endpoint, "tcp-tls", test.tlsServerName)
2018-11-01 11:45:32 +00:00
if err != nil {
t.Errorf("cannot create a DNS-over-TLS upstream")
}
testUpstream(t, u)
}
}
func testUpstream(t *testing.T, u Upstream) {
2018-11-05 17:40:10 +00:00
var tests = []struct {
name string
expected net.IP
}{
{"google-public-dns-a.google.com.", net.IPv4(8, 8, 8, 8)},
{"google-public-dns-b.google.com.", net.IPv4(8, 8, 4, 4)},
2018-11-01 11:45:32 +00:00
}
2018-11-05 17:40:10 +00:00
for _, test := range tests {
req := dns.Msg{}
req.Id = dns.Id()
req.RecursionDesired = true
req.Question = []dns.Question{
{Name: test.name, Qtype: dns.TypeA, Qclass: dns.ClassINET},
}
2018-11-01 11:45:32 +00:00
2018-11-05 17:40:10 +00:00
resp, err := u.Exchange(nil, &req)
2018-11-01 11:45:32 +00:00
2018-11-05 17:40:10 +00:00
if err != nil {
t.Errorf("error while making an upstream request: %s", err)
}
if len(resp.Answer) != 1 {
t.Errorf("no answer section in the response")
}
if answer, ok := resp.Answer[0].(*dns.A); ok {
if !test.expected.Equal(answer.A) {
t.Errorf("wrong IP in the response: %v", answer.A)
}
2018-11-01 11:45:32 +00:00
}
}
2018-11-05 17:40:10 +00:00
err := u.Close()
if err != nil {
t.Errorf("Error while closing the upstream: %s", err)
}
2018-11-01 11:45:32 +00:00
}