ooni-probe-cli/internal/errorsx/errorsx_test.go
Simone Basso 83440cf110
refactor: split errorsx in good and legacy (#477)
The legacy part for now is internal/errorsx. It will stay there until
I figure out whether it also needs some extra bug fixing.

The good part is now in internal/netxlite/errorsx and contains all the
logic for mapping errors. We need to further improve upon this logic
by writing more thorough integration tests for QUIC.

We also need to copy the various dialer, conn, etc adapters that set
errors. We will put them inside netxlite and we will generate errors in
a way that is less crazy with respect to the major operation. (The
idea is to always wrap, given that now we measure in an incremental way
and we don't measure every operation together.)

Part of https://github.com/ooni/probe/issues/1591
2021-09-07 17:09:30 +02:00

77 lines
2.7 KiB
Go

package errorsx
import (
"errors"
"testing"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
)
func TestMaybeBuildFactory(t *testing.T) {
err := SafeErrWrapperBuilder{
Error: errors.New("mocked error"),
}.MaybeBuild()
var target *errorsx.ErrWrapper
if errors.As(err, &target) == false {
t.Fatal("not the expected error type")
}
if target.Failure != "unknown_failure: mocked error" {
t.Fatal("the failure string is wrong")
}
if target.WrappedErr.Error() != "mocked error" {
t.Fatal("the wrapped error is wrong")
}
}
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 {
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 {
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 {
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 {
t.Fatal("unexpected result")
}
})
t.Run("for minor operation", func(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 {
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 {
t.Fatal("unexpected result")
}
})
}