2022-05-31 21:53:01 +02:00
|
|
|
package tracex
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-05-31 21:53:01 +02:00
|
|
|
"net"
|
2021-02-02 12:05:47 +01:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-08-27 15:47:48 +02:00
|
|
|
"github.com/miekg/dns"
|
2022-05-25 17:03:58 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
refactor(tracex): internally represent errors as strings (#786)
There are two reasons why this is beneficial:
1. github.com/google/go-cmp is more annoying to use for comparing
data structures when there are interfaces to compare. Sure, there's
a recipe for teaching it to compare errors, but how about making
the errors trivially comparable instead?
2. if we want to send errors over the network, JSON serialization
works but we cannot unmarshal the resulting string back to an error,
so how about making this representation trivial to serialize (we
are not going this now, but we need this property for websteps and
it may be sensible to try to avoid to have duplicate code because
of that -- measurex currently duplicates many tracex functionality
and this is quite unfortunate because it slows development down)
Additionally, if an error is a string:
3. we can very easily use a switch for comparing its possible
values with "" representing the absence of errors, while it is
more complex to do the same when using a nullable string or even
an error (i.e., an interface)
4. if a type is not nullable, it's easier to write safe code for
it and we may want to refactor experiments to use the internal
representation of measurements for more robust processing code
For all these reasons, let's internally use strings in tracex.
The overall aim here is to reduce the duplicated code between pre
and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
2022-06-02 10:37:07 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
2022-05-31 21:53:01 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2022-06-04 14:58:48 +02:00
|
|
|
func TestWrapResolver(t *testing.T) {
|
|
|
|
var saver *Saver
|
|
|
|
reso := &mocks.Resolver{}
|
|
|
|
if saver.WrapResolver(reso) != reso {
|
|
|
|
t.Fatal("unexpected result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func TestResolverSaver(t *testing.T) {
|
2022-06-04 14:58:48 +02:00
|
|
|
t.Run("LookupHost", func(t *testing.T) {
|
|
|
|
t.Run("on failure", func(t *testing.T) {
|
|
|
|
expected := netxlite.ErrOODNSNoSuchHost
|
|
|
|
saver := &Saver{}
|
|
|
|
reso := saver.WrapResolver(newFakeResolverWithExplicitError(expected))
|
|
|
|
addrs, err := reso.LookupHost(context.Background(), "www.google.com")
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if addrs != nil {
|
|
|
|
t.Fatal("expected nil address here")
|
|
|
|
}
|
|
|
|
ev := saver.Read()
|
|
|
|
if len(ev) != 2 {
|
|
|
|
t.Fatal("expected number of events")
|
|
|
|
}
|
|
|
|
if ev[0].Value().Hostname != "www.google.com" {
|
|
|
|
t.Fatal("unexpected Hostname")
|
|
|
|
}
|
|
|
|
if ev[0].Name() != "resolve_start" {
|
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
|
|
|
if !ev[0].Value().Time.Before(time.Now()) {
|
|
|
|
t.Fatal("the saved time is wrong")
|
|
|
|
}
|
|
|
|
if ev[1].Value().Addresses != nil {
|
|
|
|
t.Fatal("unexpected Addresses")
|
|
|
|
}
|
|
|
|
if ev[1].Value().Duration <= 0 {
|
|
|
|
t.Fatal("unexpected Duration")
|
|
|
|
}
|
|
|
|
if ev[1].Value().Err != netxlite.FailureDNSNXDOMAINError {
|
|
|
|
t.Fatal("unexpected Err")
|
|
|
|
}
|
|
|
|
if ev[1].Value().Hostname != "www.google.com" {
|
|
|
|
t.Fatal("unexpected Hostname")
|
|
|
|
}
|
|
|
|
if ev[1].Name() != "resolve_done" {
|
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
|
|
|
if !ev[1].Value().Time.After(ev[0].Value().Time) {
|
|
|
|
t.Fatal("the saved time is wrong")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("on success", func(t *testing.T) {
|
|
|
|
expected := []string{"8.8.8.8", "8.8.4.4"}
|
|
|
|
saver := &Saver{}
|
|
|
|
reso := saver.WrapResolver(newFakeResolverWithResult(expected))
|
|
|
|
addrs, err := reso.LookupHost(context.Background(), "www.google.com")
|
|
|
|
if err != nil {
|
2022-08-27 15:47:48 +02:00
|
|
|
t.Fatal(err)
|
2022-06-04 14:58:48 +02:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(addrs, expected) {
|
|
|
|
t.Fatal("not the result we expected")
|
|
|
|
}
|
|
|
|
ev := saver.Read()
|
|
|
|
if len(ev) != 2 {
|
|
|
|
t.Fatal("expected number of events")
|
|
|
|
}
|
|
|
|
if ev[0].Value().Hostname != "www.google.com" {
|
|
|
|
t.Fatal("unexpected Hostname")
|
|
|
|
}
|
|
|
|
if ev[0].Name() != "resolve_start" {
|
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
|
|
|
if !ev[0].Value().Time.Before(time.Now()) {
|
|
|
|
t.Fatal("the saved time is wrong")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ev[1].Value().Addresses, expected) {
|
|
|
|
t.Fatal("unexpected Addresses")
|
|
|
|
}
|
|
|
|
if ev[1].Value().Duration <= 0 {
|
|
|
|
t.Fatal("unexpected Duration")
|
|
|
|
}
|
|
|
|
if ev[1].Value().Err.IsNotNil() {
|
|
|
|
t.Fatal("unexpected Err")
|
|
|
|
}
|
|
|
|
if ev[1].Value().Hostname != "www.google.com" {
|
|
|
|
t.Fatal("unexpected Hostname")
|
|
|
|
}
|
|
|
|
if ev[1].Name() != "resolve_done" {
|
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
|
|
|
if !ev[1].Value().Time.After(ev[0].Value().Time) {
|
|
|
|
t.Fatal("the saved time is wrong")
|
|
|
|
}
|
|
|
|
})
|
2022-08-27 15:47:48 +02:00
|
|
|
|
|
|
|
t.Run("with stdlib resolver there's correct .Network remapping", func(t *testing.T) {
|
|
|
|
saver := &Saver{}
|
|
|
|
reso := saver.WrapResolver(netxlite.NewStdlibResolver(model.DiscardLogger))
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel() // immediately fail the operation
|
|
|
|
_, _ = reso.LookupHost(ctx, "www.google.com")
|
|
|
|
// basically, we just want to ensure that the engine name is converted
|
|
|
|
ev := saver.Read()
|
|
|
|
if len(ev) != 2 {
|
|
|
|
t.Fatal("expected number of events")
|
|
|
|
}
|
|
|
|
if ev[0].Value().Proto != netxlite.StdlibResolverSystem {
|
|
|
|
t.Fatal("unexpected Proto")
|
|
|
|
}
|
|
|
|
if ev[1].Value().Proto != netxlite.StdlibResolverSystem {
|
|
|
|
t.Fatal("unexpected Proto")
|
|
|
|
}
|
|
|
|
})
|
2022-06-04 14:58:48 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Network", func(t *testing.T) {
|
2022-08-27 15:47:48 +02:00
|
|
|
t.Run("when using a custom resolver", func(t *testing.T) {
|
|
|
|
saver := &Saver{}
|
|
|
|
child := &mocks.Resolver{
|
|
|
|
MockNetwork: func() string {
|
|
|
|
return "x"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
reso := saver.WrapResolver(child)
|
|
|
|
if reso.Network() != "x" {
|
|
|
|
t.Fatal("unexpected result")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("when using the stdlib resolver", func(t *testing.T) {
|
|
|
|
child := netxlite.NewStdlibResolver(model.DiscardLogger)
|
|
|
|
switch network := child.Network(); network {
|
|
|
|
case netxlite.StdlibResolverGetaddrinfo,
|
|
|
|
netxlite.StdlibResolverGolangNetResolver:
|
|
|
|
// ok
|
|
|
|
default:
|
|
|
|
t.Fatal("unexpected child resolver network", network)
|
|
|
|
}
|
|
|
|
saver := &Saver{}
|
|
|
|
reso := saver.WrapResolver(child)
|
|
|
|
if network := reso.Network(); network != netxlite.StdlibResolverSystem {
|
|
|
|
t.Fatal("unexpected wrapped resolver network", network)
|
|
|
|
}
|
|
|
|
})
|
2022-06-01 23:15:47 +02:00
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-06-04 14:58:48 +02:00
|
|
|
t.Run("Address", func(t *testing.T) {
|
2022-06-01 23:15:47 +02:00
|
|
|
saver := &Saver{}
|
2022-06-04 14:58:48 +02:00
|
|
|
child := &mocks.Resolver{
|
|
|
|
MockAddress: func() string {
|
|
|
|
return "x"
|
|
|
|
},
|
2022-06-01 23:15:47 +02:00
|
|
|
}
|
2022-06-04 14:58:48 +02:00
|
|
|
reso := saver.WrapResolver(child)
|
|
|
|
if reso.Address() != "x" {
|
|
|
|
t.Fatal("unexpected result")
|
2022-06-01 23:15:47 +02:00
|
|
|
}
|
2022-06-04 14:58:48 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("LookupHTTPS", func(t *testing.T) {
|
|
|
|
expected := errors.New("mocked")
|
|
|
|
saver := &Saver{}
|
|
|
|
child := &mocks.Resolver{
|
|
|
|
MockLookupHTTPS: func(ctx context.Context, domain string) (*model.HTTPSSvc, error) {
|
|
|
|
return nil, expected
|
|
|
|
},
|
2022-06-01 23:15:47 +02:00
|
|
|
}
|
2022-06-04 14:58:48 +02:00
|
|
|
reso := saver.WrapResolver(child)
|
|
|
|
https, err := reso.LookupHTTPS(context.Background(), "dns.google")
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("unexpected err", err)
|
2022-06-01 23:15:47 +02:00
|
|
|
}
|
2022-06-04 14:58:48 +02:00
|
|
|
if https != nil {
|
|
|
|
t.Fatal("expected nil")
|
2022-06-01 23:15:47 +02:00
|
|
|
}
|
2022-06-04 14:58:48 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("LookupNS", func(t *testing.T) {
|
|
|
|
expected := errors.New("mocked")
|
|
|
|
saver := &Saver{}
|
|
|
|
child := &mocks.Resolver{
|
|
|
|
MockLookupNS: func(ctx context.Context, domain string) ([]*net.NS, error) {
|
|
|
|
return nil, expected
|
|
|
|
},
|
2022-06-01 23:15:47 +02:00
|
|
|
}
|
2022-06-04 14:58:48 +02:00
|
|
|
reso := saver.WrapResolver(child)
|
|
|
|
ns, err := reso.LookupNS(context.Background(), "dns.google")
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("unexpected err", err)
|
2022-06-01 23:15:47 +02:00
|
|
|
}
|
2022-06-04 14:58:48 +02:00
|
|
|
if len(ns) != 0 {
|
|
|
|
t.Fatal("expected zero length array")
|
2022-06-01 23:15:47 +02:00
|
|
|
}
|
2022-06-04 14:58:48 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("CloseIdleConnections", func(t *testing.T) {
|
|
|
|
var called bool
|
|
|
|
saver := &Saver{}
|
|
|
|
child := &mocks.Resolver{
|
|
|
|
MockCloseIdleConnections: func() {
|
|
|
|
called = true
|
|
|
|
},
|
2022-06-01 23:15:47 +02:00
|
|
|
}
|
2022-06-04 14:58:48 +02:00
|
|
|
reso := saver.WrapResolver(child)
|
|
|
|
reso.CloseIdleConnections()
|
|
|
|
if !called {
|
|
|
|
t.Fatal("not called")
|
2022-06-01 23:15:47 +02:00
|
|
|
}
|
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2022-06-04 14:58:48 +02:00
|
|
|
func TestWrapDNSTransport(t *testing.T) {
|
|
|
|
var saver *Saver
|
|
|
|
txp := &mocks.DNSTransport{}
|
|
|
|
if saver.WrapDNSTransport(txp) != txp {
|
|
|
|
t.Fatal("unexpected result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func TestDNSTransportSaver(t *testing.T) {
|
2022-06-04 14:58:48 +02:00
|
|
|
t.Run("RoundTrip", func(t *testing.T) {
|
|
|
|
t.Run("on failure", func(t *testing.T) {
|
|
|
|
expected := netxlite.ErrOODNSNoSuchHost
|
|
|
|
saver := &Saver{}
|
|
|
|
txp := saver.WrapDNSTransport(&mocks.DNSTransport{
|
|
|
|
MockRoundTrip: func(ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
MockNetwork: func() string {
|
|
|
|
return "fake"
|
|
|
|
},
|
|
|
|
MockAddress: func() string {
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
})
|
|
|
|
rawQuery := []byte{0xde, 0xad, 0xbe, 0xef}
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockBytes: func() ([]byte, error) {
|
|
|
|
return rawQuery, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
reply, err := txp.RoundTrip(context.Background(), query)
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if reply != nil {
|
|
|
|
t.Fatal("expected nil reply here")
|
|
|
|
}
|
|
|
|
ev := saver.Read()
|
|
|
|
if len(ev) != 2 {
|
|
|
|
t.Fatal("expected number of events")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(ev[0].Value().DNSQuery, rawQuery) {
|
|
|
|
t.Fatal("unexpected DNSQuery")
|
|
|
|
}
|
|
|
|
if ev[0].Name() != "dns_round_trip_start" {
|
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
|
|
|
if !ev[0].Value().Time.Before(time.Now()) {
|
|
|
|
t.Fatal("the saved time is wrong")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(ev[1].Value().DNSQuery, rawQuery) {
|
|
|
|
t.Fatal("unexpected DNSQuery")
|
|
|
|
}
|
|
|
|
if ev[1].Value().DNSResponse != nil {
|
|
|
|
t.Fatal("unexpected DNSReply")
|
|
|
|
}
|
|
|
|
if ev[1].Value().Duration <= 0 {
|
|
|
|
t.Fatal("unexpected Duration")
|
|
|
|
}
|
|
|
|
if ev[1].Value().Err != netxlite.FailureDNSNXDOMAINError {
|
|
|
|
t.Fatal("unexpected Err")
|
|
|
|
}
|
|
|
|
if ev[1].Name() != "dns_round_trip_done" {
|
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
|
|
|
if !ev[1].Value().Time.After(ev[0].Value().Time) {
|
|
|
|
t.Fatal("the saved time is wrong")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("on success", func(t *testing.T) {
|
|
|
|
expected := []byte{0xef, 0xbe, 0xad, 0xde}
|
|
|
|
saver := &Saver{}
|
|
|
|
response := &mocks.DNSResponse{
|
|
|
|
MockBytes: func() []byte {
|
|
|
|
return expected
|
|
|
|
},
|
|
|
|
}
|
|
|
|
txp := saver.WrapDNSTransport(&mocks.DNSTransport{
|
|
|
|
MockRoundTrip: func(ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
|
|
|
return response, nil
|
|
|
|
},
|
|
|
|
MockNetwork: func() string {
|
|
|
|
return "fake"
|
|
|
|
},
|
|
|
|
MockAddress: func() string {
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
})
|
|
|
|
rawQuery := []byte{0xde, 0xad, 0xbe, 0xef}
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockBytes: func() ([]byte, error) {
|
|
|
|
return rawQuery, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
reply, err := txp.RoundTrip(context.Background(), query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("we expected nil error here")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(reply.Bytes(), expected) {
|
|
|
|
t.Fatal("expected another reply here")
|
|
|
|
}
|
|
|
|
ev := saver.Read()
|
|
|
|
if len(ev) != 2 {
|
|
|
|
t.Fatal("expected number of events")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(ev[0].Value().DNSQuery, rawQuery) {
|
|
|
|
t.Fatal("unexpected DNSQuery")
|
|
|
|
}
|
|
|
|
if ev[0].Name() != "dns_round_trip_start" {
|
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
|
|
|
if !ev[0].Value().Time.Before(time.Now()) {
|
|
|
|
t.Fatal("the saved time is wrong")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(ev[1].Value().DNSQuery, rawQuery) {
|
|
|
|
t.Fatal("unexpected DNSQuery")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(ev[1].Value().DNSResponse, expected) {
|
|
|
|
t.Fatal("unexpected DNSReply")
|
|
|
|
}
|
|
|
|
if ev[1].Value().Duration <= 0 {
|
|
|
|
t.Fatal("unexpected Duration")
|
|
|
|
}
|
|
|
|
if ev[1].Value().Err.IsNotNil() {
|
|
|
|
t.Fatal("unexpected Err")
|
|
|
|
}
|
|
|
|
if ev[1].Name() != "dns_round_trip_done" {
|
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
|
|
|
if !ev[1].Value().Time.After(ev[0].Value().Time) {
|
|
|
|
t.Fatal("the saved time is wrong")
|
|
|
|
}
|
|
|
|
})
|
2022-08-27 15:47:48 +02:00
|
|
|
|
|
|
|
t.Run("with getaddrinfo transport there's correct .Network remapping", func(t *testing.T) {
|
|
|
|
saver := &Saver{}
|
|
|
|
reso := saver.WrapDNSTransport(netxlite.NewDNSOverGetaddrinfoTransport())
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel() // immediately fail the operation
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockBytes: func() ([]byte, error) {
|
|
|
|
return []byte{}, nil
|
|
|
|
},
|
|
|
|
MockType: func() uint16 {
|
|
|
|
return dns.TypeANY
|
|
|
|
},
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return 1453
|
|
|
|
},
|
|
|
|
MockDomain: func() string {
|
|
|
|
return "dns.google"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, _ = reso.RoundTrip(ctx, query)
|
|
|
|
// basically, we just want to ensure that the engine name is converted
|
|
|
|
ev := saver.Read()
|
|
|
|
if len(ev) != 2 {
|
|
|
|
t.Fatal("expected number of events")
|
|
|
|
}
|
|
|
|
if ev[0].Value().Proto != netxlite.StdlibResolverSystem {
|
|
|
|
t.Fatal("unexpected Proto")
|
|
|
|
}
|
|
|
|
if ev[1].Value().Proto != netxlite.StdlibResolverSystem {
|
|
|
|
t.Fatal("unexpected Proto")
|
|
|
|
}
|
|
|
|
})
|
2022-06-04 14:58:48 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Network", func(t *testing.T) {
|
2022-08-27 15:47:48 +02:00
|
|
|
t.Run("with custom child transport", func(t *testing.T) {
|
|
|
|
saver := &Saver{}
|
|
|
|
child := &mocks.DNSTransport{
|
|
|
|
MockNetwork: func() string {
|
|
|
|
return "x"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
txp := saver.WrapDNSTransport(child)
|
|
|
|
if txp.Network() != "x" {
|
|
|
|
t.Fatal("unexpected result")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("when using the stdlib resolver", func(t *testing.T) {
|
|
|
|
child := netxlite.NewDNSOverGetaddrinfoTransport()
|
|
|
|
switch network := child.Network(); network {
|
|
|
|
case netxlite.StdlibResolverGetaddrinfo,
|
|
|
|
netxlite.StdlibResolverGolangNetResolver:
|
|
|
|
// ok
|
|
|
|
default:
|
|
|
|
t.Fatal("unexpected child resolver network", network)
|
|
|
|
}
|
|
|
|
saver := &Saver{}
|
|
|
|
reso := saver.WrapDNSTransport(child)
|
|
|
|
if network := reso.Network(); network != netxlite.StdlibResolverSystem {
|
|
|
|
t.Fatal("unexpected wrapped resolver network", network)
|
|
|
|
}
|
|
|
|
})
|
2022-06-01 07:44:54 +02:00
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-06-04 14:58:48 +02:00
|
|
|
t.Run("Address", func(t *testing.T) {
|
2022-06-01 23:15:47 +02:00
|
|
|
saver := &Saver{}
|
2022-06-04 14:58:48 +02:00
|
|
|
child := &mocks.DNSTransport{
|
2022-06-01 23:15:47 +02:00
|
|
|
MockAddress: func() string {
|
2022-06-04 14:58:48 +02:00
|
|
|
return "x"
|
2022-06-01 23:15:47 +02:00
|
|
|
},
|
|
|
|
}
|
2022-06-04 14:58:48 +02:00
|
|
|
txp := saver.WrapDNSTransport(child)
|
|
|
|
if txp.Address() != "x" {
|
|
|
|
t.Fatal("unexpected result")
|
2022-06-01 23:15:47 +02:00
|
|
|
}
|
2022-06-04 14:58:48 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("CloseIdleConnections", func(t *testing.T) {
|
|
|
|
var called bool
|
|
|
|
saver := &Saver{}
|
|
|
|
child := &mocks.DNSTransport{
|
|
|
|
MockCloseIdleConnections: func() {
|
|
|
|
called = true
|
|
|
|
},
|
2022-06-01 23:15:47 +02:00
|
|
|
}
|
2022-06-04 14:58:48 +02:00
|
|
|
txp := saver.WrapDNSTransport(child)
|
|
|
|
txp.CloseIdleConnections()
|
|
|
|
if !called {
|
|
|
|
t.Fatal("not called")
|
2022-06-01 23:15:47 +02:00
|
|
|
}
|
2022-06-04 14:58:48 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("RequiresPadding", func(t *testing.T) {
|
|
|
|
saver := &Saver{}
|
|
|
|
child := &mocks.DNSTransport{
|
|
|
|
MockRequiresPadding: func() bool {
|
|
|
|
return true
|
|
|
|
},
|
2022-06-01 23:15:47 +02:00
|
|
|
}
|
2022-06-04 14:58:48 +02:00
|
|
|
txp := saver.WrapDNSTransport(child)
|
|
|
|
if !txp.RequiresPadding() {
|
|
|
|
t.Fatal("unexpected result")
|
2022-06-01 23:15:47 +02:00
|
|
|
}
|
2022-06-01 07:44:54 +02:00
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2022-05-31 21:53:01 +02:00
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func newFakeResolverWithExplicitError(err error) model.Resolver {
|
2022-05-31 21:53:01 +02:00
|
|
|
runtimex.PanicIfNil(err, "passed nil error")
|
|
|
|
return &mocks.Resolver{
|
|
|
|
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
|
|
|
return nil, err
|
|
|
|
},
|
|
|
|
MockNetwork: func() string {
|
|
|
|
return "fake"
|
|
|
|
},
|
|
|
|
MockAddress: func() string {
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
MockCloseIdleConnections: func() {
|
|
|
|
// nothing
|
|
|
|
},
|
|
|
|
MockLookupHTTPS: func(ctx context.Context, domain string) (*model.HTTPSSvc, error) {
|
|
|
|
return nil, errors.New("not implemented")
|
|
|
|
},
|
|
|
|
MockLookupNS: func(ctx context.Context, domain string) ([]*net.NS, error) {
|
|
|
|
return nil, errors.New("not implemented")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func newFakeResolverWithResult(r []string) model.Resolver {
|
2022-05-31 21:53:01 +02:00
|
|
|
return &mocks.Resolver{
|
|
|
|
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
|
|
|
return r, nil
|
|
|
|
},
|
|
|
|
MockNetwork: func() string {
|
|
|
|
return "fake"
|
|
|
|
},
|
|
|
|
MockAddress: func() string {
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
MockCloseIdleConnections: func() {
|
|
|
|
// nothing
|
|
|
|
},
|
|
|
|
MockLookupHTTPS: func(ctx context.Context, domain string) (*model.HTTPSSvc, error) {
|
|
|
|
return nil, errors.New("not implemented")
|
|
|
|
},
|
|
|
|
MockLookupNS: func(ctx context.Context, domain string) ([]*net.NS, error) {
|
|
|
|
return nil, errors.New("not implemented")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-08-27 15:47:48 +02:00
|
|
|
|
|
|
|
func TestResolverNetworkAdaptNames(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
input string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
}{{
|
|
|
|
name: "with StdlibResolverGetaddrinfo",
|
|
|
|
args: args{
|
|
|
|
input: netxlite.StdlibResolverGetaddrinfo,
|
|
|
|
},
|
|
|
|
want: netxlite.StdlibResolverSystem,
|
|
|
|
}, {
|
|
|
|
name: "with StdlibResolverGolangNetResolver",
|
|
|
|
args: args{
|
|
|
|
input: netxlite.StdlibResolverGolangNetResolver,
|
|
|
|
},
|
|
|
|
want: netxlite.StdlibResolverSystem,
|
|
|
|
}, {
|
|
|
|
name: "with StdlibResolverSystem",
|
|
|
|
args: args{
|
|
|
|
input: netxlite.StdlibResolverSystem,
|
|
|
|
},
|
|
|
|
want: netxlite.StdlibResolverSystem,
|
|
|
|
}, {
|
|
|
|
name: "with any other name",
|
|
|
|
args: args{
|
|
|
|
input: "doh",
|
|
|
|
},
|
|
|
|
want: "doh",
|
|
|
|
}}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := ResolverNetworkAdaptNames(tt.args.input); got != tt.want {
|
|
|
|
t.Errorf("ResolverNetworkAdaptNames() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|