2018-11-01 04:45:32 -07:00
|
|
|
package upstream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2018-11-05 11:19:01 -07:00
|
|
|
func TestDnsUpstreamIsAlive(t *testing.T) {
|
2018-11-01 04:45:32 -07:00
|
|
|
|
2018-11-05 11:19:01 -07:00
|
|
|
var tests = []struct {
|
|
|
|
endpoint string
|
|
|
|
proto string
|
|
|
|
}{
|
|
|
|
{"8.8.8.8:53", "udp"},
|
|
|
|
{"8.8.8.8:53", "tcp"},
|
|
|
|
{"1.1.1.1:53", "udp"},
|
|
|
|
}
|
2018-11-01 04:45:32 -07:00
|
|
|
|
2018-11-05 11:19:01 -07:00
|
|
|
for _, test := range tests {
|
|
|
|
u, err := NewDnsUpstream(test.endpoint, test.proto, "")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot create a DNS upstream")
|
|
|
|
}
|
|
|
|
|
|
|
|
testUpstreamIsAlive(t, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHttpsUpstreamIsAlive(t *testing.T) {
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
url string
|
|
|
|
bootstrap string
|
|
|
|
}{
|
|
|
|
{"https://cloudflare-dns.com/dns-query", "8.8.8.8:53"},
|
|
|
|
{"https://dns.google.com/experimental", "8.8.8.8:53"},
|
|
|
|
{"https://doh.cleanbrowsing.org/doh/security-filter/", ""}, // TODO: status 201??
|
2018-11-01 04:45:32 -07:00
|
|
|
}
|
|
|
|
|
2018-11-05 11:19:01 -07:00
|
|
|
for _, test := range tests {
|
|
|
|
u, err := NewHttpsUpstream(test.url, test.bootstrap)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot create a DNS-over-HTTPS upstream")
|
|
|
|
}
|
|
|
|
|
|
|
|
testUpstreamIsAlive(t, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDnsOverTlsIsAlive(t *testing.T) {
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
endpoint string
|
|
|
|
tlsServerName string
|
|
|
|
}{
|
|
|
|
{"1.1.1.1:853", ""},
|
|
|
|
{"9.9.9.9:853", ""},
|
|
|
|
{"185.228.168.10:853", "security-filter-dns.cleanbrowsing.org"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
u, err := NewDnsUpstream(test.endpoint, "tcp-tls", test.tlsServerName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot create a DNS-over-TLS upstream")
|
|
|
|
}
|
|
|
|
|
|
|
|
testUpstreamIsAlive(t, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDnsUpstream(t *testing.T) {
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
endpoint string
|
|
|
|
proto string
|
|
|
|
}{
|
|
|
|
{"8.8.8.8:53", "udp"},
|
|
|
|
{"8.8.8.8:53", "tcp"},
|
|
|
|
{"1.1.1.1:53", "udp"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
u, err := NewDnsUpstream(test.endpoint, test.proto, "")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot create a DNS upstream")
|
|
|
|
}
|
|
|
|
|
|
|
|
testUpstream(t, u)
|
|
|
|
}
|
2018-11-01 04:45:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHttpsUpstream(t *testing.T) {
|
|
|
|
|
2018-11-05 11:19:01 -07:00
|
|
|
var tests = []struct {
|
|
|
|
url string
|
|
|
|
bootstrap string
|
|
|
|
}{
|
|
|
|
{"https://cloudflare-dns.com/dns-query", "8.8.8.8:53"},
|
|
|
|
{"https://dns.google.com/experimental", "8.8.8.8:53"},
|
|
|
|
{"https://doh.cleanbrowsing.org/doh/security-filter/", ""},
|
2018-11-01 04:45:32 -07:00
|
|
|
}
|
|
|
|
|
2018-11-05 11:19:01 -07:00
|
|
|
for _, test := range tests {
|
|
|
|
u, err := NewHttpsUpstream(test.url, test.bootstrap)
|
2018-11-01 04:45:32 -07:00
|
|
|
|
|
|
|
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 10:40:10 -07:00
|
|
|
{"9.9.9.9:853", ""},
|
2018-11-01 04:45:32 -07:00
|
|
|
{"185.228.168.10:853", "security-filter-dns.cleanbrowsing.org"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2018-11-05 10:40:10 -07:00
|
|
|
u, err := NewDnsUpstream(test.endpoint, "tcp-tls", test.tlsServerName)
|
2018-11-01 04:45:32 -07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot create a DNS-over-TLS upstream")
|
|
|
|
}
|
|
|
|
|
|
|
|
testUpstream(t, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 11:19:01 -07:00
|
|
|
func testUpstreamIsAlive(t *testing.T, u Upstream) {
|
|
|
|
alive, err := IsAlive(u)
|
|
|
|
if !alive || err != nil {
|
|
|
|
t.Errorf("Upstream is not alive")
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Close()
|
|
|
|
}
|
|
|
|
|
2018-11-01 04:45:32 -07:00
|
|
|
func testUpstream(t *testing.T, u Upstream) {
|
2018-11-05 10:40:10 -07: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 04:45:32 -07:00
|
|
|
}
|
|
|
|
|
2018-11-05 10:40:10 -07: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 04:45:32 -07:00
|
|
|
|
2018-11-05 10:40:10 -07:00
|
|
|
resp, err := u.Exchange(nil, &req)
|
2018-11-01 04:45:32 -07:00
|
|
|
|
2018-11-05 10:40:10 -07: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 04:45:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 10:40:10 -07:00
|
|
|
err := u.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error while closing the upstream: %s", err)
|
|
|
|
}
|
2018-11-01 04:45:32 -07:00
|
|
|
}
|