ooni-probe-cli/internal/errorsx/resolver_test.go
Simone Basso 83440cf110
refactor: split errorsx in good and legacy (#477)
The legacy part for now is internal/errorsx. It will stay there until
I figure out whether it also needs some extra bug fixing.

The good part is now in internal/netxlite/errorsx and contains all the
logic for mapping errors. We need to further improve upon this logic
by writing more thorough integration tests for QUIC.

We also need to copy the various dialer, conn, etc adapters that set
errors. We will put them inside netxlite and we will generate errors in
a way that is less crazy with respect to the major operation. (The
idea is to always wrap, given that now we measure in an incremental way
and we don't measure every operation together.)

Part of https://github.com/ooni/probe/issues/1591
2021-09-07 17:09:30 +02:00

82 lines
1.9 KiB
Go

package errorsx
import (
"context"
"errors"
"net"
"testing"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
)
func TestErrorWrapperResolverSuccess(t *testing.T) {
orig := []string{"8.8.8.8"}
r := &ErrorWrapperResolver{
Resolver: &mocks.Resolver{
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
return orig, nil
},
},
}
addrs, err := r.LookupHost(context.Background(), "dns.google.com")
if err != nil {
t.Fatal(err)
}
if len(addrs) != len(orig) || addrs[0] != orig[0] {
t.Fatal("not the result we expected")
}
}
func TestErrorWrapperResolverFailure(t *testing.T) {
r := &ErrorWrapperResolver{
Resolver: &mocks.Resolver{
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
return nil, errors.New("no such host")
},
},
}
ctx := context.Background()
addrs, err := r.LookupHost(ctx, "dns.google.com")
if addrs != nil {
t.Fatal("expected nil addr here")
}
var errWrapper *errorsx.ErrWrapper
if !errors.As(err, &errWrapper) {
t.Fatal("cannot properly cast the returned error")
}
if errWrapper.Failure != errorsx.FailureDNSNXDOMAINError {
t.Fatal("unexpected failure")
}
if errWrapper.Operation != errorsx.ResolveOperation {
t.Fatal("unexpected Operation")
}
}
func TestErrorWrapperResolverChildNetworkAddress(t *testing.T) {
r := &ErrorWrapperResolver{Resolver: &mocks.Resolver{
MockNetwork: func() string {
return "udp"
},
MockAddress: func() string {
return "8.8.8.8:53"
},
}}
if r.Network() != "udp" {
t.Fatal("invalid Network")
}
if r.Address() != "8.8.8.8:53" {
t.Fatal("invalid Address")
}
}
func TestErrorWrapperResolverNoChildNetworkAddress(t *testing.T) {
r := &ErrorWrapperResolver{Resolver: &net.Resolver{}}
if r.Network() != "errorWrapper" {
t.Fatal("invalid Network")
}
if r.Address() != "" {
t.Fatal("invalid Address")
}
}