refactor: DNSTransport I/Os DNS messages (#760)
This diff refactors the DNSTransport model to receive in input a DNSQuery and return in output a DNSResponse. The design of DNSQuery and DNSResponse takes into account the use case of a transport using getaddrinfo, meaning that we don't need to serialize and deserialize messages when using getaddrinfo. The current codebase does not use a getaddrinfo transport, but I wrote one such a transport in the Websteps Winter 2021 prototype (https://github.com/bassosimone/websteps-illustrated/). The design conversation that lead to producing this diff is https://github.com/ooni/probe/issues/2099
This commit is contained in:
parent
7a0a156aec
commit
01a513a496
35 changed files with 1694 additions and 1039 deletions
|
|
@ -8,14 +8,13 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
||||
)
|
||||
|
||||
func TestParallelResolver(t *testing.T) {
|
||||
t.Run("transport okay", func(t *testing.T) {
|
||||
txp := NewDNSOverTLS((&tls.Dialer{}).DialContext, "8.8.8.8:853")
|
||||
txp := NewDNSOverTLSTransport((&tls.Dialer{}).DialContext, "8.8.8.8:853")
|
||||
r := NewUnwrappedParallelResolver(txp)
|
||||
rtx := r.Transport()
|
||||
if rtx.Network() != "dot" || rtx.Address() != "8.8.8.8:853" {
|
||||
|
|
@ -30,30 +29,10 @@ func TestParallelResolver(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 := ParallelResolver{
|
||||
Encoder: &mocks.DNSEncoder{
|
||||
MockEncode: func(domain string, qtype uint16, padding bool) ([]byte, uint16, error) {
|
||||
return nil, 0, mocked
|
||||
},
|
||||
},
|
||||
Txp: txp,
|
||||
}
|
||||
addrs, err := r.LookupHost(context.Background(), "www.gogle.com")
|
||||
if !errors.Is(err, mocked) {
|
||||
t.Fatal("not the error we expected")
|
||||
}
|
||||
if addrs != nil {
|
||||
t.Fatal("expected nil address here")
|
||||
}
|
||||
})
|
||||
|
||||
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) {
|
||||
MockRoundTrip: func(ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
||||
return nil, mocked
|
||||
},
|
||||
MockRequiresPadding: func() bool {
|
||||
|
|
@ -72,8 +51,13 @@ func TestParallelResolver(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 dnsGenLookupHostReplySuccess(query), nil
|
||||
MockRoundTrip: func(ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
||||
response := &mocks.DNSResponse{
|
||||
MockDecodeLookupHost: func() ([]string, error) {
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
return response, nil
|
||||
},
|
||||
MockRequiresPadding: func() bool {
|
||||
return true
|
||||
|
|
@ -91,8 +75,16 @@ func TestParallelResolver(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 dnsGenLookupHostReplySuccess(query, "8.8.8.8"), nil
|
||||
MockRoundTrip: func(ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
||||
response := &mocks.DNSResponse{
|
||||
MockDecodeLookupHost: func() ([]string, error) {
|
||||
if query.Type() != dns.TypeA {
|
||||
return nil, nil
|
||||
}
|
||||
return []string{"8.8.8.8"}, nil
|
||||
},
|
||||
}
|
||||
return response, nil
|
||||
},
|
||||
MockRequiresPadding: func() bool {
|
||||
return true
|
||||
|
|
@ -110,8 +102,16 @@ func TestParallelResolver(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 dnsGenLookupHostReplySuccess(query, "::1"), nil
|
||||
MockRoundTrip: func(ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
||||
response := &mocks.DNSResponse{
|
||||
MockDecodeLookupHost: func() ([]string, error) {
|
||||
if query.Type() != dns.TypeAAAA {
|
||||
return nil, nil
|
||||
}
|
||||
return []string{"::1"}, nil
|
||||
},
|
||||
}
|
||||
return response, nil
|
||||
},
|
||||
MockRequiresPadding: func() bool {
|
||||
return true
|
||||
|
|
@ -131,22 +131,15 @@ func TestParallelResolver(t *testing.T) {
|
|||
afailure := errors.New("a failure")
|
||||
aaaafailure := errors.New("aaaa failure")
|
||||
txp := &mocks.DNSTransport{
|
||||
MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) {
|
||||
msg := &dns.Msg{}
|
||||
if err := msg.Unpack(query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(msg.Question) != 1 {
|
||||
return nil, errors.New("expected just one question")
|
||||
}
|
||||
q := msg.Question[0]
|
||||
if q.Qtype == dns.TypeA {
|
||||
MockRoundTrip: func(ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
||||
switch query.Type() {
|
||||
case dns.TypeA:
|
||||
return nil, afailure
|
||||
}
|
||||
if q.Qtype == dns.TypeAAAA {
|
||||
case dns.TypeAAAA:
|
||||
return nil, aaaafailure
|
||||
default:
|
||||
return nil, errors.New("unexpected query")
|
||||
}
|
||||
return nil, errors.New("expected A or AAAA query")
|
||||
},
|
||||
MockRequiresPadding: func() bool {
|
||||
return true
|
||||
|
|
@ -179,44 +172,11 @@ func TestParallelResolver(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 := &ParallelResolver{
|
||||
Encoder: &mocks.DNSEncoder{
|
||||
MockEncode: func(domain string, qtype uint16, padding bool) ([]byte, uint16, error) {
|
||||
return nil, 0, expected
|
||||
},
|
||||
},
|
||||
Decoder: nil,
|
||||
NumTimeouts: &atomicx.Int64{},
|
||||
Txp: &mocks.DNSTransport{
|
||||
MockRequiresPadding: func() bool {
|
||||
return false
|
||||
},
|
||||
},
|
||||
}
|
||||
ctx := context.Background()
|
||||
https, err := r.LookupHTTPS(ctx, "example.com")
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if https != nil {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("for round-trip error", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
r := &ParallelResolver{
|
||||
Encoder: &mocks.DNSEncoder{
|
||||
MockEncode: func(domain string, qtype uint16, padding bool) ([]byte, uint16, error) {
|
||||
return make([]byte, 64), 0, nil
|
||||
},
|
||||
},
|
||||
Decoder: nil,
|
||||
NumTimeouts: &atomicx.Int64{},
|
||||
Txp: &mocks.DNSTransport{
|
||||
MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) {
|
||||
MockRoundTrip: func(ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
||||
return nil, expected
|
||||
},
|
||||
MockRequiresPadding: func() bool {
|
||||
|
|
@ -234,23 +194,17 @@ func TestParallelResolver(t *testing.T) {
|
|||
}
|
||||
})
|
||||
|
||||
t.Run("for decode error", func(t *testing.T) {
|
||||
t.Run("for DecodeHTTPS error", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
r := &ParallelResolver{
|
||||
Encoder: &mocks.DNSEncoder{
|
||||
MockEncode: func(domain string, qtype uint16, padding bool) ([]byte, uint16, error) {
|
||||
return make([]byte, 64), 0, nil
|
||||
},
|
||||
},
|
||||
Decoder: &mocks.DNSDecoder{
|
||||
MockDecodeHTTPS: func(reply []byte, queryID uint16) (*model.HTTPSSvc, error) {
|
||||
return nil, expected
|
||||
},
|
||||
},
|
||||
NumTimeouts: &atomicx.Int64{},
|
||||
Txp: &mocks.DNSTransport{
|
||||
MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) {
|
||||
return make([]byte, 128), nil
|
||||
MockRoundTrip: func(ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
||||
response := &mocks.DNSResponse{
|
||||
MockDecodeHTTPS: func() (*model.HTTPSSvc, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
return response, nil
|
||||
},
|
||||
MockRequiresPadding: func() bool {
|
||||
return false
|
||||
|
|
@ -269,44 +223,11 @@ func TestParallelResolver(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("LookupNS", func(t *testing.T) {
|
||||
t.Run("for encoding error", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
r := &ParallelResolver{
|
||||
Encoder: &mocks.DNSEncoder{
|
||||
MockEncode: func(domain string, qtype uint16, padding bool) ([]byte, uint16, error) {
|
||||
return nil, 0, expected
|
||||
},
|
||||
},
|
||||
Decoder: nil,
|
||||
NumTimeouts: &atomicx.Int64{},
|
||||
Txp: &mocks.DNSTransport{
|
||||
MockRequiresPadding: func() bool {
|
||||
return false
|
||||
},
|
||||
},
|
||||
}
|
||||
ctx := context.Background()
|
||||
ns, err := r.LookupNS(ctx, "example.com")
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if ns != nil {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("for round-trip error", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
r := &ParallelResolver{
|
||||
Encoder: &mocks.DNSEncoder{
|
||||
MockEncode: func(domain string, qtype uint16, padding bool) ([]byte, uint16, error) {
|
||||
return make([]byte, 64), 0, nil
|
||||
},
|
||||
},
|
||||
Decoder: nil,
|
||||
NumTimeouts: &atomicx.Int64{},
|
||||
Txp: &mocks.DNSTransport{
|
||||
MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) {
|
||||
MockRoundTrip: func(ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
||||
return nil, expected
|
||||
},
|
||||
MockRequiresPadding: func() bool {
|
||||
|
|
@ -327,20 +248,14 @@ func TestParallelResolver(t *testing.T) {
|
|||
t.Run("for decode error", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
r := &ParallelResolver{
|
||||
Encoder: &mocks.DNSEncoder{
|
||||
MockEncode: func(domain string, qtype uint16, padding bool) ([]byte, uint16, error) {
|
||||
return make([]byte, 64), 0, nil
|
||||
},
|
||||
},
|
||||
Decoder: &mocks.DNSDecoder{
|
||||
MockDecodeNS: func(reply []byte, queryID uint16) ([]*net.NS, error) {
|
||||
return nil, expected
|
||||
},
|
||||
},
|
||||
NumTimeouts: &atomicx.Int64{},
|
||||
Txp: &mocks.DNSTransport{
|
||||
MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) {
|
||||
return make([]byte, 128), nil
|
||||
MockRoundTrip: func(ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
||||
response := &mocks.DNSResponse{
|
||||
MockDecodeNS: func() ([]*net.NS, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
return response, nil
|
||||
},
|
||||
MockRequiresPadding: func() bool {
|
||||
return false
|
||||
|
|
|
|||
Loading…
Reference in a new issue