refactor: move ErrorWrapperResolver to errorsx pkg (#419)
Part of https://github.com/ooni/probe/issues/1505
This commit is contained in:
parent
863899469e
commit
5c52d99d57
8 changed files with 144 additions and 81 deletions
80
internal/errorsx/resolver_test.go
Normal file
80
internal/errorsx/resolver_test.go
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
package errorsx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/netxmocks"
|
||||
)
|
||||
|
||||
func TestErrorWrapperResolverSuccess(t *testing.T) {
|
||||
orig := []string{"8.8.8.8"}
|
||||
r := &ErrorWrapperResolver{
|
||||
Resolver: &netxmocks.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: &netxmocks.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 *ErrWrapper
|
||||
if !errors.As(err, &errWrapper) {
|
||||
t.Fatal("cannot properly cast the returned error")
|
||||
}
|
||||
if errWrapper.Failure != FailureDNSNXDOMAINError {
|
||||
t.Fatal("unexpected failure")
|
||||
}
|
||||
if errWrapper.Operation != ResolveOperation {
|
||||
t.Fatal("unexpected Operation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorWrapperResolverChildNetworkAddress(t *testing.T) {
|
||||
r := &ErrorWrapperResolver{Resolver: &netxmocks.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")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue