chore: improve testing and increase coverage (#794)

This diff improves testing and increases coverage inside the
./internal/netxlite and ./internal/tracex packages.

See https://github.com/ooni/probe/issues/2121
This commit is contained in:
Simone Basso 2022-06-04 14:58:48 +02:00 committed by GitHub
commit d5249a6cf7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 621 additions and 348 deletions

View file

@ -302,6 +302,31 @@ func TestQUICDialerQUICGo(t *testing.T) {
t.Fatal("the ServerName field must match")
}
})
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(
ctx, "udp", "8.8.8.8:443", tlsConfig, &quic.Config{})
if err != nil {
t.Fatal(err)
}
connOwner := qconn.(*quicConnectionOwnsConn)
if connOwner.EarlyConnection != fakeconn {
t.Fatal("invalid underlying conn")
}
})
})
}
@ -406,6 +431,33 @@ func TestQUICDialerHandshakeCompleter(t *testing.T) {
})
}
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")
}
}
func TestQUICDialerResolver(t *testing.T) {
t.Run("CloseIdleConnections", func(t *testing.T) {
var (
@ -518,6 +570,27 @@ func TestQUICDialerResolver(t *testing.T) {
t.Fatal("gotTLSConfig.ServerName has not been set")
}
})
t.Run("on success", func(t *testing.T) {
expectedQConn := &mocks.QUICEarlyConnection{}
dialer := &quicDialerResolver{
Resolver: NewResolverStdlib(log.Log),
Dialer: &mocks.QUICDialer{
MockDialContext: func(ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
return expectedQConn, nil
},
}}
qconn, err := dialer.DialContext(
context.Background(), "udp", "8.8.4.4:443",
&tls.Config{}, &quic.Config{})
if err != nil {
t.Fatal(err)
}
if qconn != expectedQConn {
t.Fatal("unexpected underlying qconn")
}
})
})
t.Run("lookup host with address", func(t *testing.T) {