refactor: merge dnsx and errorsx into netxlite (#517)

When preparing a tutorial for netxlite, I figured it is easier
to tell people "hey, this is the package you should use for all
low-level networking stuff" rather than introducing people to
a set of packages working together where some piece of functionality
is here and some other piece is there.

Part of https://github.com/ooni/probe/issues/1591
This commit is contained in:
Simone Basso
2021-09-28 12:42:01 +02:00
committed by GitHub
parent de130d249c
commit 6d3a4f1db8
169 changed files with 575 additions and 671 deletions
+9 -9
View File
@@ -4,7 +4,7 @@ import (
"context"
"net"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
// Dialer establishes network connections.
@@ -25,8 +25,8 @@ func (d *ErrorWrapperDialer) DialContext(ctx context.Context, network, address s
conn, err := d.Dialer.DialContext(ctx, network, address)
if err != nil {
return nil, SafeErrWrapperBuilder{
Classifier: errorsx.ClassifyGenericError,
Operation: errorsx.ConnectOperation,
Classifier: netxlite.ClassifyGenericError,
Operation: netxlite.ConnectOperation,
Error: err,
}.MaybeBuild()
}
@@ -44,8 +44,8 @@ func (c *errorWrapperConn) Read(b []byte) (int, error) {
count, err := c.Conn.Read(b)
if err != nil {
return 0, SafeErrWrapperBuilder{
Classifier: errorsx.ClassifyGenericError,
Operation: errorsx.ReadOperation,
Classifier: netxlite.ClassifyGenericError,
Operation: netxlite.ReadOperation,
Error: err,
}.MaybeBuild()
}
@@ -57,8 +57,8 @@ func (c *errorWrapperConn) Write(b []byte) (int, error) {
count, err := c.Conn.Write(b)
if err != nil {
return 0, SafeErrWrapperBuilder{
Classifier: errorsx.ClassifyGenericError,
Operation: errorsx.WriteOperation,
Classifier: netxlite.ClassifyGenericError,
Operation: netxlite.WriteOperation,
Error: err,
}.MaybeBuild()
}
@@ -70,8 +70,8 @@ func (c *errorWrapperConn) Close() error {
err := c.Conn.Close()
if err != nil {
return SafeErrWrapperBuilder{
Classifier: errorsx.ClassifyGenericError,
Operation: errorsx.CloseOperation,
Classifier: netxlite.ClassifyGenericError,
Operation: netxlite.CloseOperation,
Error: err,
}.MaybeBuild()
}
+13 -13
View File
@@ -7,7 +7,7 @@ import (
"net"
"testing"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
)
@@ -19,14 +19,14 @@ func TestErrorWrapperDialerFailure(t *testing.T) {
},
}}
conn, err := d.DialContext(ctx, "tcp", "www.google.com:443")
var ew *errorsx.ErrWrapper
var ew *netxlite.ErrWrapper
if !errors.As(err, &ew) {
t.Fatal("cannot convert to ErrWrapper")
}
if ew.Operation != errorsx.ConnectOperation {
if ew.Operation != netxlite.ConnectOperation {
t.Fatal("unexpected operation", ew.Operation)
}
if ew.Failure != errorsx.FailureEOFError {
if ew.Failure != netxlite.FailureEOFError {
t.Fatal("unexpected failure", ew.Failure)
}
if !errors.Is(ew.WrappedErr, io.EOF) {
@@ -68,14 +68,14 @@ func TestErrorWrapperConnReadFailure(t *testing.T) {
}
buf := make([]byte, 1024)
cnt, err := c.Read(buf)
var ew *errorsx.ErrWrapper
var ew *netxlite.ErrWrapper
if !errors.As(err, &ew) {
t.Fatal("cannot cast error to ErrWrapper")
}
if ew.Operation != errorsx.ReadOperation {
if ew.Operation != netxlite.ReadOperation {
t.Fatal("invalid operation", ew.Operation)
}
if ew.Failure != errorsx.FailureEOFError {
if ew.Failure != netxlite.FailureEOFError {
t.Fatal("invalid failure", ew.Failure)
}
if !errors.Is(ew.WrappedErr, io.EOF) {
@@ -114,14 +114,14 @@ func TestErrorWrapperConnWriteFailure(t *testing.T) {
}
buf := make([]byte, 1024)
cnt, err := c.Write(buf)
var ew *errorsx.ErrWrapper
var ew *netxlite.ErrWrapper
if !errors.As(err, &ew) {
t.Fatal("cannot cast error to ErrWrapper")
}
if ew.Operation != errorsx.WriteOperation {
if ew.Operation != netxlite.WriteOperation {
t.Fatal("invalid operation", ew.Operation)
}
if ew.Failure != errorsx.FailureEOFError {
if ew.Failure != netxlite.FailureEOFError {
t.Fatal("invalid failure", ew.Failure)
}
if !errors.Is(ew.WrappedErr, io.EOF) {
@@ -159,14 +159,14 @@ func TestErrorWrapperConnCloseFailure(t *testing.T) {
},
}
err := c.Close()
var ew *errorsx.ErrWrapper
var ew *netxlite.ErrWrapper
if !errors.As(err, &ew) {
t.Fatal("cannot cast error to ErrWrapper")
}
if ew.Operation != errorsx.CloseOperation {
if ew.Operation != netxlite.CloseOperation {
t.Fatal("invalid operation", ew.Operation)
}
if ew.Failure != errorsx.FailureEOFError {
if ew.Failure != netxlite.FailureEOFError {
t.Fatal("invalid failure", ew.Failure)
}
if !errors.Is(ew.WrappedErr, io.EOF) {
+11 -11
View File
@@ -4,7 +4,7 @@ package errorsx
import (
"errors"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
// SafeErrWrapperBuilder contains a builder for ErrWrapper that
@@ -27,9 +27,9 @@ func (b SafeErrWrapperBuilder) MaybeBuild() (err error) {
if b.Error != nil {
classifier := b.Classifier
if classifier == nil {
classifier = errorsx.ClassifyGenericError
classifier = netxlite.ClassifyGenericError
}
err = &errorsx.ErrWrapper{
err = &netxlite.ErrWrapper{
Failure: classifier(b.Error),
Operation: toOperationString(b.Error, b.Operation),
WrappedErr: b.Error,
@@ -39,30 +39,30 @@ func (b SafeErrWrapperBuilder) MaybeBuild() (err error) {
}
func toOperationString(err error, operation string) string {
var errwrapper *errorsx.ErrWrapper
var errwrapper *netxlite.ErrWrapper
if errors.As(err, &errwrapper) {
// Basically, as explained in ErrWrapper docs, let's
// keep the child major operation, if any.
if errwrapper.Operation == errorsx.ConnectOperation {
if errwrapper.Operation == netxlite.ConnectOperation {
return errwrapper.Operation
}
if errwrapper.Operation == errorsx.HTTPRoundTripOperation {
if errwrapper.Operation == netxlite.HTTPRoundTripOperation {
return errwrapper.Operation
}
if errwrapper.Operation == errorsx.ResolveOperation {
if errwrapper.Operation == netxlite.ResolveOperation {
return errwrapper.Operation
}
if errwrapper.Operation == errorsx.TLSHandshakeOperation {
if errwrapper.Operation == netxlite.TLSHandshakeOperation {
return errwrapper.Operation
}
if errwrapper.Operation == errorsx.QUICHandshakeOperation {
if errwrapper.Operation == netxlite.QUICHandshakeOperation {
return errwrapper.Operation
}
if errwrapper.Operation == "quic_handshake_start" {
return errorsx.QUICHandshakeOperation
return netxlite.QUICHandshakeOperation
}
if errwrapper.Operation == "quic_handshake_done" {
return errorsx.QUICHandshakeOperation
return netxlite.QUICHandshakeOperation
}
// FALLTHROUGH
}
+14 -14
View File
@@ -4,14 +4,14 @@ import (
"errors"
"testing"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestMaybeBuildFactory(t *testing.T) {
err := SafeErrWrapperBuilder{
Error: errors.New("mocked error"),
}.MaybeBuild()
var target *errorsx.ErrWrapper
var target *netxlite.ErrWrapper
if errors.As(err, &target) == false {
t.Fatal("not the expected error type")
}
@@ -27,32 +27,32 @@ func TestToOperationString(t *testing.T) {
t.Run("for connect", func(t *testing.T) {
// You're doing HTTP and connect fails. You want to know
// that connect failed not that HTTP failed.
err := &errorsx.ErrWrapper{Operation: errorsx.ConnectOperation}
if toOperationString(err, errorsx.HTTPRoundTripOperation) != errorsx.ConnectOperation {
err := &netxlite.ErrWrapper{Operation: netxlite.ConnectOperation}
if toOperationString(err, netxlite.HTTPRoundTripOperation) != netxlite.ConnectOperation {
t.Fatal("unexpected result")
}
})
t.Run("for http_round_trip", func(t *testing.T) {
// You're doing DoH and something fails inside HTTP. You want
// to know about the internal HTTP error, not resolve.
err := &errorsx.ErrWrapper{Operation: errorsx.HTTPRoundTripOperation}
if toOperationString(err, errorsx.ResolveOperation) != errorsx.HTTPRoundTripOperation {
err := &netxlite.ErrWrapper{Operation: netxlite.HTTPRoundTripOperation}
if toOperationString(err, netxlite.ResolveOperation) != netxlite.HTTPRoundTripOperation {
t.Fatal("unexpected result")
}
})
t.Run("for resolve", func(t *testing.T) {
// You're doing HTTP and the DNS fails. You want to
// know that resolve failed.
err := &errorsx.ErrWrapper{Operation: errorsx.ResolveOperation}
if toOperationString(err, errorsx.HTTPRoundTripOperation) != errorsx.ResolveOperation {
err := &netxlite.ErrWrapper{Operation: netxlite.ResolveOperation}
if toOperationString(err, netxlite.HTTPRoundTripOperation) != netxlite.ResolveOperation {
t.Fatal("unexpected result")
}
})
t.Run("for tls_handshake", func(t *testing.T) {
// You're doing HTTP and the TLS handshake fails. You want
// to know about a TLS handshake error.
err := &errorsx.ErrWrapper{Operation: errorsx.TLSHandshakeOperation}
if toOperationString(err, errorsx.HTTPRoundTripOperation) != errorsx.TLSHandshakeOperation {
err := &netxlite.ErrWrapper{Operation: netxlite.TLSHandshakeOperation}
if toOperationString(err, netxlite.HTTPRoundTripOperation) != netxlite.TLSHandshakeOperation {
t.Fatal("unexpected result")
}
})
@@ -60,16 +60,16 @@ func TestToOperationString(t *testing.T) {
// You just noticed that TLS handshake failed and you
// have a child error telling you that read failed. Here
// you want to know about a TLS handshake error.
err := &errorsx.ErrWrapper{Operation: errorsx.ReadOperation}
if toOperationString(err, errorsx.TLSHandshakeOperation) != errorsx.TLSHandshakeOperation {
err := &netxlite.ErrWrapper{Operation: netxlite.ReadOperation}
if toOperationString(err, netxlite.TLSHandshakeOperation) != netxlite.TLSHandshakeOperation {
t.Fatal("unexpected result")
}
})
t.Run("for quic_handshake", func(t *testing.T) {
// You're doing HTTP and the TLS handshake fails. You want
// to know about a TLS handshake error.
err := &errorsx.ErrWrapper{Operation: errorsx.QUICHandshakeOperation}
if toOperationString(err, errorsx.HTTPRoundTripOperation) != errorsx.QUICHandshakeOperation {
err := &netxlite.ErrWrapper{Operation: netxlite.QUICHandshakeOperation}
if toOperationString(err, netxlite.HTTPRoundTripOperation) != netxlite.QUICHandshakeOperation {
t.Fatal("unexpected result")
}
})
@@ -8,7 +8,6 @@ import (
"github.com/lucas-clemente/quic-go"
errorsxlegacy "github.com/ooni/probe-cli/v3/internal/engine/legacy/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
)
func TestErrorWrapperQUICDialerInvalidCertificate(t *testing.T) {
@@ -31,7 +30,7 @@ func TestErrorWrapperQUICDialerInvalidCertificate(t *testing.T) {
if sess != nil {
t.Fatal("expected nil sess here")
}
if err.Error() != errorsx.FailureSSLInvalidCertificate {
if err.Error() != netxlite.FailureSSLInvalidCertificate {
t.Fatal("unexpected failure")
}
}
+6 -6
View File
@@ -6,7 +6,7 @@ import (
"net"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/netxlite/quicx"
)
@@ -39,7 +39,7 @@ func (qls *ErrorWrapperQUICListener) Listen(addr *net.UDPAddr) (quicx.UDPLikeCon
if err != nil {
return nil, SafeErrWrapperBuilder{
Error: err,
Operation: errorsx.QUICListenOperation,
Operation: netxlite.QUICListenOperation,
}.MaybeBuild()
}
return &errorWrapperUDPConn{pconn}, nil
@@ -59,7 +59,7 @@ func (c *errorWrapperUDPConn) WriteTo(p []byte, addr net.Addr) (int, error) {
if err != nil {
return 0, SafeErrWrapperBuilder{
Error: err,
Operation: errorsx.WriteToOperation,
Operation: netxlite.WriteToOperation,
}.MaybeBuild()
}
return count, nil
@@ -71,7 +71,7 @@ func (c *errorWrapperUDPConn) ReadFrom(b []byte) (int, net.Addr, error) {
if err != nil {
return 0, nil, SafeErrWrapperBuilder{
Error: err,
Operation: errorsx.ReadFromOperation,
Operation: netxlite.ReadFromOperation,
}.MaybeBuild()
}
return n, addr, nil
@@ -89,9 +89,9 @@ func (d *ErrorWrapperQUICDialer) DialContext(
sess, err := d.Dialer.DialContext(ctx, network, host, tlsCfg, cfg)
if err != nil {
return nil, SafeErrWrapperBuilder{
Classifier: errorsx.ClassifyQUICHandshakeError,
Classifier: netxlite.ClassifyQUICHandshakeError,
Error: err,
Operation: errorsx.QUICHandshakeOperation,
Operation: netxlite.QUICHandshakeOperation,
}.MaybeBuild()
}
return sess, nil
+4 -4
View File
@@ -9,7 +9,7 @@ import (
"testing"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
"github.com/ooni/probe-cli/v3/internal/netxlite/quicx"
)
@@ -143,14 +143,14 @@ func TestErrorWrapperQUICDialerFailure(t *testing.T) {
if !errors.Is(err, io.EOF) {
t.Fatal("expected another error here")
}
var errWrapper *errorsx.ErrWrapper
var errWrapper *netxlite.ErrWrapper
if !errors.As(err, &errWrapper) {
t.Fatal("cannot cast to ErrWrapper")
}
if errWrapper.Operation != errorsx.QUICHandshakeOperation {
if errWrapper.Operation != netxlite.QUICHandshakeOperation {
t.Fatal("unexpected Operation")
}
if errWrapper.Failure != errorsx.FailureEOFError {
if errWrapper.Failure != netxlite.FailureEOFError {
t.Fatal("unexpected failure")
}
}
+3 -3
View File
@@ -3,7 +3,7 @@ package errorsx
import (
"context"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
// Resolver is a DNS resolver. The *net.Resolver used by Go implements
@@ -24,9 +24,9 @@ var _ Resolver = &ErrorWrapperResolver{}
func (r *ErrorWrapperResolver) LookupHost(ctx context.Context, hostname string) ([]string, error) {
addrs, err := r.Resolver.LookupHost(ctx, hostname)
err = SafeErrWrapperBuilder{
Classifier: errorsx.ClassifyResolverError,
Classifier: netxlite.ClassifyResolverError,
Error: err,
Operation: errorsx.ResolveOperation,
Operation: netxlite.ResolveOperation,
}.MaybeBuild()
return addrs, err
}
@@ -6,7 +6,7 @@ import (
"net"
"testing"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
)
@@ -41,14 +41,14 @@ func TestErrorWrapperResolverFailure(t *testing.T) {
if addrs != nil {
t.Fatal("expected nil addr here")
}
var errWrapper *errorsx.ErrWrapper
var errWrapper *netxlite.ErrWrapper
if !errors.As(err, &errWrapper) {
t.Fatal("cannot properly cast the returned error")
}
if errWrapper.Failure != errorsx.FailureDNSNXDOMAINError {
if errWrapper.Failure != netxlite.FailureDNSNXDOMAINError {
t.Fatal("unexpected failure")
}
if errWrapper.Operation != errorsx.ResolveOperation {
if errWrapper.Operation != netxlite.ResolveOperation {
t.Fatal("unexpected Operation")
}
}
+3 -3
View File
@@ -5,7 +5,7 @@ import (
"crypto/tls"
"net"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
// TLSHandshaker is the generic TLS handshaker
@@ -25,9 +25,9 @@ func (h *ErrorWrapperTLSHandshaker) Handshake(
) (net.Conn, tls.ConnectionState, error) {
tlsconn, state, err := h.TLSHandshaker.Handshake(ctx, conn, config)
err = SafeErrWrapperBuilder{
Classifier: errorsx.ClassifyTLSHandshakeError,
Classifier: netxlite.ClassifyTLSHandshakeError,
Error: err,
Operation: errorsx.TLSHandshakeOperation,
Operation: netxlite.TLSHandshakeOperation,
}.MaybeBuild()
return tlsconn, state, err
}
+4 -4
View File
@@ -8,7 +8,7 @@ import (
"net"
"testing"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
)
@@ -30,14 +30,14 @@ func TestErrorWrapperTLSHandshakerFailure(t *testing.T) {
if conn != nil {
t.Fatal("expected nil con here")
}
var errWrapper *errorsx.ErrWrapper
var errWrapper *netxlite.ErrWrapper
if !errors.As(err, &errWrapper) {
t.Fatal("cannot cast to ErrWrapper")
}
if errWrapper.Failure != errorsx.FailureEOFError {
if errWrapper.Failure != netxlite.FailureEOFError {
t.Fatal("unexpected Failure")
}
if errWrapper.Operation != errorsx.TLSHandshakeOperation {
if errWrapper.Operation != netxlite.TLSHandshakeOperation {
t.Fatal("unexpected Operation")
}
}
+3 -3
View File
@@ -9,7 +9,7 @@ import (
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/handlers"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/oldhttptransport"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"golang.org/x/net/http2"
)
@@ -82,10 +82,10 @@ func (t *HTTPTransport) RoundTrip(
resp, err = t.roundTripper.RoundTrip(req)
// For safety wrap the error as modelx.HTTPRoundTripOperation but this
// will only be used if the error chain does not contain any
// other major operation failure. See errorsx.ErrWrapper.
// other major operation failure. See netxlite.ErrWrapper.
err = errorsxlegacy.SafeErrWrapperBuilder{
Error: err,
Operation: errorsx.HTTPRoundTripOperation,
Operation: netxlite.HTTPRoundTripOperation,
}.MaybeBuild()
return resp, err
}
+4 -5
View File
@@ -13,8 +13,7 @@ import (
"time"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func dowithclient(t *testing.T, client *netx.HTTPClient) {
@@ -24,7 +23,7 @@ func dowithclient(t *testing.T, client *netx.HTTPClient) {
t.Fatal(err)
}
defer resp.Body.Close()
_, err = iox.ReadAllContext(context.Background(), resp.Body)
_, err = netxlite.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}
@@ -126,7 +125,7 @@ func httpProxyTestMain(t *testing.T, client *http.Client, expect int) {
t.Fatal(err)
}
defer resp.Body.Close()
_, err = iox.ReadAllContext(context.Background(), resp.Body)
_, err = netxlite.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}
@@ -148,7 +147,7 @@ func TestHTTPTransportTimeout(t *testing.T) {
if err == nil {
t.Fatal("expected an error here")
}
if !strings.HasSuffix(err.Error(), errorsx.FailureGenericTimeoutError) {
if !strings.HasSuffix(err.Error(), netxlite.FailureGenericTimeoutError) {
t.Fatal("not the error we expected")
}
if resp != nil {
@@ -8,7 +8,7 @@ import (
"testing"
"time"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestNewTLSConnectionState(t *testing.T) {
@@ -61,7 +61,7 @@ func TestMeasurementRootWithMeasurementRootPanic(t *testing.T) {
func TestErrWrapperPublicAPI(t *testing.T) {
child := errors.New("mocked error")
wrapper := &errorsx.ErrWrapper{
wrapper := &netxlite.ErrWrapper{
Failure: "moobar",
WrappedErr: child,
}
@@ -5,7 +5,7 @@ import (
"net/http"
"testing"
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestBodyTracerSuccess(t *testing.T) {
@@ -17,7 +17,7 @@ func TestBodyTracerSuccess(t *testing.T) {
t.Fatal(err)
}
defer resp.Body.Close()
_, err = iox.ReadAllContext(context.Background(), resp.Body)
_, err = netxlite.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}
@@ -5,7 +5,7 @@ import (
"net/http"
"testing"
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestGood(t *testing.T) {
@@ -17,7 +17,7 @@ func TestGood(t *testing.T) {
t.Fatal(err)
}
defer resp.Body.Close()
_, err = iox.ReadAllContext(context.Background(), resp.Body)
_, err = netxlite.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}
@@ -13,8 +13,7 @@ import (
"github.com/ooni/probe-cli/v3/internal/atomicx"
errorsxlegacy "github.com/ooni/probe-cli/v3/internal/engine/legacy/errorsx"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
// TraceTripper performs single HTTP transactions.
@@ -28,7 +27,7 @@ type TraceTripper struct {
func NewTraceTripper(roundTripper http.RoundTripper) *TraceTripper {
return &TraceTripper{
readAllErrs: &atomicx.Int64{},
readAllContext: iox.ReadAllContext,
readAllContext: netxlite.ReadAllContext,
roundTripper: roundTripper,
}
}
@@ -84,7 +83,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
var (
err error
majorOp = errorsx.HTTPRoundTripOperation
majorOp = netxlite.HTTPRoundTripOperation
majorOpMu sync.Mutex
requestBody []byte
requestHeaders = http.Header{}
@@ -104,7 +103,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
tracer := &httptrace.ClientTrace{
TLSHandshakeStart: func() {
majorOpMu.Lock()
majorOp = errorsx.TLSHandshakeOperation
majorOp = netxlite.TLSHandshakeOperation
majorOpMu.Unlock()
// Event emitted by net/http when DialTLS is not
// configured in the http.Transport
@@ -119,7 +118,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
// less confusing to users to see the wrapped name
err = errorsxlegacy.SafeErrWrapperBuilder{
Error: err,
Operation: errorsx.TLSHandshakeOperation,
Operation: netxlite.TLSHandshakeOperation,
}.MaybeBuild()
durationSinceBeginning := time.Since(root.Beginning)
// Event emitted by net/http when DialTLS is not
@@ -134,7 +133,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
},
GotConn: func(info httptrace.GotConnInfo) {
majorOpMu.Lock()
majorOp = errorsx.HTTPRoundTripOperation
majorOp = netxlite.HTTPRoundTripOperation
majorOpMu.Unlock()
root.Handler.OnMeasurement(modelx.Measurement{
HTTPConnectionReady: &modelx.HTTPConnectionReadyEvent{
@@ -174,7 +173,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
// less confusing to users to see the wrapped name
err := errorsxlegacy.SafeErrWrapperBuilder{
Error: info.Err,
Operation: errorsx.HTTPRoundTripOperation,
Operation: netxlite.HTTPRoundTripOperation,
}.MaybeBuild()
root.Handler.OnMeasurement(modelx.Measurement{
HTTPRequestDone: &modelx.HTTPRequestDoneEvent{
@@ -13,7 +13,7 @@ import (
"github.com/miekg/dns"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestTraceTripperSuccess(t *testing.T) {
@@ -25,7 +25,7 @@ func TestTraceTripperSuccess(t *testing.T) {
t.Fatal(err)
}
defer resp.Body.Close()
_, err = iox.ReadAllContext(context.Background(), resp.Body)
_, err = netxlite.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}
@@ -156,7 +156,7 @@ func TestTraceTripperWithCorrectSnaps(t *testing.T) {
// Read the whole response body, parse it as valid DNS
// reply and verify we obtained what we expected
replyData, err := iox.ReadAllContext(context.Background(), resp.Body)
replyData, err := netxlite.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}
+2 -1
View File
@@ -118,7 +118,8 @@ func newResolver(
if network == "udp" {
// Same rationale as above: avoid possible endless loop
return newResolverWrapper(beginning, handler, newResolverUDP(
newDialer(beginning, handler), withPort(address, "53"),
netxlite.NewDialerLegacyAdapter(newDialer(beginning, handler)),
withPort(address, "53"),
)), nil
}
return nil, errors.New("resolver.New: unsupported network value")
@@ -10,7 +10,7 @@ import (
"github.com/apex/log/handlers/discard"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestGood(t *testing.T) {
@@ -33,7 +33,7 @@ func TestGood(t *testing.T) {
t.Fatal("expected non-nil resp here")
}
defer resp.Body.Close()
_, err = iox.ReadAllContext(context.Background(), resp.Body)
_, err = netxlite.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}
@@ -20,7 +20,6 @@ import (
"github.com/ooni/probe-cli/v3/internal/engine/legacy/oonitemplates"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
)
// ExtSpec describes a data format extension
@@ -418,7 +417,7 @@ func NewNetworkEventsList(results oonitemplates.Results) NetworkEventsList {
out = append(out, &NetworkEvent{
Address: in.Connect.RemoteAddress,
Failure: makeFailure(in.Connect.Error),
Operation: errorsx.ConnectOperation,
Operation: netxlite.ConnectOperation,
T: in.Connect.DurationSinceBeginning.Seconds(),
})
// fallthrough
@@ -426,7 +425,7 @@ func NewNetworkEventsList(results oonitemplates.Results) NetworkEventsList {
if in.Read != nil {
out = append(out, &NetworkEvent{
Failure: makeFailure(in.Read.Error),
Operation: errorsx.ReadOperation,
Operation: netxlite.ReadOperation,
NumBytes: in.Read.NumBytes,
T: in.Read.DurationSinceBeginning.Seconds(),
})
@@ -435,7 +434,7 @@ func NewNetworkEventsList(results oonitemplates.Results) NetworkEventsList {
if in.Write != nil {
out = append(out, &NetworkEvent{
Failure: makeFailure(in.Write.Error),
Operation: errorsx.WriteOperation,
Operation: netxlite.WriteOperation,
NumBytes: in.Write.NumBytes,
T: in.Write.DurationSinceBeginning.Seconds(),
})
@@ -12,7 +12,7 @@ import (
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/oonitemplates"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestNewTCPConnectListEmpty(t *testing.T) {
@@ -67,7 +67,7 @@ func TestNewTCPConnectListFailure(t *testing.T) {
Connects: []*modelx.ConnectEvent{
{
RemoteAddress: "8.8.8.8:53",
Error: errors.New(errorsx.FailureConnectionReset),
Error: errors.New(netxlite.FailureConnectionReset),
},
},
})
@@ -80,7 +80,7 @@ func TestNewTCPConnectListFailure(t *testing.T) {
if out[0].Port != 53 {
t.Fatal("unexpected out[0].Port")
}
if *out[0].Status.Failure != errorsx.FailureConnectionReset {
if *out[0].Status.Failure != netxlite.FailureConnectionReset {
t.Fatal("unexpected out[0].Failure")
}
if out[0].Status.Success != false {
@@ -93,7 +93,7 @@ func TestNewTCPConnectListInvalidInput(t *testing.T) {
Connects: []*modelx.ConnectEvent{
{
RemoteAddress: "8.8.8.8",
Error: errors.New(errorsx.FailureConnectionReset),
Error: errors.New(netxlite.FailureConnectionReset),
},
},
})
@@ -106,7 +106,7 @@ func TestNewTCPConnectListInvalidInput(t *testing.T) {
if out[0].Port != 0 {
t.Fatal("unexpected out[0].Port")
}
if *out[0].Status.Failure != errorsx.FailureConnectionReset {
if *out[0].Status.Failure != netxlite.FailureConnectionReset {
t.Fatal("unexpected out[0].Failure")
}
if out[0].Status.Success != false {
@@ -649,7 +649,7 @@ func TestNewDNSQueriesListSuccess(t *testing.T) {
TransportNetwork: "system",
},
{
Error: errors.New(errorsx.FailureDNSNXDOMAINError),
Error: errors.New(netxlite.FailureDNSNXDOMAINError),
Hostname: "dns.googlex",
TransportNetwork: "system",
},
@@ -768,7 +768,7 @@ func dnscheckbad(e DNSQueryEntry) error {
if e.Engine != "system" {
return errors.New("invalid engine")
}
if *e.Failure != errorsx.FailureDNSNXDOMAINError {
if *e.Failure != netxlite.FailureDNSNXDOMAINError {
return errors.New("invalid failure")
}
if e.Hostname != "dns.googlex" {
@@ -854,7 +854,7 @@ func TestNewNetworkEventsListGood(t *testing.T) {
if out[0].NumBytes != 0 {
t.Fatal("wrong out[0].NumBytes")
}
if out[0].Operation != errorsx.ConnectOperation {
if out[0].Operation != netxlite.ConnectOperation {
t.Fatal("wrong out[0].Operation")
}
if !floatEquals(out[0].T, 0.010) {
@@ -870,7 +870,7 @@ func TestNewNetworkEventsListGood(t *testing.T) {
if out[1].NumBytes != 1789 {
t.Fatal("wrong out[1].NumBytes")
}
if out[1].Operation != errorsx.ReadOperation {
if out[1].Operation != netxlite.ReadOperation {
t.Fatal("wrong out[1].Operation")
}
if !floatEquals(out[1].T, 0.020) {
@@ -886,7 +886,7 @@ func TestNewNetworkEventsListGood(t *testing.T) {
if out[2].NumBytes != 17714 {
t.Fatal("wrong out[2].NumBytes")
}
if out[2].Operation != errorsx.WriteOperation {
if out[2].Operation != netxlite.WriteOperation {
t.Fatal("wrong out[2].Operation")
}
if !floatEquals(out[2].T, 0.030) {
@@ -933,7 +933,7 @@ func TestNewNetworkEventsListGoodUDPAndErrors(t *testing.T) {
if out[0].NumBytes != 0 {
t.Fatal("wrong out[0].NumBytes")
}
if out[0].Operation != errorsx.ConnectOperation {
if out[0].Operation != netxlite.ConnectOperation {
t.Fatal("wrong out[0].Operation")
}
if !floatEquals(out[0].T, 0.010) {
@@ -949,7 +949,7 @@ func TestNewNetworkEventsListGoodUDPAndErrors(t *testing.T) {
if out[1].NumBytes != 1789 {
t.Fatal("wrong out[1].NumBytes")
}
if out[1].Operation != errorsx.ReadOperation {
if out[1].Operation != netxlite.ReadOperation {
t.Fatal("wrong out[1].Operation")
}
if !floatEquals(out[1].T, 0.020) {
@@ -965,7 +965,7 @@ func TestNewNetworkEventsListGoodUDPAndErrors(t *testing.T) {
if out[2].NumBytes != 17714 {
t.Fatal("wrong out[2].NumBytes")
}
if out[2].Operation != errorsx.WriteOperation {
if out[2].Operation != netxlite.WriteOperation {
t.Fatal("wrong out[2].Operation")
}
if !floatEquals(out[2].T, 0.030) {
@@ -24,7 +24,7 @@ import (
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/handlers"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/runtimex"
"gitlab.com/yawning/obfs4.git/transports"
obfs4base "gitlab.com/yawning/obfs4.git/transports/base"
@@ -327,7 +327,7 @@ func HTTPDo(
config.MaxResponseBodySnapSize,
),
)
data, err := iox.ReadAllContext(ctx, reader)
data, err := netxlite.ReadAllContext(ctx, reader)
mu.Lock()
results.BodySnap, results.Error = data, err
mu.Unlock()
@@ -11,7 +11,7 @@ import (
goptlib "git.torproject.org/pluggable-transports/goptlib.git"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"gitlab.com/yawning/obfs4.git/transports"
obfs4base "gitlab.com/yawning/obfs4.git/transports/base"
)
@@ -55,7 +55,7 @@ func TestDNSLookupCancellation(t *testing.T) {
if results.Error == nil {
t.Fatal("expected an error here")
}
if results.Error.Error() != errorsx.FailureGenericTimeoutError {
if results.Error.Error() != netxlite.FailureGenericTimeoutError {
t.Fatal("not the error we expected")
}
if len(results.Addresses) > 0 {
@@ -170,7 +170,7 @@ func TestTLSConnectCancellation(t *testing.T) {
if results.Error == nil {
t.Fatal("expected an error here")
}
if results.Error.Error() != errorsx.FailureGenericTimeoutError {
if results.Error.Error() != netxlite.FailureGenericTimeoutError {
t.Fatal("not the error we expected")
}
}