ooni-probe-cli/internal/engine/netx/resolver/integration_test.go
Simone Basso a3654f60b7
refactor(netxlite): add more functions to resolver (#455)
We would like to refactor the code so that a DoH resolver owns the
connections of its underlying HTTP client.

To do that, we need first to incorporate CloseIdleConnections
into the Resolver model. Then, we need to add the same function
to all netxlite types that wrap a Resolver type.

At the same time, we want the rest of the code for now to continue
with the simpler definition of a Resolver, now called ResolverLegacy.

We will eventually propagate this change to the rest of the tree
and simplify the way in which we manage Resolvers.

To make this possible, we introduce a new factory function that
adapts a ResolverLegacy to become a Resolver.

See https://github.com/ooni/probe/issues/1591.
2021-09-05 18:03:50 +02:00

119 lines
3.1 KiB
Go

package resolver_test
import (
"context"
"net"
"net/http"
"testing"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func init() {
log.SetLevel(log.DebugLevel)
}
func testresolverquick(t *testing.T, reso resolver.Resolver) {
if testing.Short() {
t.Skip("skip test in short mode")
}
reso = &netxlite.ResolverLogger{
Logger: log.Log,
Resolver: netxlite.NewResolverLegacyAdapter(reso),
}
addrs, err := reso.LookupHost(context.Background(), "dns.google.com")
if err != nil {
t.Fatal(err)
}
if addrs == nil {
t.Fatal("expected non-nil addrs here")
}
var foundquad8 bool
for _, addr := range addrs {
// See https://github.com/ooni/probe-cli/v3/internal/engine/pull/954/checks?check_run_id=1182269025
if addr == "8.8.8.8" || addr == "2001:4860:4860::8888" {
foundquad8 = true
}
}
if !foundquad8 {
t.Fatalf("did not find 8.8.8.8 in output; output=%+v", addrs)
}
}
// Ensuring we can handle Internationalized Domain Names (IDNs) without issues
func testresolverquickidna(t *testing.T, reso resolver.Resolver) {
if testing.Short() {
t.Skip("skip test in short mode")
}
reso = &resolver.IDNAResolver{
Resolver: &netxlite.ResolverLogger{
Logger: log.Log,
Resolver: netxlite.NewResolverLegacyAdapter(reso),
},
}
addrs, err := reso.LookupHost(context.Background(), "яндекс.рф")
if err != nil {
t.Fatal(err)
}
if addrs == nil {
t.Fatal("expected non-nil addrs here")
}
}
func TestNewResolverSystem(t *testing.T) {
reso := &netxlite.ResolverSystem{}
testresolverquick(t, reso)
testresolverquickidna(t, reso)
}
func TestNewResolverUDPAddress(t *testing.T) {
reso := resolver.NewSerialResolver(
resolver.NewDNSOverUDP(new(net.Dialer), "8.8.8.8:53"))
testresolverquick(t, reso)
testresolverquickidna(t, reso)
}
func TestNewResolverUDPDomain(t *testing.T) {
reso := resolver.NewSerialResolver(
resolver.NewDNSOverUDP(new(net.Dialer), "dns.google.com:53"))
testresolverquick(t, reso)
testresolverquickidna(t, reso)
}
func TestNewResolverTCPAddress(t *testing.T) {
reso := resolver.NewSerialResolver(
resolver.NewDNSOverTCP(new(net.Dialer).DialContext, "8.8.8.8:53"))
testresolverquick(t, reso)
testresolverquickidna(t, reso)
}
func TestNewResolverTCPDomain(t *testing.T) {
reso := resolver.NewSerialResolver(
resolver.NewDNSOverTCP(new(net.Dialer).DialContext, "dns.google.com:53"))
testresolverquick(t, reso)
testresolverquickidna(t, reso)
}
func TestNewResolverDoTAddress(t *testing.T) {
reso := resolver.NewSerialResolver(
resolver.NewDNSOverTLS(resolver.DialTLSContext, "8.8.8.8:853"))
testresolverquick(t, reso)
testresolverquickidna(t, reso)
}
func TestNewResolverDoTDomain(t *testing.T) {
reso := resolver.NewSerialResolver(
resolver.NewDNSOverTLS(resolver.DialTLSContext, "dns.google.com:853"))
testresolverquick(t, reso)
testresolverquickidna(t, reso)
}
func TestNewResolverDoH(t *testing.T) {
reso := resolver.NewSerialResolver(
resolver.NewDNSOverHTTPS(http.DefaultClient, "https://cloudflare-dns.com/dns-query"))
testresolverquick(t, reso)
testresolverquickidna(t, reso)
}