refactor(netx): use netxlite for AddressResolver (#500)

Part of https://github.com/ooni/probe/issues/1591.
This commit is contained in:
Simone Basso 2021-09-09 20:21:43 +02:00 committed by GitHub
parent 254a5511e9
commit 5ab3c3b689
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 66 deletions

View File

@ -118,7 +118,9 @@ func NewResolver(config Config) Resolver {
config.BaseResolver = &netxlite.ResolverSystem{}
}
var r Resolver = config.BaseResolver
r = resolver.AddressResolver{Resolver: r}
r = &resolver.AddressResolver{
Resolver: netxlite.NewResolverLegacyAdapter(r),
}
if config.CacheResolutions {
r = &resolver.CacheResolver{Resolver: r}
}

View File

@ -35,11 +35,15 @@ func TestNewResolverVanilla(t *testing.T) {
if !ok {
t.Fatal("not the resolver we expected")
}
ar, ok := ewr.Resolver.(resolver.AddressResolver)
ar, ok := ewr.Resolver.(*resolver.AddressResolver)
if !ok {
t.Fatal("not the resolver we expected")
}
_, ok = ar.Resolver.(*netxlite.ResolverSystem)
arw, ok := ar.Resolver.(*netxlite.ResolverLegacyAdapter)
if !ok {
t.Fatal("not the resolver we expected")
}
_, ok = arw.ResolverLegacy.(*netxlite.ResolverSystem)
if !ok {
t.Fatal("not the resolver we expected")
}
@ -63,11 +67,15 @@ func TestNewResolverSpecificResolver(t *testing.T) {
if !ok {
t.Fatal("not the resolver we expected")
}
ar, ok := ewr.Resolver.(resolver.AddressResolver)
ar, ok := ewr.Resolver.(*resolver.AddressResolver)
if !ok {
t.Fatal("not the resolver we expected")
}
_, ok = ar.Resolver.(resolver.BogonResolver)
arw, ok := ar.Resolver.(*netxlite.ResolverLegacyAdapter)
if !ok {
t.Fatal("not the resolver we expected")
}
_, ok = arw.ResolverLegacy.(resolver.BogonResolver)
if !ok {
t.Fatal("not the resolver we expected")
}
@ -93,11 +101,15 @@ func TestNewResolverWithBogonFilter(t *testing.T) {
if !ok {
t.Fatal("not the resolver we expected")
}
ar, ok := br.Resolver.(resolver.AddressResolver)
ar, ok := br.Resolver.(*resolver.AddressResolver)
if !ok {
t.Fatal("not the resolver we expected")
}
_, ok = ar.Resolver.(*netxlite.ResolverSystem)
arw, ok := ar.Resolver.(*netxlite.ResolverLegacyAdapter)
if !ok {
t.Fatal("not the resolver we expected")
}
_, ok = arw.ResolverLegacy.(*netxlite.ResolverSystem)
if !ok {
t.Fatal("not the resolver we expected")
}
@ -138,11 +150,15 @@ func TestNewResolverWithLogging(t *testing.T) {
if !ok {
t.Fatalf("not the resolver we expected %T", rla.ResolverLegacy)
}
ar, ok := ewr.Resolver.(resolver.AddressResolver)
ar, ok := ewr.Resolver.(*resolver.AddressResolver)
if !ok {
t.Fatal("not the resolver we expected")
}
_, ok = ar.Resolver.(*netxlite.ResolverSystem)
arw, ok := ar.Resolver.(*netxlite.ResolverLegacyAdapter)
if !ok {
t.Fatal("not the resolver we expected")
}
_, ok = arw.ResolverLegacy.(*netxlite.ResolverSystem)
if !ok {
t.Fatal("not the resolver we expected")
}
@ -172,11 +188,15 @@ func TestNewResolverWithSaver(t *testing.T) {
if !ok {
t.Fatal("not the resolver we expected")
}
ar, ok := ewr.Resolver.(resolver.AddressResolver)
ar, ok := ewr.Resolver.(*resolver.AddressResolver)
if !ok {
t.Fatal("not the resolver we expected")
}
_, ok = ar.Resolver.(*netxlite.ResolverSystem)
arw, ok := ar.Resolver.(*netxlite.ResolverLegacyAdapter)
if !ok {
t.Fatal("not the resolver we expected")
}
_, ok = arw.ResolverLegacy.(*netxlite.ResolverSystem)
if !ok {
t.Fatal("not the resolver we expected")
}
@ -205,11 +225,15 @@ func TestNewResolverWithReadWriteCache(t *testing.T) {
if cr.ReadOnly != false {
t.Fatal("expected readwrite cache here")
}
ar, ok := cr.Resolver.(resolver.AddressResolver)
ar, ok := cr.Resolver.(*resolver.AddressResolver)
if !ok {
t.Fatal("not the resolver we expected")
}
_, ok = ar.Resolver.(*netxlite.ResolverSystem)
arw, ok := ar.Resolver.(*netxlite.ResolverLegacyAdapter)
if !ok {
t.Fatal("not the resolver we expected")
}
_, ok = arw.ResolverLegacy.(*netxlite.ResolverSystem)
if !ok {
t.Fatal("not the resolver we expected")
}
@ -243,11 +267,15 @@ func TestNewResolverWithPrefilledReadonlyCache(t *testing.T) {
if cr.Get("dns.google.com")[0] != "8.8.8.8" {
t.Fatal("cache not correctly prefilled")
}
ar, ok := cr.Resolver.(resolver.AddressResolver)
ar, ok := cr.Resolver.(*resolver.AddressResolver)
if !ok {
t.Fatal("not the resolver we expected")
}
_, ok = ar.Resolver.(*netxlite.ResolverSystem)
arw, ok := ar.Resolver.(*netxlite.ResolverLegacyAdapter)
if !ok {
t.Fatal("not the resolver we expected")
}
_, ok = arw.ResolverLegacy.(*netxlite.ResolverSystem)
if !ok {
t.Fatal("not the resolver we expected")
}

View File

@ -1,22 +1,9 @@
package resolver
import (
"context"
"net"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
// AddressResolver is a resolver that knows how to correctly
// resolve IP addresses to themselves.
type AddressResolver struct {
Resolver
}
// LookupHost implements Resolver.LookupHost
func (r AddressResolver) LookupHost(ctx context.Context, hostname string) ([]string, error) {
if net.ParseIP(hostname) != nil {
return []string{hostname}, nil
}
return r.Resolver.LookupHost(ctx, hostname)
}
var _ Resolver = AddressResolver{}
type AddressResolver = netxlite.AddressResolver

View File

@ -1,36 +0,0 @@
package resolver_test
import (
"context"
"errors"
"testing"
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
)
func TestAddressSuccess(t *testing.T) {
r := resolver.AddressResolver{}
addrs, err := r.LookupHost(context.Background(), "8.8.8.8")
if err != nil {
t.Fatal(err)
}
if len(addrs) != 1 || addrs[0] != "8.8.8.8" {
t.Fatal("not the result we expected")
}
}
func TestAddressFailure(t *testing.T) {
expected := errors.New("mocked error")
r := resolver.AddressResolver{
Resolver: resolver.FakeResolver{
Err: expected,
},
}
addrs, err := r.LookupHost(context.Background(), "dns.google.com")
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if addrs != nil {
t.Fatal("expected nil addrs")
}
}

View File

@ -33,6 +33,7 @@ type (
DialerSystem = dialerSystem
TLSDialerLegacy = tlsDialer
UserAgentTransport = httpUserAgentTransport
AddressResolver = resolverShortCircuitIPAddr
)
// ResolverLegacy performs domain name resolutions.