From f054ec3201e0f0bc7fd71fbb835ede78a522682a Mon Sep 17 00:00:00 2001 From: Simone Basso Date: Tue, 7 Sep 2021 23:12:23 +0200 Subject: [PATCH] refactor(netxlite/mocks): group tests, fix naming inconsistencies (#485) Part of https://github.com/ooni/probe/issues/1591 --- internal/engine/legacy/errorsx/quic_test.go | 8 +- internal/netxlite/mocks/conn.go | 60 -- internal/netxlite/mocks/conn_test.go | 126 --- internal/netxlite/mocks/dialer.go | 55 ++ internal/netxlite/mocks/dialer_test.go | 175 ++++- internal/netxlite/mocks/http_test.go | 54 +- internal/netxlite/mocks/legacy_test.go | 36 +- internal/netxlite/mocks/quic.go | 26 +- internal/netxlite/mocks/quic_test.go | 810 ++++++++++---------- internal/netxlite/mocks/reader_test.go | 32 +- internal/netxlite/mocks/resolver_test.go | 96 +-- internal/netxlite/mocks/tls_test.go | 150 ++-- internal/netxlite/quic_test.go | 10 +- 13 files changed, 826 insertions(+), 812 deletions(-) delete mode 100644 internal/netxlite/mocks/conn.go delete mode 100644 internal/netxlite/mocks/conn_test.go diff --git a/internal/engine/legacy/errorsx/quic_test.go b/internal/engine/legacy/errorsx/quic_test.go index 90fa05c..af66cab 100644 --- a/internal/engine/legacy/errorsx/quic_test.go +++ b/internal/engine/legacy/errorsx/quic_test.go @@ -48,7 +48,7 @@ func TestErrorWrapperQUICListenerFailure(t *testing.T) { func TestErrorWrapperUDPConnWriteToSuccess(t *testing.T) { quc := &errorWrapperUDPConn{ - UDPLikeConn: &mocks.QUICUDPConn{ + UDPLikeConn: &mocks.QUICUDPLikeConn{ MockWriteTo: func(p []byte, addr net.Addr) (int, error) { return 10, nil }, @@ -68,7 +68,7 @@ func TestErrorWrapperUDPConnWriteToSuccess(t *testing.T) { func TestErrorWrapperUDPConnWriteToFailure(t *testing.T) { expected := errors.New("mocked error") quc := &errorWrapperUDPConn{ - UDPLikeConn: &mocks.QUICUDPConn{ + UDPLikeConn: &mocks.QUICUDPLikeConn{ MockWriteTo: func(p []byte, addr net.Addr) (int, error) { return 0, expected }, @@ -88,7 +88,7 @@ func TestErrorWrapperUDPConnWriteToFailure(t *testing.T) { func TestErrorWrapperUDPConnReadFromSuccess(t *testing.T) { expected := errors.New("mocked error") quc := &errorWrapperUDPConn{ - UDPLikeConn: &mocks.QUICUDPConn{ + UDPLikeConn: &mocks.QUICUDPLikeConn{ MockReadFrom: func(b []byte) (int, net.Addr, error) { return 0, nil, expected }, @@ -109,7 +109,7 @@ func TestErrorWrapperUDPConnReadFromSuccess(t *testing.T) { func TestErrorWrapperUDPConnReadFromFailure(t *testing.T) { quc := &errorWrapperUDPConn{ - UDPLikeConn: &mocks.QUICUDPConn{ + UDPLikeConn: &mocks.QUICUDPLikeConn{ MockReadFrom: func(b []byte) (int, net.Addr, error) { return 10, nil, nil }, diff --git a/internal/netxlite/mocks/conn.go b/internal/netxlite/mocks/conn.go deleted file mode 100644 index c8520fe..0000000 --- a/internal/netxlite/mocks/conn.go +++ /dev/null @@ -1,60 +0,0 @@ -package mocks - -import ( - "net" - "time" -) - -// Conn is a mockable net.Conn. -type Conn struct { - MockRead func(b []byte) (int, error) - MockWrite func(b []byte) (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 -} - -// Read calls MockRead. -func (c *Conn) Read(b []byte) (int, error) { - return c.MockRead(b) -} - -// Write calls MockWrite. -func (c *Conn) Write(b []byte) (int, error) { - return c.MockWrite(b) -} - -// Close calls MockClose. -func (c *Conn) Close() error { - return c.MockClose() -} - -// LocalAddr calls MockLocalAddr. -func (c *Conn) LocalAddr() net.Addr { - return c.MockLocalAddr() -} - -// RemoteAddr calls MockRemoteAddr. -func (c *Conn) RemoteAddr() net.Addr { - return c.MockRemoteAddr() -} - -// SetDeadline calls MockSetDeadline. -func (c *Conn) SetDeadline(t time.Time) error { - return c.MockSetDeadline(t) -} - -// SetReadDeadline calls MockSetReadDeadline. -func (c *Conn) SetReadDeadline(t time.Time) error { - return c.MockSetReadDeadline(t) -} - -// SetWriteDeadline calls MockSetWriteDeadline. -func (c *Conn) SetWriteDeadline(t time.Time) error { - return c.MockSetWriteDeadline(t) -} - -var _ net.Conn = &Conn{} diff --git a/internal/netxlite/mocks/conn_test.go b/internal/netxlite/mocks/conn_test.go deleted file mode 100644 index 91f5650..0000000 --- a/internal/netxlite/mocks/conn_test.go +++ /dev/null @@ -1,126 +0,0 @@ -package mocks - -import ( - "errors" - "net" - "testing" - "time" - - "github.com/google/go-cmp/cmp" -) - -func TestConnReadWorks(t *testing.T) { - expected := errors.New("mocked error") - c := &Conn{ - MockRead: func(b []byte) (int, error) { - return 0, expected - }, - } - count, err := c.Read(make([]byte, 128)) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected") - } - if count != 0 { - t.Fatal("expected 0 bytes") - } -} - -func TestConnWriteWorks(t *testing.T) { - expected := errors.New("mocked error") - c := &Conn{ - MockWrite: func(b []byte) (int, error) { - return 0, expected - }, - } - count, err := c.Write(make([]byte, 128)) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected") - } - if count != 0 { - t.Fatal("expected 0 bytes") - } -} - -func TestConnCloseWorks(t *testing.T) { - expected := errors.New("mocked error") - c := &Conn{ - MockClose: func() error { - return expected - }, - } - err := c.Close() - if !errors.Is(err, expected) { - t.Fatal("not the error we expected") - } -} - -func TestConnLocalAddrWorks(t *testing.T) { - expected := &net.TCPAddr{ - IP: net.IPv6loopback, - Port: 1234, - } - c := &Conn{ - MockLocalAddr: func() net.Addr { - return expected - }, - } - out := c.LocalAddr() - if diff := cmp.Diff(expected, out); diff != "" { - t.Fatal(diff) - } -} - -func TestConnRemoteAddrWorks(t *testing.T) { - expected := &net.TCPAddr{ - IP: net.IPv6loopback, - Port: 1234, - } - c := &Conn{ - MockRemoteAddr: func() net.Addr { - return expected - }, - } - out := c.RemoteAddr() - if diff := cmp.Diff(expected, out); diff != "" { - t.Fatal(diff) - } -} - -func TestConnSetDeadline(t *testing.T) { - expected := errors.New("mocked error") - c := &Conn{ - MockSetDeadline: func(t time.Time) error { - return expected - }, - } - err := c.SetDeadline(time.Time{}) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } -} - -func TestConnSetReadDeadline(t *testing.T) { - expected := errors.New("mocked error") - c := &Conn{ - MockSetReadDeadline: func(t time.Time) error { - return expected - }, - } - err := c.SetReadDeadline(time.Time{}) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } -} - -func TestConnSetWriteDeadline(t *testing.T) { - expected := errors.New("mocked error") - c := &Conn{ - MockSetWriteDeadline: func(t time.Time) error { - return expected - }, - } - err := c.SetWriteDeadline(time.Time{}) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } -} diff --git a/internal/netxlite/mocks/dialer.go b/internal/netxlite/mocks/dialer.go index f896cca..95a0ade 100644 --- a/internal/netxlite/mocks/dialer.go +++ b/internal/netxlite/mocks/dialer.go @@ -3,6 +3,7 @@ package mocks import ( "context" "net" + "time" ) // Dialer is a mockable Dialer. @@ -20,3 +21,57 @@ func (d *Dialer) DialContext(ctx context.Context, network, address string) (net. func (d *Dialer) CloseIdleConnections() { d.MockCloseIdleConnections() } + +// Conn is a mockable net.Conn. +type Conn struct { + MockRead func(b []byte) (int, error) + MockWrite func(b []byte) (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 +} + +// Read calls MockRead. +func (c *Conn) Read(b []byte) (int, error) { + return c.MockRead(b) +} + +// Write calls MockWrite. +func (c *Conn) Write(b []byte) (int, error) { + return c.MockWrite(b) +} + +// Close calls MockClose. +func (c *Conn) Close() error { + return c.MockClose() +} + +// LocalAddr calls MockLocalAddr. +func (c *Conn) LocalAddr() net.Addr { + return c.MockLocalAddr() +} + +// RemoteAddr calls MockRemoteAddr. +func (c *Conn) RemoteAddr() net.Addr { + return c.MockRemoteAddr() +} + +// SetDeadline calls MockSetDeadline. +func (c *Conn) SetDeadline(t time.Time) error { + return c.MockSetDeadline(t) +} + +// SetReadDeadline calls MockSetReadDeadline. +func (c *Conn) SetReadDeadline(t time.Time) error { + return c.MockSetReadDeadline(t) +} + +// SetWriteDeadline calls MockSetWriteDeadline. +func (c *Conn) SetWriteDeadline(t time.Time) error { + return c.MockSetWriteDeadline(t) +} + +var _ net.Conn = &Conn{} diff --git a/internal/netxlite/mocks/dialer_test.go b/internal/netxlite/mocks/dialer_test.go index 6166e94..891128a 100644 --- a/internal/netxlite/mocks/dialer_test.go +++ b/internal/netxlite/mocks/dialer_test.go @@ -5,34 +5,157 @@ import ( "errors" "net" "testing" + "time" + + "github.com/google/go-cmp/cmp" ) -func TestDialerDialContext(t *testing.T) { - expected := errors.New("mocked error") - d := Dialer{ - MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) { - return nil, expected - }, - } - ctx := context.Background() - conn, err := d.DialContext(ctx, "tcp", "8.8.8.8:53") - if !errors.Is(err, expected) { - t.Fatal("not the error we expected") - } - if conn != nil { - t.Fatal("expected nil conn") - } +func TestDialer(t *testing.T) { + t.Run("DialContext", func(t *testing.T) { + expected := errors.New("mocked error") + d := Dialer{ + MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) { + return nil, expected + }, + } + ctx := context.Background() + conn, err := d.DialContext(ctx, "tcp", "8.8.8.8:53") + if !errors.Is(err, expected) { + t.Fatal("not the error we expected") + } + if conn != nil { + t.Fatal("expected nil conn") + } + }) + + t.Run("CloseIdleConnections", func(t *testing.T) { + var called bool + d := &Dialer{ + MockCloseIdleConnections: func() { + called = true + }, + } + d.CloseIdleConnections() + if !called { + t.Fatal("not called") + } + }) } -func TestDialerCloseIdleConnections(t *testing.T) { - var called bool - d := &Dialer{ - MockCloseIdleConnections: func() { - called = true - }, - } - d.CloseIdleConnections() - if !called { - t.Fatal("not called") - } +func TestConn(t *testing.T) { + t.Run("Read", func(t *testing.T) { + expected := errors.New("mocked error") + c := &Conn{ + MockRead: func(b []byte) (int, error) { + return 0, expected + }, + } + count, err := c.Read(make([]byte, 128)) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected") + } + if count != 0 { + t.Fatal("expected 0 bytes") + } + }) + + t.Run("Write", func(t *testing.T) { + expected := errors.New("mocked error") + c := &Conn{ + MockWrite: func(b []byte) (int, error) { + return 0, expected + }, + } + count, err := c.Write(make([]byte, 128)) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected") + } + if count != 0 { + t.Fatal("expected 0 bytes") + } + }) + + t.Run("Close", func(t *testing.T) { + expected := errors.New("mocked error") + c := &Conn{ + MockClose: func() error { + return expected + }, + } + err := c.Close() + if !errors.Is(err, expected) { + t.Fatal("not the error we expected") + } + }) + + t.Run("LocalAddr", func(t *testing.T) { + expected := &net.TCPAddr{ + IP: net.IPv6loopback, + Port: 1234, + } + c := &Conn{ + MockLocalAddr: func() net.Addr { + return expected + }, + } + out := c.LocalAddr() + if diff := cmp.Diff(expected, out); diff != "" { + t.Fatal(diff) + } + }) + + t.Run("RemoteAddr", func(t *testing.T) { + expected := &net.TCPAddr{ + IP: net.IPv6loopback, + Port: 1234, + } + c := &Conn{ + MockRemoteAddr: func() net.Addr { + return expected + }, + } + out := c.RemoteAddr() + if diff := cmp.Diff(expected, out); diff != "" { + t.Fatal(diff) + } + }) + + t.Run("SetDeadline", func(t *testing.T) { + expected := errors.New("mocked error") + c := &Conn{ + MockSetDeadline: func(t time.Time) error { + return expected + }, + } + err := c.SetDeadline(time.Time{}) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + }) + + t.Run("SetReadDeadline", func(t *testing.T) { + expected := errors.New("mocked error") + c := &Conn{ + MockSetReadDeadline: func(t time.Time) error { + return expected + }, + } + err := c.SetReadDeadline(time.Time{}) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + }) + + t.Run("SetWriteDeadline", func(t *testing.T) { + expected := errors.New("mocked error") + c := &Conn{ + MockSetWriteDeadline: func(t time.Time) error { + return expected + }, + } + err := c.SetWriteDeadline(time.Time{}) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + }) } diff --git a/internal/netxlite/mocks/http_test.go b/internal/netxlite/mocks/http_test.go index d3457d0..94107b0 100644 --- a/internal/netxlite/mocks/http_test.go +++ b/internal/netxlite/mocks/http_test.go @@ -8,31 +8,33 @@ import ( "github.com/ooni/probe-cli/v3/internal/atomicx" ) -func TestHTTPTransportRoundTrip(t *testing.T) { - expected := errors.New("mocked error") - txp := &HTTPTransport{ - MockRoundTrip: func(req *http.Request) (*http.Response, error) { - return nil, expected - }, - } - resp, err := txp.RoundTrip(&http.Request{}) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } - if resp != nil { - t.Fatal("expected nil response here") - } -} +func TestHTTPTransport(t *testing.T) { + t.Run("RoundTrip", func(t *testing.T) { + expected := errors.New("mocked error") + txp := &HTTPTransport{ + MockRoundTrip: func(req *http.Request) (*http.Response, error) { + return nil, expected + }, + } + resp, err := txp.RoundTrip(&http.Request{}) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + if resp != nil { + t.Fatal("expected nil response here") + } + }) -func TestHTTPTransportCloseIdleConnections(t *testing.T) { - called := &atomicx.Int64{} - txp := &HTTPTransport{ - MockCloseIdleConnections: func() { - called.Add(1) - }, - } - txp.CloseIdleConnections() - if called.Load() != 1 { - t.Fatal("not called") - } + t.Run("CloseIdleConnections", func(t *testing.T) { + called := &atomicx.Int64{} + txp := &HTTPTransport{ + MockCloseIdleConnections: func() { + called.Add(1) + }, + } + txp.CloseIdleConnections() + if called.Load() != 1 { + t.Fatal("not called") + } + }) } diff --git a/internal/netxlite/mocks/legacy_test.go b/internal/netxlite/mocks/legacy_test.go index d4e739e..9c5f4c1 100644 --- a/internal/netxlite/mocks/legacy_test.go +++ b/internal/netxlite/mocks/legacy_test.go @@ -9,21 +9,23 @@ import ( "github.com/lucas-clemente/quic-go" ) -func TestQUICContextDialerDialContext(t *testing.T) { - expected := errors.New("mocked error") - qcd := &QUICContextDialer{ - MockDialContext: func(ctx context.Context, network string, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { - return nil, expected - }, - } - ctx := context.Background() - tlsConfig := &tls.Config{} - quicConfig := &quic.Config{} - sess, err := qcd.DialContext(ctx, "udp", "dns.google:443", tlsConfig, quicConfig) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected") - } - if sess != nil { - t.Fatal("expected nil session") - } +func TestQUICContextDialer(t *testing.T) { + t.Run("DialContext", func(t *testing.T) { + expected := errors.New("mocked error") + qcd := &QUICContextDialer{ + MockDialContext: func(ctx context.Context, network string, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { + return nil, expected + }, + } + ctx := context.Background() + tlsConfig := &tls.Config{} + quicConfig := &quic.Config{} + sess, err := qcd.DialContext(ctx, "udp", "dns.google:443", tlsConfig, quicConfig) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected") + } + if sess != nil { + t.Fatal("expected nil session") + } + }) } diff --git a/internal/netxlite/mocks/quic.go b/internal/netxlite/mocks/quic.go index 3adf446..6cac16c 100644 --- a/internal/netxlite/mocks/quic.go +++ b/internal/netxlite/mocks/quic.go @@ -139,8 +139,8 @@ func (s *QUICEarlySession) ReceiveMessage() ([]byte, error) { return s.MockReceiveMessage() } -// QUICUDPConn is an UDP conn used by QUIC. -type QUICUDPConn struct { +// QUICUDPLikeConn is an UDP conn used by QUIC. +type QUICUDPLikeConn struct { MockWriteTo func(p []byte, addr net.Addr) (int, error) MockClose func() error MockLocalAddr func() net.Addr @@ -153,54 +153,54 @@ type QUICUDPConn struct { MockSetReadBuffer func(n int) error } -var _ quicx.UDPLikeConn = &QUICUDPConn{} +var _ quicx.UDPLikeConn = &QUICUDPLikeConn{} // WriteTo calls MockWriteTo. -func (c *QUICUDPConn) WriteTo(p []byte, addr net.Addr) (int, error) { +func (c *QUICUDPLikeConn) WriteTo(p []byte, addr net.Addr) (int, error) { return c.MockWriteTo(p, addr) } // Close calls MockClose. -func (c *QUICUDPConn) Close() error { +func (c *QUICUDPLikeConn) Close() error { return c.MockClose() } // LocalAddr calls MockLocalAddr. -func (c *QUICUDPConn) LocalAddr() net.Addr { +func (c *QUICUDPLikeConn) LocalAddr() net.Addr { return c.MockLocalAddr() } // RemoteAddr calls MockRemoteAddr. -func (c *QUICUDPConn) RemoteAddr() net.Addr { +func (c *QUICUDPLikeConn) RemoteAddr() net.Addr { return c.MockRemoteAddr() } // SetDeadline calls MockSetDeadline. -func (c *QUICUDPConn) SetDeadline(t time.Time) error { +func (c *QUICUDPLikeConn) SetDeadline(t time.Time) error { return c.MockSetDeadline(t) } // SetReadDeadline calls MockSetReadDeadline. -func (c *QUICUDPConn) SetReadDeadline(t time.Time) error { +func (c *QUICUDPLikeConn) SetReadDeadline(t time.Time) error { return c.MockSetReadDeadline(t) } // SetWriteDeadline calls MockSetWriteDeadline. -func (c *QUICUDPConn) SetWriteDeadline(t time.Time) error { +func (c *QUICUDPLikeConn) SetWriteDeadline(t time.Time) error { return c.MockSetWriteDeadline(t) } // ReadFrom calls MockReadFrom. -func (c *QUICUDPConn) ReadFrom(b []byte) (int, net.Addr, error) { +func (c *QUICUDPLikeConn) ReadFrom(b []byte) (int, net.Addr, error) { return c.MockReadFrom(b) } // SyscallConn calls MockSyscallConn. -func (c *QUICUDPConn) SyscallConn() (syscall.RawConn, error) { +func (c *QUICUDPLikeConn) SyscallConn() (syscall.RawConn, error) { return c.MockSyscallConn() } // SetReadBuffer calls MockSetReadBuffer. -func (c *QUICUDPConn) SetReadBuffer(n int) error { +func (c *QUICUDPLikeConn) SetReadBuffer(n int) error { return c.MockSetReadBuffer(n) } diff --git a/internal/netxlite/mocks/quic_test.go b/internal/netxlite/mocks/quic_test.go index 614a5ad..4403889 100644 --- a/internal/netxlite/mocks/quic_test.go +++ b/internal/netxlite/mocks/quic_test.go @@ -16,420 +16,428 @@ import ( ) func TestQUICListenerListen(t *testing.T) { - expected := errors.New("mocked error") - ql := &QUICListener{ - MockListen: func(addr *net.UDPAddr) (quicx.UDPLikeConn, error) { - return nil, expected - }, - } - pconn, err := ql.Listen(&net.UDPAddr{}) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", expected) - } - if pconn != nil { - t.Fatal("expected nil conn here") - } + t.Run("Listen", func(t *testing.T) { + expected := errors.New("mocked error") + ql := &QUICListener{ + MockListen: func(addr *net.UDPAddr) (quicx.UDPLikeConn, error) { + return nil, expected + }, + } + pconn, err := ql.Listen(&net.UDPAddr{}) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", expected) + } + if pconn != nil { + t.Fatal("expected nil conn here") + } + }) } -func TestQUICDialerDialContext(t *testing.T) { - expected := errors.New("mocked error") - qcd := &QUICDialer{ - MockDialContext: func(ctx context.Context, network string, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { - return nil, expected - }, - } - ctx := context.Background() - tlsConfig := &tls.Config{} - quicConfig := &quic.Config{} - sess, err := qcd.DialContext(ctx, "udp", "dns.google:443", tlsConfig, quicConfig) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected") - } - if sess != nil { - t.Fatal("expected nil session") - } +func TestQUICDialer(t *testing.T) { + t.Run("DialContext", func(t *testing.T) { + expected := errors.New("mocked error") + qcd := &QUICDialer{ + MockDialContext: func(ctx context.Context, network string, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { + return nil, expected + }, + } + ctx := context.Background() + tlsConfig := &tls.Config{} + quicConfig := &quic.Config{} + sess, err := qcd.DialContext(ctx, "udp", "dns.google:443", tlsConfig, quicConfig) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected") + } + if sess != nil { + t.Fatal("expected nil session") + } + }) + + t.Run("CloseIdleConnections", func(t *testing.T) { + var called bool + qcd := &QUICDialer{ + MockCloseIdleConnections: func() { + called = true + }, + } + qcd.CloseIdleConnections() + if !called { + t.Fatal("not called") + } + }) } -func TestQUICDialerCloseIdleConnections(t *testing.T) { - var called bool - qcd := &QUICDialer{ - MockCloseIdleConnections: func() { - called = true - }, - } - qcd.CloseIdleConnections() - if !called { - t.Fatal("not called") - } +func TestQUICEarlySession(t *testing.T) { + t.Run("AcceptStream", func(t *testing.T) { + expected := errors.New("mocked error") + sess := &QUICEarlySession{ + MockAcceptStream: func(ctx context.Context) (quic.Stream, error) { + return nil, expected + }, + } + ctx := context.Background() + stream, err := sess.AcceptStream(ctx) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + if stream != nil { + t.Fatal("expected nil stream here") + } + }) + + t.Run("AcceptUniStream", func(t *testing.T) { + expected := errors.New("mocked error") + sess := &QUICEarlySession{ + MockAcceptUniStream: func(ctx context.Context) (quic.ReceiveStream, error) { + return nil, expected + }, + } + ctx := context.Background() + stream, err := sess.AcceptUniStream(ctx) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + if stream != nil { + t.Fatal("expected nil stream here") + } + }) + + t.Run("OpenStream", func(t *testing.T) { + expected := errors.New("mocked error") + sess := &QUICEarlySession{ + MockOpenStream: func() (quic.Stream, error) { + return nil, expected + }, + } + stream, err := sess.OpenStream() + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + if stream != nil { + t.Fatal("expected nil stream here") + } + }) + + t.Run("OpenStreamSync", func(t *testing.T) { + expected := errors.New("mocked error") + sess := &QUICEarlySession{ + MockOpenStreamSync: func(ctx context.Context) (quic.Stream, error) { + return nil, expected + }, + } + ctx := context.Background() + stream, err := sess.OpenStreamSync(ctx) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + if stream != nil { + t.Fatal("expected nil stream here") + } + }) + + t.Run("OpenUniStream", func(t *testing.T) { + expected := errors.New("mocked error") + sess := &QUICEarlySession{ + MockOpenUniStream: func() (quic.SendStream, error) { + return nil, expected + }, + } + stream, err := sess.OpenUniStream() + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + if stream != nil { + t.Fatal("expected nil stream here") + } + }) + + t.Run("OpenUniStreamSync", func(t *testing.T) { + expected := errors.New("mocked error") + sess := &QUICEarlySession{ + MockOpenUniStreamSync: func(ctx context.Context) (quic.SendStream, error) { + return nil, expected + }, + } + ctx := context.Background() + stream, err := sess.OpenUniStreamSync(ctx) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + if stream != nil { + t.Fatal("expected nil stream here") + } + }) + + t.Run("LocalAddr", func(t *testing.T) { + sess := &QUICEarlySession{ + MockLocalAddr: func() net.Addr { + return &net.UDPAddr{} + }, + } + addr := sess.LocalAddr() + if !reflect.ValueOf(addr).Elem().IsZero() { + t.Fatal("expected a zero address here") + } + }) + + t.Run("RemoteAddr", func(t *testing.T) { + sess := &QUICEarlySession{ + MockRemoteAddr: func() net.Addr { + return &net.UDPAddr{} + }, + } + addr := sess.RemoteAddr() + if !reflect.ValueOf(addr).Elem().IsZero() { + t.Fatal("expected a zero address here") + } + }) + + t.Run("CloseWithError", func(t *testing.T) { + expected := errors.New("mocked error") + sess := &QUICEarlySession{ + MockCloseWithError: func( + code quic.ApplicationErrorCode, reason string) error { + return expected + }, + } + err := sess.CloseWithError(0, "") + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + }) + + t.Run("Context", func(t *testing.T) { + ctx := context.Background() + sess := &QUICEarlySession{ + MockContext: func() context.Context { + return ctx + }, + } + out := sess.Context() + if !reflect.DeepEqual(ctx, out) { + t.Fatal("not the context we expected") + } + }) + + t.Run("ConnectionState", func(t *testing.T) { + state := quic.ConnectionState{SupportsDatagrams: true} + sess := &QUICEarlySession{ + MockConnectionState: func() quic.ConnectionState { + return state + }, + } + out := sess.ConnectionState() + if !reflect.DeepEqual(state, out) { + t.Fatal("not the context we expected") + } + }) + + t.Run("HandshakeComplete", func(t *testing.T) { + ctx := context.Background() + sess := &QUICEarlySession{ + MockHandshakeComplete: func() context.Context { + return ctx + }, + } + out := sess.HandshakeComplete() + if !reflect.DeepEqual(ctx, out) { + t.Fatal("not the context we expected") + } + }) + + t.Run("NextSession", func(t *testing.T) { + next := &QUICEarlySession{} + sess := &QUICEarlySession{ + MockNextSession: func() quic.Session { + return next + }, + } + out := sess.NextSession() + if !reflect.DeepEqual(next, out) { + t.Fatal("not the context we expected") + } + }) + + t.Run("SendMessage", func(t *testing.T) { + expected := errors.New("mocked error") + sess := &QUICEarlySession{ + MockSendMessage: func(b []byte) error { + return expected + }, + } + b := make([]byte, 17) + err := sess.SendMessage(b) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + }) + + t.Run("ReceiveMessage", func(t *testing.T) { + expected := errors.New("mocked error") + sess := &QUICEarlySession{ + MockReceiveMessage: func() ([]byte, error) { + return nil, expected + }, + } + b, err := sess.ReceiveMessage() + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + if b != nil { + t.Fatal("expected nil buffer here") + } + }) } -func TestQUICEarlySessionAcceptStream(t *testing.T) { - expected := errors.New("mocked error") - sess := &QUICEarlySession{ - MockAcceptStream: func(ctx context.Context) (quic.Stream, error) { - return nil, expected - }, - } - ctx := context.Background() - stream, err := sess.AcceptStream(ctx) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } - if stream != nil { - t.Fatal("expected nil stream here") - } -} +func TestQUICUDPLikeConn(t *testing.T) { + t.Run("WriteTo", func(t *testing.T) { + expected := errors.New("mocked error") + quc := &QUICUDPLikeConn{ + MockWriteTo: func(p []byte, addr net.Addr) (int, error) { + return 0, expected + }, + } + pkt := make([]byte, 128) + addr := &net.UDPAddr{} + cnt, err := quc.WriteTo(pkt, addr) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + if cnt != 0 { + t.Fatal("expected zero here") + } + }) -func TestQUICEarlySessionAcceptUniStream(t *testing.T) { - expected := errors.New("mocked error") - sess := &QUICEarlySession{ - MockAcceptUniStream: func(ctx context.Context) (quic.ReceiveStream, error) { - return nil, expected - }, - } - ctx := context.Background() - stream, err := sess.AcceptUniStream(ctx) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } - if stream != nil { - t.Fatal("expected nil stream here") - } -} + t.Run("ConnClose", func(t *testing.T) { + expected := errors.New("mocked error") + quc := &QUICUDPLikeConn{ + MockClose: func() error { + return expected + }, + } + err := quc.Close() + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + }) -func TestQUICEarlySessionOpenStream(t *testing.T) { - expected := errors.New("mocked error") - sess := &QUICEarlySession{ - MockOpenStream: func() (quic.Stream, error) { - return nil, expected - }, - } - stream, err := sess.OpenStream() - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } - if stream != nil { - t.Fatal("expected nil stream here") - } -} + t.Run("LocalAddr", func(t *testing.T) { + expected := &net.TCPAddr{ + IP: net.IPv6loopback, + Port: 1234, + } + c := &QUICUDPLikeConn{ + MockLocalAddr: func() net.Addr { + return expected + }, + } + out := c.LocalAddr() + if diff := cmp.Diff(expected, out); diff != "" { + t.Fatal(diff) + } + }) -func TestQUICEarlySessionOpenStreamSync(t *testing.T) { - expected := errors.New("mocked error") - sess := &QUICEarlySession{ - MockOpenStreamSync: func(ctx context.Context) (quic.Stream, error) { - return nil, expected - }, - } - ctx := context.Background() - stream, err := sess.OpenStreamSync(ctx) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } - if stream != nil { - t.Fatal("expected nil stream here") - } -} + t.Run("RemoteAddr", func(t *testing.T) { + expected := &net.TCPAddr{ + IP: net.IPv6loopback, + Port: 1234, + } + c := &QUICUDPLikeConn{ + MockRemoteAddr: func() net.Addr { + return expected + }, + } + out := c.RemoteAddr() + if diff := cmp.Diff(expected, out); diff != "" { + t.Fatal(diff) + } + }) -func TestQUICEarlySessionOpenUniStream(t *testing.T) { - expected := errors.New("mocked error") - sess := &QUICEarlySession{ - MockOpenUniStream: func() (quic.SendStream, error) { - return nil, expected - }, - } - stream, err := sess.OpenUniStream() - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } - if stream != nil { - t.Fatal("expected nil stream here") - } -} + t.Run("SetDeadline", func(t *testing.T) { + expected := errors.New("mocked error") + c := &QUICUDPLikeConn{ + MockSetDeadline: func(t time.Time) error { + return expected + }, + } + err := c.SetDeadline(time.Time{}) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + }) -func TestQUICEarlySessionOpenUniStreamSync(t *testing.T) { - expected := errors.New("mocked error") - sess := &QUICEarlySession{ - MockOpenUniStreamSync: func(ctx context.Context) (quic.SendStream, error) { - return nil, expected - }, - } - ctx := context.Background() - stream, err := sess.OpenUniStreamSync(ctx) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } - if stream != nil { - t.Fatal("expected nil stream here") - } -} + t.Run("SetReadDeadline", func(t *testing.T) { + expected := errors.New("mocked error") + c := &QUICUDPLikeConn{ + MockSetReadDeadline: func(t time.Time) error { + return expected + }, + } + err := c.SetReadDeadline(time.Time{}) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + }) -func TestQUICEarlySessionLocalAddr(t *testing.T) { - sess := &QUICEarlySession{ - MockLocalAddr: func() net.Addr { - return &net.UDPAddr{} - }, - } - addr := sess.LocalAddr() - if !reflect.ValueOf(addr).Elem().IsZero() { - t.Fatal("expected a zero address here") - } -} + t.Run("SetWriteDeadline", func(t *testing.T) { + expected := errors.New("mocked error") + c := &QUICUDPLikeConn{ + MockSetWriteDeadline: func(t time.Time) error { + return expected + }, + } + err := c.SetWriteDeadline(time.Time{}) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + }) -func TestQUICEarlySessionRemoteAddr(t *testing.T) { - sess := &QUICEarlySession{ - MockRemoteAddr: func() net.Addr { - return &net.UDPAddr{} - }, - } - addr := sess.RemoteAddr() - if !reflect.ValueOf(addr).Elem().IsZero() { - t.Fatal("expected a zero address here") - } -} + t.Run("ConnReadFrom", func(t *testing.T) { + expected := errors.New("mocked error") + quc := &QUICUDPLikeConn{ + MockReadFrom: func(b []byte) (int, net.Addr, error) { + return 0, nil, expected + }, + } + b := make([]byte, 128) + n, addr, err := quc.ReadFrom(b) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + if n != 0 { + t.Fatal("expected zero here") + } + if addr != nil { + t.Fatal("expected nil here") + } + }) -func TestQUICEarlySessionCloseWithError(t *testing.T) { - expected := errors.New("mocked error") - sess := &QUICEarlySession{ - MockCloseWithError: func( - code quic.ApplicationErrorCode, reason string) error { - return expected - }, - } - err := sess.CloseWithError(0, "") - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } -} + t.Run("SyscallConn", func(t *testing.T) { + expected := errors.New("mocked error") + quc := &QUICUDPLikeConn{ + MockSyscallConn: func() (syscall.RawConn, error) { + return nil, expected + }, + } + conn, err := quc.SyscallConn() + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + if conn != nil { + t.Fatal("expected nil here") + } + }) -func TestQUICEarlySessionContext(t *testing.T) { - ctx := context.Background() - sess := &QUICEarlySession{ - MockContext: func() context.Context { - return ctx - }, - } - out := sess.Context() - if !reflect.DeepEqual(ctx, out) { - t.Fatal("not the context we expected") - } -} - -func TestQUICEarlySessionConnectionState(t *testing.T) { - state := quic.ConnectionState{SupportsDatagrams: true} - sess := &QUICEarlySession{ - MockConnectionState: func() quic.ConnectionState { - return state - }, - } - out := sess.ConnectionState() - if !reflect.DeepEqual(state, out) { - t.Fatal("not the context we expected") - } -} - -func TestQUICEarlySessionHandshakeComplete(t *testing.T) { - ctx := context.Background() - sess := &QUICEarlySession{ - MockHandshakeComplete: func() context.Context { - return ctx - }, - } - out := sess.HandshakeComplete() - if !reflect.DeepEqual(ctx, out) { - t.Fatal("not the context we expected") - } -} - -func TestQUICEarlySessionNextSession(t *testing.T) { - next := &QUICEarlySession{} - sess := &QUICEarlySession{ - MockNextSession: func() quic.Session { - return next - }, - } - out := sess.NextSession() - if !reflect.DeepEqual(next, out) { - t.Fatal("not the context we expected") - } -} - -func TestQUICEarlySessionSendMessage(t *testing.T) { - expected := errors.New("mocked error") - sess := &QUICEarlySession{ - MockSendMessage: func(b []byte) error { - return expected - }, - } - b := make([]byte, 17) - err := sess.SendMessage(b) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } -} - -func TestQUICEarlySessionReceiveMessage(t *testing.T) { - expected := errors.New("mocked error") - sess := &QUICEarlySession{ - MockReceiveMessage: func() ([]byte, error) { - return nil, expected - }, - } - b, err := sess.ReceiveMessage() - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } - if b != nil { - t.Fatal("expected nil buffer here") - } -} - -func TestQUICUDPConnWriteTo(t *testing.T) { - expected := errors.New("mocked error") - quc := &QUICUDPConn{ - MockWriteTo: func(p []byte, addr net.Addr) (int, error) { - return 0, expected - }, - } - pkt := make([]byte, 128) - addr := &net.UDPAddr{} - cnt, err := quc.WriteTo(pkt, addr) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } - if cnt != 0 { - t.Fatal("expected zero here") - } -} - -func TestQUICUDPConnClose(t *testing.T) { - expected := errors.New("mocked error") - quc := &QUICUDPConn{ - MockClose: func() error { - return expected - }, - } - err := quc.Close() - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } -} - -func TestQUICUDPConnLocalAddrWorks(t *testing.T) { - expected := &net.TCPAddr{ - IP: net.IPv6loopback, - Port: 1234, - } - c := &QUICUDPConn{ - MockLocalAddr: func() net.Addr { - return expected - }, - } - out := c.LocalAddr() - if diff := cmp.Diff(expected, out); diff != "" { - t.Fatal(diff) - } -} - -func TestQUICUDPConnRemoteAddrWorks(t *testing.T) { - expected := &net.TCPAddr{ - IP: net.IPv6loopback, - Port: 1234, - } - c := &QUICUDPConn{ - MockRemoteAddr: func() net.Addr { - return expected - }, - } - out := c.RemoteAddr() - if diff := cmp.Diff(expected, out); diff != "" { - t.Fatal(diff) - } -} - -func TestQUICUDPConnSetDeadline(t *testing.T) { - expected := errors.New("mocked error") - c := &QUICUDPConn{ - MockSetDeadline: func(t time.Time) error { - return expected - }, - } - err := c.SetDeadline(time.Time{}) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } -} - -func TestQUICUDPConnSetReadDeadline(t *testing.T) { - expected := errors.New("mocked error") - c := &QUICUDPConn{ - MockSetReadDeadline: func(t time.Time) error { - return expected - }, - } - err := c.SetReadDeadline(time.Time{}) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } -} - -func TestQUICUDPConnSetWriteDeadline(t *testing.T) { - expected := errors.New("mocked error") - c := &QUICUDPConn{ - MockSetWriteDeadline: func(t time.Time) error { - return expected - }, - } - err := c.SetWriteDeadline(time.Time{}) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } -} - -func TestQUICUDPConnReadFrom(t *testing.T) { - expected := errors.New("mocked error") - quc := &QUICUDPConn{ - MockReadFrom: func(b []byte) (int, net.Addr, error) { - return 0, nil, expected - }, - } - b := make([]byte, 128) - n, addr, err := quc.ReadFrom(b) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } - if n != 0 { - t.Fatal("expected zero here") - } - if addr != nil { - t.Fatal("expected nil here") - } -} - -func TestQUICUDPConnSyscallConn(t *testing.T) { - expected := errors.New("mocked error") - quc := &QUICUDPConn{ - MockSyscallConn: func() (syscall.RawConn, error) { - return nil, expected - }, - } - conn, err := quc.SyscallConn() - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } - if conn != nil { - t.Fatal("expected nil here") - } -} - -func TestQUICUDPConnSetReadBuffer(t *testing.T) { - expected := errors.New("mocked error") - quc := &QUICUDPConn{ - MockSetReadBuffer: func(n int) error { - return expected - }, - } - err := quc.SetReadBuffer(1 << 10) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } + t.Run("SetReadBuffer", func(t *testing.T) { + expected := errors.New("mocked error") + quc := &QUICUDPLikeConn{ + MockSetReadBuffer: func(n int) error { + return expected + }, + } + err := quc.SetReadBuffer(1 << 10) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + }) } diff --git a/internal/netxlite/mocks/reader_test.go b/internal/netxlite/mocks/reader_test.go index 6b37ac4..2d602dc 100644 --- a/internal/netxlite/mocks/reader_test.go +++ b/internal/netxlite/mocks/reader_test.go @@ -5,19 +5,21 @@ import ( "testing" ) -func TestReaderRead(t *testing.T) { - expected := errors.New("mocked error") - r := &Reader{ - MockRead: func(b []byte) (int, error) { - return 0, expected - }, - } - b := make([]byte, 128) - count, err := r.Read(b) - if !errors.Is(err, expected) { - t.Fatal("unexpected error", err) - } - if count != 0 { - t.Fatal("unexpected count", count) - } +func TestReader(t *testing.T) { + t.Run("Read", func(t *testing.T) { + expected := errors.New("mocked error") + r := &Reader{ + MockRead: func(b []byte) (int, error) { + return 0, expected + }, + } + b := make([]byte, 128) + count, err := r.Read(b) + if !errors.Is(err, expected) { + t.Fatal("unexpected error", err) + } + if count != 0 { + t.Fatal("unexpected count", count) + } + }) } diff --git a/internal/netxlite/mocks/resolver_test.go b/internal/netxlite/mocks/resolver_test.go index 8d1ad85..b696201 100644 --- a/internal/netxlite/mocks/resolver_test.go +++ b/internal/netxlite/mocks/resolver_test.go @@ -6,54 +6,56 @@ import ( "testing" ) -func TestResolverLookupHost(t *testing.T) { - expected := errors.New("mocked error") - r := &Resolver{ - MockLookupHost: func(ctx context.Context, domain string) ([]string, error) { - return nil, expected - }, - } - ctx := context.Background() - addrs, err := r.LookupHost(ctx, "dns.google") - if !errors.Is(err, expected) { - t.Fatal("unexpected error", err) - } - if addrs != nil { - t.Fatal("expected nil addr") - } -} +func TestResolver(t *testing.T) { + t.Run("LookupHost", func(t *testing.T) { + expected := errors.New("mocked error") + r := &Resolver{ + MockLookupHost: func(ctx context.Context, domain string) ([]string, error) { + return nil, expected + }, + } + ctx := context.Background() + addrs, err := r.LookupHost(ctx, "dns.google") + if !errors.Is(err, expected) { + t.Fatal("unexpected error", err) + } + if addrs != nil { + t.Fatal("expected nil addr") + } + }) -func TestResolverNetwork(t *testing.T) { - r := &Resolver{ - MockNetwork: func() string { - return "antani" - }, - } - if v := r.Network(); v != "antani" { - t.Fatal("unexpected network", v) - } -} + t.Run("Network", func(t *testing.T) { + r := &Resolver{ + MockNetwork: func() string { + return "antani" + }, + } + if v := r.Network(); v != "antani" { + t.Fatal("unexpected network", v) + } + }) -func TestResolverAddress(t *testing.T) { - r := &Resolver{ - MockAddress: func() string { - return "1.1.1.1" - }, - } - if v := r.Address(); v != "1.1.1.1" { - t.Fatal("unexpected address", v) - } -} + t.Run("Address", func(t *testing.T) { + r := &Resolver{ + MockAddress: func() string { + return "1.1.1.1" + }, + } + if v := r.Address(); v != "1.1.1.1" { + t.Fatal("unexpected address", v) + } + }) -func TestResolverCloseIdleConnections(t *testing.T) { - var called bool - r := &Resolver{ - MockCloseIdleConnections: func() { - called = true - }, - } - r.CloseIdleConnections() - if !called { - t.Fatal("not called") - } + t.Run("CloseIdleConnections", func(t *testing.T) { + var called bool + r := &Resolver{ + MockCloseIdleConnections: func() { + called = true + }, + } + r.CloseIdleConnections() + if !called { + t.Fatal("not called") + } + }) } diff --git a/internal/netxlite/mocks/tls_test.go b/internal/netxlite/mocks/tls_test.go index 3e4bac6..9b9d3ca 100644 --- a/internal/netxlite/mocks/tls_test.go +++ b/internal/netxlite/mocks/tls_test.go @@ -9,81 +9,87 @@ import ( "testing" ) -func TestTLSHandshakerHandshake(t *testing.T) { - expected := errors.New("mocked error") - conn := &Conn{} - ctx := context.Background() - config := &tls.Config{} - th := &TLSHandshaker{ - MockHandshake: func(ctx context.Context, conn net.Conn, - config *tls.Config) (net.Conn, tls.ConnectionState, error) { - return nil, tls.ConnectionState{}, expected - }, - } - tlsConn, connState, err := th.Handshake(ctx, conn, config) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } - if !reflect.ValueOf(connState).IsZero() { - t.Fatal("expected zero ConnectionState here") - } - if tlsConn != nil { - t.Fatal("expected nil conn here") - } +func TestTLSHandshaker(t *testing.T) { + t.Run("Handshake", func(t *testing.T) { + expected := errors.New("mocked error") + conn := &Conn{} + ctx := context.Background() + config := &tls.Config{} + th := &TLSHandshaker{ + MockHandshake: func(ctx context.Context, conn net.Conn, + config *tls.Config) (net.Conn, tls.ConnectionState, error) { + return nil, tls.ConnectionState{}, expected + }, + } + tlsConn, connState, err := th.Handshake(ctx, conn, config) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + if !reflect.ValueOf(connState).IsZero() { + t.Fatal("expected zero ConnectionState here") + } + if tlsConn != nil { + t.Fatal("expected nil conn here") + } + }) } -func TestTLSConnConnectionState(t *testing.T) { - state := tls.ConnectionState{Version: tls.VersionTLS12} - c := &TLSConn{ - MockConnectionState: func() tls.ConnectionState { - return state - }, - } - out := c.ConnectionState() - if !reflect.DeepEqual(out, state) { - t.Fatal("not the result we expected") - } +func TestTLSConn(t *testing.T) { + t.Run("ConnectionState", func(t *testing.T) { + state := tls.ConnectionState{Version: tls.VersionTLS12} + c := &TLSConn{ + MockConnectionState: func() tls.ConnectionState { + return state + }, + } + out := c.ConnectionState() + if !reflect.DeepEqual(out, state) { + t.Fatal("not the result we expected") + } + }) + + t.Run("HandshakeContext", func(t *testing.T) { + expected := errors.New("mocked error") + c := &TLSConn{ + MockHandshakeContext: func(ctx context.Context) error { + return expected + }, + } + err := c.HandshakeContext(context.Background()) + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + }) } -func TestTLSConnHandshakeContext(t *testing.T) { - expected := errors.New("mocked error") - c := &TLSConn{ - MockHandshakeContext: func(ctx context.Context) error { - return expected - }, - } - err := c.HandshakeContext(context.Background()) - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } -} +func TestTLSDialer(t *testing.T) { + t.Run("CloseIdleConnections", func(t *testing.T) { + var called bool + td := &TLSDialer{ + MockCloseIdleConnections: func() { + called = true + }, + } + td.CloseIdleConnections() + if !called { + t.Fatal("not called") + } + }) -func TestTLSDialerCloseIdleConnections(t *testing.T) { - var called bool - td := &TLSDialer{ - MockCloseIdleConnections: func() { - called = true - }, - } - td.CloseIdleConnections() - if !called { - t.Fatal("not called") - } -} - -func TestTLSDialerDialTLSContext(t *testing.T) { - expected := errors.New("mocked error") - td := &TLSDialer{ - MockDialTLSContext: func(ctx context.Context, network, address string) (net.Conn, error) { - return nil, expected - }, - } - ctx := context.Background() - conn, err := td.DialTLSContext(ctx, "", "") - if !errors.Is(err, expected) { - t.Fatal("not the error we expected", err) - } - if conn != nil { - t.Fatal("expected nil conn here") - } + t.Run("DialTLSContext", func(t *testing.T) { + expected := errors.New("mocked error") + td := &TLSDialer{ + MockDialTLSContext: func(ctx context.Context, network, address string) (net.Conn, error) { + return nil, expected + }, + } + ctx := context.Background() + conn, err := td.DialTLSContext(ctx, "", "") + if !errors.Is(err, expected) { + t.Fatal("not the error we expected", err) + } + if conn != nil { + t.Fatal("expected nil conn here") + } + }) } diff --git a/internal/netxlite/quic_test.go b/internal/netxlite/quic_test.go index ec55b53..59728f6 100644 --- a/internal/netxlite/quic_test.go +++ b/internal/netxlite/quic_test.go @@ -493,7 +493,7 @@ func TestNewSingleUseQUICDialerWorksAsIntended(t *testing.T) { func TestQUICListenerErrWrapper(t *testing.T) { t.Run("Listen", func(t *testing.T) { t.Run("on success", func(t *testing.T) { - expectedConn := &mocks.QUICUDPConn{} + expectedConn := &mocks.QUICUDPLikeConn{} ql := &quicListenerErrWrapper{ QUICListener: &mocks.QUICListener{ MockListen: func(addr *net.UDPAddr) (quicx.UDPLikeConn, error) { @@ -537,7 +537,7 @@ func TestQUICErrWrapperUDPLikeConn(t *testing.T) { expectedAddr := &net.UDPAddr{} p := make([]byte, 128) conn := &quicErrWrapperUDPLikeConn{ - UDPLikeConn: &mocks.QUICUDPConn{ + UDPLikeConn: &mocks.QUICUDPLikeConn{ MockReadFrom: func(p []byte) (n int, addr net.Addr, err error) { return len(p), expectedAddr, nil }, @@ -559,7 +559,7 @@ func TestQUICErrWrapperUDPLikeConn(t *testing.T) { p := make([]byte, 128) expectedErr := io.EOF conn := &quicErrWrapperUDPLikeConn{ - UDPLikeConn: &mocks.QUICUDPConn{ + UDPLikeConn: &mocks.QUICUDPLikeConn{ MockReadFrom: func(p []byte) (n int, addr net.Addr, err error) { return 0, nil, expectedErr }, @@ -582,7 +582,7 @@ func TestQUICErrWrapperUDPLikeConn(t *testing.T) { t.Run("on success", func(t *testing.T) { p := make([]byte, 128) conn := &quicErrWrapperUDPLikeConn{ - UDPLikeConn: &mocks.QUICUDPConn{ + UDPLikeConn: &mocks.QUICUDPLikeConn{ MockWriteTo: func(p []byte, addr net.Addr) (int, error) { return len(p), nil }, @@ -601,7 +601,7 @@ func TestQUICErrWrapperUDPLikeConn(t *testing.T) { p := make([]byte, 128) expectedErr := io.EOF conn := &quicErrWrapperUDPLikeConn{ - UDPLikeConn: &mocks.QUICUDPConn{ + UDPLikeConn: &mocks.QUICUDPLikeConn{ MockWriteTo: func(p []byte, addr net.Addr) (int, error) { return 0, expectedErr },