refactor(dnsx): group tests together (#516)
Part of https://github.com/ooni/probe/issues/1591
This commit is contained in:
parent
12cf4b9990
commit
de130d249c
|
@ -11,7 +11,9 @@ import (
|
|||
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
||||
)
|
||||
|
||||
func TestDecoderUnpackError(t *testing.T) {
|
||||
func TestDNSDecoder(t *testing.T) {
|
||||
t.Run("LookupHost", func(t *testing.T) {
|
||||
t.Run("UnpackError", func(t *testing.T) {
|
||||
d := &DNSDecoderMiekg{}
|
||||
data, err := d.DecodeLookupHost(dns.TypeA, nil)
|
||||
if err == nil {
|
||||
|
@ -20,45 +22,47 @@ func TestDecoderUnpackError(t *testing.T) {
|
|||
if data != nil {
|
||||
t.Fatal("expected nil data here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDecoderNXDOMAIN(t *testing.T) {
|
||||
t.Run("NXDOMAIN", func(t *testing.T) {
|
||||
d := &DNSDecoderMiekg{}
|
||||
data, err := d.DecodeLookupHost(dns.TypeA, genReplyError(t, dns.RcodeNameError))
|
||||
data, err := d.DecodeLookupHost(
|
||||
dns.TypeA, dnsGenReplyWithError(t, dns.TypeA, dns.RcodeNameError))
|
||||
if err == nil || !strings.HasSuffix(err.Error(), "no such host") {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if data != nil {
|
||||
t.Fatal("expected nil data here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDecoderRefusedError(t *testing.T) {
|
||||
t.Run("Refused", func(t *testing.T) {
|
||||
d := &DNSDecoderMiekg{}
|
||||
data, err := d.DecodeLookupHost(dns.TypeA, genReplyError(t, dns.RcodeRefused))
|
||||
data, err := d.DecodeLookupHost(
|
||||
dns.TypeA, dnsGenReplyWithError(t, dns.TypeA, dns.RcodeRefused))
|
||||
if !errors.Is(err, errorsx.ErrOODNSRefused) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if data != nil {
|
||||
t.Fatal("expected nil data here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDecoderNoAddress(t *testing.T) {
|
||||
t.Run("no address", func(t *testing.T) {
|
||||
d := &DNSDecoderMiekg{}
|
||||
data, err := d.DecodeLookupHost(dns.TypeA, genReplySuccess(t, dns.TypeA))
|
||||
data, err := d.DecodeLookupHost(dns.TypeA, dnsGenLookupHostReplySuccess(t, dns.TypeA))
|
||||
if !errors.Is(err, errorsx.ErrOODNSNoAnswer) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if data != nil {
|
||||
t.Fatal("expected nil data here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDecoderDecodeA(t *testing.T) {
|
||||
t.Run("decode A", func(t *testing.T) {
|
||||
d := &DNSDecoderMiekg{}
|
||||
data, err := d.DecodeLookupHost(
|
||||
dns.TypeA, genReplySuccess(t, dns.TypeA, "1.1.1.1", "8.8.8.8"))
|
||||
dns.TypeA, dnsGenLookupHostReplySuccess(t, dns.TypeA, "1.1.1.1", "8.8.8.8"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -71,12 +75,12 @@ func TestDecoderDecodeA(t *testing.T) {
|
|||
if data[1] != "8.8.8.8" {
|
||||
t.Fatal("invalid second IPv4 entry")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDecoderDecodeAAAA(t *testing.T) {
|
||||
t.Run("decode AAAA", func(t *testing.T) {
|
||||
d := &DNSDecoderMiekg{}
|
||||
data, err := d.DecodeLookupHost(
|
||||
dns.TypeAAAA, genReplySuccess(t, dns.TypeAAAA, "::1", "fe80::1"))
|
||||
dns.TypeAAAA, dnsGenLookupHostReplySuccess(t, dns.TypeAAAA, "::1", "fe80::1"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -89,36 +93,103 @@ func TestDecoderDecodeAAAA(t *testing.T) {
|
|||
if data[1] != "fe80::1" {
|
||||
t.Fatal("invalid second IPv6 entry")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDecoderUnexpectedAReply(t *testing.T) {
|
||||
t.Run("unexpected A reply", func(t *testing.T) {
|
||||
d := &DNSDecoderMiekg{}
|
||||
data, err := d.DecodeLookupHost(
|
||||
dns.TypeA, genReplySuccess(t, dns.TypeAAAA, "::1", "fe80::1"))
|
||||
dns.TypeA, dnsGenLookupHostReplySuccess(t, dns.TypeAAAA, "::1", "fe80::1"))
|
||||
if !errors.Is(err, errorsx.ErrOODNSNoAnswer) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if data != nil {
|
||||
t.Fatal("expected nil data here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDecoderUnexpectedAAAAReply(t *testing.T) {
|
||||
t.Run("unexpected AAAA reply", func(t *testing.T) {
|
||||
d := &DNSDecoderMiekg{}
|
||||
data, err := d.DecodeLookupHost(
|
||||
dns.TypeAAAA, genReplySuccess(t, dns.TypeA, "1.1.1.1", "8.8.4.4."))
|
||||
dns.TypeAAAA, dnsGenLookupHostReplySuccess(t, dns.TypeA, "1.1.1.1", "8.8.4.4."))
|
||||
if !errors.Is(err, errorsx.ErrOODNSNoAnswer) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if data != nil {
|
||||
t.Fatal("expected nil data here")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("parseReply", func(t *testing.T) {
|
||||
d := &DNSDecoderMiekg{}
|
||||
msg := &dns.Msg{}
|
||||
msg.Rcode = dns.RcodeFormatError // an rcode we don't handle
|
||||
data, err := msg.Pack()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reply, err := d.parseReply(data)
|
||||
if !errors.Is(err, errorsx.ErrOODNSMisbehaving) { // catch all error
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if reply != nil {
|
||||
t.Fatal("expected nil reply")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("DecodeHTTPS", func(t *testing.T) {
|
||||
t.Run("with nil data", func(t *testing.T) {
|
||||
d := &DNSDecoderMiekg{}
|
||||
reply, err := d.DecodeHTTPS(nil)
|
||||
if err == nil || err.Error() != "dns: overflow unpacking uint16" {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if reply != nil {
|
||||
t.Fatal("expected nil reply")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("with empty answer", func(t *testing.T) {
|
||||
data := dnsGenHTTPSReplySuccess(t, nil, nil, nil)
|
||||
d := &DNSDecoderMiekg{}
|
||||
reply, err := d.DecodeHTTPS(data)
|
||||
if !errors.Is(err, errorsx.ErrOODNSNoAnswer) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if reply != nil {
|
||||
t.Fatal("expected nil reply")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("with full answer", func(t *testing.T) {
|
||||
alpn := []string{"h3"}
|
||||
v4 := []string{"1.1.1.1"}
|
||||
v6 := []string{"::1"}
|
||||
data := dnsGenHTTPSReplySuccess(t, alpn, v4, v6)
|
||||
d := &DNSDecoderMiekg{}
|
||||
reply, err := d.DecodeHTTPS(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(alpn, reply.ALPN); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
if diff := cmp.Diff(v4, reply.IPv4); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
if diff := cmp.Diff(v6, reply.IPv6); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func genReplyError(t *testing.T, code int) []byte {
|
||||
// dnsGenReplyWithError generates a DNS reply for the given
|
||||
// query type (e.g., dns.TypeA) using code as the Rcode.
|
||||
func dnsGenReplyWithError(t *testing.T, qtype uint16, code int) []byte {
|
||||
question := dns.Question{
|
||||
Name: dns.Fqdn("x.org"),
|
||||
Qtype: dns.TypeA,
|
||||
Qtype: qtype,
|
||||
Qclass: dns.ClassINET,
|
||||
}
|
||||
query := new(dns.Msg)
|
||||
|
@ -137,7 +208,9 @@ func genReplyError(t *testing.T, code int) []byte {
|
|||
return data
|
||||
}
|
||||
|
||||
func genReplySuccess(t *testing.T, qtype uint16, ips ...string) []byte {
|
||||
// dnsGenLookupHostReplySuccess generates a successful DNS reply for the given
|
||||
// qtype (e.g., dns.TypeA) containing the given ips... in the answer.
|
||||
func dnsGenLookupHostReplySuccess(t *testing.T, qtype uint16, ips ...string) []byte {
|
||||
question := dns.Question{
|
||||
Name: dns.Fqdn("x.org"),
|
||||
Qtype: qtype,
|
||||
|
@ -183,24 +256,9 @@ func genReplySuccess(t *testing.T, qtype uint16, ips ...string) []byte {
|
|||
return data
|
||||
}
|
||||
|
||||
func TestParseReply(t *testing.T) {
|
||||
d := &DNSDecoderMiekg{}
|
||||
msg := &dns.Msg{}
|
||||
msg.Rcode = dns.RcodeFormatError // an rcode we don't handle
|
||||
data, err := msg.Pack()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reply, err := d.parseReply(data)
|
||||
if !errors.Is(err, errorsx.ErrOODNSMisbehaving) { // catch all error
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if reply != nil {
|
||||
t.Fatal("expected nil reply")
|
||||
}
|
||||
}
|
||||
|
||||
func genReplyHTTPS(t *testing.T, alpns, ipv4, ipv6 []string) []byte {
|
||||
// dnsGenHTTPSReplySuccess generates a successful HTTPS response containing
|
||||
// the given (possibly nil) alpns, ipv4s, and ipv6s.
|
||||
func dnsGenHTTPSReplySuccess(t *testing.T, alpns, ipv4s, ipv6s []string) []byte {
|
||||
question := dns.Question{
|
||||
Name: dns.Fqdn("x.org"),
|
||||
Qtype: dns.TypeHTTPS,
|
||||
|
@ -222,39 +280,28 @@ func genReplyHTTPS(t *testing.T, alpns, ipv4, ipv6 []string) []byte {
|
|||
Rrtype: dns.TypeHTTPS,
|
||||
Class: dns.ClassINET,
|
||||
Ttl: 100,
|
||||
Rdlength: 0,
|
||||
},
|
||||
Priority: 5,
|
||||
Target: dns.Fqdn("x.org"),
|
||||
Value: []dns.SVCBKeyValue{},
|
||||
},
|
||||
}
|
||||
reply.Answer = append(reply.Answer, answer)
|
||||
if len(alpns) > 0 {
|
||||
answer.Value = append(answer.Value, &dns.SVCBAlpn{
|
||||
Alpn: alpns,
|
||||
})
|
||||
answer.Hdr.Rdlength++
|
||||
answer.Value = append(answer.Value, &dns.SVCBAlpn{Alpn: alpns})
|
||||
}
|
||||
if len(ipv4) > 0 {
|
||||
if len(ipv4s) > 0 {
|
||||
var addrs []net.IP
|
||||
for _, addr := range ipv4 {
|
||||
for _, addr := range ipv4s {
|
||||
addrs = append(addrs, net.ParseIP(addr))
|
||||
}
|
||||
answer.Value = append(answer.Value, &dns.SVCBIPv4Hint{
|
||||
Hint: addrs,
|
||||
})
|
||||
answer.Hdr.Rdlength++
|
||||
answer.Value = append(answer.Value, &dns.SVCBIPv4Hint{Hint: addrs})
|
||||
}
|
||||
if len(ipv6) > 0 {
|
||||
if len(ipv6s) > 0 {
|
||||
var addrs []net.IP
|
||||
for _, addr := range ipv6 {
|
||||
for _, addr := range ipv6s {
|
||||
addrs = append(addrs, net.ParseIP(addr))
|
||||
}
|
||||
answer.Value = append(answer.Value, &dns.SVCBIPv6Hint{
|
||||
Hint: addrs,
|
||||
})
|
||||
answer.Hdr.Rdlength++
|
||||
answer.Value = append(answer.Value, &dns.SVCBIPv6Hint{Hint: addrs})
|
||||
}
|
||||
data, err := reply.Pack()
|
||||
if err != nil {
|
||||
|
@ -262,49 +309,3 @@ func genReplyHTTPS(t *testing.T, alpns, ipv4, ipv6 []string) []byte {
|
|||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func TestDecodeHTTPS(t *testing.T) {
|
||||
t.Run("with nil data", func(t *testing.T) {
|
||||
d := &DNSDecoderMiekg{}
|
||||
reply, err := d.DecodeHTTPS(nil)
|
||||
if err == nil || err.Error() != "dns: overflow unpacking uint16" {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if reply != nil {
|
||||
t.Fatal("expected nil reply")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("with empty answer", func(t *testing.T) {
|
||||
data := genReplyHTTPS(t, nil, nil, nil)
|
||||
d := &DNSDecoderMiekg{}
|
||||
reply, err := d.DecodeHTTPS(data)
|
||||
if !errors.Is(err, errorsx.ErrOODNSNoAnswer) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if reply != nil {
|
||||
t.Fatal("expected nil reply")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("with full answer", func(t *testing.T) {
|
||||
alpn := []string{"h3"}
|
||||
v4 := []string{"1.1.1.1"}
|
||||
v6 := []string{"::1"}
|
||||
data := genReplyHTTPS(t, alpn, v4, v6)
|
||||
d := &DNSDecoderMiekg{}
|
||||
reply, err := d.DecodeHTTPS(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(alpn, reply.ALPN); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
if diff := cmp.Diff(v4, reply.IPv4); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
if diff := cmp.Diff(v6, reply.IPv6); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -7,25 +7,64 @@ import (
|
|||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
func TestEncoderEncodeA(t *testing.T) {
|
||||
func TestDNSEncoder(t *testing.T) {
|
||||
|
||||
t.Run("encode A", func(t *testing.T) {
|
||||
e := &DNSEncoderMiekg{}
|
||||
data, err := e.Encode("x.org", dns.TypeA, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
validate(t, data, byte(dns.TypeA))
|
||||
}
|
||||
dnsValidateEncodedQueryBytes(t, data, byte(dns.TypeA))
|
||||
})
|
||||
|
||||
func TestEncoderEncodeAAAA(t *testing.T) {
|
||||
t.Run("encode AAAA", func(t *testing.T) {
|
||||
e := &DNSEncoderMiekg{}
|
||||
data, err := e.Encode("x.org", dns.TypeAAAA, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
validate(t, data, byte(dns.TypeA))
|
||||
dnsValidateEncodedQueryBytes(t, data, byte(dns.TypeA))
|
||||
})
|
||||
|
||||
t.Run("encode padding", func(t *testing.T) {
|
||||
// The purpose of this unit test is to make sure that for a wide
|
||||
// array of values we obtain the right query size.
|
||||
getquerylen := func(domainlen int, padding bool) int {
|
||||
e := &DNSEncoderMiekg{}
|
||||
data, err := e.Encode(
|
||||
// This is not a valid name because it ends up being way
|
||||
// longer than 255 octets. However, the library is allowing
|
||||
// us to generate such name and we are not going to send
|
||||
// it on the wire. Also, we check below that the query that
|
||||
// we generate is long enough, so we should be good.
|
||||
dns.Fqdn(strings.Repeat("x.", domainlen)),
|
||||
dns.TypeA, padding,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return len(data)
|
||||
}
|
||||
for domainlen := 1; domainlen <= 4000; domainlen++ {
|
||||
vanillalen := getquerylen(domainlen, false)
|
||||
paddedlen := getquerylen(domainlen, true)
|
||||
if vanillalen < domainlen {
|
||||
t.Fatal("vanillalen is smaller than domainlen")
|
||||
}
|
||||
if (paddedlen % dnsPaddingDesiredBlockSize) != 0 {
|
||||
t.Fatal("paddedlen is not a multiple of PaddingDesiredBlockSize")
|
||||
}
|
||||
if paddedlen < vanillalen {
|
||||
t.Fatal("paddedlen is smaller than vanillalen")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func validate(t *testing.T, data []byte, qtype byte) {
|
||||
// dnsValidateEncodedQueryBytes validates the query serialized in data
|
||||
// for the given query type qtype (e.g., dns.TypeAAAA).
|
||||
func dnsValidateEncodedQueryBytes(t *testing.T, data []byte, qtype byte) {
|
||||
// skipping over the query ID
|
||||
if data[2] != 1 {
|
||||
t.Fatal("FLAGS should only have RD set")
|
||||
|
@ -62,37 +101,3 @@ func validate(t *testing.T, data []byte, qtype byte) {
|
|||
t.Fatal("The query is not IN")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoderPadding(t *testing.T) {
|
||||
// The purpose of this unit test is to make sure that for a wide
|
||||
// array of values we obtain the right query size.
|
||||
getquerylen := func(domainlen int, padding bool) int {
|
||||
e := &DNSEncoderMiekg{}
|
||||
data, err := e.Encode(
|
||||
// This is not a valid name because it ends up being way
|
||||
// longer than 255 octets. However, the library is allowing
|
||||
// us to generate such name and we are not going to send
|
||||
// it on the wire. Also, we check below that the query that
|
||||
// we generate is long enough, so we should be good.
|
||||
dns.Fqdn(strings.Repeat("x.", domainlen)),
|
||||
dns.TypeA, padding,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return len(data)
|
||||
}
|
||||
for domainlen := 1; domainlen <= 4000; domainlen++ {
|
||||
vanillalen := getquerylen(domainlen, false)
|
||||
paddedlen := getquerylen(domainlen, true)
|
||||
if vanillalen < domainlen {
|
||||
t.Fatal("vanillalen is smaller than domainlen")
|
||||
}
|
||||
if (paddedlen % dnsPaddingDesiredBlockSize) != 0 {
|
||||
t.Fatal("paddedlen is not a multiple of PaddingDesiredBlockSize")
|
||||
}
|
||||
if paddedlen < vanillalen {
|
||||
t.Fatal("paddedlen is smaller than vanillalen")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,9 @@ import (
|
|||
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
|
||||
)
|
||||
|
||||
func TestDNSOverHTTPSNewRequestFailure(t *testing.T) {
|
||||
func TestDNSOverHTTPS(t *testing.T) {
|
||||
t.Run("RoundTrip", func(t *testing.T) {
|
||||
t.Run("NewRequestFailure", func(t *testing.T) {
|
||||
const invalidURL = "\t"
|
||||
txp := NewDNSOverHTTPS(http.DefaultClient, invalidURL)
|
||||
data, err := txp.RoundTrip(context.Background(), nil)
|
||||
|
@ -23,9 +25,9 @@ func TestDNSOverHTTPSNewRequestFailure(t *testing.T) {
|
|||
if data != nil {
|
||||
t.Fatal("expected no response here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverHTTPSClientDoFailure(t *testing.T) {
|
||||
t.Run("client.Do failure", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
txp := &DNSOverHTTPS{
|
||||
Client: &mocks.HTTPClient{
|
||||
|
@ -42,9 +44,9 @@ func TestDNSOverHTTPSClientDoFailure(t *testing.T) {
|
|||
if data != nil {
|
||||
t.Fatal("expected no response here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverHTTPSHTTPFailure(t *testing.T) {
|
||||
t.Run("server returns 500", func(t *testing.T) {
|
||||
txp := &DNSOverHTTPS{
|
||||
Client: &mocks.HTTPClient{
|
||||
MockDo: func(*http.Request) (*http.Response, error) {
|
||||
|
@ -63,9 +65,9 @@ func TestDNSOverHTTPSHTTPFailure(t *testing.T) {
|
|||
if data != nil {
|
||||
t.Fatal("expected no response here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverHTTPSMissingContentType(t *testing.T) {
|
||||
t.Run("missing content type", func(t *testing.T) {
|
||||
txp := &DNSOverHTTPS{
|
||||
Client: &mocks.HTTPClient{
|
||||
MockDo: func(*http.Request) (*http.Response, error) {
|
||||
|
@ -84,9 +86,9 @@ func TestDNSOverHTTPSMissingContentType(t *testing.T) {
|
|||
if data != nil {
|
||||
t.Fatal("expected no response here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverHTTPSSuccess(t *testing.T) {
|
||||
t.Run("success", func(t *testing.T) {
|
||||
body := []byte("AAA")
|
||||
txp := &DNSOverHTTPS{
|
||||
Client: &mocks.HTTPClient{
|
||||
|
@ -109,23 +111,9 @@ func TestDNSOverHTTPSSuccess(t *testing.T) {
|
|||
if !bytes.Equal(data, body) {
|
||||
t.Fatal("not the response we expected")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverHTTPTransportOK(t *testing.T) {
|
||||
const queryURL = "https://cloudflare-dns.com/dns-query"
|
||||
txp := NewDNSOverHTTPS(http.DefaultClient, queryURL)
|
||||
if txp.Network() != "doh" {
|
||||
t.Fatal("invalid network")
|
||||
}
|
||||
if txp.RequiresPadding() != true {
|
||||
t.Fatal("should require padding")
|
||||
}
|
||||
if txp.Address() != queryURL {
|
||||
t.Fatal("invalid address")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNSOverHTTPSClientSetsUserAgent(t *testing.T) {
|
||||
t.Run("sets the correct user-agent", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
var correct bool
|
||||
txp := &DNSOverHTTPS{
|
||||
|
@ -147,12 +135,11 @@ func TestDNSOverHTTPSClientSetsUserAgent(t *testing.T) {
|
|||
if !correct {
|
||||
t.Fatal("did not see correct user agent")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverHTTPSHostOverride(t *testing.T) {
|
||||
t.Run("we can override the Host header", func(t *testing.T) {
|
||||
var correct bool
|
||||
expected := errors.New("mocked error")
|
||||
|
||||
hostOverride := "test.com"
|
||||
txp := &DNSOverHTTPS{
|
||||
Client: &mocks.HTTPClient{
|
||||
|
@ -174,9 +161,25 @@ func TestDNSOverHTTPSHostOverride(t *testing.T) {
|
|||
if !correct {
|
||||
t.Fatal("did not see correct host override")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverHTTPSCloseIdleConnections(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("other functions behave correctly", func(t *testing.T) {
|
||||
const queryURL = "https://cloudflare-dns.com/dns-query"
|
||||
txp := NewDNSOverHTTPS(http.DefaultClient, queryURL)
|
||||
if txp.Network() != "doh" {
|
||||
t.Fatal("invalid network")
|
||||
}
|
||||
if txp.RequiresPadding() != true {
|
||||
t.Fatal("should require padding")
|
||||
}
|
||||
if txp.Address() != queryURL {
|
||||
t.Fatal("invalid address")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("CloseIdleConnections", func(t *testing.T) {
|
||||
var called bool
|
||||
doh := &DNSOverHTTPS{
|
||||
Client: &mocks.HTTPClient{
|
||||
|
@ -189,4 +192,5 @@ func TestDNSOverHTTPSCloseIdleConnections(t *testing.T) {
|
|||
if !called {
|
||||
t.Fatal("not called")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -13,7 +13,9 @@ import (
|
|||
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
|
||||
)
|
||||
|
||||
func TestDNSOverTCPTransportQueryTooLarge(t *testing.T) {
|
||||
func TestDNSOverTCP(t *testing.T) {
|
||||
t.Run("RoundTrip", func(t *testing.T) {
|
||||
t.Run("query too large", func(t *testing.T) {
|
||||
const address = "9.9.9.9:53"
|
||||
txp := NewDNSOverTCP(new(net.Dialer).DialContext, address)
|
||||
reply, err := txp.RoundTrip(context.Background(), make([]byte, 1<<18))
|
||||
|
@ -23,9 +25,9 @@ func TestDNSOverTCPTransportQueryTooLarge(t *testing.T) {
|
|||
if reply != nil {
|
||||
t.Fatal("expected nil reply here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverTCPTransportDialFailure(t *testing.T) {
|
||||
t.Run("dial failure", func(t *testing.T) {
|
||||
const address = "9.9.9.9:53"
|
||||
mocked := errors.New("mocked error")
|
||||
fakedialer := &mocks.Dialer{
|
||||
|
@ -41,9 +43,9 @@ func TestDNSOverTCPTransportDialFailure(t *testing.T) {
|
|||
if reply != nil {
|
||||
t.Fatal("expected nil reply here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverTCPTransportSetDealineFailure(t *testing.T) {
|
||||
t.Run("SetDeadline failure", func(t *testing.T) {
|
||||
const address = "9.9.9.9:53"
|
||||
mocked := errors.New("mocked error")
|
||||
fakedialer := &mocks.Dialer{
|
||||
|
@ -66,9 +68,9 @@ func TestDNSOverTCPTransportSetDealineFailure(t *testing.T) {
|
|||
if reply != nil {
|
||||
t.Fatal("expected nil reply here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverTCPTransportWriteFailure(t *testing.T) {
|
||||
t.Run("write failure", func(t *testing.T) {
|
||||
const address = "9.9.9.9:53"
|
||||
mocked := errors.New("mocked error")
|
||||
fakedialer := &mocks.Dialer{
|
||||
|
@ -94,9 +96,9 @@ func TestDNSOverTCPTransportWriteFailure(t *testing.T) {
|
|||
if reply != nil {
|
||||
t.Fatal("expected nil reply here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverTCPTransportReadFailure(t *testing.T) {
|
||||
t.Run("first read fails", func(t *testing.T) {
|
||||
const address = "9.9.9.9:53"
|
||||
mocked := errors.New("mocked error")
|
||||
fakedialer := &mocks.Dialer{
|
||||
|
@ -125,9 +127,9 @@ func TestDNSOverTCPTransportReadFailure(t *testing.T) {
|
|||
if reply != nil {
|
||||
t.Fatal("expected nil reply here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverTCPTransportSecondReadFailure(t *testing.T) {
|
||||
t.Run("second read fails", func(t *testing.T) {
|
||||
const address = "9.9.9.9:53"
|
||||
mocked := errors.New("mocked error")
|
||||
input := io.MultiReader(
|
||||
|
@ -162,9 +164,9 @@ func TestDNSOverTCPTransportSecondReadFailure(t *testing.T) {
|
|||
if reply != nil {
|
||||
t.Fatal("expected nil reply here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverTCPTransportAllGood(t *testing.T) {
|
||||
t.Run("successful case", func(t *testing.T) {
|
||||
const address = "9.9.9.9:53"
|
||||
input := bytes.NewReader([]byte{byte(0), byte(1), byte(1)})
|
||||
fakedialer := &mocks.Dialer{
|
||||
|
@ -191,9 +193,10 @@ func TestDNSOverTCPTransportAllGood(t *testing.T) {
|
|||
if len(reply) != 1 || reply[0] != 1 {
|
||||
t.Fatal("not the response we expected")
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
func TestDNSOverTCPTransportOK(t *testing.T) {
|
||||
t.Run("other functions okay with TCP", func(t *testing.T) {
|
||||
const address = "9.9.9.9:53"
|
||||
txp := NewDNSOverTCP(new(net.Dialer).DialContext, address)
|
||||
if txp.RequiresPadding() != false {
|
||||
|
@ -205,9 +208,9 @@ func TestDNSOverTCPTransportOK(t *testing.T) {
|
|||
if txp.Address() != address {
|
||||
t.Fatal("invalid Address")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverTLSTransportOK(t *testing.T) {
|
||||
t.Run("other functions okay with TLS", func(t *testing.T) {
|
||||
const address = "9.9.9.9:853"
|
||||
txp := NewDNSOverTLS((&tls.Dialer{}).DialContext, address)
|
||||
if txp.RequiresPadding() != true {
|
||||
|
@ -219,4 +222,5 @@ func TestDNSOverTLSTransportOK(t *testing.T) {
|
|||
if txp.Address() != address {
|
||||
t.Fatal("invalid Address")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -11,7 +11,9 @@ import (
|
|||
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
|
||||
)
|
||||
|
||||
func TestDNSOverUDPDialFailure(t *testing.T) {
|
||||
func TestDNSOverUDP(t *testing.T) {
|
||||
t.Run("RoundTrip", func(t *testing.T) {
|
||||
t.Run("dial failure", func(t *testing.T) {
|
||||
mocked := errors.New("mocked error")
|
||||
const address = "9.9.9.9:53"
|
||||
txp := NewDNSOverUDP(&mocks.Dialer{
|
||||
|
@ -26,9 +28,9 @@ func TestDNSOverUDPDialFailure(t *testing.T) {
|
|||
if data != nil {
|
||||
t.Fatal("expected no response here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverUDPSetDeadlineError(t *testing.T) {
|
||||
t.Run("SetDeadline failure", func(t *testing.T) {
|
||||
mocked := errors.New("mocked error")
|
||||
txp := NewDNSOverUDP(
|
||||
&mocks.Dialer{
|
||||
|
@ -51,9 +53,9 @@ func TestDNSOverUDPSetDeadlineError(t *testing.T) {
|
|||
if data != nil {
|
||||
t.Fatal("expected no response here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverUDPWriteFailure(t *testing.T) {
|
||||
t.Run("Write failure", func(t *testing.T) {
|
||||
mocked := errors.New("mocked error")
|
||||
txp := NewDNSOverUDP(
|
||||
&mocks.Dialer{
|
||||
|
@ -79,9 +81,9 @@ func TestDNSOverUDPWriteFailure(t *testing.T) {
|
|||
if data != nil {
|
||||
t.Fatal("expected no response here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverUDPReadFailure(t *testing.T) {
|
||||
t.Run("Read failure", func(t *testing.T) {
|
||||
mocked := errors.New("mocked error")
|
||||
txp := NewDNSOverUDP(
|
||||
&mocks.Dialer{
|
||||
|
@ -110,9 +112,9 @@ func TestDNSOverUDPReadFailure(t *testing.T) {
|
|||
if data != nil {
|
||||
t.Fatal("expected no response here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestDNSOverUDPReadSuccess(t *testing.T) {
|
||||
t.Run("read success", func(t *testing.T) {
|
||||
const expected = 17
|
||||
input := bytes.NewReader(make([]byte, expected))
|
||||
txp := NewDNSOverUDP(
|
||||
|
@ -140,9 +142,10 @@ func TestDNSOverUDPReadSuccess(t *testing.T) {
|
|||
if len(data) != expected {
|
||||
t.Fatal("expected non nil data")
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
func TestDNSOverUDPTransportOK(t *testing.T) {
|
||||
t.Run("other functions okay", func(t *testing.T) {
|
||||
const address = "9.9.9.9:53"
|
||||
txp := NewDNSOverUDP(&net.Dialer{}, address)
|
||||
if txp.RequiresPadding() != false {
|
||||
|
@ -154,4 +157,5 @@ func TestDNSOverUDPTransportOK(t *testing.T) {
|
|||
if txp.Address() != address {
|
||||
t.Fatal("invalid Address")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -61,7 +61,7 @@ func (r *SerialResolver) LookupHost(ctx context.Context, hostname string) ([]str
|
|||
return addrs, nil
|
||||
}
|
||||
|
||||
// LookupHTTPS issues an HTTPS query without retrying on failure.
|
||||
// LookupHTTPS implements Resolver.LookupHTTPS.
|
||||
func (r *SerialResolver) LookupHTTPS(
|
||||
ctx context.Context, hostname string) (*HTTPSSvc, error) {
|
||||
querydata, err := r.Encoder.Encode(
|
||||
|
|
|
@ -13,7 +13,8 @@ import (
|
|||
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
||||
)
|
||||
|
||||
func TestOONIGettingTransport(t *testing.T) {
|
||||
func TestSerialResolver(t *testing.T) {
|
||||
t.Run("transport okay", func(t *testing.T) {
|
||||
txp := NewDNSOverTLS((&tls.Dialer{}).DialContext, "8.8.8.8:853")
|
||||
r := NewSerialResolver(txp)
|
||||
rtx := r.Transport()
|
||||
|
@ -26,9 +27,10 @@ func TestOONIGettingTransport(t *testing.T) {
|
|||
if r.Address() != rtx.Address() {
|
||||
t.Fatal("invalid address seen from the resolver")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestOONIEncodeError(t *testing.T) {
|
||||
t.Run("LookupHost", func(t *testing.T) {
|
||||
t.Run("Encode error", func(t *testing.T) {
|
||||
mocked := errors.New("mocked error")
|
||||
txp := NewDNSOverTLS((&tls.Dialer{}).DialContext, "8.8.8.8:853")
|
||||
r := SerialResolver{
|
||||
|
@ -46,9 +48,9 @@ func TestOONIEncodeError(t *testing.T) {
|
|||
if addrs != nil {
|
||||
t.Fatal("expected nil address here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestOONIRoundTripError(t *testing.T) {
|
||||
t.Run("RoundTrip error", func(t *testing.T) {
|
||||
mocked := errors.New("mocked error")
|
||||
txp := &mocks.DNSTransport{
|
||||
MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) {
|
||||
|
@ -66,12 +68,12 @@ func TestOONIRoundTripError(t *testing.T) {
|
|||
if addrs != nil {
|
||||
t.Fatal("expected nil address here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestOONIWithEmptyReply(t *testing.T) {
|
||||
t.Run("empty reply", func(t *testing.T) {
|
||||
txp := &mocks.DNSTransport{
|
||||
MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) {
|
||||
return genReplySuccess(t, dns.TypeA), nil
|
||||
return dnsGenLookupHostReplySuccess(t, dns.TypeA), nil
|
||||
},
|
||||
MockRequiresPadding: func() bool {
|
||||
return true
|
||||
|
@ -85,12 +87,12 @@ func TestOONIWithEmptyReply(t *testing.T) {
|
|||
if addrs != nil {
|
||||
t.Fatal("expected nil address here")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestOONIWithAReply(t *testing.T) {
|
||||
t.Run("with A reply", func(t *testing.T) {
|
||||
txp := &mocks.DNSTransport{
|
||||
MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) {
|
||||
return genReplySuccess(t, dns.TypeA, "8.8.8.8"), nil
|
||||
return dnsGenLookupHostReplySuccess(t, dns.TypeA, "8.8.8.8"), nil
|
||||
},
|
||||
MockRequiresPadding: func() bool {
|
||||
return true
|
||||
|
@ -104,12 +106,12 @@ func TestOONIWithAReply(t *testing.T) {
|
|||
if len(addrs) != 1 || addrs[0] != "8.8.8.8" {
|
||||
t.Fatal("not the result we expected")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestOONIWithAAAAReply(t *testing.T) {
|
||||
t.Run("with AAAA reply", func(t *testing.T) {
|
||||
txp := &mocks.DNSTransport{
|
||||
MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) {
|
||||
return genReplySuccess(t, dns.TypeAAAA, "::1"), nil
|
||||
return dnsGenLookupHostReplySuccess(t, dns.TypeAAAA, "::1"), nil
|
||||
},
|
||||
MockRequiresPadding: func() bool {
|
||||
return true
|
||||
|
@ -123,9 +125,9 @@ func TestOONIWithAAAAReply(t *testing.T) {
|
|||
if len(addrs) != 1 || addrs[0] != "::1" {
|
||||
t.Fatal("not the result we expected")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestOONIWithTimeout(t *testing.T) {
|
||||
t.Run("with timeout", func(t *testing.T) {
|
||||
txp := &mocks.DNSTransport{
|
||||
MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) {
|
||||
return nil, &net.OpError{Err: errorsx.ETIMEDOUT, Op: "dial"}
|
||||
|
@ -145,9 +147,10 @@ func TestOONIWithTimeout(t *testing.T) {
|
|||
if r.NumTimeouts.Load() <= 0 {
|
||||
t.Fatal("we didn't actually take the timeouts")
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
func TestSerialResolverCloseIdleConnections(t *testing.T) {
|
||||
t.Run("CloseIdleConnections", func(t *testing.T) {
|
||||
var called bool
|
||||
r := &SerialResolver{
|
||||
Txp: &mocks.DNSTransport{
|
||||
|
@ -160,9 +163,9 @@ func TestSerialResolverCloseIdleConnections(t *testing.T) {
|
|||
if !called {
|
||||
t.Fatal("not called")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
func TestSerialResolverLookupHTTPS(t *testing.T) {
|
||||
t.Run("LookupHTTPS", func(t *testing.T) {
|
||||
t.Run("for encoding error", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
r := &SerialResolver{
|
||||
|
@ -250,4 +253,5 @@ func TestSerialResolverLookupHTTPS(t *testing.T) {
|
|||
t.Fatal("unexpected result")
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user