refactor(netx): use netxlite for AddressResolver (#500)
Part of https://github.com/ooni/probe/issues/1591.
This commit is contained in:
parent
254a5511e9
commit
5ab3c3b689
|
@ -118,7 +118,9 @@ func NewResolver(config Config) Resolver {
|
||||||
config.BaseResolver = &netxlite.ResolverSystem{}
|
config.BaseResolver = &netxlite.ResolverSystem{}
|
||||||
}
|
}
|
||||||
var r Resolver = config.BaseResolver
|
var r Resolver = config.BaseResolver
|
||||||
r = resolver.AddressResolver{Resolver: r}
|
r = &resolver.AddressResolver{
|
||||||
|
Resolver: netxlite.NewResolverLegacyAdapter(r),
|
||||||
|
}
|
||||||
if config.CacheResolutions {
|
if config.CacheResolutions {
|
||||||
r = &resolver.CacheResolver{Resolver: r}
|
r = &resolver.CacheResolver{Resolver: r}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,11 +35,15 @@ func TestNewResolverVanilla(t *testing.T) {
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
ar, ok := ewr.Resolver.(resolver.AddressResolver)
|
ar, ok := ewr.Resolver.(*resolver.AddressResolver)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
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 {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
|
@ -63,11 +67,15 @@ func TestNewResolverSpecificResolver(t *testing.T) {
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
ar, ok := ewr.Resolver.(resolver.AddressResolver)
|
ar, ok := ewr.Resolver.(*resolver.AddressResolver)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
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 {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
|
@ -93,11 +101,15 @@ func TestNewResolverWithBogonFilter(t *testing.T) {
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
ar, ok := br.Resolver.(resolver.AddressResolver)
|
ar, ok := br.Resolver.(*resolver.AddressResolver)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
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 {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
|
@ -138,11 +150,15 @@ func TestNewResolverWithLogging(t *testing.T) {
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("not the resolver we expected %T", rla.ResolverLegacy)
|
t.Fatalf("not the resolver we expected %T", rla.ResolverLegacy)
|
||||||
}
|
}
|
||||||
ar, ok := ewr.Resolver.(resolver.AddressResolver)
|
ar, ok := ewr.Resolver.(*resolver.AddressResolver)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
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 {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
|
@ -172,11 +188,15 @@ func TestNewResolverWithSaver(t *testing.T) {
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
ar, ok := ewr.Resolver.(resolver.AddressResolver)
|
ar, ok := ewr.Resolver.(*resolver.AddressResolver)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
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 {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
|
@ -205,11 +225,15 @@ func TestNewResolverWithReadWriteCache(t *testing.T) {
|
||||||
if cr.ReadOnly != false {
|
if cr.ReadOnly != false {
|
||||||
t.Fatal("expected readwrite cache here")
|
t.Fatal("expected readwrite cache here")
|
||||||
}
|
}
|
||||||
ar, ok := cr.Resolver.(resolver.AddressResolver)
|
ar, ok := cr.Resolver.(*resolver.AddressResolver)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
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 {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
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" {
|
if cr.Get("dns.google.com")[0] != "8.8.8.8" {
|
||||||
t.Fatal("cache not correctly prefilled")
|
t.Fatal("cache not correctly prefilled")
|
||||||
}
|
}
|
||||||
ar, ok := cr.Resolver.(resolver.AddressResolver)
|
ar, ok := cr.Resolver.(*resolver.AddressResolver)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
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 {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,9 @@
|
||||||
package resolver
|
package resolver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||||
"net"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddressResolver is a resolver that knows how to correctly
|
// AddressResolver is a resolver that knows how to correctly
|
||||||
// resolve IP addresses to themselves.
|
// resolve IP addresses to themselves.
|
||||||
type AddressResolver struct {
|
type AddressResolver = netxlite.AddressResolver
|
||||||
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{}
|
|
||||||
|
|
|
@ -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")
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -33,6 +33,7 @@ type (
|
||||||
DialerSystem = dialerSystem
|
DialerSystem = dialerSystem
|
||||||
TLSDialerLegacy = tlsDialer
|
TLSDialerLegacy = tlsDialer
|
||||||
UserAgentTransport = httpUserAgentTransport
|
UserAgentTransport = httpUserAgentTransport
|
||||||
|
AddressResolver = resolverShortCircuitIPAddr
|
||||||
)
|
)
|
||||||
|
|
||||||
// ResolverLegacy performs domain name resolutions.
|
// ResolverLegacy performs domain name resolutions.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user