refactor(simplequicping): use step-by-step (#852)

See https://github.com/ooni/probe/issues/2159 and https://github.com/ooni/spec/pull/254
This commit is contained in:
DecFox 2022-08-17 12:49:11 +05:30 committed by GitHub
commit 69602abe8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 922 additions and 26 deletions

View file

@ -6,9 +6,11 @@ import (
"errors"
"net"
"reflect"
"syscall"
"testing"
"time"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/model/mocks"
"github.com/ooni/probe-cli/v3/internal/netxlite"
@ -76,6 +78,12 @@ func TestNewTrace(t *testing.T) {
}
})
t.Run("NewQUICDialerWithoutResolverFn is nil", func(t *testing.T) {
if trace.NewQUICDialerWithoutResolverFn != nil {
t.Fatal("expected nil NewQUICDialerQithoutResolverFn")
}
})
t.Run("DNSLookup has the expected buffer size", func(t *testing.T) {
ff := &testingx.FakeFiller{}
var idx int
@ -133,6 +141,25 @@ func TestNewTrace(t *testing.T) {
}
})
t.Run("QUICHandshake has the expected buffer size", func(t *testing.T) {
ff := &testingx.FakeFiller{}
var idx int
Loop:
for {
ev := &model.ArchivalTLSOrQUICHandshakeResult{}
ff.Fill(ev)
select {
case trace.QUICHandshake <- ev:
idx++
default:
break Loop
}
}
if idx != QUICHandshakeBufferSize {
t.Fatal("invalid QUICHandshake channel buffer size")
}
})
t.Run("TimeNowFn is nil", func(t *testing.T) {
if trace.TimeNowFn != nil {
t.Fatal("expected nil TimeNowFn")
@ -380,6 +407,73 @@ func TestTrace(t *testing.T) {
})
})
t.Run("NewQUICDialerWithoutResolverFn works as intended", func(t *testing.T) {
t.Run("when not nil", func(t *testing.T) {
mockedErr := errors.New("mocked")
tx := &Trace{
NewQUICDialerWithoutResolverFn: func(listener model.QUICListener, dl model.DebugLogger) model.QUICDialer {
return &mocks.QUICDialer{
MockDialContext: func(ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
return nil, mockedErr
},
}
},
}
qdx := tx.newQUICDialerWithoutResolver(&mocks.QUICListener{}, model.DiscardLogger)
ctx := context.Background()
qconn, err := qdx.DialContext(ctx, "udp", "1.1.1.1:443", &tls.Config{}, &quic.Config{})
if !errors.Is(err, mockedErr) {
t.Fatal("unexpected err", err)
}
if qconn != nil {
t.Fatal("expected nil conn")
}
})
t.Run("when nil", func(t *testing.T) {
mockedErr := errors.New("mocked")
tx := &Trace{
NewQUICDialerWithoutResolverFn: nil, // explicit
}
pconn := &mocks.UDPLikeConn{
MockLocalAddr: func() net.Addr {
return &net.UDPAddr{
Port: 0,
}
},
MockRemoteAddr: func() net.Addr {
return &net.UDPAddr{
Port: 0,
}
},
MockSyscallConn: func() (syscall.RawConn, error) {
return nil, mockedErr
},
MockClose: func() error {
return nil
},
}
listener := &mocks.QUICListener{
MockListen: func(addr *net.UDPAddr) (model.UDPLikeConn, error) {
return pconn, nil
},
}
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
dialer := tx.newQUICDialerWithoutResolver(listener, model.DiscardLogger)
ctx := context.Background()
qconn, err := dialer.DialContext(ctx, "udp", "1.1.1.1:443", tlsConfig, &quic.Config{})
if !errors.Is(err, mockedErr) {
t.Fatal("unexpected err", err)
}
if qconn != nil {
t.Fatal("expected nil conn")
}
})
})
t.Run("TimeNowFn works as intended", func(t *testing.T) {
fixedTime := time.Date(2022, 01, 01, 00, 00, 00, 00, time.UTC)
tx := &Trace{