ooni-probe-cli/internal/model/mocks/quic.go
DecFox 5d2afaade4
cli: upgrade to lucas-clemente/quic-go@v0.27.0 (#715)
* quic-go upgrade: replaced Session/EarlySession with Connection/EarlyConnection

* quic-go upgrade: added context to RoundTripper.Dial

* quic-go upgrade: made corresponding changes to tutorial

* quic-go upgrade: changed sess variable instances to qconn

* quic-go upgrade: made corresponding changes to tutorial

* cleanup: remove unnecessary comments

Those comments made sense in terms of illustrating the changes
but they're going to be less useful once we merge.

* fix(go.mod): apparently we needed `go1.18.1 mod tidy`

VSCode just warned me about this. It seems fine to apply this
change as part of the pull request at hand.

* cleanup(netxlite): http3dialer can be removed

We used to use http3dialer to glue a QUIC dialer, which had a
context as its first argument, to the Dial function used by the
HTTP3 transport, which did not have a context as its first
argument.

Now that HTTP3 transport has a Dial function taking a context as
its first argument, we don't need http3dialer
anymore, since we can use the QUIC dialer directly.

Cc: @DecFox

* Revert "cleanup(netxlite): http3dialer can be removed"

This reverts commit c62244c620cee5fadcc2ca89d8228c8db0b96add
to investigate the build failure mentioned at
https://github.com/ooni/probe-cli/pull/715#issuecomment-1119450484

* chore(netx): show that test was already broken

We didn't see the breakage before because we were not using
the created transport, but the issue of using a nil dialer was
already present before, we just didn't see it.

Now we understand why removing the http3transport in
c62244c620cee5fadcc2ca89d8228c8db0b96add did cause the
breakage mentioned at
https://github.com/ooni/probe-cli/pull/715#issuecomment-1119450484

* fix(netx): convert broken integration test to working unit test

There's no point in using the network here. Add a fake dialer that
breaks and ensure we're getting the expected error.

We've now improved upon the original test because the original test was
not doing anything while now we're testing whether we get back a QUIC
dialer that _can be used_.

After this commit, I can then readd the cleanup commit
c62244c620cee5fadcc2ca89d8228c8db0b96add and it won't be
broken anymore (at least, this is what I expected to happen).

* Revert "Revert "cleanup(netxlite): http3dialer can be removed""

This reverts commit 0e254bfc6ba3bfd65365ce3d8de2c8ec51b925ff
because now we should have fixed the broken test.

Co-authored-by: decfox <decfox>
Co-authored-by: Simone Basso <bassosimone@gmail.com>
2022-05-06 12:24:03 +02:00

207 lines
6.1 KiB
Go

package mocks
import (
"context"
"crypto/tls"
"net"
"syscall"
"time"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/model"
)
// QUICListener is a mockable netxlite.QUICListener.
type QUICListener struct {
MockListen func(addr *net.UDPAddr) (model.UDPLikeConn, error)
}
// Listen calls MockListen.
func (ql *QUICListener) Listen(addr *net.UDPAddr) (model.UDPLikeConn, error) {
return ql.MockListen(addr)
}
// QUICDialer is a mockable netxlite.QUICDialer.
type QUICDialer struct {
// MockDialContext allows mocking DialContext.
MockDialContext func(ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error)
// MockCloseIdleConnections allows mocking CloseIdleConnections.
MockCloseIdleConnections func()
}
// DialContext calls MockDialContext.
func (qcd *QUICDialer) DialContext(ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
return qcd.MockDialContext(ctx, network, address, tlsConfig, quicConfig)
}
// CloseIdleConnections calls MockCloseIdleConnections.
func (qcd *QUICDialer) CloseIdleConnections() {
qcd.MockCloseIdleConnections()
}
// QUICEarlyConnection is a mockable quic.EarlyConnection.
type QUICEarlyConnection struct {
MockAcceptStream func(context.Context) (quic.Stream, error)
MockAcceptUniStream func(context.Context) (quic.ReceiveStream, error)
MockOpenStream func() (quic.Stream, error)
MockOpenStreamSync func(ctx context.Context) (quic.Stream, error)
MockOpenUniStream func() (quic.SendStream, error)
MockOpenUniStreamSync func(ctx context.Context) (quic.SendStream, error)
MockLocalAddr func() net.Addr
MockRemoteAddr func() net.Addr
MockCloseWithError func(code quic.ApplicationErrorCode, reason string) error
MockContext func() context.Context
MockConnectionState func() quic.ConnectionState
MockHandshakeComplete func() context.Context
MockNextConnection func() quic.Connection
MockSendMessage func(b []byte) error
MockReceiveMessage func() ([]byte, error)
}
var _ quic.EarlyConnection = &QUICEarlyConnection{}
// AcceptStream calls MockAcceptStream.
func (s *QUICEarlyConnection) AcceptStream(ctx context.Context) (quic.Stream, error) {
return s.MockAcceptStream(ctx)
}
// AcceptUniStream calls MockAcceptUniStream.
func (s *QUICEarlyConnection) AcceptUniStream(ctx context.Context) (quic.ReceiveStream, error) {
return s.MockAcceptUniStream(ctx)
}
// OpenStream calls MockOpenStream.
func (s *QUICEarlyConnection) OpenStream() (quic.Stream, error) {
return s.MockOpenStream()
}
// OpenStreamSync calls MockOpenStreamSync.
func (s *QUICEarlyConnection) OpenStreamSync(ctx context.Context) (quic.Stream, error) {
return s.MockOpenStreamSync(ctx)
}
// OpenUniStream calls MockOpenUniStream.
func (s *QUICEarlyConnection) OpenUniStream() (quic.SendStream, error) {
return s.MockOpenUniStream()
}
// OpenUniStreamSync calls MockOpenUniStreamSync.
func (s *QUICEarlyConnection) OpenUniStreamSync(ctx context.Context) (quic.SendStream, error) {
return s.MockOpenUniStreamSync(ctx)
}
// LocalAddr class MockLocalAddr.
func (c *QUICEarlyConnection) LocalAddr() net.Addr {
return c.MockLocalAddr()
}
// RemoteAddr calls MockRemoteAddr.
func (c *QUICEarlyConnection) RemoteAddr() net.Addr {
return c.MockRemoteAddr()
}
// CloseWithError calls MockCloseWithError.
func (c *QUICEarlyConnection) CloseWithError(
code quic.ApplicationErrorCode, reason string) error {
return c.MockCloseWithError(code, reason)
}
// Context calls MockContext.
func (s *QUICEarlyConnection) Context() context.Context {
return s.MockContext()
}
// ConnectionState calls MockConnectionState.
func (s *QUICEarlyConnection) ConnectionState() quic.ConnectionState {
return s.MockConnectionState()
}
// HandshakeComplete calls MockHandshakeComplete.
func (s *QUICEarlyConnection) HandshakeComplete() context.Context {
return s.MockHandshakeComplete()
}
// NextConnection calls MockNextConnection.
func (s *QUICEarlyConnection) NextConnection() quic.Connection {
return s.MockNextConnection()
}
// SendMessage calls MockSendMessage.
func (s *QUICEarlyConnection) SendMessage(b []byte) error {
return s.MockSendMessage(b)
}
// ReceiveMessage calls MockReceiveMessage.
func (s *QUICEarlyConnection) ReceiveMessage() ([]byte, error) {
return s.MockReceiveMessage()
}
// UDPLikeConn is an UDP conn used by QUIC.
type UDPLikeConn struct {
MockWriteTo func(p []byte, addr net.Addr) (int, error)
MockClose func() error
MockLocalAddr func() net.Addr
MockRemoteAddr func() net.Addr
MockSetDeadline func(t time.Time) error
MockSetReadDeadline func(t time.Time) error
MockSetWriteDeadline func(t time.Time) error
MockReadFrom func(p []byte) (int, net.Addr, error)
MockSyscallConn func() (syscall.RawConn, error)
MockSetReadBuffer func(n int) error
}
var _ model.UDPLikeConn = &UDPLikeConn{}
// WriteTo calls MockWriteTo.
func (c *UDPLikeConn) WriteTo(p []byte, addr net.Addr) (int, error) {
return c.MockWriteTo(p, addr)
}
// Close calls MockClose.
func (c *UDPLikeConn) Close() error {
return c.MockClose()
}
// LocalAddr calls MockLocalAddr.
func (c *UDPLikeConn) LocalAddr() net.Addr {
return c.MockLocalAddr()
}
// RemoteAddr calls MockRemoteAddr.
func (c *UDPLikeConn) RemoteAddr() net.Addr {
return c.MockRemoteAddr()
}
// SetDeadline calls MockSetDeadline.
func (c *UDPLikeConn) SetDeadline(t time.Time) error {
return c.MockSetDeadline(t)
}
// SetReadDeadline calls MockSetReadDeadline.
func (c *UDPLikeConn) SetReadDeadline(t time.Time) error {
return c.MockSetReadDeadline(t)
}
// SetWriteDeadline calls MockSetWriteDeadline.
func (c *UDPLikeConn) SetWriteDeadline(t time.Time) error {
return c.MockSetWriteDeadline(t)
}
// ReadFrom calls MockReadFrom.
func (c *UDPLikeConn) ReadFrom(b []byte) (int, net.Addr, error) {
return c.MockReadFrom(b)
}
// SyscallConn calls MockSyscallConn.
func (c *UDPLikeConn) SyscallConn() (syscall.RawConn, error) {
return c.MockSyscallConn()
}
// SetReadBuffer calls MockSetReadBuffer.
func (c *UDPLikeConn) SetReadBuffer(n int) error {
return c.MockSetReadBuffer(n)
}