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")
}
}