273b70bacc
## Checklist - [x] I have read the [contribution guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md) - [x] reference issue for this pull request: https://github.com/ooni/probe/issues/1885 - [x] related ooni/spec pull request: N/A Location of the issue tracker: https://github.com/ooni/probe ## Description This PR contains a set of changes to move important interfaces and data types into the `./internal/model` package. The criteria for including an interface or data type in here is roughly that the type should be important and used by several packages. We are especially interested to move more interfaces here to increase modularity. An additional side effect is that, by reading this package, one should be able to understand more quickly how different parts of the codebase interact with each other. This is what I want to move in `internal/model`: - [x] most important interfaces from `internal/netxlite` - [x] everything that was previously part of `internal/engine/model` - [x] mocks from `internal/netxlite/mocks` should also be moved in here as a subpackage
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
package errorsx
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
|
"github.com/ooni/probe-cli/v3/internal/model/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 *netxlite.ErrWrapper
|
|
if !errors.As(err, &errWrapper) {
|
|
t.Fatal("cannot properly cast the returned error")
|
|
}
|
|
if errWrapper.Failure != netxlite.FailureDNSNXDOMAINError {
|
|
t.Fatal("unexpected failure")
|
|
}
|
|
if errWrapper.Operation != netxlite.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")
|
|
}
|
|
}
|