2021-06-25 17:04:24 +02:00
|
|
|
package netxlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
2021-09-07 19:56:42 +02:00
|
|
|
"io"
|
2021-06-25 17:04:24 +02:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-06-26 16:54:02 +02:00
|
|
|
"github.com/apex/log"
|
2021-06-25 20:51:59 +02:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-06-25 17:04:24 +02:00
|
|
|
"github.com/lucas-clemente/quic-go"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
2021-06-25 17:04:24 +02:00
|
|
|
)
|
|
|
|
|
2021-09-08 22:48:10 +02:00
|
|
|
func TestNewQUICListener(t *testing.T) {
|
|
|
|
ql := NewQUICListener()
|
|
|
|
qew := ql.(*quicListenerErrWrapper)
|
|
|
|
_ = qew.QUICListener.(*quicListenerStdlib)
|
|
|
|
}
|
|
|
|
|
2022-05-31 20:28:25 +02:00
|
|
|
type extensionQUICDialerFirst struct {
|
|
|
|
model.QUICDialer
|
|
|
|
}
|
|
|
|
|
2022-06-01 08:31:20 +02:00
|
|
|
type quicDialerWrapperFirst struct{}
|
|
|
|
|
|
|
|
func (*quicDialerWrapperFirst) WrapQUICDialer(qd model.QUICDialer) model.QUICDialer {
|
|
|
|
return &extensionQUICDialerFirst{qd}
|
|
|
|
}
|
|
|
|
|
2022-05-31 20:28:25 +02:00
|
|
|
type extensionQUICDialerSecond struct {
|
|
|
|
model.QUICDialer
|
|
|
|
}
|
|
|
|
|
2022-06-01 08:31:20 +02:00
|
|
|
type quicDialerWrapperSecond struct {
|
|
|
|
model.QUICDialer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*quicDialerWrapperSecond) WrapQUICDialer(qd model.QUICDialer) model.QUICDialer {
|
|
|
|
return &extensionQUICDialerSecond{qd}
|
|
|
|
}
|
|
|
|
|
2021-09-08 22:48:10 +02:00
|
|
|
func TestNewQUICDialer(t *testing.T) {
|
|
|
|
ql := NewQUICListener()
|
2022-06-01 08:31:20 +02:00
|
|
|
extensions := []model.QUICDialerWrapper{
|
|
|
|
&quicDialerWrapperFirst{},
|
|
|
|
nil, // explicitly test for this documented case
|
|
|
|
&quicDialerWrapperSecond{},
|
2022-05-31 20:28:25 +02:00
|
|
|
}
|
|
|
|
dlr := NewQUICDialerWithoutResolver(ql, log.Log, extensions...)
|
2021-09-08 22:48:10 +02:00
|
|
|
logger := dlr.(*quicDialerLogger)
|
|
|
|
if logger.Logger != log.Log {
|
|
|
|
t.Fatal("invalid logger")
|
|
|
|
}
|
|
|
|
resolver := logger.Dialer.(*quicDialerResolver)
|
2022-05-31 20:02:11 +02:00
|
|
|
if _, okay := resolver.Resolver.(*NullResolver); !okay {
|
2021-09-08 22:48:10 +02:00
|
|
|
t.Fatal("invalid resolver type")
|
|
|
|
}
|
|
|
|
logger = resolver.Dialer.(*quicDialerLogger)
|
|
|
|
if logger.Logger != log.Log {
|
|
|
|
t.Fatal("invalid logger")
|
|
|
|
}
|
2022-05-31 20:28:25 +02:00
|
|
|
ext2 := logger.Dialer.(*extensionQUICDialerSecond)
|
|
|
|
ext1 := ext2.QUICDialer.(*extensionQUICDialerFirst)
|
|
|
|
errWrapper := ext1.QUICDialer.(*quicDialerErrWrapper)
|
2022-05-14 16:32:32 +02:00
|
|
|
handshakeCompleter := errWrapper.QUICDialer.(*quicDialerHandshakeCompleter)
|
|
|
|
base := handshakeCompleter.Dialer.(*quicDialerQUICGo)
|
2021-09-08 22:48:10 +02:00
|
|
|
if base.QUICListener != ql {
|
|
|
|
t.Fatal("invalid quic listener")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 17:15:08 +02:00
|
|
|
func TestParseUDPAddr(t *testing.T) {
|
|
|
|
t.Run("cannot split host and port", func(t *testing.T) {
|
|
|
|
addr, err := ParseUDPAddr("1.2.3.4")
|
|
|
|
if err == nil || !strings.HasSuffix(err.Error(), "missing port in address") {
|
|
|
|
t.Fatal("unexpected error", err)
|
|
|
|
}
|
|
|
|
if addr != nil {
|
|
|
|
t.Fatal("expected nil addr")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with invalid IP addr", func(t *testing.T) {
|
|
|
|
addr, err := ParseUDPAddr("www.google.com:80")
|
|
|
|
if !errors.Is(err, ErrInvalidIP) {
|
|
|
|
t.Fatal("unexpected error", err)
|
|
|
|
}
|
|
|
|
if addr != nil {
|
|
|
|
t.Fatal("expected nil addr")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with invalid port", func(t *testing.T) {
|
|
|
|
addr, err := ParseUDPAddr("8.8.8.8:www")
|
|
|
|
if err == nil || !strings.HasSuffix(err.Error(), "invalid syntax") {
|
|
|
|
t.Fatal("unexpected error", err)
|
|
|
|
}
|
|
|
|
if addr != nil {
|
|
|
|
t.Fatal("expected nil addr")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with valid input", func(t *testing.T) {
|
|
|
|
addr, err := ParseUDPAddr("8.8.8.8:80")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if addr.IP.String() != "8.8.8.8" {
|
|
|
|
t.Fatal("invalid IP")
|
|
|
|
}
|
|
|
|
if addr.Port != 80 {
|
|
|
|
t.Fatal("invalid port")
|
|
|
|
}
|
|
|
|
if addr.Zone != "" {
|
|
|
|
t.Fatal("invalid zone")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-08 11:39:27 +02:00
|
|
|
func TestQUICDialerQUICGo(t *testing.T) {
|
|
|
|
t.Run("DialContext", func(t *testing.T) {
|
|
|
|
t.Run("cannot split host port", func(t *testing.T) {
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "www.google.com",
|
|
|
|
}
|
|
|
|
systemdialer := quicDialerQUICGo{
|
|
|
|
QUICListener: &quicListenerStdlib{},
|
|
|
|
}
|
|
|
|
defer systemdialer.CloseIdleConnections() // just to see it running
|
|
|
|
ctx := context.Background()
|
2022-05-06 12:24:03 +02:00
|
|
|
qconn, err := systemdialer.DialContext(
|
2022-08-19 11:26:50 +02:00
|
|
|
ctx, "a.b.c.d", tlsConfig, &quic.Config{})
|
2021-09-08 11:39:27 +02:00
|
|
|
if err == nil || !strings.HasSuffix(err.Error(), "missing port in address") {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if qconn != nil {
|
|
|
|
t.Fatal("expected nil connection here")
|
2021-09-08 11:39:27 +02:00
|
|
|
}
|
|
|
|
})
|
2021-06-25 17:04:24 +02:00
|
|
|
|
2021-09-08 11:39:27 +02:00
|
|
|
t.Run("with invalid port", func(t *testing.T) {
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "www.google.com",
|
|
|
|
}
|
|
|
|
systemdialer := quicDialerQUICGo{
|
|
|
|
QUICListener: &quicListenerStdlib{},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
2022-05-06 12:24:03 +02:00
|
|
|
qconn, err := systemdialer.DialContext(
|
2022-08-19 11:26:50 +02:00
|
|
|
ctx, "8.8.4.4:xyz", tlsConfig, &quic.Config{})
|
2021-09-08 11:39:27 +02:00
|
|
|
if err == nil || !strings.HasSuffix(err.Error(), "invalid syntax") {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if qconn != nil {
|
|
|
|
t.Fatal("expected nil connection here")
|
2021-09-08 11:39:27 +02:00
|
|
|
}
|
|
|
|
})
|
2021-06-25 17:04:24 +02:00
|
|
|
|
2021-09-08 11:39:27 +02:00
|
|
|
t.Run("with invalid IP", func(t *testing.T) {
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "www.google.com",
|
|
|
|
}
|
|
|
|
systemdialer := quicDialerQUICGo{
|
|
|
|
QUICListener: &quicListenerStdlib{},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
2022-05-06 12:24:03 +02:00
|
|
|
qconn, err := systemdialer.DialContext(
|
2022-08-19 11:26:50 +02:00
|
|
|
ctx, "a.b.c.d:0", tlsConfig, &quic.Config{})
|
2022-05-13 15:32:47 +02:00
|
|
|
if !errors.Is(err, ErrInvalidIP) {
|
2021-09-08 11:39:27 +02:00
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if qconn != nil {
|
|
|
|
t.Fatal("expected nil connection here")
|
2021-09-08 11:39:27 +02:00
|
|
|
}
|
|
|
|
})
|
2021-06-25 17:04:24 +02:00
|
|
|
|
2021-09-08 11:39:27 +02:00
|
|
|
t.Run("with listen error", func(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "www.google.com",
|
|
|
|
}
|
|
|
|
systemdialer := quicDialerQUICGo{
|
|
|
|
QUICListener: &mocks.QUICListener{
|
2022-01-03 13:53:23 +01:00
|
|
|
MockListen: func(addr *net.UDPAddr) (model.UDPLikeConn, error) {
|
2021-09-08 11:39:27 +02:00
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
2022-05-06 12:24:03 +02:00
|
|
|
qconn, err := systemdialer.DialContext(
|
2022-08-19 11:26:50 +02:00
|
|
|
ctx, "8.8.8.8:443", tlsConfig, &quic.Config{})
|
2021-09-08 11:39:27 +02:00
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if qconn != nil {
|
|
|
|
t.Fatal("expected nil connection here")
|
2021-09-08 11:39:27 +02:00
|
|
|
}
|
|
|
|
})
|
2021-06-25 17:04:24 +02:00
|
|
|
|
2021-09-08 11:39:27 +02:00
|
|
|
t.Run("with handshake failure", func(t *testing.T) {
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "dns.google",
|
|
|
|
}
|
|
|
|
systemdialer := quicDialerQUICGo{
|
|
|
|
QUICListener: &quicListenerStdlib{},
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel() // fail immediately
|
2022-05-06 12:24:03 +02:00
|
|
|
qconn, err := systemdialer.DialContext(
|
2022-08-19 11:26:50 +02:00
|
|
|
ctx, "8.8.8.8:443", tlsConfig, &quic.Config{})
|
2021-09-08 11:39:27 +02:00
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if qconn != nil {
|
|
|
|
log.Fatal("expected nil connection here")
|
2021-09-08 11:39:27 +02:00
|
|
|
}
|
|
|
|
})
|
2021-06-25 17:58:42 +02:00
|
|
|
|
2021-09-08 11:39:27 +02:00
|
|
|
t.Run("TLS defaults for web", func(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
var gotTLSConfig *tls.Config
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "dns.google",
|
|
|
|
}
|
|
|
|
systemdialer := quicDialerQUICGo{
|
|
|
|
QUICListener: &quicListenerStdlib{},
|
|
|
|
mockDialEarlyContext: func(ctx context.Context, pconn net.PacketConn,
|
|
|
|
remoteAddr net.Addr, host string, tlsConfig *tls.Config,
|
2022-05-06 12:24:03 +02:00
|
|
|
quicConfig *quic.Config) (quic.EarlyConnection, error) {
|
2021-09-08 11:39:27 +02:00
|
|
|
gotTLSConfig = tlsConfig
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
2022-05-06 12:24:03 +02:00
|
|
|
qconn, err := systemdialer.DialContext(
|
2022-08-19 11:26:50 +02:00
|
|
|
ctx, "8.8.8.8:443", tlsConfig, &quic.Config{})
|
2021-09-08 11:39:27 +02:00
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if qconn != nil {
|
|
|
|
t.Fatal("expected nil connection here")
|
2021-09-08 11:39:27 +02:00
|
|
|
}
|
|
|
|
if tlsConfig.RootCAs != nil {
|
|
|
|
t.Fatal("tlsConfig.RootCAs should not have been changed")
|
|
|
|
}
|
|
|
|
if gotTLSConfig.RootCAs != defaultCertPool {
|
|
|
|
t.Fatal("invalid gotTLSConfig.RootCAs")
|
|
|
|
}
|
|
|
|
if tlsConfig.NextProtos != nil {
|
|
|
|
t.Fatal("tlsConfig.NextProtos should not have been changed")
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(gotTLSConfig.NextProtos, []string{"h3"}); diff != "" {
|
|
|
|
t.Fatal("invalid gotTLSConfig.NextProtos", diff)
|
|
|
|
}
|
|
|
|
if tlsConfig.ServerName != gotTLSConfig.ServerName {
|
|
|
|
t.Fatal("the ServerName field must match")
|
|
|
|
}
|
|
|
|
})
|
2021-06-25 20:51:59 +02:00
|
|
|
|
2021-09-08 11:39:27 +02:00
|
|
|
t.Run("TLS defaults for DoQ", func(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
var gotTLSConfig *tls.Config
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "dns.google",
|
|
|
|
}
|
|
|
|
systemdialer := quicDialerQUICGo{
|
|
|
|
QUICListener: &quicListenerStdlib{},
|
|
|
|
mockDialEarlyContext: func(ctx context.Context, pconn net.PacketConn,
|
|
|
|
remoteAddr net.Addr, host string, tlsConfig *tls.Config,
|
2022-05-06 12:24:03 +02:00
|
|
|
quicConfig *quic.Config) (quic.EarlyConnection, error) {
|
2021-09-08 11:39:27 +02:00
|
|
|
gotTLSConfig = tlsConfig
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
2022-05-06 12:24:03 +02:00
|
|
|
qconn, err := systemdialer.DialContext(
|
2022-08-19 11:26:50 +02:00
|
|
|
ctx, "8.8.8.8:8853", tlsConfig, &quic.Config{})
|
2021-09-08 11:39:27 +02:00
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if qconn != nil {
|
|
|
|
t.Fatal("expected nil connection here")
|
2021-09-08 11:39:27 +02:00
|
|
|
}
|
|
|
|
if tlsConfig.RootCAs != nil {
|
|
|
|
t.Fatal("tlsConfig.RootCAs should not have been changed")
|
|
|
|
}
|
|
|
|
if gotTLSConfig.RootCAs != defaultCertPool {
|
|
|
|
t.Fatal("invalid gotTLSConfig.RootCAs")
|
|
|
|
}
|
|
|
|
if tlsConfig.NextProtos != nil {
|
|
|
|
t.Fatal("tlsConfig.NextProtos should not have been changed")
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(gotTLSConfig.NextProtos, []string{"dq"}); diff != "" {
|
|
|
|
t.Fatal("invalid gotTLSConfig.NextProtos", diff)
|
|
|
|
}
|
|
|
|
if tlsConfig.ServerName != gotTLSConfig.ServerName {
|
|
|
|
t.Fatal("the ServerName field must match")
|
|
|
|
}
|
|
|
|
})
|
2022-06-04 14:58:48 +02:00
|
|
|
|
|
|
|
t.Run("returns a quicDialerOwnConn in case of success", func(t *testing.T) {
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "dns.google",
|
|
|
|
}
|
|
|
|
fakeconn := &mocks.QUICEarlyConnection{}
|
|
|
|
systemdialer := quicDialerQUICGo{
|
|
|
|
QUICListener: &quicListenerStdlib{},
|
|
|
|
mockDialEarlyContext: func(ctx context.Context, pconn net.PacketConn,
|
|
|
|
remoteAddr net.Addr, host string, tlsConfig *tls.Config,
|
|
|
|
quicConfig *quic.Config) (quic.EarlyConnection, error) {
|
|
|
|
return fakeconn, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
qconn, err := systemdialer.DialContext(
|
2022-08-19 11:26:50 +02:00
|
|
|
ctx, "8.8.8.8:443", tlsConfig, &quic.Config{})
|
2022-06-04 14:58:48 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
connOwner := qconn.(*quicConnectionOwnsConn)
|
|
|
|
if connOwner.EarlyConnection != fakeconn {
|
|
|
|
t.Fatal("invalid underlying conn")
|
|
|
|
}
|
|
|
|
})
|
2021-09-08 11:39:27 +02:00
|
|
|
})
|
2021-06-25 20:51:59 +02:00
|
|
|
}
|
|
|
|
|
2022-05-14 16:32:32 +02:00
|
|
|
func TestQUICDialerHandshakeCompleter(t *testing.T) {
|
|
|
|
t.Run("DialContext", func(t *testing.T) {
|
|
|
|
t.Run("in case of failure", func(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
d := &quicDialerHandshakeCompleter{
|
|
|
|
Dialer: &mocks.QUICDialer{
|
2022-08-19 11:26:50 +02:00
|
|
|
MockDialContext: func(ctx context.Context, address string,
|
2022-05-14 16:32:32 +02:00
|
|
|
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
2022-08-19 11:26:50 +02:00
|
|
|
conn, err := d.DialContext(ctx, "8.8.8.8:443", &tls.Config{}, &quic.Config{})
|
2022-05-14 16:32:32 +02:00
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
|
|
|
if conn != nil {
|
|
|
|
t.Fatal("expected nil conn")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("in case of context cancellation", func(t *testing.T) {
|
|
|
|
handshakeCtx, handshakeCancel := context.WithCancel(context.Background())
|
|
|
|
defer handshakeCancel()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var called bool
|
|
|
|
expected := &mocks.QUICEarlyConnection{
|
|
|
|
MockHandshakeComplete: func() context.Context {
|
|
|
|
cancel()
|
|
|
|
return handshakeCtx
|
|
|
|
},
|
|
|
|
MockCloseWithError: func(code quic.ApplicationErrorCode, reason string) error {
|
|
|
|
called = true
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
d := &quicDialerHandshakeCompleter{
|
|
|
|
Dialer: &mocks.QUICDialer{
|
2022-08-19 11:26:50 +02:00
|
|
|
MockDialContext: func(ctx context.Context, address string,
|
2022-05-14 16:32:32 +02:00
|
|
|
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
|
|
|
|
return expected, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-08-19 11:26:50 +02:00
|
|
|
conn, err := d.DialContext(ctx, "8.8.8.8:443", &tls.Config{}, &quic.Config{})
|
2022-05-14 16:32:32 +02:00
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
|
|
|
if conn != nil {
|
|
|
|
t.Fatal("expected nil conn")
|
|
|
|
}
|
|
|
|
if !called {
|
|
|
|
t.Fatal("not called")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("in case of success", func(t *testing.T) {
|
|
|
|
handshakeCtx, handshakeCancel := context.WithCancel(context.Background())
|
|
|
|
defer handshakeCancel()
|
|
|
|
expected := &mocks.QUICEarlyConnection{
|
|
|
|
MockHandshakeComplete: func() context.Context {
|
|
|
|
handshakeCancel()
|
|
|
|
return handshakeCtx
|
|
|
|
},
|
|
|
|
}
|
|
|
|
d := &quicDialerHandshakeCompleter{
|
|
|
|
Dialer: &mocks.QUICDialer{
|
2022-08-19 11:26:50 +02:00
|
|
|
MockDialContext: func(ctx context.Context, address string,
|
2022-05-14 16:32:32 +02:00
|
|
|
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
|
|
|
|
return expected, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
conn, err := d.DialContext(
|
2022-08-19 11:26:50 +02:00
|
|
|
context.Background(), "8.8.8.8:443", &tls.Config{}, &quic.Config{})
|
2022-05-14 16:32:32 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if conn == nil {
|
|
|
|
t.Fatal("expected non-nil conn")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("CloseIdleConnections", func(t *testing.T) {
|
|
|
|
var forDialer bool
|
|
|
|
d := &quicDialerHandshakeCompleter{
|
|
|
|
Dialer: &mocks.QUICDialer{
|
|
|
|
MockCloseIdleConnections: func() {
|
|
|
|
forDialer = true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
d.CloseIdleConnections()
|
|
|
|
if !forDialer {
|
|
|
|
t.Fatal("not called")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-04 14:58:48 +02:00
|
|
|
func TestQUICConnectionOwnsConn(t *testing.T) {
|
|
|
|
var (
|
|
|
|
quicClose bool
|
|
|
|
udpClose bool
|
|
|
|
)
|
|
|
|
qconn := &mocks.QUICEarlyConnection{
|
|
|
|
MockCloseWithError: func(code quic.ApplicationErrorCode, reason string) error {
|
|
|
|
quicClose = true
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
pconn := &mocks.UDPLikeConn{
|
|
|
|
MockClose: func() error {
|
|
|
|
udpClose = true
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
conn := newQUICConnectionOwnsConn(qconn, pconn)
|
|
|
|
conn.CloseWithError(0, "")
|
|
|
|
if !quicClose {
|
|
|
|
t.Fatal("did not call qconn.CloseWithError")
|
|
|
|
}
|
|
|
|
if !udpClose {
|
|
|
|
t.Fatal("did not call pconn.Close")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 11:39:27 +02:00
|
|
|
func TestQUICDialerResolver(t *testing.T) {
|
|
|
|
t.Run("CloseIdleConnections", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
forDialer bool
|
|
|
|
forResolver bool
|
|
|
|
)
|
|
|
|
d := &quicDialerResolver{
|
|
|
|
Dialer: &mocks.QUICDialer{
|
|
|
|
MockCloseIdleConnections: func() {
|
|
|
|
forDialer = true
|
|
|
|
},
|
2021-09-06 20:56:14 +02:00
|
|
|
},
|
2021-09-08 11:39:27 +02:00
|
|
|
Resolver: &mocks.Resolver{
|
|
|
|
MockCloseIdleConnections: func() {
|
|
|
|
forResolver = true
|
|
|
|
},
|
2021-09-06 20:56:14 +02:00
|
|
|
},
|
2021-09-08 11:39:27 +02:00
|
|
|
}
|
|
|
|
d.CloseIdleConnections()
|
|
|
|
if !forDialer || !forResolver {
|
|
|
|
t.Fatal("not called")
|
|
|
|
}
|
|
|
|
})
|
2021-09-06 20:56:14 +02:00
|
|
|
|
2021-09-08 11:39:27 +02:00
|
|
|
t.Run("DialContext", func(t *testing.T) {
|
|
|
|
t.Run("with missing port", func(t *testing.T) {
|
|
|
|
tlsConfig := &tls.Config{}
|
|
|
|
dialer := &quicDialerResolver{
|
2022-06-06 14:46:44 +02:00
|
|
|
Resolver: NewStdlibResolver(log.Log),
|
2021-09-08 11:39:27 +02:00
|
|
|
Dialer: &quicDialerQUICGo{}}
|
2022-05-06 12:24:03 +02:00
|
|
|
qconn, err := dialer.DialContext(
|
2022-08-19 11:26:50 +02:00
|
|
|
context.Background(), "www.google.com",
|
2021-09-08 11:39:27 +02:00
|
|
|
tlsConfig, &quic.Config{})
|
|
|
|
if err == nil || !strings.HasSuffix(err.Error(), "missing port in address") {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if qconn != nil {
|
|
|
|
t.Fatal("expected a nil connection here")
|
2021-09-08 11:39:27 +02:00
|
|
|
}
|
|
|
|
})
|
2021-06-25 18:38:13 +02:00
|
|
|
|
2021-09-08 11:39:27 +02:00
|
|
|
t.Run("with lookup host failure", func(t *testing.T) {
|
|
|
|
tlsConfig := &tls.Config{}
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
dialer := &quicDialerResolver{Resolver: &mocks.Resolver{
|
|
|
|
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
}}
|
2022-05-06 12:24:03 +02:00
|
|
|
qconn, err := dialer.DialContext(
|
2022-08-19 11:26:50 +02:00
|
|
|
context.Background(), "dns.google.com:853",
|
2021-09-08 11:39:27 +02:00
|
|
|
tlsConfig, &quic.Config{})
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if qconn != nil {
|
|
|
|
t.Fatal("expected nil connection")
|
2021-09-08 11:39:27 +02:00
|
|
|
}
|
|
|
|
})
|
2021-06-25 18:38:13 +02:00
|
|
|
|
2021-09-29 16:04:26 +02:00
|
|
|
t.Run("with invalid, non-numeric port)", func(t *testing.T) {
|
2021-09-08 11:39:27 +02:00
|
|
|
// This test allows us to check for the case where every attempt
|
|
|
|
// to establish a connection leads to a failure
|
|
|
|
tlsConf := &tls.Config{}
|
|
|
|
dialer := &quicDialerResolver{
|
2022-06-06 14:46:44 +02:00
|
|
|
Resolver: NewStdlibResolver(log.Log),
|
2021-09-08 11:39:27 +02:00
|
|
|
Dialer: &quicDialerQUICGo{
|
|
|
|
QUICListener: &quicListenerStdlib{},
|
|
|
|
}}
|
2022-05-06 12:24:03 +02:00
|
|
|
qconn, err := dialer.DialContext(
|
2022-08-19 11:26:50 +02:00
|
|
|
context.Background(), "8.8.4.4:x",
|
2021-09-08 11:39:27 +02:00
|
|
|
tlsConf, &quic.Config{})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected an error here")
|
|
|
|
}
|
2021-09-29 16:04:26 +02:00
|
|
|
if !strings.HasSuffix(err.Error(), "invalid syntax") {
|
2021-09-08 11:39:27 +02:00
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if qconn != nil {
|
|
|
|
t.Fatal("expected nil connection")
|
2021-09-08 11:39:27 +02:00
|
|
|
}
|
|
|
|
})
|
2021-06-25 18:38:13 +02:00
|
|
|
|
2021-09-08 11:39:27 +02:00
|
|
|
t.Run("we apply TLS defaults", func(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
var gotTLSConfig *tls.Config
|
|
|
|
tlsConfig := &tls.Config{}
|
|
|
|
dialer := &quicDialerResolver{
|
2022-06-06 14:46:44 +02:00
|
|
|
Resolver: NewStdlibResolver(log.Log),
|
2021-09-08 11:39:27 +02:00
|
|
|
Dialer: &mocks.QUICDialer{
|
2022-08-19 11:26:50 +02:00
|
|
|
MockDialContext: func(ctx context.Context, address string,
|
2022-05-06 12:24:03 +02:00
|
|
|
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
|
2021-09-08 11:39:27 +02:00
|
|
|
gotTLSConfig = tlsConfig
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
}}
|
2022-05-06 12:24:03 +02:00
|
|
|
qconn, err := dialer.DialContext(
|
2022-08-19 11:26:50 +02:00
|
|
|
context.Background(), "8.8.4.4:443",
|
2021-09-08 11:39:27 +02:00
|
|
|
tlsConfig, &quic.Config{})
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if qconn != nil {
|
|
|
|
t.Fatal("expected nil connection here")
|
2021-09-08 11:39:27 +02:00
|
|
|
}
|
|
|
|
if tlsConfig.ServerName != "" {
|
|
|
|
t.Fatal("should not have changed tlsConfig.ServerName")
|
|
|
|
}
|
2021-09-29 16:04:26 +02:00
|
|
|
if gotTLSConfig.ServerName != "8.8.4.4" {
|
2021-09-08 11:39:27 +02:00
|
|
|
t.Fatal("gotTLSConfig.ServerName has not been set")
|
|
|
|
}
|
|
|
|
})
|
2022-06-04 14:58:48 +02:00
|
|
|
|
|
|
|
t.Run("on success", func(t *testing.T) {
|
|
|
|
expectedQConn := &mocks.QUICEarlyConnection{}
|
|
|
|
dialer := &quicDialerResolver{
|
2022-06-06 14:46:44 +02:00
|
|
|
Resolver: NewStdlibResolver(log.Log),
|
2022-06-04 14:58:48 +02:00
|
|
|
Dialer: &mocks.QUICDialer{
|
2022-08-19 11:26:50 +02:00
|
|
|
MockDialContext: func(ctx context.Context, address string,
|
2022-06-04 14:58:48 +02:00
|
|
|
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
|
|
|
|
return expectedQConn, nil
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
qconn, err := dialer.DialContext(
|
2022-08-19 11:26:50 +02:00
|
|
|
context.Background(), "8.8.4.4:443",
|
2022-06-04 14:58:48 +02:00
|
|
|
&tls.Config{}, &quic.Config{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if qconn != expectedQConn {
|
|
|
|
t.Fatal("unexpected underlying qconn")
|
|
|
|
}
|
|
|
|
})
|
2021-09-08 11:39:27 +02:00
|
|
|
})
|
2021-06-25 20:51:59 +02:00
|
|
|
|
2021-09-08 11:39:27 +02:00
|
|
|
t.Run("lookup host with address", func(t *testing.T) {
|
|
|
|
dialer := &quicDialerResolver{Resolver: &mocks.Resolver{
|
|
|
|
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
|
|
|
// We should not arrive here and call this function but if we do then
|
|
|
|
// there is going to be an error that fails this test.
|
|
|
|
return nil, errors.New("mocked error")
|
2021-06-25 20:51:59 +02:00
|
|
|
},
|
|
|
|
}}
|
2021-09-08 11:39:27 +02:00
|
|
|
addrs, err := dialer.lookupHost(context.Background(), "1.1.1.1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(addrs) != 1 || addrs[0] != "1.1.1.1" {
|
|
|
|
t.Fatal("not the result we expected")
|
|
|
|
}
|
|
|
|
})
|
2021-06-25 20:51:59 +02:00
|
|
|
}
|
2021-06-26 16:54:02 +02:00
|
|
|
|
2021-09-08 11:39:27 +02:00
|
|
|
func TestQUICLoggerDialer(t *testing.T) {
|
|
|
|
t.Run("CloseIdleConnections", func(t *testing.T) {
|
|
|
|
var forDialer bool
|
|
|
|
d := &quicDialerLogger{
|
|
|
|
Dialer: &mocks.QUICDialer{
|
|
|
|
MockCloseIdleConnections: func() {
|
|
|
|
forDialer = true
|
|
|
|
},
|
2021-09-06 20:56:14 +02:00
|
|
|
},
|
2021-09-08 11:39:27 +02:00
|
|
|
}
|
|
|
|
d.CloseIdleConnections()
|
|
|
|
if !forDialer {
|
|
|
|
t.Fatal("not called")
|
|
|
|
}
|
|
|
|
})
|
2021-09-06 20:56:14 +02:00
|
|
|
|
2021-09-08 11:39:27 +02:00
|
|
|
t.Run("DialContext", func(t *testing.T) {
|
|
|
|
t.Run("on success", func(t *testing.T) {
|
2021-09-08 22:48:10 +02:00
|
|
|
var called int
|
|
|
|
lo := &mocks.Logger{
|
|
|
|
MockDebugf: func(format string, v ...interface{}) {
|
|
|
|
called++
|
|
|
|
},
|
|
|
|
}
|
2021-09-08 11:39:27 +02:00
|
|
|
d := &quicDialerLogger{
|
|
|
|
Dialer: &mocks.QUICDialer{
|
2022-08-19 11:26:50 +02:00
|
|
|
MockDialContext: func(ctx context.Context,
|
2021-09-08 11:39:27 +02:00
|
|
|
address string, tlsConfig *tls.Config,
|
2022-05-06 12:24:03 +02:00
|
|
|
quicConfig *quic.Config) (quic.EarlyConnection, error) {
|
|
|
|
return &mocks.QUICEarlyConnection{
|
2021-09-08 11:39:27 +02:00
|
|
|
MockCloseWithError: func(
|
|
|
|
code quic.ApplicationErrorCode, reason string) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}, nil
|
2021-06-26 16:54:02 +02:00
|
|
|
},
|
2021-09-08 11:39:27 +02:00
|
|
|
},
|
2021-09-08 22:48:10 +02:00
|
|
|
Logger: lo,
|
2021-09-08 11:39:27 +02:00
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
tlsConfig := &tls.Config{}
|
|
|
|
quicConfig := &quic.Config{}
|
2022-08-19 11:26:50 +02:00
|
|
|
qconn, err := d.DialContext(ctx, "8.8.8.8:443", tlsConfig, quicConfig)
|
2021-09-08 11:39:27 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if err := qconn.CloseWithError(0, ""); err != nil {
|
2021-09-08 11:39:27 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-09-08 22:48:10 +02:00
|
|
|
if called != 2 {
|
|
|
|
t.Fatal("invalid number of calls")
|
|
|
|
}
|
2021-09-08 11:39:27 +02:00
|
|
|
})
|
2021-06-26 16:54:02 +02:00
|
|
|
|
2021-09-08 11:39:27 +02:00
|
|
|
t.Run("on failure", func(t *testing.T) {
|
2021-09-08 22:48:10 +02:00
|
|
|
var called int
|
|
|
|
lo := &mocks.Logger{
|
|
|
|
MockDebugf: func(format string, v ...interface{}) {
|
|
|
|
called++
|
|
|
|
},
|
|
|
|
}
|
2021-09-08 11:39:27 +02:00
|
|
|
expected := errors.New("mocked error")
|
|
|
|
d := &quicDialerLogger{
|
|
|
|
Dialer: &mocks.QUICDialer{
|
2022-08-19 11:26:50 +02:00
|
|
|
MockDialContext: func(ctx context.Context,
|
2021-09-08 11:39:27 +02:00
|
|
|
address string, tlsConfig *tls.Config,
|
2022-05-06 12:24:03 +02:00
|
|
|
quicConfig *quic.Config) (quic.EarlyConnection, error) {
|
2021-09-08 11:39:27 +02:00
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
},
|
2021-09-08 22:48:10 +02:00
|
|
|
Logger: lo,
|
2021-09-08 11:39:27 +02:00
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
tlsConfig := &tls.Config{}
|
|
|
|
quicConfig := &quic.Config{}
|
2022-08-19 11:26:50 +02:00
|
|
|
qconn, err := d.DialContext(ctx, "8.8.8.8:443", tlsConfig, quicConfig)
|
2021-09-08 11:39:27 +02:00
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if qconn != nil {
|
|
|
|
t.Fatal("expected nil connection")
|
2021-09-08 11:39:27 +02:00
|
|
|
}
|
2021-09-08 22:48:10 +02:00
|
|
|
if called != 2 {
|
|
|
|
t.Fatal("invalid number of calls")
|
|
|
|
}
|
2021-09-08 11:39:27 +02:00
|
|
|
})
|
|
|
|
})
|
2021-06-26 16:54:02 +02:00
|
|
|
}
|
2021-09-06 21:34:14 +02:00
|
|
|
|
2021-09-08 11:39:27 +02:00
|
|
|
func TestNewSingleUseQUICDialer(t *testing.T) {
|
2022-05-06 12:24:03 +02:00
|
|
|
qconn := &mocks.QUICEarlyConnection{}
|
|
|
|
qd := NewSingleUseQUICDialer(qconn)
|
2021-09-27 14:14:17 +02:00
|
|
|
defer qd.CloseIdleConnections()
|
2022-05-06 12:24:03 +02:00
|
|
|
outconn, err := qd.DialContext(
|
2022-08-19 11:26:50 +02:00
|
|
|
context.Background(), "", &tls.Config{}, &quic.Config{})
|
2021-09-06 22:14:49 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if qconn != outconn {
|
|
|
|
t.Fatal("invalid outconn")
|
2021-09-06 22:14:49 +02:00
|
|
|
}
|
|
|
|
for i := 0; i < 4; i++ {
|
2022-05-06 12:24:03 +02:00
|
|
|
outconn, err = qd.DialContext(
|
2022-08-19 11:26:50 +02:00
|
|
|
context.Background(), "", &tls.Config{}, &quic.Config{})
|
2021-09-06 22:14:49 +02:00
|
|
|
if !errors.Is(err, ErrNoConnReuse) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if outconn != nil {
|
2021-09-06 22:14:49 +02:00
|
|
|
t.Fatal("expected nil outconn here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-07 19:56:42 +02:00
|
|
|
|
|
|
|
func TestQUICListenerErrWrapper(t *testing.T) {
|
|
|
|
t.Run("Listen", func(t *testing.T) {
|
|
|
|
t.Run("on success", func(t *testing.T) {
|
feature: merge measurex and netx archival layer (1/N) (#663)
This diff introduces a new package called `./internal/archival`. This package collects data from `./internal/model` network interfaces (e.g., `Dialer`, `QUICDialer`, `HTTPTransport`), saves such data into an internal tabular data format suitable for on-line processing and analysis, and allows exporting data into the OONI data format.
The code for collecting and the internal tabular data formats are adapted from `measurex`. The code for formatting and exporting OONI data-format-compliant structures is adapted from `netx/archival`.
My original objective was to _also_ (1) fully replace `netx/archival` with this package and (2) adapt `measurex` to use this package rather than its own code. Both operations seem easily feasible because: (a) this code is `measurex` code without extensions that are `measurex` related, which will need to be added back as part of the process; (b) the API provided by this code allows for trivially converting from using `netx/archival` to using this code.
Yet, both changes should not be taken lightly. After implementing them, there's need to spend some time doing QA and ensuring all nettests work as intended. However, I am planning a release in the next two weeks, and this QA task is likely going to defer the release. For this reason, I have chosen to commit the work done so far into the tree and defer the second part of this refactoring for a later moment in time. (This explains why the title mentions "1/N").
On a more high-level perspective, it would also be beneficial, I guess, to explain _why_ I am doing these changes. There are two intertwined reasons. The first reason is that `netx/archival` has shortcomings deriving from its original https://github.com/ooni/netx legacy. The most relevant shortcoming is that it saves all kind of data into the same tabular structure named `Event`. This design choice is unfortunate because it does not allow one to apply data-type specific logic when processing the results. In turn, this choice results in complex processing code. Therefore, I believe that replacing the code with event-specific data structures is clearly an improvement in terms of code maintainability and would quite likely lead us to more confidently change and evolve the codebase.
The second reason why I would like to move forward these changes is to unify the codepaths used for measuring. At this point in time, we basically have two codepaths: `./internal/engine/netx` and `./internal/measurex`. They both have pros and cons and I don't think we want to rewrite whole experiments using `netx`. Rather, what we probably want is to gradually merge these two codepaths such that `netx` is a set of abstractions on top of `measurex` (which is more low-level and has a more-easily-testable design). Because saving events and generating an archival data format out of them consists of at least 50% of the complexity of both `netx` and `measurex`, it seems reasonable to unify this archival-related part of the two codebases as the first step.
At the highest level of abstraction, these changes are part of the train of changes which will eventually lead us to bless `websteps` as a first class citizen in OONI land. Because `websteps` requires different underlying primitives, I chose to develop these primitives from scratch rather than wrestling with `netx`, which used another model. The model used by `websteps` is that we perform each operation in isolation and immediately we save the results, while `netx` creates whole data structures and collects all the events happening via tracing. We believe the model used by `websteps` to be better because it does not require your code to figure out everything that happened after the measurement, which is a source of subtle bugs in the current implementation. So, when I started implementing websteps I extracted the bits of `netx` that could also be beneficial to `websteps` into a separate library, thus `netxlite` was born.
The reference issue describing merging the archival of `netx` and `measurex` is https://github.com/ooni/probe/issues/1957. As of this writing the issue still references the original plan, which I could not complete by the end of this Sprint, so I am going to adapt the text of the issue to only refer to what was done in here next. Of course, I also need follow-up issues.
2022-01-14 12:13:10 +01:00
|
|
|
expectedConn := &mocks.UDPLikeConn{}
|
2021-09-07 19:56:42 +02:00
|
|
|
ql := &quicListenerErrWrapper{
|
|
|
|
QUICListener: &mocks.QUICListener{
|
2022-01-03 13:53:23 +01:00
|
|
|
MockListen: func(addr *net.UDPAddr) (model.UDPLikeConn, error) {
|
2021-09-07 19:56:42 +02:00
|
|
|
return expectedConn, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
conn, err := ql.Listen(&net.UDPAddr{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ewconn := conn.(*quicErrWrapperUDPLikeConn)
|
|
|
|
if ewconn.UDPLikeConn != expectedConn {
|
|
|
|
t.Fatal("unexpected conn")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("on failure", func(t *testing.T) {
|
|
|
|
expectedErr := io.EOF
|
|
|
|
ql := &quicListenerErrWrapper{
|
|
|
|
QUICListener: &mocks.QUICListener{
|
2022-01-03 13:53:23 +01:00
|
|
|
MockListen: func(addr *net.UDPAddr) (model.UDPLikeConn, error) {
|
2021-09-07 19:56:42 +02:00
|
|
|
return nil, expectedErr
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
conn, err := ql.Listen(&net.UDPAddr{})
|
2021-09-28 12:42:01 +02:00
|
|
|
if err == nil || err.Error() != FailureEOFError {
|
2021-09-07 19:56:42 +02:00
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
|
|
|
if conn != nil {
|
|
|
|
t.Fatal("expected nil conn")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQUICErrWrapperUDPLikeConn(t *testing.T) {
|
|
|
|
t.Run("ReadFrom", func(t *testing.T) {
|
|
|
|
t.Run("on success", func(t *testing.T) {
|
|
|
|
expectedAddr := &net.UDPAddr{}
|
|
|
|
p := make([]byte, 128)
|
|
|
|
conn := &quicErrWrapperUDPLikeConn{
|
feature: merge measurex and netx archival layer (1/N) (#663)
This diff introduces a new package called `./internal/archival`. This package collects data from `./internal/model` network interfaces (e.g., `Dialer`, `QUICDialer`, `HTTPTransport`), saves such data into an internal tabular data format suitable for on-line processing and analysis, and allows exporting data into the OONI data format.
The code for collecting and the internal tabular data formats are adapted from `measurex`. The code for formatting and exporting OONI data-format-compliant structures is adapted from `netx/archival`.
My original objective was to _also_ (1) fully replace `netx/archival` with this package and (2) adapt `measurex` to use this package rather than its own code. Both operations seem easily feasible because: (a) this code is `measurex` code without extensions that are `measurex` related, which will need to be added back as part of the process; (b) the API provided by this code allows for trivially converting from using `netx/archival` to using this code.
Yet, both changes should not be taken lightly. After implementing them, there's need to spend some time doing QA and ensuring all nettests work as intended. However, I am planning a release in the next two weeks, and this QA task is likely going to defer the release. For this reason, I have chosen to commit the work done so far into the tree and defer the second part of this refactoring for a later moment in time. (This explains why the title mentions "1/N").
On a more high-level perspective, it would also be beneficial, I guess, to explain _why_ I am doing these changes. There are two intertwined reasons. The first reason is that `netx/archival` has shortcomings deriving from its original https://github.com/ooni/netx legacy. The most relevant shortcoming is that it saves all kind of data into the same tabular structure named `Event`. This design choice is unfortunate because it does not allow one to apply data-type specific logic when processing the results. In turn, this choice results in complex processing code. Therefore, I believe that replacing the code with event-specific data structures is clearly an improvement in terms of code maintainability and would quite likely lead us to more confidently change and evolve the codebase.
The second reason why I would like to move forward these changes is to unify the codepaths used for measuring. At this point in time, we basically have two codepaths: `./internal/engine/netx` and `./internal/measurex`. They both have pros and cons and I don't think we want to rewrite whole experiments using `netx`. Rather, what we probably want is to gradually merge these two codepaths such that `netx` is a set of abstractions on top of `measurex` (which is more low-level and has a more-easily-testable design). Because saving events and generating an archival data format out of them consists of at least 50% of the complexity of both `netx` and `measurex`, it seems reasonable to unify this archival-related part of the two codebases as the first step.
At the highest level of abstraction, these changes are part of the train of changes which will eventually lead us to bless `websteps` as a first class citizen in OONI land. Because `websteps` requires different underlying primitives, I chose to develop these primitives from scratch rather than wrestling with `netx`, which used another model. The model used by `websteps` is that we perform each operation in isolation and immediately we save the results, while `netx` creates whole data structures and collects all the events happening via tracing. We believe the model used by `websteps` to be better because it does not require your code to figure out everything that happened after the measurement, which is a source of subtle bugs in the current implementation. So, when I started implementing websteps I extracted the bits of `netx` that could also be beneficial to `websteps` into a separate library, thus `netxlite` was born.
The reference issue describing merging the archival of `netx` and `measurex` is https://github.com/ooni/probe/issues/1957. As of this writing the issue still references the original plan, which I could not complete by the end of this Sprint, so I am going to adapt the text of the issue to only refer to what was done in here next. Of course, I also need follow-up issues.
2022-01-14 12:13:10 +01:00
|
|
|
UDPLikeConn: &mocks.UDPLikeConn{
|
2021-09-07 19:56:42 +02:00
|
|
|
MockReadFrom: func(p []byte) (n int, addr net.Addr, err error) {
|
|
|
|
return len(p), expectedAddr, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
count, addr, err := conn.ReadFrom(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if count != len(p) {
|
|
|
|
t.Fatal("unexpected count")
|
|
|
|
}
|
|
|
|
if addr != expectedAddr {
|
|
|
|
t.Fatal("unexpected addr")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("on failure", func(t *testing.T) {
|
|
|
|
p := make([]byte, 128)
|
|
|
|
expectedErr := io.EOF
|
|
|
|
conn := &quicErrWrapperUDPLikeConn{
|
feature: merge measurex and netx archival layer (1/N) (#663)
This diff introduces a new package called `./internal/archival`. This package collects data from `./internal/model` network interfaces (e.g., `Dialer`, `QUICDialer`, `HTTPTransport`), saves such data into an internal tabular data format suitable for on-line processing and analysis, and allows exporting data into the OONI data format.
The code for collecting and the internal tabular data formats are adapted from `measurex`. The code for formatting and exporting OONI data-format-compliant structures is adapted from `netx/archival`.
My original objective was to _also_ (1) fully replace `netx/archival` with this package and (2) adapt `measurex` to use this package rather than its own code. Both operations seem easily feasible because: (a) this code is `measurex` code without extensions that are `measurex` related, which will need to be added back as part of the process; (b) the API provided by this code allows for trivially converting from using `netx/archival` to using this code.
Yet, both changes should not be taken lightly. After implementing them, there's need to spend some time doing QA and ensuring all nettests work as intended. However, I am planning a release in the next two weeks, and this QA task is likely going to defer the release. For this reason, I have chosen to commit the work done so far into the tree and defer the second part of this refactoring for a later moment in time. (This explains why the title mentions "1/N").
On a more high-level perspective, it would also be beneficial, I guess, to explain _why_ I am doing these changes. There are two intertwined reasons. The first reason is that `netx/archival` has shortcomings deriving from its original https://github.com/ooni/netx legacy. The most relevant shortcoming is that it saves all kind of data into the same tabular structure named `Event`. This design choice is unfortunate because it does not allow one to apply data-type specific logic when processing the results. In turn, this choice results in complex processing code. Therefore, I believe that replacing the code with event-specific data structures is clearly an improvement in terms of code maintainability and would quite likely lead us to more confidently change and evolve the codebase.
The second reason why I would like to move forward these changes is to unify the codepaths used for measuring. At this point in time, we basically have two codepaths: `./internal/engine/netx` and `./internal/measurex`. They both have pros and cons and I don't think we want to rewrite whole experiments using `netx`. Rather, what we probably want is to gradually merge these two codepaths such that `netx` is a set of abstractions on top of `measurex` (which is more low-level and has a more-easily-testable design). Because saving events and generating an archival data format out of them consists of at least 50% of the complexity of both `netx` and `measurex`, it seems reasonable to unify this archival-related part of the two codebases as the first step.
At the highest level of abstraction, these changes are part of the train of changes which will eventually lead us to bless `websteps` as a first class citizen in OONI land. Because `websteps` requires different underlying primitives, I chose to develop these primitives from scratch rather than wrestling with `netx`, which used another model. The model used by `websteps` is that we perform each operation in isolation and immediately we save the results, while `netx` creates whole data structures and collects all the events happening via tracing. We believe the model used by `websteps` to be better because it does not require your code to figure out everything that happened after the measurement, which is a source of subtle bugs in the current implementation. So, when I started implementing websteps I extracted the bits of `netx` that could also be beneficial to `websteps` into a separate library, thus `netxlite` was born.
The reference issue describing merging the archival of `netx` and `measurex` is https://github.com/ooni/probe/issues/1957. As of this writing the issue still references the original plan, which I could not complete by the end of this Sprint, so I am going to adapt the text of the issue to only refer to what was done in here next. Of course, I also need follow-up issues.
2022-01-14 12:13:10 +01:00
|
|
|
UDPLikeConn: &mocks.UDPLikeConn{
|
2021-09-07 19:56:42 +02:00
|
|
|
MockReadFrom: func(p []byte) (n int, addr net.Addr, err error) {
|
|
|
|
return 0, nil, expectedErr
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
count, addr, err := conn.ReadFrom(p)
|
2021-09-28 12:42:01 +02:00
|
|
|
if err == nil || err.Error() != FailureEOFError {
|
2021-09-07 19:56:42 +02:00
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
|
|
|
if count != 0 {
|
|
|
|
t.Fatal("unexpected count")
|
|
|
|
}
|
|
|
|
if addr != nil {
|
|
|
|
t.Fatal("unexpected addr")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("WriteTo", func(t *testing.T) {
|
|
|
|
t.Run("on success", func(t *testing.T) {
|
|
|
|
p := make([]byte, 128)
|
|
|
|
conn := &quicErrWrapperUDPLikeConn{
|
feature: merge measurex and netx archival layer (1/N) (#663)
This diff introduces a new package called `./internal/archival`. This package collects data from `./internal/model` network interfaces (e.g., `Dialer`, `QUICDialer`, `HTTPTransport`), saves such data into an internal tabular data format suitable for on-line processing and analysis, and allows exporting data into the OONI data format.
The code for collecting and the internal tabular data formats are adapted from `measurex`. The code for formatting and exporting OONI data-format-compliant structures is adapted from `netx/archival`.
My original objective was to _also_ (1) fully replace `netx/archival` with this package and (2) adapt `measurex` to use this package rather than its own code. Both operations seem easily feasible because: (a) this code is `measurex` code without extensions that are `measurex` related, which will need to be added back as part of the process; (b) the API provided by this code allows for trivially converting from using `netx/archival` to using this code.
Yet, both changes should not be taken lightly. After implementing them, there's need to spend some time doing QA and ensuring all nettests work as intended. However, I am planning a release in the next two weeks, and this QA task is likely going to defer the release. For this reason, I have chosen to commit the work done so far into the tree and defer the second part of this refactoring for a later moment in time. (This explains why the title mentions "1/N").
On a more high-level perspective, it would also be beneficial, I guess, to explain _why_ I am doing these changes. There are two intertwined reasons. The first reason is that `netx/archival` has shortcomings deriving from its original https://github.com/ooni/netx legacy. The most relevant shortcoming is that it saves all kind of data into the same tabular structure named `Event`. This design choice is unfortunate because it does not allow one to apply data-type specific logic when processing the results. In turn, this choice results in complex processing code. Therefore, I believe that replacing the code with event-specific data structures is clearly an improvement in terms of code maintainability and would quite likely lead us to more confidently change and evolve the codebase.
The second reason why I would like to move forward these changes is to unify the codepaths used for measuring. At this point in time, we basically have two codepaths: `./internal/engine/netx` and `./internal/measurex`. They both have pros and cons and I don't think we want to rewrite whole experiments using `netx`. Rather, what we probably want is to gradually merge these two codepaths such that `netx` is a set of abstractions on top of `measurex` (which is more low-level and has a more-easily-testable design). Because saving events and generating an archival data format out of them consists of at least 50% of the complexity of both `netx` and `measurex`, it seems reasonable to unify this archival-related part of the two codebases as the first step.
At the highest level of abstraction, these changes are part of the train of changes which will eventually lead us to bless `websteps` as a first class citizen in OONI land. Because `websteps` requires different underlying primitives, I chose to develop these primitives from scratch rather than wrestling with `netx`, which used another model. The model used by `websteps` is that we perform each operation in isolation and immediately we save the results, while `netx` creates whole data structures and collects all the events happening via tracing. We believe the model used by `websteps` to be better because it does not require your code to figure out everything that happened after the measurement, which is a source of subtle bugs in the current implementation. So, when I started implementing websteps I extracted the bits of `netx` that could also be beneficial to `websteps` into a separate library, thus `netxlite` was born.
The reference issue describing merging the archival of `netx` and `measurex` is https://github.com/ooni/probe/issues/1957. As of this writing the issue still references the original plan, which I could not complete by the end of this Sprint, so I am going to adapt the text of the issue to only refer to what was done in here next. Of course, I also need follow-up issues.
2022-01-14 12:13:10 +01:00
|
|
|
UDPLikeConn: &mocks.UDPLikeConn{
|
2021-09-07 19:56:42 +02:00
|
|
|
MockWriteTo: func(p []byte, addr net.Addr) (int, error) {
|
|
|
|
return len(p), nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
count, err := conn.WriteTo(p, &net.UDPAddr{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if count != len(p) {
|
|
|
|
t.Fatal("unexpected count")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("on failure", func(t *testing.T) {
|
|
|
|
p := make([]byte, 128)
|
|
|
|
expectedErr := io.EOF
|
|
|
|
conn := &quicErrWrapperUDPLikeConn{
|
feature: merge measurex and netx archival layer (1/N) (#663)
This diff introduces a new package called `./internal/archival`. This package collects data from `./internal/model` network interfaces (e.g., `Dialer`, `QUICDialer`, `HTTPTransport`), saves such data into an internal tabular data format suitable for on-line processing and analysis, and allows exporting data into the OONI data format.
The code for collecting and the internal tabular data formats are adapted from `measurex`. The code for formatting and exporting OONI data-format-compliant structures is adapted from `netx/archival`.
My original objective was to _also_ (1) fully replace `netx/archival` with this package and (2) adapt `measurex` to use this package rather than its own code. Both operations seem easily feasible because: (a) this code is `measurex` code without extensions that are `measurex` related, which will need to be added back as part of the process; (b) the API provided by this code allows for trivially converting from using `netx/archival` to using this code.
Yet, both changes should not be taken lightly. After implementing them, there's need to spend some time doing QA and ensuring all nettests work as intended. However, I am planning a release in the next two weeks, and this QA task is likely going to defer the release. For this reason, I have chosen to commit the work done so far into the tree and defer the second part of this refactoring for a later moment in time. (This explains why the title mentions "1/N").
On a more high-level perspective, it would also be beneficial, I guess, to explain _why_ I am doing these changes. There are two intertwined reasons. The first reason is that `netx/archival` has shortcomings deriving from its original https://github.com/ooni/netx legacy. The most relevant shortcoming is that it saves all kind of data into the same tabular structure named `Event`. This design choice is unfortunate because it does not allow one to apply data-type specific logic when processing the results. In turn, this choice results in complex processing code. Therefore, I believe that replacing the code with event-specific data structures is clearly an improvement in terms of code maintainability and would quite likely lead us to more confidently change and evolve the codebase.
The second reason why I would like to move forward these changes is to unify the codepaths used for measuring. At this point in time, we basically have two codepaths: `./internal/engine/netx` and `./internal/measurex`. They both have pros and cons and I don't think we want to rewrite whole experiments using `netx`. Rather, what we probably want is to gradually merge these two codepaths such that `netx` is a set of abstractions on top of `measurex` (which is more low-level and has a more-easily-testable design). Because saving events and generating an archival data format out of them consists of at least 50% of the complexity of both `netx` and `measurex`, it seems reasonable to unify this archival-related part of the two codebases as the first step.
At the highest level of abstraction, these changes are part of the train of changes which will eventually lead us to bless `websteps` as a first class citizen in OONI land. Because `websteps` requires different underlying primitives, I chose to develop these primitives from scratch rather than wrestling with `netx`, which used another model. The model used by `websteps` is that we perform each operation in isolation and immediately we save the results, while `netx` creates whole data structures and collects all the events happening via tracing. We believe the model used by `websteps` to be better because it does not require your code to figure out everything that happened after the measurement, which is a source of subtle bugs in the current implementation. So, when I started implementing websteps I extracted the bits of `netx` that could also be beneficial to `websteps` into a separate library, thus `netxlite` was born.
The reference issue describing merging the archival of `netx` and `measurex` is https://github.com/ooni/probe/issues/1957. As of this writing the issue still references the original plan, which I could not complete by the end of this Sprint, so I am going to adapt the text of the issue to only refer to what was done in here next. Of course, I also need follow-up issues.
2022-01-14 12:13:10 +01:00
|
|
|
UDPLikeConn: &mocks.UDPLikeConn{
|
2021-09-07 19:56:42 +02:00
|
|
|
MockWriteTo: func(p []byte, addr net.Addr) (int, error) {
|
|
|
|
return 0, expectedErr
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
count, err := conn.WriteTo(p, &net.UDPAddr{})
|
2021-09-28 12:42:01 +02:00
|
|
|
if err == nil || err.Error() != FailureEOFError {
|
2021-09-07 19:56:42 +02:00
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
|
|
|
if count != 0 {
|
|
|
|
t.Fatal("unexpected count")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2021-09-27 14:14:17 +02:00
|
|
|
|
|
|
|
t.Run("Close", func(t *testing.T) {
|
|
|
|
t.Run("on success", func(t *testing.T) {
|
|
|
|
conn := &quicErrWrapperUDPLikeConn{
|
feature: merge measurex and netx archival layer (1/N) (#663)
This diff introduces a new package called `./internal/archival`. This package collects data from `./internal/model` network interfaces (e.g., `Dialer`, `QUICDialer`, `HTTPTransport`), saves such data into an internal tabular data format suitable for on-line processing and analysis, and allows exporting data into the OONI data format.
The code for collecting and the internal tabular data formats are adapted from `measurex`. The code for formatting and exporting OONI data-format-compliant structures is adapted from `netx/archival`.
My original objective was to _also_ (1) fully replace `netx/archival` with this package and (2) adapt `measurex` to use this package rather than its own code. Both operations seem easily feasible because: (a) this code is `measurex` code without extensions that are `measurex` related, which will need to be added back as part of the process; (b) the API provided by this code allows for trivially converting from using `netx/archival` to using this code.
Yet, both changes should not be taken lightly. After implementing them, there's need to spend some time doing QA and ensuring all nettests work as intended. However, I am planning a release in the next two weeks, and this QA task is likely going to defer the release. For this reason, I have chosen to commit the work done so far into the tree and defer the second part of this refactoring for a later moment in time. (This explains why the title mentions "1/N").
On a more high-level perspective, it would also be beneficial, I guess, to explain _why_ I am doing these changes. There are two intertwined reasons. The first reason is that `netx/archival` has shortcomings deriving from its original https://github.com/ooni/netx legacy. The most relevant shortcoming is that it saves all kind of data into the same tabular structure named `Event`. This design choice is unfortunate because it does not allow one to apply data-type specific logic when processing the results. In turn, this choice results in complex processing code. Therefore, I believe that replacing the code with event-specific data structures is clearly an improvement in terms of code maintainability and would quite likely lead us to more confidently change and evolve the codebase.
The second reason why I would like to move forward these changes is to unify the codepaths used for measuring. At this point in time, we basically have two codepaths: `./internal/engine/netx` and `./internal/measurex`. They both have pros and cons and I don't think we want to rewrite whole experiments using `netx`. Rather, what we probably want is to gradually merge these two codepaths such that `netx` is a set of abstractions on top of `measurex` (which is more low-level and has a more-easily-testable design). Because saving events and generating an archival data format out of them consists of at least 50% of the complexity of both `netx` and `measurex`, it seems reasonable to unify this archival-related part of the two codebases as the first step.
At the highest level of abstraction, these changes are part of the train of changes which will eventually lead us to bless `websteps` as a first class citizen in OONI land. Because `websteps` requires different underlying primitives, I chose to develop these primitives from scratch rather than wrestling with `netx`, which used another model. The model used by `websteps` is that we perform each operation in isolation and immediately we save the results, while `netx` creates whole data structures and collects all the events happening via tracing. We believe the model used by `websteps` to be better because it does not require your code to figure out everything that happened after the measurement, which is a source of subtle bugs in the current implementation. So, when I started implementing websteps I extracted the bits of `netx` that could also be beneficial to `websteps` into a separate library, thus `netxlite` was born.
The reference issue describing merging the archival of `netx` and `measurex` is https://github.com/ooni/probe/issues/1957. As of this writing the issue still references the original plan, which I could not complete by the end of this Sprint, so I am going to adapt the text of the issue to only refer to what was done in here next. Of course, I also need follow-up issues.
2022-01-14 12:13:10 +01:00
|
|
|
UDPLikeConn: &mocks.UDPLikeConn{
|
2021-09-27 14:14:17 +02:00
|
|
|
MockClose: func() error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := conn.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("on failure", func(t *testing.T) {
|
|
|
|
expectedErr := io.EOF
|
|
|
|
conn := &quicErrWrapperUDPLikeConn{
|
feature: merge measurex and netx archival layer (1/N) (#663)
This diff introduces a new package called `./internal/archival`. This package collects data from `./internal/model` network interfaces (e.g., `Dialer`, `QUICDialer`, `HTTPTransport`), saves such data into an internal tabular data format suitable for on-line processing and analysis, and allows exporting data into the OONI data format.
The code for collecting and the internal tabular data formats are adapted from `measurex`. The code for formatting and exporting OONI data-format-compliant structures is adapted from `netx/archival`.
My original objective was to _also_ (1) fully replace `netx/archival` with this package and (2) adapt `measurex` to use this package rather than its own code. Both operations seem easily feasible because: (a) this code is `measurex` code without extensions that are `measurex` related, which will need to be added back as part of the process; (b) the API provided by this code allows for trivially converting from using `netx/archival` to using this code.
Yet, both changes should not be taken lightly. After implementing them, there's need to spend some time doing QA and ensuring all nettests work as intended. However, I am planning a release in the next two weeks, and this QA task is likely going to defer the release. For this reason, I have chosen to commit the work done so far into the tree and defer the second part of this refactoring for a later moment in time. (This explains why the title mentions "1/N").
On a more high-level perspective, it would also be beneficial, I guess, to explain _why_ I am doing these changes. There are two intertwined reasons. The first reason is that `netx/archival` has shortcomings deriving from its original https://github.com/ooni/netx legacy. The most relevant shortcoming is that it saves all kind of data into the same tabular structure named `Event`. This design choice is unfortunate because it does not allow one to apply data-type specific logic when processing the results. In turn, this choice results in complex processing code. Therefore, I believe that replacing the code with event-specific data structures is clearly an improvement in terms of code maintainability and would quite likely lead us to more confidently change and evolve the codebase.
The second reason why I would like to move forward these changes is to unify the codepaths used for measuring. At this point in time, we basically have two codepaths: `./internal/engine/netx` and `./internal/measurex`. They both have pros and cons and I don't think we want to rewrite whole experiments using `netx`. Rather, what we probably want is to gradually merge these two codepaths such that `netx` is a set of abstractions on top of `measurex` (which is more low-level and has a more-easily-testable design). Because saving events and generating an archival data format out of them consists of at least 50% of the complexity of both `netx` and `measurex`, it seems reasonable to unify this archival-related part of the two codebases as the first step.
At the highest level of abstraction, these changes are part of the train of changes which will eventually lead us to bless `websteps` as a first class citizen in OONI land. Because `websteps` requires different underlying primitives, I chose to develop these primitives from scratch rather than wrestling with `netx`, which used another model. The model used by `websteps` is that we perform each operation in isolation and immediately we save the results, while `netx` creates whole data structures and collects all the events happening via tracing. We believe the model used by `websteps` to be better because it does not require your code to figure out everything that happened after the measurement, which is a source of subtle bugs in the current implementation. So, when I started implementing websteps I extracted the bits of `netx` that could also be beneficial to `websteps` into a separate library, thus `netxlite` was born.
The reference issue describing merging the archival of `netx` and `measurex` is https://github.com/ooni/probe/issues/1957. As of this writing the issue still references the original plan, which I could not complete by the end of this Sprint, so I am going to adapt the text of the issue to only refer to what was done in here next. Of course, I also need follow-up issues.
2022-01-14 12:13:10 +01:00
|
|
|
UDPLikeConn: &mocks.UDPLikeConn{
|
2021-09-27 14:14:17 +02:00
|
|
|
MockClose: func() error {
|
|
|
|
return expectedErr
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := conn.Close()
|
2021-09-28 12:42:01 +02:00
|
|
|
if err == nil || err.Error() != FailureEOFError {
|
2021-09-27 14:14:17 +02:00
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2021-09-07 19:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestQUICDialerErrWrapper(t *testing.T) {
|
|
|
|
t.Run("CloseIdleConnections", func(t *testing.T) {
|
|
|
|
var called bool
|
|
|
|
d := &quicDialerErrWrapper{
|
|
|
|
QUICDialer: &mocks.QUICDialer{
|
|
|
|
MockCloseIdleConnections: func() {
|
|
|
|
called = true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
d.CloseIdleConnections()
|
|
|
|
if !called {
|
|
|
|
t.Fatal("not called")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("DialContext", func(t *testing.T) {
|
|
|
|
t.Run("on success", func(t *testing.T) {
|
2022-05-06 12:24:03 +02:00
|
|
|
expectedConn := &mocks.QUICEarlyConnection{}
|
2021-09-07 19:56:42 +02:00
|
|
|
d := &quicDialerErrWrapper{
|
|
|
|
QUICDialer: &mocks.QUICDialer{
|
2022-08-19 11:26:50 +02:00
|
|
|
MockDialContext: func(ctx context.Context, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
|
2022-05-06 12:24:03 +02:00
|
|
|
return expectedConn, nil
|
2021-09-07 19:56:42 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
2022-08-19 11:26:50 +02:00
|
|
|
qconn, err := d.DialContext(ctx, "", &tls.Config{}, &quic.Config{})
|
2021-09-07 19:56:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if qconn != expectedConn {
|
|
|
|
t.Fatal("unexpected connection")
|
2021-09-07 19:56:42 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("on failure", func(t *testing.T) {
|
|
|
|
expectedErr := io.EOF
|
|
|
|
d := &quicDialerErrWrapper{
|
|
|
|
QUICDialer: &mocks.QUICDialer{
|
2022-08-19 11:26:50 +02:00
|
|
|
MockDialContext: func(ctx context.Context, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
|
2021-09-07 19:56:42 +02:00
|
|
|
return nil, expectedErr
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
2022-08-19 11:26:50 +02:00
|
|
|
qconn, err := d.DialContext(ctx, "", &tls.Config{}, &quic.Config{})
|
2021-09-28 12:42:01 +02:00
|
|
|
if err == nil || err.Error() != FailureEOFError {
|
2021-09-07 19:56:42 +02:00
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
2022-05-06 12:24:03 +02:00
|
|
|
if qconn != nil {
|
|
|
|
t.Fatal("unexpected connection")
|
2021-09-07 19:56:42 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|