feat(netxlite): add QUICDialerLogger (#410)
Part of https://github.com/ooni/probe/issues/1505
This commit is contained in:
@@ -145,6 +145,8 @@ type QUICDialerResolver struct {
|
||||
Resolver Resolver
|
||||
}
|
||||
|
||||
var _ QUICContextDialer = &QUICDialerResolver{}
|
||||
|
||||
// DialContext implements QUICContextDialer.DialContext. This function
|
||||
// will apply the following TLS defaults:
|
||||
//
|
||||
@@ -195,3 +197,28 @@ func (d *QUICDialerResolver) lookupHost(ctx context.Context, hostname string) ([
|
||||
}
|
||||
return d.Resolver.LookupHost(ctx, hostname)
|
||||
}
|
||||
|
||||
// QUICDialerLogger is a dialer with logging.
|
||||
type QUICDialerLogger struct {
|
||||
// Dialer is the underlying QUIC dialer.
|
||||
Dialer QUICContextDialer
|
||||
|
||||
// Logger is the underlying logger.
|
||||
Logger Logger
|
||||
}
|
||||
|
||||
var _ QUICContextDialer = &QUICDialerLogger{}
|
||||
|
||||
// DialContext implements QUICContextDialer.DialContext.
|
||||
func (d *QUICDialerLogger) DialContext(
|
||||
ctx context.Context, network, address string,
|
||||
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) {
|
||||
d.Logger.Debugf("quic %s/%s...", address, network)
|
||||
sess, err := d.Dialer.DialContext(ctx, network, address, tlsConfig, quicConfig)
|
||||
if err != nil {
|
||||
d.Logger.Debugf("quic %s/%s... %s", address, network, err)
|
||||
return nil, err
|
||||
}
|
||||
d.Logger.Debugf("quic %s/%s... ok", address, network)
|
||||
return sess, nil
|
||||
}
|
||||
|
||||
@@ -4,11 +4,11 @@ import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"log"
|
||||
"net"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxmocks"
|
||||
@@ -125,7 +125,7 @@ func TestQUICDialerQUICGoWorksAsIntended(t *testing.T) {
|
||||
}
|
||||
<-sess.HandshakeComplete().Done()
|
||||
if err := sess.CloseWithError(0, ""); err != nil {
|
||||
log.Fatal(err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -331,3 +331,55 @@ func TestQUICDialerResolverApplyTLSDefaults(t *testing.T) {
|
||||
t.Fatal("gotTLSConfig.ServerName has not been set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestQUICDialerLoggerSuccess(t *testing.T) {
|
||||
d := &QUICDialerLogger{
|
||||
Dialer: &netxmocks.QUICContextDialer{
|
||||
MockDialContext: func(ctx context.Context, network string,
|
||||
address string, tlsConfig *tls.Config,
|
||||
quicConfig *quic.Config) (quic.EarlySession, error) {
|
||||
return &netxmocks.QUICEarlySession{
|
||||
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")
|
||||
d := &QUICDialerLogger{
|
||||
Dialer: &netxmocks.QUICContextDialer{
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user