8a0beee808
What do I mean by pivoting? Netx is currently organized by row: ``` | dialer | quicdialer | resolver | ... saving | | | | ... errorwrapping | | | | ... logging | | | | ... mocking/sys | | | | ... ``` Every row needs to implement saving, errorwrapping, logging, mocking (or adapting to the system or to some underlying library). This causes cross package dependencies and, in turn, complexity. For example, we need the `trace` package for supporting saving. And `dialer`, `quickdialer`, et al. need to depend on such a package. The same goes for errorwrapping. This arrangement further complicates testing. For example, I am currently working on https://github.com/ooni/probe/issues/1505 and I realize it need to repeat integration tests in multiple places. Let's say instead we pivot the above matrix as follows: ``` | saving | errorwrapping | logging | ... dialer | | | | ... quicdialer | | | | ... logging | | | | ... mocking/sys | | | | ... ... ``` In this way, now every row contains everything related to a specific action to perform. We can now share code without relying on extra support packages. What's more, we can write tests and, judding from the way in which things are made, it seems we only need integration testing in `errorwrapping` because it's where data quality matters whereas, in all other cases, unit testing is fine. I am going, therefore, to proceed with these changes and "pivot" `netx`. Hopefully, it won't be too painful.
162 lines
4.1 KiB
Go
162 lines
4.1 KiB
Go
package netxlite
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/apex/log"
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/mockablex"
|
|
)
|
|
|
|
func TestDialerResolverNoPort(t *testing.T) {
|
|
dialer := &DialerResolver{Dialer: new(net.Dialer), Resolver: DefaultResolver}
|
|
conn, err := dialer.DialContext(context.Background(), "tcp", "antani.ooni.nu")
|
|
if err == nil {
|
|
t.Fatal("expected an error here")
|
|
}
|
|
if conn != nil {
|
|
t.Fatal("expected a nil conn here")
|
|
}
|
|
}
|
|
|
|
func TestDialerResolverLookupHostAddress(t *testing.T) {
|
|
dialer := &DialerResolver{Dialer: new(net.Dialer), Resolver: MockableResolver{
|
|
Err: errors.New("mocked error"),
|
|
}}
|
|
addrs, err := dialer.lookupHost(context.Background(), "1.1.1.1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(addrs) != 1 || addrs[0] != "1.1.1.1" {
|
|
t.Fatal("not the result we expected")
|
|
}
|
|
}
|
|
|
|
func TestDialerResolverLookupHostFailure(t *testing.T) {
|
|
expected := errors.New("mocked error")
|
|
dialer := &DialerResolver{Dialer: new(net.Dialer), Resolver: MockableResolver{
|
|
Err: expected,
|
|
}}
|
|
conn, err := dialer.DialContext(context.Background(), "tcp", "dns.google.com:853")
|
|
if !errors.Is(err, expected) {
|
|
t.Fatal("not the error we expected")
|
|
}
|
|
if conn != nil {
|
|
t.Fatal("expected nil conn")
|
|
}
|
|
}
|
|
|
|
type MockableResolver struct {
|
|
Addresses []string
|
|
Err error
|
|
}
|
|
|
|
func (r MockableResolver) LookupHost(ctx context.Context, host string) ([]string, error) {
|
|
return r.Addresses, r.Err
|
|
}
|
|
|
|
func (r MockableResolver) Network() string {
|
|
return "mockable"
|
|
}
|
|
|
|
func (r MockableResolver) Address() string {
|
|
return ""
|
|
}
|
|
|
|
func TestDialerResolverDialForSingleIPFails(t *testing.T) {
|
|
dialer := &DialerResolver{Dialer: mockablex.Dialer{
|
|
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
|
return nil, io.EOF
|
|
},
|
|
}, Resolver: DefaultResolver}
|
|
conn, err := dialer.DialContext(context.Background(), "tcp", "1.1.1.1:853")
|
|
if !errors.Is(err, io.EOF) {
|
|
t.Fatal("not the error we expected")
|
|
}
|
|
if conn != nil {
|
|
t.Fatal("expected nil conn")
|
|
}
|
|
}
|
|
|
|
func TestDialerResolverDialForManyIPFails(t *testing.T) {
|
|
dialer := &DialerResolver{
|
|
Dialer: mockablex.Dialer{
|
|
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
|
return nil, io.EOF
|
|
},
|
|
}, Resolver: MockableResolver{
|
|
Addresses: []string{"1.1.1.1", "8.8.8.8"},
|
|
}}
|
|
conn, err := dialer.DialContext(context.Background(), "tcp", "dot.dns:853")
|
|
if !errors.Is(err, io.EOF) {
|
|
t.Fatal("not the error we expected")
|
|
}
|
|
if conn != nil {
|
|
t.Fatal("expected nil conn")
|
|
}
|
|
}
|
|
|
|
func TestDialerResolverDialForManyIPSuccess(t *testing.T) {
|
|
dialer := &DialerResolver{Dialer: mockablex.Dialer{
|
|
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
|
return &mockablex.Conn{
|
|
MockClose: func() error {
|
|
return nil
|
|
},
|
|
}, nil
|
|
},
|
|
}, Resolver: MockableResolver{
|
|
Addresses: []string{"1.1.1.1", "8.8.8.8"},
|
|
}}
|
|
conn, err := dialer.DialContext(context.Background(), "tcp", "dot.dns:853")
|
|
if err != nil {
|
|
t.Fatal("expected nil error here")
|
|
}
|
|
if conn == nil {
|
|
t.Fatal("expected non-nil conn")
|
|
}
|
|
conn.Close()
|
|
}
|
|
|
|
func TestDialerLoggerFailure(t *testing.T) {
|
|
d := &DialerLogger{
|
|
Dialer: mockablex.Dialer{
|
|
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
|
return nil, io.EOF
|
|
},
|
|
},
|
|
Logger: log.Log,
|
|
}
|
|
conn, err := d.DialContext(context.Background(), "tcp", "www.google.com:443")
|
|
if !errors.Is(err, io.EOF) {
|
|
t.Fatal("not the error we expected")
|
|
}
|
|
if conn != nil {
|
|
t.Fatal("expected nil conn here")
|
|
}
|
|
}
|
|
|
|
func TestDefaultDialerWorks(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel() // fail immediately
|
|
conn, err := DefaultDialer.DialContext(ctx, "tcp", "8.8.8.8:853")
|
|
if err == nil || !strings.HasSuffix(err.Error(), "operation was canceled") {
|
|
t.Fatal("not the error we expected", err)
|
|
}
|
|
if conn != nil {
|
|
t.Fatal("expected nil conn here")
|
|
}
|
|
}
|
|
|
|
func TestDefaultDialerHasTimeout(t *testing.T) {
|
|
expected := 15 * time.Second
|
|
if DefaultDialer.Timeout != expected {
|
|
t.Fatal("unexpected timeout value")
|
|
}
|
|
}
|