2021-09-28 12:42:01 +02:00
|
|
|
package netxlite
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
2022-05-25 17:03:58 +02:00
|
|
|
"bytes"
|
2021-09-27 16:48:46 +02:00
|
|
|
"errors"
|
2021-09-09 21:24:27 +02:00
|
|
|
"net"
|
2021-02-02 12:05:47 +01:00
|
|
|
"testing"
|
|
|
|
|
2021-09-27 23:09:41 +02:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-02-02 12:05:47 +01:00
|
|
|
"github.com/miekg/dns"
|
2022-05-25 17:03:58 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
2022-05-14 19:38:46 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2022-09-14 13:47:20 +02:00
|
|
|
func dnsDecoderErrorIsWrapped(err error) bool {
|
|
|
|
var errwrapper *ErrWrapper
|
|
|
|
return errors.As(err, &errwrapper)
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
func TestDNSDecoderMiekg(t *testing.T) {
|
|
|
|
t.Run("DecodeResponse", func(t *testing.T) {
|
2021-09-28 11:26:16 +02:00
|
|
|
t.Run("UnpackError", func(t *testing.T) {
|
|
|
|
d := &DNSDecoderMiekg{}
|
2022-05-25 17:03:58 +02:00
|
|
|
resp, err := d.DecodeResponse(nil, &mocks.DNSQuery{})
|
2022-09-14 13:47:20 +02:00
|
|
|
if err == nil || err.Error() != "unknown_failure: dns: overflow unpacking uint16" {
|
2022-05-14 19:38:46 +02:00
|
|
|
t.Fatal("unexpected error", err)
|
|
|
|
}
|
2022-09-14 13:47:20 +02:00
|
|
|
if !dnsDecoderErrorIsWrapped(err) {
|
|
|
|
t.Fatal("unwrapped error", err)
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("expected nil resp here")
|
2022-05-14 19:38:46 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-05-16 11:17:30 +02:00
|
|
|
t.Run("with bytes containing a query", func(t *testing.T) {
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeA, queryID)
|
2022-05-25 17:03:58 +02:00
|
|
|
resp, err := d.DecodeResponse(rawQuery, &mocks.DNSQuery{})
|
2022-05-16 11:17:30 +02:00
|
|
|
if !errors.Is(err, ErrDNSIsQuery) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
2022-09-14 13:47:20 +02:00
|
|
|
if !dnsDecoderErrorIsWrapped(err) {
|
|
|
|
t.Fatal("unwrapped error", err)
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("expected nil resp here")
|
2022-05-16 11:17:30 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-05-14 19:38:46 +02:00
|
|
|
t.Run("wrong query ID", func(t *testing.T) {
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
const (
|
|
|
|
queryID = 17
|
|
|
|
unrelatedID = 14
|
|
|
|
)
|
2022-08-23 13:04:00 +02:00
|
|
|
reply := dnsGenLookupHostReplySuccess(dnsGenQuery(dns.TypeA, queryID), nil)
|
2022-05-25 17:03:58 +02:00
|
|
|
resp, err := d.DecodeResponse(reply, &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return unrelatedID
|
|
|
|
},
|
|
|
|
})
|
2022-05-14 19:38:46 +02:00
|
|
|
if !errors.Is(err, ErrDNSReplyWithWrongQueryID) {
|
|
|
|
t.Fatal("unexpected error", err)
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
2022-09-14 13:47:20 +02:00
|
|
|
if !dnsDecoderErrorIsWrapped(err) {
|
|
|
|
t.Fatal("unwrapped error", err)
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("expected nil resp here")
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("dnsResponse.Query", func(t *testing.T) {
|
2021-09-28 11:26:16 +02:00
|
|
|
d := &DNSDecoderMiekg{}
|
2022-05-14 19:38:46 +02:00
|
|
|
queryID := dns.Id()
|
2022-05-25 17:03:58 +02:00
|
|
|
rawQuery := dnsGenQuery(dns.TypeA, queryID)
|
2022-08-23 13:04:00 +02:00
|
|
|
rawResponse := dnsGenLookupHostReplySuccess(rawQuery, nil)
|
2022-05-25 17:03:58 +02:00
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2022-05-13 19:25:22 +02:00
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if resp.Query().ID() != query.ID() {
|
|
|
|
t.Fatal("invalid query")
|
2022-05-13 19:25:22 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("dnsResponse.Bytes", func(t *testing.T) {
|
2021-09-28 11:26:16 +02:00
|
|
|
d := &DNSDecoderMiekg{}
|
2022-05-14 19:38:46 +02:00
|
|
|
queryID := dns.Id()
|
2022-05-25 17:03:58 +02:00
|
|
|
rawQuery := dnsGenQuery(dns.TypeA, queryID)
|
2022-08-23 13:04:00 +02:00
|
|
|
rawResponse := dnsGenLookupHostReplySuccess(rawQuery, nil)
|
2022-05-25 17:03:58 +02:00
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
2021-09-28 11:26:16 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if !bytes.Equal(rawResponse, resp.Bytes()) {
|
|
|
|
t.Fatal("invalid bytes")
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("dnsResponse.Rcode", func(t *testing.T) {
|
2021-09-28 11:26:16 +02:00
|
|
|
d := &DNSDecoderMiekg{}
|
2022-05-14 19:38:46 +02:00
|
|
|
queryID := dns.Id()
|
2022-05-25 17:03:58 +02:00
|
|
|
rawQuery := dnsGenQuery(dns.TypeA, queryID)
|
|
|
|
rawResponse := dnsGenReplyWithError(rawQuery, dns.RcodeRefused)
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
2021-09-28 11:26:16 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if resp.Rcode() != dns.RcodeRefused {
|
|
|
|
t.Fatal("invalid rcode")
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("dnsResponse.rcodeToError", func(t *testing.T) {
|
|
|
|
// Here we want to ensure we map all the errors we recognize
|
|
|
|
// correctly and we also map unrecognized errors correctly
|
|
|
|
var inputsOutputs = []struct {
|
|
|
|
name string
|
|
|
|
rcode int
|
|
|
|
err error
|
|
|
|
}{{
|
|
|
|
name: "when rcode is zero",
|
|
|
|
rcode: 0,
|
|
|
|
err: nil,
|
|
|
|
}, {
|
|
|
|
name: "NXDOMAIN",
|
|
|
|
rcode: dns.RcodeNameError,
|
|
|
|
err: ErrOODNSNoSuchHost,
|
|
|
|
}, {
|
|
|
|
name: "refused",
|
|
|
|
rcode: dns.RcodeRefused,
|
|
|
|
err: ErrOODNSRefused,
|
|
|
|
}, {
|
|
|
|
name: "servfail",
|
|
|
|
rcode: dns.RcodeServerFailure,
|
|
|
|
err: ErrOODNSServfail,
|
|
|
|
}, {
|
|
|
|
name: "anything else",
|
|
|
|
rcode: dns.RcodeFormatError,
|
|
|
|
err: ErrOODNSMisbehaving,
|
|
|
|
}}
|
|
|
|
for _, io := range inputsOutputs {
|
|
|
|
t.Run(io.name, func(t *testing.T) {
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeHTTPS, queryID)
|
|
|
|
rawResponse := dnsGenReplyWithError(rawQuery, io.rcode)
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// The following cast should always work in this configuration
|
|
|
|
err = resp.(*dnsResponse).rcodeToError()
|
|
|
|
if !errors.Is(err, io.err) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
2022-09-14 13:47:20 +02:00
|
|
|
if err != nil && !dnsDecoderErrorIsWrapped(err) {
|
|
|
|
t.Fatal("unwrapped error", err)
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
})
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("dnsResponse.DecodeHTTPS", func(t *testing.T) {
|
|
|
|
t.Run("with failure", func(t *testing.T) {
|
|
|
|
// Ensure that we're not trying to decode if rcode != 0
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeHTTPS, queryID)
|
|
|
|
rawResponse := dnsGenReplyWithError(rawQuery, dns.RcodeRefused)
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
https, err := resp.DecodeHTTPS()
|
|
|
|
if !errors.Is(err, ErrOODNSRefused) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
2022-09-14 13:47:20 +02:00
|
|
|
if !dnsDecoderErrorIsWrapped(err) {
|
|
|
|
t.Fatal("unwrapped error", err)
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if https != nil {
|
|
|
|
t.Fatal("expected nil https result")
|
|
|
|
}
|
|
|
|
})
|
2021-09-28 11:26:16 +02:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("with empty answer", func(t *testing.T) {
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeHTTPS, queryID)
|
|
|
|
rawResponse := dnsGenHTTPSReplySuccess(rawQuery, nil, nil, nil)
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
https, err := resp.DecodeHTTPS()
|
|
|
|
if !errors.Is(err, ErrOODNSNoAnswer) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
2022-09-14 13:47:20 +02:00
|
|
|
if !dnsDecoderErrorIsWrapped(err) {
|
|
|
|
t.Fatal("unwrapped error", err)
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if https != nil {
|
|
|
|
t.Fatal("expected nil https results")
|
|
|
|
}
|
|
|
|
})
|
2021-09-28 11:26:16 +02:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("with full answer", func(t *testing.T) {
|
|
|
|
alpn := []string{"h3"}
|
|
|
|
v4 := []string{"1.1.1.1"}
|
|
|
|
v6 := []string{"::1"}
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeHTTPS, queryID)
|
|
|
|
rawResponse := dnsGenHTTPSReplySuccess(rawQuery, alpn, v4, v6)
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
reply, err := resp.DecodeHTTPS()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2021-09-28 11:26:16 +02:00
|
|
|
})
|
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("dnsResponse.DecodeNS", func(t *testing.T) {
|
|
|
|
t.Run("with failure", func(t *testing.T) {
|
|
|
|
// Ensure that we're not trying to decode if rcode != 0
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeNS, queryID)
|
|
|
|
rawResponse := dnsGenReplyWithError(rawQuery, dns.RcodeRefused)
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ns, err := resp.DecodeNS()
|
|
|
|
if !errors.Is(err, ErrOODNSRefused) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
2022-09-14 13:47:20 +02:00
|
|
|
if !dnsDecoderErrorIsWrapped(err) {
|
|
|
|
t.Fatal("unwrapped error", err)
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if len(ns) > 0 {
|
|
|
|
t.Fatal("expected empty ns result")
|
|
|
|
}
|
|
|
|
})
|
2022-05-16 11:17:30 +02:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("with empty answer", func(t *testing.T) {
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeNS, queryID)
|
|
|
|
rawResponse := dnsGenNSReplySuccess(rawQuery)
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ns, err := resp.DecodeNS()
|
|
|
|
if !errors.Is(err, ErrOODNSNoAnswer) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
2022-09-14 13:47:20 +02:00
|
|
|
if !dnsDecoderErrorIsWrapped(err) {
|
|
|
|
t.Fatal("unwrapped error", err)
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if len(ns) > 0 {
|
|
|
|
t.Fatal("expected empty ns results")
|
|
|
|
}
|
|
|
|
})
|
2022-05-14 19:38:46 +02:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("with full answer", func(t *testing.T) {
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeNS, queryID)
|
|
|
|
rawResponse := dnsGenNSReplySuccess(rawQuery, "ns1.zdns.google.")
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ns, err := resp.DecodeNS()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(ns) != 1 {
|
|
|
|
t.Fatal("unexpected ns length")
|
|
|
|
}
|
|
|
|
if ns[0].Host != "ns1.zdns.google." {
|
|
|
|
t.Fatal("unexpected host")
|
|
|
|
}
|
|
|
|
})
|
2021-09-28 11:26:16 +02:00
|
|
|
})
|
|
|
|
|
2022-08-23 13:04:00 +02:00
|
|
|
t.Run("dnsResponse.DecodeLookupHost", func(t *testing.T) {
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("with failure", func(t *testing.T) {
|
|
|
|
// Ensure that we're not trying to decode if rcode != 0
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeA, queryID)
|
|
|
|
rawResponse := dnsGenReplyWithError(rawQuery, dns.RcodeRefused)
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
addrs, err := resp.DecodeLookupHost()
|
|
|
|
if !errors.Is(err, ErrOODNSRefused) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
2022-09-14 13:47:20 +02:00
|
|
|
if !dnsDecoderErrorIsWrapped(err) {
|
|
|
|
t.Fatal("unwrapped error", err)
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if len(addrs) > 0 {
|
|
|
|
t.Fatal("expected empty addrs result")
|
|
|
|
}
|
|
|
|
})
|
2022-05-16 10:46:53 +02:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("with empty answer", func(t *testing.T) {
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeA, queryID)
|
2022-08-23 13:04:00 +02:00
|
|
|
rawResponse := dnsGenLookupHostReplySuccess(rawQuery, nil)
|
2022-05-25 17:03:58 +02:00
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
addrs, err := resp.DecodeLookupHost()
|
|
|
|
if !errors.Is(err, ErrOODNSNoAnswer) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
2022-09-14 13:47:20 +02:00
|
|
|
if !dnsDecoderErrorIsWrapped(err) {
|
|
|
|
t.Fatal("unwrapped error", err)
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if len(addrs) > 0 {
|
|
|
|
t.Fatal("expected empty ns results")
|
|
|
|
}
|
|
|
|
})
|
2022-05-16 10:46:53 +02:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("decode A", func(t *testing.T) {
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeA, queryID)
|
2022-08-23 13:04:00 +02:00
|
|
|
rawResponse := dnsGenLookupHostReplySuccess(rawQuery, nil, "1.1.1.1", "8.8.8.8")
|
2022-05-25 17:03:58 +02:00
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
MockType: func() uint16 {
|
|
|
|
return dns.TypeA
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
addrs, err := resp.DecodeLookupHost()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(addrs) != 2 {
|
|
|
|
t.Fatal("expected two entries here")
|
|
|
|
}
|
|
|
|
if addrs[0] != "1.1.1.1" {
|
|
|
|
t.Fatal("invalid first IPv4 entry")
|
|
|
|
}
|
|
|
|
if addrs[1] != "8.8.8.8" {
|
|
|
|
t.Fatal("invalid second IPv4 entry")
|
|
|
|
}
|
|
|
|
})
|
2022-05-16 11:17:30 +02:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("decode AAAA", func(t *testing.T) {
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeAAAA, queryID)
|
2022-08-23 13:04:00 +02:00
|
|
|
rawResponse := dnsGenLookupHostReplySuccess(rawQuery, nil, "::1", "fe80::1")
|
2022-05-25 17:03:58 +02:00
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
MockType: func() uint16 {
|
|
|
|
return dns.TypeAAAA
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
addrs, err := resp.DecodeLookupHost()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(addrs) != 2 {
|
|
|
|
t.Fatal("expected two entries here")
|
|
|
|
}
|
|
|
|
if addrs[0] != "::1" {
|
|
|
|
t.Fatal("invalid first IPv6 entry")
|
|
|
|
}
|
|
|
|
if addrs[1] != "fe80::1" {
|
|
|
|
t.Fatal("invalid second IPv6 entry")
|
|
|
|
}
|
|
|
|
})
|
2022-05-16 10:46:53 +02:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("unexpected A reply to AAAA query", func(t *testing.T) {
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeAAAA, queryID)
|
2022-08-23 13:04:00 +02:00
|
|
|
rawResponse := dnsGenLookupHostReplySuccess(rawQuery, nil, "1.1.1.1", "8.8.8.8")
|
2022-05-25 17:03:58 +02:00
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
MockType: func() uint16 {
|
|
|
|
return dns.TypeAAAA
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
addrs, err := resp.DecodeLookupHost()
|
|
|
|
if !errors.Is(err, ErrOODNSNoAnswer) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
2022-09-14 13:47:20 +02:00
|
|
|
if !dnsDecoderErrorIsWrapped(err) {
|
|
|
|
t.Fatal("unwrapped error", err)
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if len(addrs) > 0 {
|
|
|
|
t.Fatal("expected no addrs here")
|
|
|
|
}
|
|
|
|
})
|
2022-05-16 10:46:53 +02:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("unexpected AAAA reply to A query", func(t *testing.T) {
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeA, queryID)
|
2022-08-23 13:04:00 +02:00
|
|
|
rawResponse := dnsGenLookupHostReplySuccess(rawQuery, nil, "::1", "fe80::1")
|
2022-05-25 17:03:58 +02:00
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
MockType: func() uint16 {
|
|
|
|
return dns.TypeA
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
addrs, err := resp.DecodeLookupHost()
|
|
|
|
if !errors.Is(err, ErrOODNSNoAnswer) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
2022-09-14 13:47:20 +02:00
|
|
|
if !dnsDecoderErrorIsWrapped(err) {
|
|
|
|
t.Fatal("unwrapped error", err)
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if len(addrs) > 0 {
|
|
|
|
t.Fatal("expected no addrs here")
|
|
|
|
}
|
|
|
|
})
|
2022-05-16 10:46:53 +02:00
|
|
|
})
|
2022-08-23 13:04:00 +02:00
|
|
|
|
|
|
|
t.Run("dnsResponse.DecodeCNAME", func(t *testing.T) {
|
|
|
|
t.Run("with failure", func(t *testing.T) {
|
|
|
|
// Ensure that we're not trying to decode if rcode != 0
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeA, queryID)
|
|
|
|
rawResponse := dnsGenReplyWithError(rawQuery, dns.RcodeRefused)
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cname, err := resp.DecodeCNAME()
|
|
|
|
if !errors.Is(err, ErrOODNSRefused) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
|
|
|
if cname != "" {
|
|
|
|
t.Fatal("expected empty cname result")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with empty answer", func(t *testing.T) {
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeA, queryID)
|
|
|
|
var expectedCNAME *dnsCNAMEAnswer = nil // explicity not set
|
|
|
|
rawResponse := dnsGenLookupHostReplySuccess(rawQuery, expectedCNAME, "8.8.8.8")
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cname, err := resp.DecodeCNAME()
|
|
|
|
if !errors.Is(err, ErrOODNSNoAnswer) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
2022-09-14 13:47:20 +02:00
|
|
|
if !dnsDecoderErrorIsWrapped(err) {
|
|
|
|
t.Fatal("unwrapped error", err)
|
|
|
|
}
|
2022-08-23 13:04:00 +02:00
|
|
|
if cname != "" {
|
|
|
|
t.Fatal("expected empty cname result")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with full answer", func(t *testing.T) {
|
|
|
|
expectedCNAME := &dnsCNAMEAnswer{
|
|
|
|
CNAME: "dns.google.",
|
|
|
|
}
|
|
|
|
d := &DNSDecoderMiekg{}
|
|
|
|
queryID := dns.Id()
|
|
|
|
rawQuery := dnsGenQuery(dns.TypeA, queryID)
|
|
|
|
rawResponse := dnsGenLookupHostReplySuccess(rawQuery, expectedCNAME, "8.8.8.8")
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := d.DecodeResponse(rawResponse, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cname, err := resp.DecodeCNAME()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if cname != expectedCNAME.CNAME {
|
|
|
|
t.Fatal("unexpected cname", cname)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2022-05-16 10:46:53 +02:00
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2021-09-09 21:24:27 +02:00
|
|
|
|
2022-05-14 19:38:46 +02:00
|
|
|
// dnsGenQuery generates a query suitable to be used with testing.
|
|
|
|
func dnsGenQuery(qtype uint16, queryID uint16) []byte {
|
2021-09-09 21:24:27 +02:00
|
|
|
question := dns.Question{
|
|
|
|
Name: dns.Fqdn("x.org"),
|
2021-09-28 11:26:16 +02:00
|
|
|
Qtype: qtype,
|
2021-09-09 21:24:27 +02:00
|
|
|
Qclass: dns.ClassINET,
|
|
|
|
}
|
|
|
|
query := new(dns.Msg)
|
2022-05-14 19:38:46 +02:00
|
|
|
query.Id = queryID
|
2021-09-09 21:24:27 +02:00
|
|
|
query.RecursionDesired = true
|
|
|
|
query.Question = make([]dns.Question, 1)
|
|
|
|
query.Question[0] = question
|
2022-05-14 19:38:46 +02:00
|
|
|
data, err := query.Pack()
|
|
|
|
runtimex.PanicOnError(err, "query.Pack failed")
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
// dnsGenReplyWithError generates a DNS reply for the given
|
|
|
|
// query type (e.g., dns.TypeA) using code as the Rcode.
|
|
|
|
func dnsGenReplyWithError(rawQuery []byte, code int) []byte {
|
|
|
|
query := new(dns.Msg)
|
|
|
|
err := query.Unpack(rawQuery)
|
|
|
|
runtimex.PanicOnError(err, "query.Unpack failed")
|
2021-09-09 21:24:27 +02:00
|
|
|
reply := new(dns.Msg)
|
|
|
|
reply.Compress = true
|
|
|
|
reply.MsgHdr.RecursionAvailable = true
|
|
|
|
reply.SetRcode(query, code)
|
|
|
|
data, err := reply.Pack()
|
2022-05-14 19:38:46 +02:00
|
|
|
runtimex.PanicOnError(err, "reply.Pack failed")
|
2021-09-09 21:24:27 +02:00
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2022-08-23 13:04:00 +02:00
|
|
|
// ImplementationNote: dnsCNAMEAnswer could have been a string but then
|
|
|
|
// dnsGenLookupHostReplySuccess invocations would have been confusing to read,
|
|
|
|
// because they would not have had a boundary between CNAME and addrs.
|
|
|
|
|
|
|
|
// dnsCNAMEAnswer is the DNS cname answer to include into a response.
|
|
|
|
type dnsCNAMEAnswer struct {
|
|
|
|
CNAME string
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
// dnsGenLookupHostReplySuccess generates a successful DNS reply containing the given ips...
|
|
|
|
// in the answers where each answer's type depends on the IP's type (A/AAAA).
|
2022-08-23 13:04:00 +02:00
|
|
|
func dnsGenLookupHostReplySuccess(rawQuery []byte, cname *dnsCNAMEAnswer, ips ...string) []byte {
|
2021-09-09 21:24:27 +02:00
|
|
|
query := new(dns.Msg)
|
2022-05-14 19:38:46 +02:00
|
|
|
err := query.Unpack(rawQuery)
|
|
|
|
runtimex.PanicOnError(err, "query.Unpack failed")
|
2022-08-31 18:40:27 +02:00
|
|
|
runtimex.Assert(len(query.Question) == 1, "more than one question")
|
2022-05-14 19:38:46 +02:00
|
|
|
question := query.Question[0]
|
2022-08-31 18:40:27 +02:00
|
|
|
runtimex.Assert(
|
2022-05-16 10:46:53 +02:00
|
|
|
question.Qtype == dns.TypeA || question.Qtype == dns.TypeAAAA,
|
|
|
|
"invalid query type (expected A or AAAA)",
|
|
|
|
)
|
2021-09-09 21:24:27 +02:00
|
|
|
reply := new(dns.Msg)
|
|
|
|
reply.Compress = true
|
|
|
|
reply.MsgHdr.RecursionAvailable = true
|
|
|
|
reply.SetReply(query)
|
|
|
|
for _, ip := range ips {
|
2022-05-25 17:03:58 +02:00
|
|
|
switch isIPv6(ip) {
|
|
|
|
case false:
|
2021-09-09 21:24:27 +02:00
|
|
|
reply.Answer = append(reply.Answer, &dns.A{
|
|
|
|
Hdr: dns.RR_Header{
|
2022-05-25 17:03:58 +02:00
|
|
|
Name: question.Name,
|
|
|
|
Rrtype: dns.TypeA,
|
2021-09-09 21:24:27 +02:00
|
|
|
Class: dns.ClassINET,
|
|
|
|
Ttl: 0,
|
|
|
|
},
|
|
|
|
A: net.ParseIP(ip),
|
|
|
|
})
|
2022-05-25 17:03:58 +02:00
|
|
|
case true:
|
2021-09-09 21:24:27 +02:00
|
|
|
reply.Answer = append(reply.Answer, &dns.AAAA{
|
|
|
|
Hdr: dns.RR_Header{
|
2022-05-25 17:03:58 +02:00
|
|
|
Name: question.Name,
|
|
|
|
Rrtype: dns.TypeAAAA,
|
2021-09-09 21:24:27 +02:00
|
|
|
Class: dns.ClassINET,
|
|
|
|
Ttl: 0,
|
|
|
|
},
|
|
|
|
AAAA: net.ParseIP(ip),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-08-23 13:04:00 +02:00
|
|
|
if cname != nil {
|
|
|
|
reply.Answer = append(reply.Answer, &dns.CNAME{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: question.Name,
|
|
|
|
Rrtype: dns.TypeCNAME,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
Ttl: 0,
|
|
|
|
},
|
|
|
|
Target: cname.CNAME,
|
|
|
|
})
|
|
|
|
}
|
2021-09-09 21:24:27 +02:00
|
|
|
data, err := reply.Pack()
|
2022-05-14 19:38:46 +02:00
|
|
|
runtimex.PanicOnError(err, "reply.Pack failed")
|
2021-09-09 21:24:27 +02:00
|
|
|
return data
|
|
|
|
}
|
2021-09-27 16:48:46 +02:00
|
|
|
|
2021-09-28 11:26:16 +02:00
|
|
|
// dnsGenHTTPSReplySuccess generates a successful HTTPS response containing
|
|
|
|
// the given (possibly nil) alpns, ipv4s, and ipv6s.
|
2022-05-14 19:38:46 +02:00
|
|
|
func dnsGenHTTPSReplySuccess(rawQuery []byte, alpns, ipv4s, ipv6s []string) []byte {
|
2021-09-27 23:09:41 +02:00
|
|
|
query := new(dns.Msg)
|
2022-05-14 19:38:46 +02:00
|
|
|
err := query.Unpack(rawQuery)
|
|
|
|
runtimex.PanicOnError(err, "query.Unpack failed")
|
2022-08-31 18:40:27 +02:00
|
|
|
runtimex.Assert(len(query.Question) == 1, "expected just a single question")
|
2022-05-16 10:46:53 +02:00
|
|
|
question := query.Question[0]
|
2022-08-31 18:40:27 +02:00
|
|
|
runtimex.Assert(question.Qtype == dns.TypeHTTPS, "expected HTTPS query")
|
2021-09-27 23:09:41 +02:00
|
|
|
reply := new(dns.Msg)
|
|
|
|
reply.Compress = true
|
|
|
|
reply.MsgHdr.RecursionAvailable = true
|
|
|
|
reply.SetReply(query)
|
|
|
|
answer := &dns.HTTPS{
|
|
|
|
SVCB: dns.SVCB{
|
|
|
|
Hdr: dns.RR_Header{
|
2021-09-28 11:26:16 +02:00
|
|
|
Name: dns.Fqdn("x.org"),
|
|
|
|
Rrtype: dns.TypeHTTPS,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
Ttl: 100,
|
2021-09-27 23:09:41 +02:00
|
|
|
},
|
2021-09-28 11:26:16 +02:00
|
|
|
Target: dns.Fqdn("x.org"),
|
|
|
|
Value: []dns.SVCBKeyValue{},
|
2021-09-27 23:09:41 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
reply.Answer = append(reply.Answer, answer)
|
|
|
|
if len(alpns) > 0 {
|
2021-09-28 11:26:16 +02:00
|
|
|
answer.Value = append(answer.Value, &dns.SVCBAlpn{Alpn: alpns})
|
2021-09-27 23:09:41 +02:00
|
|
|
}
|
2021-09-28 11:26:16 +02:00
|
|
|
if len(ipv4s) > 0 {
|
2021-09-27 23:09:41 +02:00
|
|
|
var addrs []net.IP
|
2021-09-28 11:26:16 +02:00
|
|
|
for _, addr := range ipv4s {
|
2021-09-27 23:09:41 +02:00
|
|
|
addrs = append(addrs, net.ParseIP(addr))
|
|
|
|
}
|
2021-09-28 11:26:16 +02:00
|
|
|
answer.Value = append(answer.Value, &dns.SVCBIPv4Hint{Hint: addrs})
|
2021-09-27 23:09:41 +02:00
|
|
|
}
|
2021-09-28 11:26:16 +02:00
|
|
|
if len(ipv6s) > 0 {
|
2021-09-27 23:09:41 +02:00
|
|
|
var addrs []net.IP
|
2021-09-28 11:26:16 +02:00
|
|
|
for _, addr := range ipv6s {
|
2021-09-27 23:09:41 +02:00
|
|
|
addrs = append(addrs, net.ParseIP(addr))
|
|
|
|
}
|
2021-09-28 11:26:16 +02:00
|
|
|
answer.Value = append(answer.Value, &dns.SVCBIPv6Hint{Hint: addrs})
|
2021-09-27 23:09:41 +02:00
|
|
|
}
|
|
|
|
data, err := reply.Pack()
|
2022-05-14 19:38:46 +02:00
|
|
|
runtimex.PanicOnError(err, "reply.Pack failed")
|
2021-09-27 23:09:41 +02:00
|
|
|
return data
|
|
|
|
}
|
2022-05-16 10:46:53 +02:00
|
|
|
|
|
|
|
// dnsGenNSReplySuccess generates a successful NS reply using the given names.
|
|
|
|
func dnsGenNSReplySuccess(rawQuery []byte, names ...string) []byte {
|
|
|
|
query := new(dns.Msg)
|
|
|
|
err := query.Unpack(rawQuery)
|
|
|
|
runtimex.PanicOnError(err, "query.Unpack failed")
|
2022-08-31 18:40:27 +02:00
|
|
|
runtimex.Assert(len(query.Question) == 1, "more than one question")
|
2022-05-16 10:46:53 +02:00
|
|
|
question := query.Question[0]
|
2022-08-31 18:40:27 +02:00
|
|
|
runtimex.Assert(question.Qtype == dns.TypeNS, "expected NS query")
|
2022-05-16 10:46:53 +02:00
|
|
|
reply := new(dns.Msg)
|
|
|
|
reply.Compress = true
|
|
|
|
reply.MsgHdr.RecursionAvailable = true
|
|
|
|
reply.SetReply(query)
|
|
|
|
for _, name := range names {
|
|
|
|
reply.Answer = append(reply.Answer, &dns.NS{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: dns.Fqdn("x.org"),
|
|
|
|
Rrtype: question.Qtype,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
Ttl: 0,
|
|
|
|
},
|
|
|
|
Ns: name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
data, err := reply.Pack()
|
|
|
|
runtimex.PanicOnError(err, "reply.Pack failed")
|
|
|
|
return data
|
|
|
|
}
|