ooni-probe-cli/internal/netxlite/resolver_test.go

154 lines
3.6 KiB
Go
Raw Normal View History

2021-06-23 15:53:12 +02:00
package netxlite
import (
"context"
"errors"
2021-06-23 15:53:12 +02:00
"net"
"strings"
2021-06-23 15:53:12 +02:00
"testing"
"github.com/apex/log"
"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/netxmocks"
2021-06-23 15:53:12 +02:00
)
func TestResolverSystemNetworkAddress(t *testing.T) {
2021-06-23 15:53:12 +02:00
r := ResolverSystem{}
if r.Network() != "system" {
t.Fatal("invalid Network")
}
if r.Address() != "" {
t.Fatal("invalid Address")
}
}
func TestResolverSystemWorksAsIntended(t *testing.T) {
r := ResolverSystem{}
2021-06-23 15:53:12 +02:00
addrs, err := r.LookupHost(context.Background(), "dns.google.com")
if err != nil {
t.Fatal(err)
}
if addrs == nil {
t.Fatal("expected non-nil result here")
}
}
func TestResolverLoggerWithSuccess(t *testing.T) {
expected := []string{"1.1.1.1"}
r := ResolverLogger{
Logger: log.Log,
Resolver: &netxmocks.Resolver{
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
return expected, nil
},
},
}
addrs, err := r.LookupHost(context.Background(), "dns.google")
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(expected, addrs); diff != "" {
t.Fatal(diff)
}
}
2021-06-23 15:53:12 +02:00
func TestResolverLoggerWithFailure(t *testing.T) {
expected := errors.New("mocked error")
2021-06-23 15:53:12 +02:00
r := ResolverLogger{
Logger: log.Log,
Resolver: &netxmocks.Resolver{
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
return nil, expected
},
},
2021-06-23 15:53:12 +02:00
}
addrs, err := r.LookupHost(context.Background(), "dns.google")
if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err)
}
if addrs != nil {
t.Fatal("expected nil addr here")
}
}
func TestResolverLoggerChildNetworkAddress(t *testing.T) {
r := &ResolverLogger{Logger: log.Log, Resolver: DefaultResolver}
2021-06-23 15:53:12 +02:00
if r.Network() != "system" {
t.Fatal("invalid Network")
}
if r.Address() != "" {
t.Fatal("invalid Address")
}
}
func TestResolverLoggerNoChildNetworkAddress(t *testing.T) {
2021-06-23 15:53:12 +02:00
r := &ResolverLogger{Logger: log.Log, Resolver: &net.Resolver{}}
if r.Network() != "logger" {
t.Fatal("invalid Network")
}
if r.Address() != "" {
t.Fatal("invalid Address")
}
}
func TestResolverIDNAWorksAsIntended(t *testing.T) {
expectedIPs := []string{"77.88.55.66"}
r := &ResolverIDNA{
Resolver: &netxmocks.Resolver{
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
if domain != "xn--d1acpjx3f.xn--p1ai" {
return nil, errors.New("passed invalid domain")
}
return expectedIPs, nil
},
},
}
ctx := context.Background()
addrs, err := r.LookupHost(ctx, "яндекс.рф")
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(expectedIPs, addrs); diff != "" {
t.Fatal(diff)
}
}
func TestIDNAResolverWithInvalidPunycode(t *testing.T) {
r := &ResolverIDNA{Resolver: &netxmocks.Resolver{
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
return nil, errors.New("should not happen")
},
}}
// See https://www.farsightsecurity.com/blog/txt-record/punycode-20180711/
ctx := context.Background()
addrs, err := r.LookupHost(ctx, "xn--0000h")
if err == nil || !strings.HasPrefix(err.Error(), "idna: invalid label") {
t.Fatal("not the error we expected")
}
if addrs != nil {
t.Fatal("expected no response here")
}
}
func TestResolverIDNAChildNetworkAddress(t *testing.T) {
r := &ResolverIDNA{
Resolver: DefaultResolver,
}
if v := r.Network(); v != "system" {
t.Fatal("invalid network", v)
}
if v := r.Address(); v != "" {
t.Fatal("invalid address", v)
}
}
func TestResolverIDNANoChildNetworkAddress(t *testing.T) {
r := &ResolverIDNA{}
if v := r.Network(); v != "idna" {
t.Fatal("invalid network", v)
}
if v := r.Address(); v != "" {
t.Fatal("invalid address", v)
}
}