2021-06-09 09:42:31 +02:00
|
|
|
package dialer
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
2021-06-08 23:59:30 +02:00
|
|
|
"net"
|
2021-02-02 12:05:47 +01:00
|
|
|
"testing"
|
|
|
|
|
2021-07-01 16:34:36 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/errorsx"
|
2021-06-23 16:06:02 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxmocks"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestErrorWrapperFailure(t *testing.T) {
|
2021-06-09 00:29:40 +02:00
|
|
|
ctx := context.Background()
|
2021-06-23 16:21:13 +02:00
|
|
|
d := &errorWrapperDialer{Dialer: &netxmocks.Dialer{
|
2021-06-08 23:59:30 +02:00
|
|
|
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
|
|
|
return nil, io.EOF
|
|
|
|
},
|
|
|
|
}}
|
2021-02-02 12:05:47 +01:00
|
|
|
conn, err := d.DialContext(ctx, "tcp", "www.google.com:443")
|
|
|
|
if conn != nil {
|
|
|
|
t.Fatal("expected a nil conn here")
|
|
|
|
}
|
2021-07-01 16:34:36 +02:00
|
|
|
errorWrapperCheckErr(t, err, errorsx.ConnectOperation)
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func errorWrapperCheckErr(t *testing.T, err error, op string) {
|
|
|
|
if !errors.Is(err, io.EOF) {
|
|
|
|
t.Fatal("expected another error here")
|
|
|
|
}
|
2021-07-01 16:34:36 +02:00
|
|
|
var errWrapper *errorsx.ErrWrapper
|
2021-02-02 12:05:47 +01:00
|
|
|
if !errors.As(err, &errWrapper) {
|
|
|
|
t.Fatal("cannot cast to ErrWrapper")
|
|
|
|
}
|
|
|
|
if errWrapper.Operation != op {
|
|
|
|
t.Fatal("unexpected Operation")
|
|
|
|
}
|
2021-07-01 16:34:36 +02:00
|
|
|
if errWrapper.Failure != errorsx.FailureEOFError {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorWrapperSuccess(t *testing.T) {
|
2021-06-09 00:29:40 +02:00
|
|
|
ctx := context.Background()
|
2021-06-23 16:21:13 +02:00
|
|
|
d := &errorWrapperDialer{Dialer: &netxmocks.Dialer{
|
2021-06-08 23:59:30 +02:00
|
|
|
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
2021-06-23 16:06:02 +02:00
|
|
|
return &netxmocks.Conn{
|
2021-06-08 23:59:30 +02:00
|
|
|
MockRead: func(b []byte) (int, error) {
|
|
|
|
return 0, io.EOF
|
|
|
|
},
|
|
|
|
MockWrite: func(b []byte) (int, error) {
|
|
|
|
return 0, io.EOF
|
|
|
|
},
|
|
|
|
MockClose: func() error {
|
|
|
|
return io.EOF
|
|
|
|
},
|
|
|
|
MockLocalAddr: func() net.Addr {
|
|
|
|
return &net.TCPAddr{Port: 12345}
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}}
|
2021-02-02 12:05:47 +01:00
|
|
|
conn, err := d.DialContext(ctx, "tcp", "www.google.com")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if conn == nil {
|
|
|
|
t.Fatal("expected non-nil conn here")
|
|
|
|
}
|
|
|
|
count, err := conn.Read(nil)
|
2021-07-01 16:34:36 +02:00
|
|
|
errorWrapperCheckIOResult(t, count, err, errorsx.ReadOperation)
|
2021-02-02 12:05:47 +01:00
|
|
|
count, err = conn.Write(nil)
|
2021-07-01 16:34:36 +02:00
|
|
|
errorWrapperCheckIOResult(t, count, err, errorsx.WriteOperation)
|
2021-02-02 12:05:47 +01:00
|
|
|
err = conn.Close()
|
2021-07-01 16:34:36 +02:00
|
|
|
errorWrapperCheckErr(t, err, errorsx.CloseOperation)
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func errorWrapperCheckIOResult(t *testing.T, count int, err error, op string) {
|
|
|
|
if count != 0 {
|
|
|
|
t.Fatal("expected nil count here")
|
|
|
|
}
|
|
|
|
errorWrapperCheckErr(t, err, op)
|
|
|
|
}
|