From 12cf4b9990bb77ddcf2448a51171e12f67d6a487 Mon Sep 17 00:00:00 2001 From: Simone Basso Date: Tue, 28 Sep 2021 10:47:59 +0200 Subject: [PATCH] refactor(dnsx): prepare for merging with netxlite (#515) Part of https://github.com/ooni/probe/issues/1591 --- internal/engine/netx/resolver/legacy.go | 6 ++-- .../dnsx/{decoder.go => dnsdecoder.go} | 16 +++++------ .../{decoder_test.go => dnsdecoder_test.go} | 24 ++++++++-------- .../dnsx/{encoder.go => dnsencoder.go} | 28 +++++++++---------- .../{encoder_test.go => dnsencoder_test.go} | 8 +++--- internal/netxlite/dnsx/dnsoverhttps.go | 2 +- internal/netxlite/dnsx/dnsovertcp.go | 2 +- internal/netxlite/dnsx/dnsoverudp.go | 2 +- .../dnsx/{roundtripper.go => dnstransport.go} | 4 +-- .../dnsx/mocks/{decoder.go => dnsdecoder.go} | 8 +++--- .../{decoder_test.go => dnsdecoder_test.go} | 6 ++-- .../dnsx/mocks/{encoder.go => dnsencoder.go} | 6 ++-- .../{encoder_test.go => dnsencoder_test.go} | 4 +-- .../{roundtripper.go => dnstransport.go} | 14 +++++----- ...ndtripper_test.go => dnstransport_test.go} | 12 ++++---- .../dnsx/{serial.go => serialresolver.go} | 14 +++++----- ...{serial_test.go => serialresolver_test.go} | 28 +++++++++---------- 17 files changed, 92 insertions(+), 92 deletions(-) rename internal/netxlite/dnsx/{decoder.go => dnsdecoder.go} (82%) rename internal/netxlite/dnsx/{decoder_test.go => dnsdecoder_test.go} (95%) rename internal/netxlite/dnsx/{encoder.go => dnsencoder.go} (54%) rename internal/netxlite/dnsx/{encoder_test.go => dnsencoder_test.go} (95%) rename internal/netxlite/dnsx/{roundtripper.go => dnstransport.go} (85%) rename internal/netxlite/dnsx/mocks/{decoder.go => dnsdecoder.go} (67%) rename internal/netxlite/dnsx/mocks/{decoder_test.go => dnsdecoder_test.go} (91%) rename internal/netxlite/dnsx/mocks/{encoder.go => dnsencoder.go} (51%) rename internal/netxlite/dnsx/mocks/{encoder_test.go => dnsencoder_test.go} (88%) rename internal/netxlite/dnsx/mocks/{roundtripper.go => dnstransport.go} (66%) rename internal/netxlite/dnsx/mocks/{roundtripper_test.go => dnstransport_test.go} (89%) rename internal/netxlite/dnsx/{serial.go => serialresolver.go} (92%) rename internal/netxlite/dnsx/{serial_test.go => serialresolver_test.go} (93%) diff --git a/internal/engine/netx/resolver/legacy.go b/internal/engine/netx/resolver/legacy.go index 654e260..9e58a2a 100644 --- a/internal/engine/netx/resolver/legacy.go +++ b/internal/engine/netx/resolver/legacy.go @@ -19,9 +19,9 @@ type ( DNSOverHTTPS = dnsx.DNSOverHTTPS DNSOverTCP = dnsx.DNSOverTCP DNSOverUDP = dnsx.DNSOverUDP - MiekgEncoder = dnsx.MiekgEncoder - MiekgDecoder = dnsx.MiekgDecoder - RoundTripper = dnsx.RoundTripper + MiekgEncoder = dnsx.DNSEncoderMiekg + MiekgDecoder = dnsx.DNSDecoderMiekg + RoundTripper = dnsx.DNSTransport SerialResolver = dnsx.SerialResolver Dialer = dnsx.Dialer DialContextFunc = dnsx.DialContextFunc diff --git a/internal/netxlite/dnsx/decoder.go b/internal/netxlite/dnsx/dnsdecoder.go similarity index 82% rename from internal/netxlite/dnsx/decoder.go rename to internal/netxlite/dnsx/dnsdecoder.go index 9e539d3..067f3f5 100644 --- a/internal/netxlite/dnsx/decoder.go +++ b/internal/netxlite/dnsx/dnsdecoder.go @@ -9,8 +9,8 @@ import ( // HTTPSSvc is an HTTPSSvc reply. type HTTPSSvc = model.HTTPSSvc -// The Decoder decodes DNS replies. -type Decoder interface { +// The DNSDecoder decodes DNS replies. +type DNSDecoder interface { // DecodeLookupHost decodes an A or AAAA reply. DecodeLookupHost(qtype uint16, data []byte) ([]string, error) @@ -18,10 +18,10 @@ type Decoder interface { DecodeHTTPS(data []byte) (*HTTPSSvc, error) } -// MiekgDecoder uses github.com/miekg/dns to implement the Decoder. -type MiekgDecoder struct{} +// DNSDecoderMiekg uses github.com/miekg/dns to implement the Decoder. +type DNSDecoderMiekg struct{} -func (d *MiekgDecoder) parseReply(data []byte) (*dns.Msg, error) { +func (d *DNSDecoderMiekg) parseReply(data []byte) (*dns.Msg, error) { reply := new(dns.Msg) if err := reply.Unpack(data); err != nil { return nil, err @@ -40,7 +40,7 @@ func (d *MiekgDecoder) parseReply(data []byte) (*dns.Msg, error) { } } -func (d *MiekgDecoder) DecodeHTTPS(data []byte) (*HTTPSSvc, error) { +func (d *DNSDecoderMiekg) DecodeHTTPS(data []byte) (*HTTPSSvc, error) { reply, err := d.parseReply(data) if err != nil { return nil, err @@ -71,7 +71,7 @@ func (d *MiekgDecoder) DecodeHTTPS(data []byte) (*HTTPSSvc, error) { return out, nil } -func (d *MiekgDecoder) DecodeLookupHost(qtype uint16, data []byte) ([]string, error) { +func (d *DNSDecoderMiekg) DecodeLookupHost(qtype uint16, data []byte) ([]string, error) { reply, err := d.parseReply(data) if err != nil { return nil, err @@ -97,4 +97,4 @@ func (d *MiekgDecoder) DecodeLookupHost(qtype uint16, data []byte) ([]string, er return addrs, nil } -var _ Decoder = &MiekgDecoder{} +var _ DNSDecoder = &DNSDecoderMiekg{} diff --git a/internal/netxlite/dnsx/decoder_test.go b/internal/netxlite/dnsx/dnsdecoder_test.go similarity index 95% rename from internal/netxlite/dnsx/decoder_test.go rename to internal/netxlite/dnsx/dnsdecoder_test.go index 2269b38..9976722 100644 --- a/internal/netxlite/dnsx/decoder_test.go +++ b/internal/netxlite/dnsx/dnsdecoder_test.go @@ -12,7 +12,7 @@ import ( ) func TestDecoderUnpackError(t *testing.T) { - d := &MiekgDecoder{} + d := &DNSDecoderMiekg{} data, err := d.DecodeLookupHost(dns.TypeA, nil) if err == nil { t.Fatal("expected an error here") @@ -23,7 +23,7 @@ func TestDecoderUnpackError(t *testing.T) { } func TestDecoderNXDOMAIN(t *testing.T) { - d := &MiekgDecoder{} + d := &DNSDecoderMiekg{} data, err := d.DecodeLookupHost(dns.TypeA, genReplyError(t, dns.RcodeNameError)) if err == nil || !strings.HasSuffix(err.Error(), "no such host") { t.Fatal("not the error we expected", err) @@ -34,7 +34,7 @@ func TestDecoderNXDOMAIN(t *testing.T) { } func TestDecoderRefusedError(t *testing.T) { - d := &MiekgDecoder{} + d := &DNSDecoderMiekg{} data, err := d.DecodeLookupHost(dns.TypeA, genReplyError(t, dns.RcodeRefused)) if !errors.Is(err, errorsx.ErrOODNSRefused) { t.Fatal("not the error we expected", err) @@ -45,7 +45,7 @@ func TestDecoderRefusedError(t *testing.T) { } func TestDecoderNoAddress(t *testing.T) { - d := &MiekgDecoder{} + d := &DNSDecoderMiekg{} data, err := d.DecodeLookupHost(dns.TypeA, genReplySuccess(t, dns.TypeA)) if !errors.Is(err, errorsx.ErrOODNSNoAnswer) { t.Fatal("not the error we expected", err) @@ -56,7 +56,7 @@ func TestDecoderNoAddress(t *testing.T) { } func TestDecoderDecodeA(t *testing.T) { - d := &MiekgDecoder{} + d := &DNSDecoderMiekg{} data, err := d.DecodeLookupHost( dns.TypeA, genReplySuccess(t, dns.TypeA, "1.1.1.1", "8.8.8.8")) if err != nil { @@ -74,7 +74,7 @@ func TestDecoderDecodeA(t *testing.T) { } func TestDecoderDecodeAAAA(t *testing.T) { - d := &MiekgDecoder{} + d := &DNSDecoderMiekg{} data, err := d.DecodeLookupHost( dns.TypeAAAA, genReplySuccess(t, dns.TypeAAAA, "::1", "fe80::1")) if err != nil { @@ -92,7 +92,7 @@ func TestDecoderDecodeAAAA(t *testing.T) { } func TestDecoderUnexpectedAReply(t *testing.T) { - d := &MiekgDecoder{} + d := &DNSDecoderMiekg{} data, err := d.DecodeLookupHost( dns.TypeA, genReplySuccess(t, dns.TypeAAAA, "::1", "fe80::1")) if !errors.Is(err, errorsx.ErrOODNSNoAnswer) { @@ -104,7 +104,7 @@ func TestDecoderUnexpectedAReply(t *testing.T) { } func TestDecoderUnexpectedAAAAReply(t *testing.T) { - d := &MiekgDecoder{} + d := &DNSDecoderMiekg{} data, err := d.DecodeLookupHost( dns.TypeAAAA, genReplySuccess(t, dns.TypeA, "1.1.1.1", "8.8.4.4.")) if !errors.Is(err, errorsx.ErrOODNSNoAnswer) { @@ -184,7 +184,7 @@ func genReplySuccess(t *testing.T, qtype uint16, ips ...string) []byte { } func TestParseReply(t *testing.T) { - d := &MiekgDecoder{} + d := &DNSDecoderMiekg{} msg := &dns.Msg{} msg.Rcode = dns.RcodeFormatError // an rcode we don't handle data, err := msg.Pack() @@ -265,7 +265,7 @@ func genReplyHTTPS(t *testing.T, alpns, ipv4, ipv6 []string) []byte { func TestDecodeHTTPS(t *testing.T) { t.Run("with nil data", func(t *testing.T) { - d := &MiekgDecoder{} + d := &DNSDecoderMiekg{} reply, err := d.DecodeHTTPS(nil) if err == nil || err.Error() != "dns: overflow unpacking uint16" { t.Fatal("not the error we expected", err) @@ -277,7 +277,7 @@ func TestDecodeHTTPS(t *testing.T) { t.Run("with empty answer", func(t *testing.T) { data := genReplyHTTPS(t, nil, nil, nil) - d := &MiekgDecoder{} + d := &DNSDecoderMiekg{} reply, err := d.DecodeHTTPS(data) if !errors.Is(err, errorsx.ErrOODNSNoAnswer) { t.Fatal("unexpected err", err) @@ -292,7 +292,7 @@ func TestDecodeHTTPS(t *testing.T) { v4 := []string{"1.1.1.1"} v6 := []string{"::1"} data := genReplyHTTPS(t, alpn, v4, v6) - d := &MiekgDecoder{} + d := &DNSDecoderMiekg{} reply, err := d.DecodeHTTPS(data) if err != nil { t.Fatal(err) diff --git a/internal/netxlite/dnsx/encoder.go b/internal/netxlite/dnsx/dnsencoder.go similarity index 54% rename from internal/netxlite/dnsx/encoder.go rename to internal/netxlite/dnsx/dnsencoder.go index d0bb217..d238308 100644 --- a/internal/netxlite/dnsx/encoder.go +++ b/internal/netxlite/dnsx/dnsencoder.go @@ -2,27 +2,27 @@ package dnsx import "github.com/miekg/dns" -// The Encoder encodes DNS queries to bytes -type Encoder interface { +// The DNSEncoder encodes DNS queries to bytes +type DNSEncoder interface { Encode(domain string, qtype uint16, padding bool) ([]byte, error) } -// MiekgEncoder uses github.com/miekg/dns to implement the Encoder. -type MiekgEncoder struct{} +// DNSEncoderMiekg uses github.com/miekg/dns to implement the Encoder. +type DNSEncoderMiekg struct{} const ( - // PaddingDesiredBlockSize is the size that the padded query should be multiple of - PaddingDesiredBlockSize = 128 + // dnsPaddingDesiredBlockSize is the size that the padded query should be multiple of + dnsPaddingDesiredBlockSize = 128 - // EDNS0MaxResponseSize is the maximum response size for EDNS0 - EDNS0MaxResponseSize = 4096 + // dnsEDNS0MaxResponseSize is the maximum response size for EDNS0 + dnsEDNS0MaxResponseSize = 4096 - // DNSSECEnabled turns on support for DNSSEC when using EDNS0 - DNSSECEnabled = true + // dnsDNSSECEnabled turns on support for DNSSEC when using EDNS0 + dnsDNSSECEnabled = true ) // Encode implements Encoder.Encode -func (e *MiekgEncoder) Encode(domain string, qtype uint16, padding bool) ([]byte, error) { +func (e *DNSEncoderMiekg) Encode(domain string, qtype uint16, padding bool) ([]byte, error) { question := dns.Question{ Name: dns.Fqdn(domain), Qtype: qtype, @@ -34,14 +34,14 @@ func (e *MiekgEncoder) Encode(domain string, qtype uint16, padding bool) ([]byte query.Question = make([]dns.Question, 1) query.Question[0] = question if padding { - query.SetEdns0(EDNS0MaxResponseSize, DNSSECEnabled) + query.SetEdns0(dnsEDNS0MaxResponseSize, dnsDNSSECEnabled) // Clients SHOULD pad queries to the closest multiple of // 128 octets RFC8467#section-4.1. We inflate the query // length by the size of the option (i.e. 4 octets). The // cast to uint is necessary to make the modulus operation // work as intended when the desiredBlockSize is smaller // than (query.Len()+4) ¯\_(ツ)_/¯. - remainder := (PaddingDesiredBlockSize - uint(query.Len()+4)) % PaddingDesiredBlockSize + remainder := (dnsPaddingDesiredBlockSize - uint(query.Len()+4)) % dnsPaddingDesiredBlockSize opt := new(dns.EDNS0_PADDING) opt.Padding = make([]byte, remainder) query.IsEdns0().Option = append(query.IsEdns0().Option, opt) @@ -49,4 +49,4 @@ func (e *MiekgEncoder) Encode(domain string, qtype uint16, padding bool) ([]byte return query.Pack() } -var _ Encoder = &MiekgEncoder{} +var _ DNSEncoder = &DNSEncoderMiekg{} diff --git a/internal/netxlite/dnsx/encoder_test.go b/internal/netxlite/dnsx/dnsencoder_test.go similarity index 95% rename from internal/netxlite/dnsx/encoder_test.go rename to internal/netxlite/dnsx/dnsencoder_test.go index 1ce4c70..fa3a9cf 100644 --- a/internal/netxlite/dnsx/encoder_test.go +++ b/internal/netxlite/dnsx/dnsencoder_test.go @@ -8,7 +8,7 @@ import ( ) func TestEncoderEncodeA(t *testing.T) { - e := &MiekgEncoder{} + e := &DNSEncoderMiekg{} data, err := e.Encode("x.org", dns.TypeA, false) if err != nil { t.Fatal(err) @@ -17,7 +17,7 @@ func TestEncoderEncodeA(t *testing.T) { } func TestEncoderEncodeAAAA(t *testing.T) { - e := &MiekgEncoder{} + e := &DNSEncoderMiekg{} data, err := e.Encode("x.org", dns.TypeAAAA, false) if err != nil { t.Fatal(err) @@ -67,7 +67,7 @@ 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 := &MiekgEncoder{} + 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 @@ -88,7 +88,7 @@ func TestEncoderPadding(t *testing.T) { if vanillalen < domainlen { t.Fatal("vanillalen is smaller than domainlen") } - if (paddedlen % PaddingDesiredBlockSize) != 0 { + if (paddedlen % dnsPaddingDesiredBlockSize) != 0 { t.Fatal("paddedlen is not a multiple of PaddingDesiredBlockSize") } if paddedlen < vanillalen { diff --git a/internal/netxlite/dnsx/dnsoverhttps.go b/internal/netxlite/dnsx/dnsoverhttps.go index b5b176e..30f9804 100644 --- a/internal/netxlite/dnsx/dnsoverhttps.go +++ b/internal/netxlite/dnsx/dnsoverhttps.go @@ -86,4 +86,4 @@ func (t *DNSOverHTTPS) CloseIdleConnections() { t.Client.CloseIdleConnections() } -var _ RoundTripper = &DNSOverHTTPS{} +var _ DNSTransport = &DNSOverHTTPS{} diff --git a/internal/netxlite/dnsx/dnsovertcp.go b/internal/netxlite/dnsx/dnsovertcp.go index 9f16655..2e9cd2b 100644 --- a/internal/netxlite/dnsx/dnsovertcp.go +++ b/internal/netxlite/dnsx/dnsovertcp.go @@ -99,4 +99,4 @@ func (t *DNSOverTCP) CloseIdleConnections() { // nothing to do } -var _ RoundTripper = &DNSOverTCP{} +var _ DNSTransport = &DNSOverTCP{} diff --git a/internal/netxlite/dnsx/dnsoverudp.go b/internal/netxlite/dnsx/dnsoverudp.go index cae53a7..08b95f0 100644 --- a/internal/netxlite/dnsx/dnsoverudp.go +++ b/internal/netxlite/dnsx/dnsoverudp.go @@ -66,4 +66,4 @@ func (t *DNSOverUDP) CloseIdleConnections() { // nothing to do } -var _ RoundTripper = &DNSOverUDP{} +var _ DNSTransport = &DNSOverUDP{} diff --git a/internal/netxlite/dnsx/roundtripper.go b/internal/netxlite/dnsx/dnstransport.go similarity index 85% rename from internal/netxlite/dnsx/roundtripper.go rename to internal/netxlite/dnsx/dnstransport.go index e2440e5..95fe571 100644 --- a/internal/netxlite/dnsx/roundtripper.go +++ b/internal/netxlite/dnsx/dnstransport.go @@ -2,8 +2,8 @@ package dnsx import "context" -// RoundTripper represents an abstract DNS transport. -type RoundTripper interface { +// DNSTransport represents an abstract DNS transport. +type DNSTransport interface { // RoundTrip sends a DNS query and receives the reply. RoundTrip(ctx context.Context, query []byte) (reply []byte, err error) diff --git a/internal/netxlite/dnsx/mocks/decoder.go b/internal/netxlite/dnsx/mocks/dnsdecoder.go similarity index 67% rename from internal/netxlite/dnsx/mocks/decoder.go rename to internal/netxlite/dnsx/mocks/dnsdecoder.go index 6b79394..a04858d 100644 --- a/internal/netxlite/dnsx/mocks/decoder.go +++ b/internal/netxlite/dnsx/mocks/dnsdecoder.go @@ -5,19 +5,19 @@ import "github.com/ooni/probe-cli/v3/internal/netxlite/dnsx/model" // HTTPSSvc is the result of HTTPS queries. type HTTPSSvc = model.HTTPSSvc -// Decoder allows mocking dnsx.Decoder. -type Decoder struct { +// DNSDecoder allows mocking dnsx.DNSDecoder. +type DNSDecoder struct { MockDecodeLookupHost func(qtype uint16, reply []byte) ([]string, error) MockDecodeHTTPS func(reply []byte) (*HTTPSSvc, error) } // DecodeLookupHost calls MockDecodeLookupHost. -func (e *Decoder) DecodeLookupHost(qtype uint16, reply []byte) ([]string, error) { +func (e *DNSDecoder) DecodeLookupHost(qtype uint16, reply []byte) ([]string, error) { return e.MockDecodeLookupHost(qtype, reply) } // DecodeHTTPS calls MockDecodeHTTPS. -func (e *Decoder) DecodeHTTPS(reply []byte) (*HTTPSSvc, error) { +func (e *DNSDecoder) DecodeHTTPS(reply []byte) (*HTTPSSvc, error) { return e.MockDecodeHTTPS(reply) } diff --git a/internal/netxlite/dnsx/mocks/decoder_test.go b/internal/netxlite/dnsx/mocks/dnsdecoder_test.go similarity index 91% rename from internal/netxlite/dnsx/mocks/decoder_test.go rename to internal/netxlite/dnsx/mocks/dnsdecoder_test.go index 67437fa..0d646ff 100644 --- a/internal/netxlite/dnsx/mocks/decoder_test.go +++ b/internal/netxlite/dnsx/mocks/dnsdecoder_test.go @@ -7,10 +7,10 @@ import ( "github.com/miekg/dns" ) -func TestDecoder(t *testing.T) { +func TestDNSDecoder(t *testing.T) { t.Run("DecodeLookupHost", func(t *testing.T) { expected := errors.New("mocked error") - e := &Decoder{ + e := &DNSDecoder{ MockDecodeLookupHost: func(qtype uint16, reply []byte) ([]string, error) { return nil, expected }, @@ -26,7 +26,7 @@ func TestDecoder(t *testing.T) { t.Run("DecodeHTTPS", func(t *testing.T) { expected := errors.New("mocked error") - e := &Decoder{ + e := &DNSDecoder{ MockDecodeHTTPS: func(reply []byte) (*HTTPSSvc, error) { return nil, expected }, diff --git a/internal/netxlite/dnsx/mocks/encoder.go b/internal/netxlite/dnsx/mocks/dnsencoder.go similarity index 51% rename from internal/netxlite/dnsx/mocks/encoder.go rename to internal/netxlite/dnsx/mocks/dnsencoder.go index f110036..0723f2f 100644 --- a/internal/netxlite/dnsx/mocks/encoder.go +++ b/internal/netxlite/dnsx/mocks/dnsencoder.go @@ -1,11 +1,11 @@ package mocks -// Encoder allows mocking dnsx.Encoder. -type Encoder struct { +// DNSEncoder allows mocking dnsx.DNSEncoder. +type DNSEncoder struct { MockEncode func(domain string, qtype uint16, padding bool) ([]byte, error) } // Encode calls MockEncode. -func (e *Encoder) Encode(domain string, qtype uint16, padding bool) ([]byte, error) { +func (e *DNSEncoder) Encode(domain string, qtype uint16, padding bool) ([]byte, error) { return e.MockEncode(domain, qtype, padding) } diff --git a/internal/netxlite/dnsx/mocks/encoder_test.go b/internal/netxlite/dnsx/mocks/dnsencoder_test.go similarity index 88% rename from internal/netxlite/dnsx/mocks/encoder_test.go rename to internal/netxlite/dnsx/mocks/dnsencoder_test.go index 5f2e5ea..7380cfd 100644 --- a/internal/netxlite/dnsx/mocks/encoder_test.go +++ b/internal/netxlite/dnsx/mocks/dnsencoder_test.go @@ -7,10 +7,10 @@ import ( "github.com/miekg/dns" ) -func TestEncoder(t *testing.T) { +func TestDNSEncoder(t *testing.T) { t.Run("Encode", func(t *testing.T) { expected := errors.New("mocked error") - e := &Encoder{ + e := &DNSEncoder{ MockEncode: func(domain string, qtype uint16, padding bool) ([]byte, error) { return nil, expected }, diff --git a/internal/netxlite/dnsx/mocks/roundtripper.go b/internal/netxlite/dnsx/mocks/dnstransport.go similarity index 66% rename from internal/netxlite/dnsx/mocks/roundtripper.go rename to internal/netxlite/dnsx/mocks/dnstransport.go index 182bd4f..9e74845 100644 --- a/internal/netxlite/dnsx/mocks/roundtripper.go +++ b/internal/netxlite/dnsx/mocks/dnstransport.go @@ -2,8 +2,8 @@ package mocks import "context" -// RoundTripper allows mocking dnsx.RoundTripper. -type RoundTripper struct { +// DNSTransport allows mocking dnsx.DNSTransport. +type DNSTransport struct { MockRoundTrip func(ctx context.Context, query []byte) (reply []byte, err error) MockRequiresPadding func() bool @@ -16,26 +16,26 @@ type RoundTripper struct { } // RoundTrip calls MockRoundTrip. -func (txp *RoundTripper) RoundTrip(ctx context.Context, query []byte) (reply []byte, err error) { +func (txp *DNSTransport) RoundTrip(ctx context.Context, query []byte) (reply []byte, err error) { return txp.MockRoundTrip(ctx, query) } // RequiresPadding calls MockRequiresPadding. -func (txp *RoundTripper) RequiresPadding() bool { +func (txp *DNSTransport) RequiresPadding() bool { return txp.MockRequiresPadding() } // Network calls MockNetwork. -func (txp *RoundTripper) Network() string { +func (txp *DNSTransport) Network() string { return txp.MockNetwork() } // Address calls MockAddress. -func (txp *RoundTripper) Address() string { +func (txp *DNSTransport) Address() string { return txp.MockAddress() } // CloseIdleConnections calls MockCloseIdleConnections. -func (txp *RoundTripper) CloseIdleConnections() { +func (txp *DNSTransport) CloseIdleConnections() { txp.MockCloseIdleConnections() } diff --git a/internal/netxlite/dnsx/mocks/roundtripper_test.go b/internal/netxlite/dnsx/mocks/dnstransport_test.go similarity index 89% rename from internal/netxlite/dnsx/mocks/roundtripper_test.go rename to internal/netxlite/dnsx/mocks/dnstransport_test.go index 8fd24dc..ba42a04 100644 --- a/internal/netxlite/dnsx/mocks/roundtripper_test.go +++ b/internal/netxlite/dnsx/mocks/dnstransport_test.go @@ -8,10 +8,10 @@ import ( "github.com/ooni/probe-cli/v3/internal/atomicx" ) -func TestRoundTripper(t *testing.T) { +func TestDNSTransport(t *testing.T) { t.Run("RoundTrip", func(t *testing.T) { expected := errors.New("mocked error") - txp := &RoundTripper{ + txp := &DNSTransport{ MockRoundTrip: func(ctx context.Context, query []byte) ([]byte, error) { return nil, expected }, @@ -26,7 +26,7 @@ func TestRoundTripper(t *testing.T) { }) t.Run("RequiresPadding", func(t *testing.T) { - txp := &RoundTripper{ + txp := &DNSTransport{ MockRequiresPadding: func() bool { return true }, @@ -37,7 +37,7 @@ func TestRoundTripper(t *testing.T) { }) t.Run("Network", func(t *testing.T) { - txp := &RoundTripper{ + txp := &DNSTransport{ MockNetwork: func() string { return "antani" }, @@ -48,7 +48,7 @@ func TestRoundTripper(t *testing.T) { }) t.Run("Address", func(t *testing.T) { - txp := &RoundTripper{ + txp := &DNSTransport{ MockAddress: func() string { return "mascetti" }, @@ -60,7 +60,7 @@ func TestRoundTripper(t *testing.T) { t.Run("CloseIdleConnections", func(t *testing.T) { called := &atomicx.Int64{} - txp := &RoundTripper{ + txp := &DNSTransport{ MockCloseIdleConnections: func() { called.Add(1) }, diff --git a/internal/netxlite/dnsx/serial.go b/internal/netxlite/dnsx/serialresolver.go similarity index 92% rename from internal/netxlite/dnsx/serial.go rename to internal/netxlite/dnsx/serialresolver.go index 3f8eac2..6ac1745 100644 --- a/internal/netxlite/dnsx/serial.go +++ b/internal/netxlite/dnsx/serialresolver.go @@ -12,24 +12,24 @@ import ( // SerialResolver is a resolver that first issues an A query and then // issues an AAAA query for the requested domain. type SerialResolver struct { - Encoder Encoder - Decoder Decoder + Encoder DNSEncoder + Decoder DNSDecoder NumTimeouts *atomicx.Int64 - Txp RoundTripper + Txp DNSTransport } // NewSerialResolver creates a new OONI Resolver instance. -func NewSerialResolver(t RoundTripper) *SerialResolver { +func NewSerialResolver(t DNSTransport) *SerialResolver { return &SerialResolver{ - Encoder: &MiekgEncoder{}, - Decoder: &MiekgDecoder{}, + Encoder: &DNSEncoderMiekg{}, + Decoder: &DNSDecoderMiekg{}, NumTimeouts: &atomicx.Int64{}, Txp: t, } } // Transport returns the transport being used. -func (r *SerialResolver) Transport() RoundTripper { +func (r *SerialResolver) Transport() DNSTransport { return r.Txp } diff --git a/internal/netxlite/dnsx/serial_test.go b/internal/netxlite/dnsx/serialresolver_test.go similarity index 93% rename from internal/netxlite/dnsx/serial_test.go rename to internal/netxlite/dnsx/serialresolver_test.go index afcdc84..5efe8f7 100644 --- a/internal/netxlite/dnsx/serial_test.go +++ b/internal/netxlite/dnsx/serialresolver_test.go @@ -32,7 +32,7 @@ func TestOONIEncodeError(t *testing.T) { mocked := errors.New("mocked error") txp := NewDNSOverTLS((&tls.Dialer{}).DialContext, "8.8.8.8:853") r := SerialResolver{ - Encoder: &mocks.Encoder{ + Encoder: &mocks.DNSEncoder{ MockEncode: func(domain string, qtype uint16, padding bool) ([]byte, error) { return nil, mocked }, @@ -50,7 +50,7 @@ func TestOONIEncodeError(t *testing.T) { func TestOONIRoundTripError(t *testing.T) { mocked := errors.New("mocked error") - txp := &mocks.RoundTripper{ + txp := &mocks.DNSTransport{ MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) { return nil, mocked }, @@ -69,7 +69,7 @@ func TestOONIRoundTripError(t *testing.T) { } func TestOONIWithEmptyReply(t *testing.T) { - txp := &mocks.RoundTripper{ + txp := &mocks.DNSTransport{ MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) { return genReplySuccess(t, dns.TypeA), nil }, @@ -88,7 +88,7 @@ func TestOONIWithEmptyReply(t *testing.T) { } func TestOONIWithAReply(t *testing.T) { - txp := &mocks.RoundTripper{ + txp := &mocks.DNSTransport{ MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) { return genReplySuccess(t, dns.TypeA, "8.8.8.8"), nil }, @@ -107,7 +107,7 @@ func TestOONIWithAReply(t *testing.T) { } func TestOONIWithAAAAReply(t *testing.T) { - txp := &mocks.RoundTripper{ + txp := &mocks.DNSTransport{ MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) { return genReplySuccess(t, dns.TypeAAAA, "::1"), nil }, @@ -126,7 +126,7 @@ func TestOONIWithAAAAReply(t *testing.T) { } func TestOONIWithTimeout(t *testing.T) { - txp := &mocks.RoundTripper{ + txp := &mocks.DNSTransport{ MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) { return nil, &net.OpError{Err: errorsx.ETIMEDOUT, Op: "dial"} }, @@ -150,7 +150,7 @@ func TestOONIWithTimeout(t *testing.T) { func TestSerialResolverCloseIdleConnections(t *testing.T) { var called bool r := &SerialResolver{ - Txp: &mocks.RoundTripper{ + Txp: &mocks.DNSTransport{ MockCloseIdleConnections: func() { called = true }, @@ -166,14 +166,14 @@ func TestSerialResolverLookupHTTPS(t *testing.T) { t.Run("for encoding error", func(t *testing.T) { expected := errors.New("mocked error") r := &SerialResolver{ - Encoder: &mocks.Encoder{ + Encoder: &mocks.DNSEncoder{ MockEncode: func(domain string, qtype uint16, padding bool) ([]byte, error) { return nil, expected }, }, Decoder: nil, NumTimeouts: &atomicx.Int64{}, - Txp: &mocks.RoundTripper{ + Txp: &mocks.DNSTransport{ MockRequiresPadding: func() bool { return false }, @@ -192,14 +192,14 @@ func TestSerialResolverLookupHTTPS(t *testing.T) { t.Run("for round-trip error", func(t *testing.T) { expected := errors.New("mocked error") r := &SerialResolver{ - Encoder: &mocks.Encoder{ + Encoder: &mocks.DNSEncoder{ MockEncode: func(domain string, qtype uint16, padding bool) ([]byte, error) { return make([]byte, 64), nil }, }, Decoder: nil, NumTimeouts: &atomicx.Int64{}, - Txp: &mocks.RoundTripper{ + Txp: &mocks.DNSTransport{ MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) { return nil, expected }, @@ -221,18 +221,18 @@ func TestSerialResolverLookupHTTPS(t *testing.T) { t.Run("for decode error", func(t *testing.T) { expected := errors.New("mocked error") r := &SerialResolver{ - Encoder: &mocks.Encoder{ + Encoder: &mocks.DNSEncoder{ MockEncode: func(domain string, qtype uint16, padding bool) ([]byte, error) { return make([]byte, 64), nil }, }, - Decoder: &mocks.Decoder{ + Decoder: &mocks.DNSDecoder{ MockDecodeHTTPS: func(reply []byte) (*mocks.HTTPSSvc, error) { return nil, expected }, }, NumTimeouts: &atomicx.Int64{}, - Txp: &mocks.RoundTripper{ + Txp: &mocks.DNSTransport{ MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) { return make([]byte, 128), nil },