2e0118d1a6
## Description This PR continues the refactoring of `netx` under the following principles: 1. do not break the rest of the tree and do not engage in extensive tree-wide refactoring yet 2. move under `netxlite` clearly related subpackages (e.g., `iox`, `netxmocks`) 3. move into `internal/netxlite/internal` stuff that is clearly private of `netxlite` 4. hide implementation details in `netxlite` pending new factories 5. refactor `tls` code in `netxlite` to clearly separate `crypto/tls` code from `utls` code After each commit, I run `go test -short -race ./...` locally. Each individual commit explains what it does. I will squash, but this operation will preserve the original commit titles, so this will give further insight on each step. ## Commits * refactor: rename netxmocks -> netxlite/mocks Part of https://github.com/ooni/probe/issues/1591 * refactor: rename quicx -> netxlite/quicx See https://github.com/ooni/probe/issues/1591 * refactor: rename iox -> netxlite/iox Regenerate sources and make sure the tests pass. See https://github.com/ooni/probe/issues/1591. * refactor(iox): move MockableReader to netxlite/mocks See https://github.com/ooni/probe/issues/1591 * refactor(netxlite): generator is an implementation detail See https://github.com/ooni/probe/issues/1591 * refactor(netxlite): separate tls and utls code See https://github.com/ooni/probe/issues/1591 * refactor(netxlite): hide most types but keep old names as legacy With this change we avoid breaking the rest of the tree, but we start hiding some implementation details a bit. Factories will follow. See https://github.com/ooni/probe/issues/1591
189 lines
4.0 KiB
Go
189 lines
4.0 KiB
Go
package errorsx
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
|
|
)
|
|
|
|
func TestErrorWrapperDialerFailure(t *testing.T) {
|
|
ctx := context.Background()
|
|
d := &ErrorWrapperDialer{Dialer: &mocks.Dialer{
|
|
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
|
return nil, io.EOF
|
|
},
|
|
}}
|
|
conn, err := d.DialContext(ctx, "tcp", "www.google.com:443")
|
|
var ew *ErrWrapper
|
|
if !errors.As(err, &ew) {
|
|
t.Fatal("cannot convert to ErrWrapper")
|
|
}
|
|
if ew.Operation != ConnectOperation {
|
|
t.Fatal("unexpected operation", ew.Operation)
|
|
}
|
|
if ew.Failure != FailureEOFError {
|
|
t.Fatal("unexpected failure", ew.Failure)
|
|
}
|
|
if !errors.Is(ew.WrappedErr, io.EOF) {
|
|
t.Fatal("unexpected underlying error", ew.WrappedErr)
|
|
}
|
|
if conn != nil {
|
|
t.Fatal("expected a nil conn here")
|
|
}
|
|
}
|
|
|
|
func TestErrorWrapperDialerSuccess(t *testing.T) {
|
|
origConn := &net.TCPConn{}
|
|
ctx := context.Background()
|
|
d := &ErrorWrapperDialer{Dialer: &mocks.Dialer{
|
|
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
|
return origConn, nil
|
|
},
|
|
}}
|
|
conn, err := d.DialContext(ctx, "tcp", "www.google.com:443")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ewConn, ok := conn.(*errorWrapperConn)
|
|
if !ok {
|
|
t.Fatal("cannot cast to errorWrapperConn")
|
|
}
|
|
if ewConn.Conn != origConn {
|
|
t.Fatal("not the connection we expected")
|
|
}
|
|
}
|
|
|
|
func TestErrorWrapperConnReadFailure(t *testing.T) {
|
|
c := &errorWrapperConn{
|
|
Conn: &mocks.Conn{
|
|
MockRead: func(b []byte) (int, error) {
|
|
return 0, io.EOF
|
|
},
|
|
},
|
|
}
|
|
buf := make([]byte, 1024)
|
|
cnt, err := c.Read(buf)
|
|
var ew *ErrWrapper
|
|
if !errors.As(err, &ew) {
|
|
t.Fatal("cannot cast error to ErrWrapper")
|
|
}
|
|
if ew.Operation != ReadOperation {
|
|
t.Fatal("invalid operation", ew.Operation)
|
|
}
|
|
if ew.Failure != FailureEOFError {
|
|
t.Fatal("invalid failure", ew.Failure)
|
|
}
|
|
if !errors.Is(ew.WrappedErr, io.EOF) {
|
|
t.Fatal("invalid wrapped error", ew.WrappedErr)
|
|
}
|
|
if cnt != 0 {
|
|
t.Fatal("expected zero here", cnt)
|
|
}
|
|
}
|
|
|
|
func TestErrorWrapperConnReadSuccess(t *testing.T) {
|
|
c := &errorWrapperConn{
|
|
Conn: &mocks.Conn{
|
|
MockRead: func(b []byte) (int, error) {
|
|
return len(b), nil
|
|
},
|
|
},
|
|
}
|
|
buf := make([]byte, 1024)
|
|
cnt, err := c.Read(buf)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cnt != len(buf) {
|
|
t.Fatal("expected len(buf) here", cnt)
|
|
}
|
|
}
|
|
|
|
func TestErrorWrapperConnWriteFailure(t *testing.T) {
|
|
c := &errorWrapperConn{
|
|
Conn: &mocks.Conn{
|
|
MockWrite: func(b []byte) (int, error) {
|
|
return 0, io.EOF
|
|
},
|
|
},
|
|
}
|
|
buf := make([]byte, 1024)
|
|
cnt, err := c.Write(buf)
|
|
var ew *ErrWrapper
|
|
if !errors.As(err, &ew) {
|
|
t.Fatal("cannot cast error to ErrWrapper")
|
|
}
|
|
if ew.Operation != WriteOperation {
|
|
t.Fatal("invalid operation", ew.Operation)
|
|
}
|
|
if ew.Failure != FailureEOFError {
|
|
t.Fatal("invalid failure", ew.Failure)
|
|
}
|
|
if !errors.Is(ew.WrappedErr, io.EOF) {
|
|
t.Fatal("invalid wrapped error", ew.WrappedErr)
|
|
}
|
|
if cnt != 0 {
|
|
t.Fatal("expected zero here", cnt)
|
|
}
|
|
}
|
|
|
|
func TestErrorWrapperConnWriteSuccess(t *testing.T) {
|
|
c := &errorWrapperConn{
|
|
Conn: &mocks.Conn{
|
|
MockWrite: func(b []byte) (int, error) {
|
|
return len(b), nil
|
|
},
|
|
},
|
|
}
|
|
buf := make([]byte, 1024)
|
|
cnt, err := c.Write(buf)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cnt != len(buf) {
|
|
t.Fatal("expected len(buf) here", cnt)
|
|
}
|
|
}
|
|
|
|
func TestErrorWrapperConnCloseFailure(t *testing.T) {
|
|
c := &errorWrapperConn{
|
|
Conn: &mocks.Conn{
|
|
MockClose: func() error {
|
|
return io.EOF
|
|
},
|
|
},
|
|
}
|
|
err := c.Close()
|
|
var ew *ErrWrapper
|
|
if !errors.As(err, &ew) {
|
|
t.Fatal("cannot cast error to ErrWrapper")
|
|
}
|
|
if ew.Operation != CloseOperation {
|
|
t.Fatal("invalid operation", ew.Operation)
|
|
}
|
|
if ew.Failure != FailureEOFError {
|
|
t.Fatal("invalid failure", ew.Failure)
|
|
}
|
|
if !errors.Is(ew.WrappedErr, io.EOF) {
|
|
t.Fatal("invalid wrapped error", ew.WrappedErr)
|
|
}
|
|
}
|
|
|
|
func TestErrorWrapperConnCloseSuccess(t *testing.T) {
|
|
c := &errorWrapperConn{
|
|
Conn: &mocks.Conn{
|
|
MockClose: func() error {
|
|
return nil
|
|
},
|
|
},
|
|
}
|
|
err := c.Close()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|