2018-11-01 11:45:32 +00:00
|
|
|
package upstream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
2018-11-05 20:52:11 +00:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
"golang.org/x/net/context"
|
2018-11-01 11:45:32 +00:00
|
|
|
)
|
|
|
|
|
2018-11-05 18:19:01 +00:00
|
|
|
func TestDnsUpstreamIsAlive(t *testing.T) {
|
2018-11-01 11:45:32 +00:00
|
|
|
|
2018-11-05 18:19:01 +00:00
|
|
|
var tests = []struct {
|
2018-11-05 19:11:13 +00:00
|
|
|
url string
|
|
|
|
bootstrap string
|
2018-11-05 18:19:01 +00:00
|
|
|
}{
|
2018-11-05 19:11:13 +00:00
|
|
|
{"8.8.8.8:53", "8.8.8.8:53"},
|
|
|
|
{"1.1.1.1", ""},
|
|
|
|
{"tcp://1.1.1.1:53", ""},
|
|
|
|
{"176.103.130.130:5353", ""},
|
2018-11-05 18:19:01 +00:00
|
|
|
}
|
2018-11-01 11:45:32 +00:00
|
|
|
|
2018-11-05 18:19:01 +00:00
|
|
|
for _, test := range tests {
|
2018-11-05 19:11:13 +00:00
|
|
|
u, err := NewUpstream(test.url, test.bootstrap)
|
2018-11-05 18:19:01 +00:00
|
|
|
|
|
|
|
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"},
|
2018-11-05 19:11:13 +00:00
|
|
|
{"https://doh.cleanbrowsing.org/doh/security-filter/", ""},
|
2018-11-01 11:45:32 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 18:19:01 +00:00
|
|
|
for _, test := range tests {
|
2018-11-05 19:11:13 +00:00
|
|
|
u, err := NewUpstream(test.url, test.bootstrap)
|
2018-11-05 18:19:01 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot create a DNS-over-HTTPS upstream")
|
|
|
|
}
|
|
|
|
|
|
|
|
testUpstreamIsAlive(t, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDnsOverTlsIsAlive(t *testing.T) {
|
|
|
|
|
|
|
|
var tests = []struct {
|
2018-11-05 19:11:13 +00:00
|
|
|
url string
|
|
|
|
bootstrap string
|
2018-11-05 18:19:01 +00:00
|
|
|
}{
|
2018-11-05 19:11:13 +00:00
|
|
|
{"tls://1.1.1.1", ""},
|
|
|
|
{"tls://9.9.9.9:853", ""},
|
|
|
|
{"tls://security-filter-dns.cleanbrowsing.org", "8.8.8.8:53"},
|
|
|
|
{"tls://adult-filter-dns.cleanbrowsing.org:853", "8.8.8.8:53"},
|
2018-11-05 18:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2018-11-05 19:11:13 +00:00
|
|
|
u, err := NewUpstream(test.url, test.bootstrap)
|
2018-11-05 18:19:01 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot create a DNS-over-TLS upstream")
|
|
|
|
}
|
|
|
|
|
|
|
|
testUpstreamIsAlive(t, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDnsUpstream(t *testing.T) {
|
|
|
|
|
|
|
|
var tests = []struct {
|
2018-11-05 19:11:13 +00:00
|
|
|
url string
|
|
|
|
bootstrap string
|
2018-11-05 18:19:01 +00:00
|
|
|
}{
|
2018-11-05 19:11:13 +00:00
|
|
|
{"8.8.8.8:53", "8.8.8.8:53"},
|
|
|
|
{"1.1.1.1", ""},
|
|
|
|
{"tcp://1.1.1.1:53", ""},
|
|
|
|
{"176.103.130.130:5353", ""},
|
2018-11-05 18:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2018-11-05 19:11:13 +00:00
|
|
|
u, err := NewUpstream(test.url, test.bootstrap)
|
2018-11-05 18:19:01 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot create a DNS upstream")
|
|
|
|
}
|
|
|
|
|
|
|
|
testUpstream(t, u)
|
|
|
|
}
|
2018-11-01 11:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHttpsUpstream(t *testing.T) {
|
|
|
|
|
2018-11-05 18:19:01 +00: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 11:45:32 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 18:19:01 +00:00
|
|
|
for _, test := range tests {
|
2018-11-05 19:11:13 +00:00
|
|
|
u, err := NewUpstream(test.url, test.bootstrap)
|
2018-11-01 11:45:32 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot create a DNS-over-HTTPS upstream")
|
|
|
|
}
|
|
|
|
|
|
|
|
testUpstream(t, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDnsOverTlsUpstream(t *testing.T) {
|
|
|
|
|
|
|
|
var tests = []struct {
|
2018-11-05 19:11:13 +00:00
|
|
|
url string
|
|
|
|
bootstrap string
|
2018-11-01 11:45:32 +00:00
|
|
|
}{
|
2018-11-05 19:11:13 +00:00
|
|
|
{"tls://1.1.1.1", ""},
|
|
|
|
{"tls://9.9.9.9:853", ""},
|
|
|
|
{"tls://security-filter-dns.cleanbrowsing.org", "8.8.8.8:53"},
|
|
|
|
{"tls://adult-filter-dns.cleanbrowsing.org:853", "8.8.8.8:53"},
|
2018-11-01 11:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2018-11-05 19:11:13 +00:00
|
|
|
u, err := NewUpstream(test.url, test.bootstrap)
|
2018-11-01 11:45:32 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot create a DNS-over-TLS upstream")
|
|
|
|
}
|
|
|
|
|
|
|
|
testUpstream(t, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 18:19:01 +00: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 11:45:32 +00:00
|
|
|
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 20:49:31 +00:00
|
|
|
resp, err := u.Exchange(context.Background(), &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
|
|
|
}
|