2021-06-25 17:04:24 +02:00
|
|
|
package netxlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"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"
|
2021-09-05 14:49:38 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite/quicx"
|
2021-06-25 17:04:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestQUICDialerQUICGoCannotSplitHostPort(t *testing.T) {
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "www.google.com",
|
|
|
|
}
|
2021-09-05 14:49:38 +02:00
|
|
|
systemdialer := quicDialerQUICGo{
|
|
|
|
QUICListener: &quicListenerStdlib{},
|
2021-06-25 17:04:24 +02:00
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
sess, err := systemdialer.DialContext(
|
|
|
|
ctx, "udp", "a.b.c.d", tlsConfig, &quic.Config{})
|
|
|
|
if err == nil || !strings.HasSuffix(err.Error(), "missing port in address") {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if sess != nil {
|
|
|
|
t.Fatal("expected nil sess here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQUICDialerQUICGoInvalidPort(t *testing.T) {
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "www.google.com",
|
|
|
|
}
|
2021-09-05 14:49:38 +02:00
|
|
|
systemdialer := quicDialerQUICGo{
|
|
|
|
QUICListener: &quicListenerStdlib{},
|
2021-06-25 17:04:24 +02:00
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
sess, err := systemdialer.DialContext(
|
|
|
|
ctx, "udp", "8.8.4.4:xyz", tlsConfig, &quic.Config{})
|
|
|
|
if err == nil || !strings.HasSuffix(err.Error(), "invalid syntax") {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if sess != nil {
|
|
|
|
t.Fatal("expected nil sess here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQUICDialerQUICGoInvalidIP(t *testing.T) {
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "www.google.com",
|
|
|
|
}
|
2021-09-05 14:49:38 +02:00
|
|
|
systemdialer := quicDialerQUICGo{
|
|
|
|
QUICListener: &quicListenerStdlib{},
|
2021-06-25 17:04:24 +02:00
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
sess, err := systemdialer.DialContext(
|
|
|
|
ctx, "udp", "a.b.c.d:0", tlsConfig, &quic.Config{})
|
|
|
|
if !errors.Is(err, errInvalidIP) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if sess != nil {
|
|
|
|
t.Fatal("expected nil sess here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQUICDialerQUICGoCannotListen(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "www.google.com",
|
|
|
|
}
|
2021-09-05 14:49:38 +02:00
|
|
|
systemdialer := quicDialerQUICGo{
|
|
|
|
QUICListener: &mocks.QUICListener{
|
2021-08-23 16:49:22 +02:00
|
|
|
MockListen: func(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
|
2021-06-25 17:04:24 +02:00
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
sess, err := systemdialer.DialContext(
|
|
|
|
ctx, "udp", "8.8.8.8:443", tlsConfig, &quic.Config{})
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if sess != nil {
|
|
|
|
t.Fatal("expected nil sess here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 20:51:59 +02:00
|
|
|
func TestQUICDialerQUICGoCannotPerformHandshake(t *testing.T) {
|
2021-06-25 17:58:42 +02:00
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "dns.google",
|
|
|
|
}
|
2021-09-05 14:49:38 +02:00
|
|
|
systemdialer := quicDialerQUICGo{
|
|
|
|
QUICListener: &quicListenerStdlib{},
|
2021-06-25 17:58:42 +02:00
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel() // fail immediately
|
|
|
|
sess, err := systemdialer.DialContext(
|
|
|
|
ctx, "udp", "8.8.8.8:443", tlsConfig, &quic.Config{})
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if sess != nil {
|
|
|
|
log.Fatal("expected nil session here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 20:51:59 +02:00
|
|
|
func TestQUICDialerQUICGoWorksAsIntended(t *testing.T) {
|
2021-06-25 17:04:24 +02:00
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "dns.google",
|
|
|
|
}
|
2021-09-05 14:49:38 +02:00
|
|
|
systemdialer := quicDialerQUICGo{
|
|
|
|
QUICListener: &quicListenerStdlib{},
|
2021-06-25 17:04:24 +02:00
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
sess, err := systemdialer.DialContext(
|
|
|
|
ctx, "udp", "8.8.8.8:443", tlsConfig, &quic.Config{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
2021-06-25 17:58:42 +02:00
|
|
|
<-sess.HandshakeComplete().Done()
|
2021-06-25 17:04:24 +02:00
|
|
|
if err := sess.CloseWithError(0, ""); err != nil {
|
2021-06-26 16:54:02 +02:00
|
|
|
t.Fatal(err)
|
2021-06-25 17:04:24 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-25 18:38:13 +02:00
|
|
|
|
2021-06-25 20:51:59 +02:00
|
|
|
func TestQUICDialerQUICGoTLSDefaultsForWeb(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
var gotTLSConfig *tls.Config
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "dns.google",
|
|
|
|
}
|
2021-09-05 14:49:38 +02:00
|
|
|
systemdialer := quicDialerQUICGo{
|
|
|
|
QUICListener: &quicListenerStdlib{},
|
2021-06-25 20:51:59 +02:00
|
|
|
mockDialEarlyContext: func(ctx context.Context, pconn net.PacketConn,
|
|
|
|
remoteAddr net.Addr, host string, tlsConfig *tls.Config,
|
|
|
|
quicConfig *quic.Config) (quic.EarlySession, error) {
|
|
|
|
gotTLSConfig = tlsConfig
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
sess, err := systemdialer.DialContext(
|
|
|
|
ctx, "udp", "8.8.8.8:443", tlsConfig, &quic.Config{})
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if sess != nil {
|
|
|
|
t.Fatal("expected nil session here")
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQUICDialerQUICGoTLSDefaultsForDoQ(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
var gotTLSConfig *tls.Config
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "dns.google",
|
|
|
|
}
|
2021-09-05 14:49:38 +02:00
|
|
|
systemdialer := quicDialerQUICGo{
|
|
|
|
QUICListener: &quicListenerStdlib{},
|
2021-06-25 20:51:59 +02:00
|
|
|
mockDialEarlyContext: func(ctx context.Context, pconn net.PacketConn,
|
|
|
|
remoteAddr net.Addr, host string, tlsConfig *tls.Config,
|
|
|
|
quicConfig *quic.Config) (quic.EarlySession, error) {
|
|
|
|
gotTLSConfig = tlsConfig
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
sess, err := systemdialer.DialContext(
|
|
|
|
ctx, "udp", "8.8.8.8:8853", tlsConfig, &quic.Config{})
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if sess != nil {
|
|
|
|
t.Fatal("expected nil session here")
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:38:13 +02:00
|
|
|
func TestQUICDialerResolverSuccess(t *testing.T) {
|
2021-06-25 20:51:59 +02:00
|
|
|
tlsConfig := &tls.Config{}
|
2021-09-05 14:49:38 +02:00
|
|
|
dialer := &quicDialerResolver{
|
2021-09-05 18:03:50 +02:00
|
|
|
Resolver: NewResolver(&ResolverConfig{Logger: log.Log}),
|
|
|
|
Dialer: &quicDialerQUICGo{
|
2021-09-05 14:49:38 +02:00
|
|
|
QUICListener: &quicListenerStdlib{},
|
2021-06-25 18:38:13 +02:00
|
|
|
}}
|
|
|
|
sess, err := dialer.DialContext(
|
|
|
|
context.Background(), "udp", "www.google.com:443",
|
|
|
|
tlsConfig, &quic.Config{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
<-sess.HandshakeComplete().Done()
|
|
|
|
if err := sess.CloseWithError(0, ""); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQUICDialerResolverNoPort(t *testing.T) {
|
2021-06-25 20:51:59 +02:00
|
|
|
tlsConfig := &tls.Config{}
|
2021-09-05 14:49:38 +02:00
|
|
|
dialer := &quicDialerResolver{
|
2021-09-05 18:03:50 +02:00
|
|
|
Resolver: NewResolver(&ResolverConfig{Logger: log.Log}),
|
|
|
|
Dialer: &quicDialerQUICGo{}}
|
2021-06-25 18:38:13 +02:00
|
|
|
sess, err := dialer.DialContext(
|
|
|
|
context.Background(), "udp", "www.google.com",
|
|
|
|
tlsConfig, &quic.Config{})
|
|
|
|
if err == nil || !strings.HasSuffix(err.Error(), "missing port in address") {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if sess != nil {
|
|
|
|
t.Fatal("expected a nil sess here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQUICDialerResolverLookupHostAddress(t *testing.T) {
|
2021-09-05 14:49:38 +02:00
|
|
|
dialer := &quicDialerResolver{Resolver: &mocks.Resolver{
|
2021-06-25 18:38:13 +02:00
|
|
|
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")
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQUICDialerResolverLookupHostFailure(t *testing.T) {
|
2021-06-25 20:51:59 +02:00
|
|
|
tlsConfig := &tls.Config{}
|
2021-06-25 18:38:13 +02:00
|
|
|
expected := errors.New("mocked error")
|
2021-09-05 14:49:38 +02:00
|
|
|
dialer := &quicDialerResolver{Resolver: &mocks.Resolver{
|
2021-06-25 18:38:13 +02:00
|
|
|
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
sess, err := dialer.DialContext(
|
|
|
|
context.Background(), "udp", "dns.google.com:853",
|
|
|
|
tlsConfig, &quic.Config{})
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if sess != nil {
|
|
|
|
t.Fatal("expected nil sess")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQUICDialerResolverInvalidPort(t *testing.T) {
|
|
|
|
// This test allows us to check for the case where every attempt
|
|
|
|
// to establish a connection leads to a failure
|
2021-06-25 20:51:59 +02:00
|
|
|
tlsConf := &tls.Config{}
|
2021-09-05 14:49:38 +02:00
|
|
|
dialer := &quicDialerResolver{
|
2021-09-05 18:03:50 +02:00
|
|
|
Resolver: NewResolver(&ResolverConfig{Logger: log.Log}),
|
|
|
|
Dialer: &quicDialerQUICGo{
|
2021-09-05 14:49:38 +02:00
|
|
|
QUICListener: &quicListenerStdlib{},
|
2021-06-25 18:38:13 +02:00
|
|
|
}}
|
|
|
|
sess, err := dialer.DialContext(
|
|
|
|
context.Background(), "udp", "www.google.com:0",
|
|
|
|
tlsConf, &quic.Config{})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected an error here")
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(err.Error(), "sendto: invalid argument") &&
|
|
|
|
!strings.HasSuffix(err.Error(), "sendto: can't assign requested address") {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if sess != nil {
|
|
|
|
t.Fatal("expected nil sess")
|
|
|
|
}
|
|
|
|
}
|
2021-06-25 20:51:59 +02:00
|
|
|
|
|
|
|
func TestQUICDialerResolverApplyTLSDefaults(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
var gotTLSConfig *tls.Config
|
|
|
|
tlsConfig := &tls.Config{}
|
2021-09-05 14:49:38 +02:00
|
|
|
dialer := &quicDialerResolver{
|
2021-09-05 18:03:50 +02:00
|
|
|
Resolver: NewResolver(&ResolverConfig{Logger: log.Log}),
|
|
|
|
Dialer: &mocks.QUICContextDialer{
|
2021-06-25 20:51:59 +02:00
|
|
|
MockDialContext: func(ctx context.Context, network, address string,
|
|
|
|
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) {
|
|
|
|
gotTLSConfig = tlsConfig
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
sess, err := dialer.DialContext(
|
|
|
|
context.Background(), "udp", "www.google.com:443",
|
|
|
|
tlsConfig, &quic.Config{})
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if sess != nil {
|
|
|
|
t.Fatal("expected nil session here")
|
|
|
|
}
|
|
|
|
if tlsConfig.ServerName != "" {
|
|
|
|
t.Fatal("should not have changed tlsConfig.ServerName")
|
|
|
|
}
|
|
|
|
if gotTLSConfig.ServerName != "www.google.com" {
|
|
|
|
t.Fatal("gotTLSConfig.ServerName has not been set")
|
|
|
|
}
|
|
|
|
}
|
2021-06-26 16:54:02 +02:00
|
|
|
|
|
|
|
func TestQUICDialerLoggerSuccess(t *testing.T) {
|
2021-09-05 14:49:38 +02:00
|
|
|
d := &quicDialerLogger{
|
|
|
|
Dialer: &mocks.QUICContextDialer{
|
2021-06-26 16:54:02 +02:00
|
|
|
MockDialContext: func(ctx context.Context, network string,
|
|
|
|
address string, tlsConfig *tls.Config,
|
|
|
|
quicConfig *quic.Config) (quic.EarlySession, error) {
|
2021-09-05 14:49:38 +02:00
|
|
|
return &mocks.QUICEarlySession{
|
2021-06-26 16:54:02 +02:00
|
|
|
MockCloseWithError: func(
|
|
|
|
code quic.ApplicationErrorCode, reason string) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Logger: log.Log,
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
tlsConfig := &tls.Config{}
|
|
|
|
quicConfig := &quic.Config{}
|
|
|
|
sess, err := d.DialContext(ctx, "udp", "8.8.8.8:443", tlsConfig, quicConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := sess.CloseWithError(0, ""); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQUICDialerLoggerFailure(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
2021-09-05 14:49:38 +02:00
|
|
|
d := &quicDialerLogger{
|
|
|
|
Dialer: &mocks.QUICContextDialer{
|
2021-06-26 16:54:02 +02:00
|
|
|
MockDialContext: func(ctx context.Context, network string,
|
|
|
|
address string, tlsConfig *tls.Config,
|
|
|
|
quicConfig *quic.Config) (quic.EarlySession, error) {
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Logger: log.Log,
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
tlsConfig := &tls.Config{}
|
|
|
|
quicConfig := &quic.Config{}
|
|
|
|
sess, err := d.DialContext(ctx, "udp", "8.8.8.8:443", tlsConfig, quicConfig)
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if sess != nil {
|
|
|
|
t.Fatal("expected nil session")
|
|
|
|
}
|
|
|
|
}
|