refactor: move ErrorWrapperTLSHandshaker to errorsx (#418)
Part of https://github.com/ooni/probe/issues/1505
This commit is contained in:
parent
ceefcaf45e
commit
863899469e
|
@ -14,6 +14,7 @@ import (
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
|
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/dialer"
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/dialer"
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/tlsdialer"
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/tlsdialer"
|
||||||
|
"github.com/ooni/probe-cli/v3/internal/errorsx"
|
||||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -107,7 +108,7 @@ func newTLSDialer(d dialer.Dialer, config *tls.Config) *netxlite.TLSDialer {
|
||||||
Config: config,
|
Config: config,
|
||||||
Dialer: d,
|
Dialer: d,
|
||||||
TLSHandshaker: tlsdialer.EmitterTLSHandshaker{
|
TLSHandshaker: tlsdialer.EmitterTLSHandshaker{
|
||||||
TLSHandshaker: tlsdialer.ErrorWrapperTLSHandshaker{
|
TLSHandshaker: &errorsx.ErrorWrapperTLSHandshaker{
|
||||||
TLSHandshaker: &netxlite.TLSHandshakerConfigurable{},
|
TLSHandshaker: &netxlite.TLSHandshakerConfigurable{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -38,6 +38,7 @@ import (
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/tlsdialer"
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/tlsdialer"
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
||||||
|
"github.com/ooni/probe-cli/v3/internal/errorsx"
|
||||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -184,7 +185,7 @@ func NewTLSDialer(config Config) TLSDialer {
|
||||||
config.Dialer = NewDialer(config)
|
config.Dialer = NewDialer(config)
|
||||||
}
|
}
|
||||||
var h tlsHandshaker = &netxlite.TLSHandshakerConfigurable{}
|
var h tlsHandshaker = &netxlite.TLSHandshakerConfigurable{}
|
||||||
h = tlsdialer.ErrorWrapperTLSHandshaker{TLSHandshaker: h}
|
h = &errorsx.ErrorWrapperTLSHandshaker{TLSHandshaker: h}
|
||||||
if config.Logger != nil {
|
if config.Logger != nil {
|
||||||
h = &netxlite.TLSHandshakerLogger{Logger: config.Logger, TLSHandshaker: h}
|
h = &netxlite.TLSHandshakerLogger{Logger: config.Logger, TLSHandshaker: h}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ import (
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/tlsdialer"
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/tlsdialer"
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
||||||
|
"github.com/ooni/probe-cli/v3/internal/errorsx"
|
||||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -230,7 +231,7 @@ func TestNewTLSDialerVanilla(t *testing.T) {
|
||||||
if rtd.TLSHandshaker == nil {
|
if rtd.TLSHandshaker == nil {
|
||||||
t.Fatal("invalid TLSHandshaker")
|
t.Fatal("invalid TLSHandshaker")
|
||||||
}
|
}
|
||||||
ewth, ok := rtd.TLSHandshaker.(tlsdialer.ErrorWrapperTLSHandshaker)
|
ewth, ok := rtd.TLSHandshaker.(*errorsx.ErrorWrapperTLSHandshaker)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the TLSHandshaker we expected")
|
t.Fatal("not the TLSHandshaker we expected")
|
||||||
}
|
}
|
||||||
|
@ -259,7 +260,7 @@ func TestNewTLSDialerWithConfig(t *testing.T) {
|
||||||
if rtd.TLSHandshaker == nil {
|
if rtd.TLSHandshaker == nil {
|
||||||
t.Fatal("invalid TLSHandshaker")
|
t.Fatal("invalid TLSHandshaker")
|
||||||
}
|
}
|
||||||
ewth, ok := rtd.TLSHandshaker.(tlsdialer.ErrorWrapperTLSHandshaker)
|
ewth, ok := rtd.TLSHandshaker.(*errorsx.ErrorWrapperTLSHandshaker)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the TLSHandshaker we expected")
|
t.Fatal("not the TLSHandshaker we expected")
|
||||||
}
|
}
|
||||||
|
@ -298,7 +299,7 @@ func TestNewTLSDialerWithLogging(t *testing.T) {
|
||||||
if lth.Logger != log.Log {
|
if lth.Logger != log.Log {
|
||||||
t.Fatal("not the Logger we expected")
|
t.Fatal("not the Logger we expected")
|
||||||
}
|
}
|
||||||
ewth, ok := lth.TLSHandshaker.(tlsdialer.ErrorWrapperTLSHandshaker)
|
ewth, ok := lth.TLSHandshaker.(*errorsx.ErrorWrapperTLSHandshaker)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the TLSHandshaker we expected")
|
t.Fatal("not the TLSHandshaker we expected")
|
||||||
}
|
}
|
||||||
|
@ -338,7 +339,7 @@ func TestNewTLSDialerWithSaver(t *testing.T) {
|
||||||
if sth.Saver != saver {
|
if sth.Saver != saver {
|
||||||
t.Fatal("not the Logger we expected")
|
t.Fatal("not the Logger we expected")
|
||||||
}
|
}
|
||||||
ewth, ok := sth.TLSHandshaker.(tlsdialer.ErrorWrapperTLSHandshaker)
|
ewth, ok := sth.TLSHandshaker.(*errorsx.ErrorWrapperTLSHandshaker)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the TLSHandshaker we expected")
|
t.Fatal("not the TLSHandshaker we expected")
|
||||||
}
|
}
|
||||||
|
@ -371,7 +372,7 @@ func TestNewTLSDialerWithNoTLSVerifyAndConfig(t *testing.T) {
|
||||||
if rtd.TLSHandshaker == nil {
|
if rtd.TLSHandshaker == nil {
|
||||||
t.Fatal("invalid TLSHandshaker")
|
t.Fatal("invalid TLSHandshaker")
|
||||||
}
|
}
|
||||||
ewth, ok := rtd.TLSHandshaker.(tlsdialer.ErrorWrapperTLSHandshaker)
|
ewth, ok := rtd.TLSHandshaker.(*errorsx.ErrorWrapperTLSHandshaker)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the TLSHandshaker we expected")
|
t.Fatal("not the TLSHandshaker we expected")
|
||||||
}
|
}
|
||||||
|
@ -406,7 +407,7 @@ func TestNewTLSDialerWithNoTLSVerifyAndNoConfig(t *testing.T) {
|
||||||
if rtd.TLSHandshaker == nil {
|
if rtd.TLSHandshaker == nil {
|
||||||
t.Fatal("invalid TLSHandshaker")
|
t.Fatal("invalid TLSHandshaker")
|
||||||
}
|
}
|
||||||
ewth, ok := rtd.TLSHandshaker.(tlsdialer.ErrorWrapperTLSHandshaker)
|
ewth, ok := rtd.TLSHandshaker.(*errorsx.ErrorWrapperTLSHandshaker)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the TLSHandshaker we expected")
|
t.Fatal("not the TLSHandshaker we expected")
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
|
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
|
||||||
"github.com/ooni/probe-cli/v3/internal/errorsx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// UnderlyingDialer is the underlying dialer type.
|
// UnderlyingDialer is the underlying dialer type.
|
||||||
|
@ -22,24 +21,6 @@ type TLSHandshaker interface {
|
||||||
net.Conn, tls.ConnectionState, error)
|
net.Conn, tls.ConnectionState, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrorWrapperTLSHandshaker wraps the returned error to be an OONI error
|
|
||||||
type ErrorWrapperTLSHandshaker struct {
|
|
||||||
TLSHandshaker
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handshake implements Handshaker.Handshake
|
|
||||||
func (h ErrorWrapperTLSHandshaker) Handshake(
|
|
||||||
ctx context.Context, conn net.Conn, config *tls.Config,
|
|
||||||
) (net.Conn, tls.ConnectionState, error) {
|
|
||||||
tlsconn, state, err := h.TLSHandshaker.Handshake(ctx, conn, config)
|
|
||||||
err = errorsx.SafeErrWrapperBuilder{
|
|
||||||
Classifier: errorsx.ClassifyTLSFailure,
|
|
||||||
Error: err,
|
|
||||||
Operation: errorsx.TLSHandshakeOperation,
|
|
||||||
}.MaybeBuild()
|
|
||||||
return tlsconn, state, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// EmitterTLSHandshaker emits events using the MeasurementRoot
|
// EmitterTLSHandshaker emits events using the MeasurementRoot
|
||||||
type EmitterTLSHandshaker struct {
|
type EmitterTLSHandshaker struct {
|
||||||
TLSHandshaker
|
TLSHandshaker
|
||||||
|
|
|
@ -11,7 +11,6 @@ import (
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/handlers"
|
"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/modelx"
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/tlsdialer"
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/tlsdialer"
|
||||||
"github.com/ooni/probe-cli/v3/internal/errorsx"
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -38,28 +37,6 @@ func (c *SetDeadlineConn) SetDeadline(t time.Time) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestErrorWrapperTLSHandshakerFailure(t *testing.T) {
|
|
||||||
h := tlsdialer.ErrorWrapperTLSHandshaker{TLSHandshaker: tlsdialer.EOFTLSHandshaker{}}
|
|
||||||
conn, _, err := h.Handshake(
|
|
||||||
context.Background(), tlsdialer.EOFConn{}, new(tls.Config))
|
|
||||||
if !errors.Is(err, io.EOF) {
|
|
||||||
t.Fatal("not the error that we expected")
|
|
||||||
}
|
|
||||||
if conn != nil {
|
|
||||||
t.Fatal("expected nil con here")
|
|
||||||
}
|
|
||||||
var errWrapper *errorsx.ErrWrapper
|
|
||||||
if !errors.As(err, &errWrapper) {
|
|
||||||
t.Fatal("cannot cast to ErrWrapper")
|
|
||||||
}
|
|
||||||
if errWrapper.Failure != errorsx.FailureEOFError {
|
|
||||||
t.Fatal("unexpected Failure")
|
|
||||||
}
|
|
||||||
if errWrapper.Operation != errorsx.TLSHandshakeOperation {
|
|
||||||
t.Fatal("unexpected Operation")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEmitterTLSHandshakerFailure(t *testing.T) {
|
func TestEmitterTLSHandshakerFailure(t *testing.T) {
|
||||||
saver := &handlers.SavingHandler{}
|
saver := &handlers.SavingHandler{}
|
||||||
ctx := modelx.WithMeasurementRoot(context.Background(), &modelx.MeasurementRoot{
|
ctx := modelx.WithMeasurementRoot(context.Background(), &modelx.MeasurementRoot{
|
||||||
|
|
31
internal/errorsx/tls.go
Normal file
31
internal/errorsx/tls.go
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package errorsx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TLSHandshaker is the generic TLS handshaker
|
||||||
|
type TLSHandshaker interface {
|
||||||
|
Handshake(ctx context.Context, conn net.Conn, config *tls.Config) (
|
||||||
|
net.Conn, tls.ConnectionState, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrorWrapperTLSHandshaker wraps the returned error to be an OONI error
|
||||||
|
type ErrorWrapperTLSHandshaker struct {
|
||||||
|
TLSHandshaker
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handshake implements TLSHandshaker.Handshake
|
||||||
|
func (h *ErrorWrapperTLSHandshaker) Handshake(
|
||||||
|
ctx context.Context, conn net.Conn, config *tls.Config,
|
||||||
|
) (net.Conn, tls.ConnectionState, error) {
|
||||||
|
tlsconn, state, err := h.TLSHandshaker.Handshake(ctx, conn, config)
|
||||||
|
err = SafeErrWrapperBuilder{
|
||||||
|
Classifier: ClassifyTLSFailure,
|
||||||
|
Error: err,
|
||||||
|
Operation: TLSHandshakeOperation,
|
||||||
|
}.MaybeBuild()
|
||||||
|
return tlsconn, state, err
|
||||||
|
}
|
42
internal/errorsx/tls_test.go
Normal file
42
internal/errorsx/tls_test.go
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
package errorsx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ooni/probe-cli/v3/internal/netxmocks"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestErrorWrapperTLSHandshakerFailure(t *testing.T) {
|
||||||
|
th := ErrorWrapperTLSHandshaker{TLSHandshaker: &netxmocks.TLSHandshaker{
|
||||||
|
MockHandshake: func(ctx context.Context, conn net.Conn, config *tls.Config) (net.Conn, tls.ConnectionState, error) {
|
||||||
|
return nil, tls.ConnectionState{}, io.EOF
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
conn, _, err := th.Handshake(
|
||||||
|
context.Background(), &netxmocks.Conn{
|
||||||
|
MockRead: func(b []byte) (int, error) {
|
||||||
|
return 0, io.EOF
|
||||||
|
},
|
||||||
|
}, new(tls.Config))
|
||||||
|
if !errors.Is(err, io.EOF) {
|
||||||
|
t.Fatal("not the error that we expected")
|
||||||
|
}
|
||||||
|
if conn != nil {
|
||||||
|
t.Fatal("expected nil con here")
|
||||||
|
}
|
||||||
|
var errWrapper *ErrWrapper
|
||||||
|
if !errors.As(err, &errWrapper) {
|
||||||
|
t.Fatal("cannot cast to ErrWrapper")
|
||||||
|
}
|
||||||
|
if errWrapper.Failure != FailureEOFError {
|
||||||
|
t.Fatal("unexpected Failure")
|
||||||
|
}
|
||||||
|
if errWrapper.Operation != TLSHandshakeOperation {
|
||||||
|
t.Fatal("unexpected Operation")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user