2021-06-08 11:24:13 +02:00
|
|
|
package tlsdialer_test
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/handlers"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
|
2021-06-08 11:24:13 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/tlsdialer"
|
2021-07-01 16:34:36 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/errorsx"
|
2021-06-25 11:07:26 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSystemTLSHandshakerEOFError(t *testing.T) {
|
2021-06-25 20:51:59 +02:00
|
|
|
h := &netxlite.TLSHandshakerConfigurable{}
|
2021-06-08 11:24:13 +02:00
|
|
|
conn, _, err := h.Handshake(context.Background(), tlsdialer.EOFConn{}, &tls.Config{
|
2021-02-02 12:05:47 +01:00
|
|
|
ServerName: "x.org",
|
|
|
|
})
|
|
|
|
if err != io.EOF {
|
|
|
|
t.Fatal("not the error that we expected")
|
|
|
|
}
|
|
|
|
if conn != nil {
|
|
|
|
t.Fatal("expected nil con here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SetDeadlineConn struct {
|
2021-06-08 11:24:13 +02:00
|
|
|
tlsdialer.EOFConn
|
2021-02-02 12:05:47 +01:00
|
|
|
deadlines []time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SetDeadlineConn) SetDeadline(t time.Time) error {
|
|
|
|
c.deadlines = append(c.deadlines, t)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorWrapperTLSHandshakerFailure(t *testing.T) {
|
2021-06-08 11:24:13 +02:00
|
|
|
h := tlsdialer.ErrorWrapperTLSHandshaker{TLSHandshaker: tlsdialer.EOFTLSHandshaker{}}
|
2021-02-02 12:05:47 +01:00
|
|
|
conn, _, err := h.Handshake(
|
2021-06-08 11:24:13 +02:00
|
|
|
context.Background(), tlsdialer.EOFConn{}, new(tls.Config))
|
2021-02-02 12:05:47 +01:00
|
|
|
if !errors.Is(err, io.EOF) {
|
|
|
|
t.Fatal("not the error that we expected")
|
|
|
|
}
|
|
|
|
if conn != nil {
|
|
|
|
t.Fatal("expected nil con 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")
|
|
|
|
}
|
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")
|
|
|
|
}
|
2021-07-01 16:34:36 +02:00
|
|
|
if errWrapper.Operation != errorsx.TLSHandshakeOperation {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected Operation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmitterTLSHandshakerFailure(t *testing.T) {
|
|
|
|
saver := &handlers.SavingHandler{}
|
|
|
|
ctx := modelx.WithMeasurementRoot(context.Background(), &modelx.MeasurementRoot{
|
|
|
|
Beginning: time.Now(),
|
|
|
|
Handler: saver,
|
|
|
|
})
|
2021-06-08 11:24:13 +02:00
|
|
|
h := tlsdialer.EmitterTLSHandshaker{TLSHandshaker: tlsdialer.EOFTLSHandshaker{}}
|
|
|
|
conn, _, err := h.Handshake(ctx, tlsdialer.EOFConn{}, &tls.Config{
|
2021-02-02 12:05:47 +01:00
|
|
|
ServerName: "www.kernel.org",
|
|
|
|
})
|
|
|
|
if !errors.Is(err, io.EOF) {
|
|
|
|
t.Fatal("not the error that we expected")
|
|
|
|
}
|
|
|
|
if conn != nil {
|
|
|
|
t.Fatal("expected nil con here")
|
|
|
|
}
|
|
|
|
events := saver.Read()
|
|
|
|
if len(events) != 2 {
|
|
|
|
t.Fatal("Wrong number of events")
|
|
|
|
}
|
|
|
|
if events[0].TLSHandshakeStart == nil {
|
|
|
|
t.Fatal("missing TLSHandshakeStart event")
|
|
|
|
}
|
|
|
|
if events[0].TLSHandshakeStart.DurationSinceBeginning == 0 {
|
|
|
|
t.Fatal("expected nonzero DurationSinceBeginning")
|
|
|
|
}
|
|
|
|
if events[0].TLSHandshakeStart.SNI != "www.kernel.org" {
|
|
|
|
t.Fatal("expected nonzero SNI")
|
|
|
|
}
|
|
|
|
if events[1].TLSHandshakeDone == nil {
|
|
|
|
t.Fatal("missing TLSHandshakeDone event")
|
|
|
|
}
|
|
|
|
if events[1].TLSHandshakeDone.DurationSinceBeginning == 0 {
|
|
|
|
t.Fatal("expected nonzero DurationSinceBeginning")
|
|
|
|
}
|
|
|
|
}
|