fix(netxlite/quic): wrap Close (#509)

While there, make sure netxlite has 100% coverage.

Part of https://github.com/ooni/probe/issues/1733 and diff
has been extracted from https://github.com/ooni/probe-cli/pull/506.
This commit is contained in:
Simone Basso 2021-09-27 14:14:17 +02:00 committed by GitHub
parent deb1589bdb
commit 273774bb03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 1 deletions

View File

@ -75,7 +75,12 @@ func TestHTTPTransportLogger(t *testing.T) {
},
}
client := &http.Client{Transport: txp}
resp, err := client.Get("https://www.google.com")
req, err := http.NewRequest("GET", "https://www.google.com", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("User-Agent", "miniooni/0.1.0-dev")
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}

View File

@ -394,6 +394,16 @@ func (c *quicErrWrapperUDPLikeConn) ReadFrom(b []byte) (int, net.Addr, error) {
return n, addr, nil
}
// Close implements quicx.UDPLikeConn.Close.
func (c *quicErrWrapperUDPLikeConn) Close() error {
err := c.UDPLikeConn.Close()
if err != nil {
return errorsx.NewErrWrapper(
errorsx.ClassifyGenericError, errorsx.ReadFromOperation, err)
}
return nil
}
// quicDialerErrWrapper is a dialer that performs quic err wrapping
type quicDialerErrWrapper struct {
QUICDialer

View File

@ -472,6 +472,7 @@ func TestQUICLoggerDialer(t *testing.T) {
func TestNewSingleUseQUICDialer(t *testing.T) {
sess := &mocks.QUICEarlySession{}
qd := NewSingleUseQUICDialer(sess)
defer qd.CloseIdleConnections()
outsess, err := qd.DialContext(
context.Background(), "", "", &tls.Config{}, &quic.Config{})
if err != nil {
@ -618,6 +619,37 @@ func TestQUICErrWrapperUDPLikeConn(t *testing.T) {
}
})
})
t.Run("Close", func(t *testing.T) {
t.Run("on success", func(t *testing.T) {
conn := &quicErrWrapperUDPLikeConn{
UDPLikeConn: &mocks.QUICUDPLikeConn{
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{
UDPLikeConn: &mocks.QUICUDPLikeConn{
MockClose: func() error {
return expectedErr
},
},
}
err := conn.Close()
if err == nil || err.Error() != errorsx.FailureEOFError {
t.Fatal("unexpected err", err)
}
})
})
}
func TestQUICDialerErrWrapper(t *testing.T) {