refactor(netxlite/mocks): group tests, fix naming inconsistencies (#485)
Part of https://github.com/ooni/probe/issues/1591
This commit is contained in:
parent
9e82e37ab8
commit
f054ec3201
|
@ -48,7 +48,7 @@ func TestErrorWrapperQUICListenerFailure(t *testing.T) {
|
||||||
|
|
||||||
func TestErrorWrapperUDPConnWriteToSuccess(t *testing.T) {
|
func TestErrorWrapperUDPConnWriteToSuccess(t *testing.T) {
|
||||||
quc := &errorWrapperUDPConn{
|
quc := &errorWrapperUDPConn{
|
||||||
UDPLikeConn: &mocks.QUICUDPConn{
|
UDPLikeConn: &mocks.QUICUDPLikeConn{
|
||||||
MockWriteTo: func(p []byte, addr net.Addr) (int, error) {
|
MockWriteTo: func(p []byte, addr net.Addr) (int, error) {
|
||||||
return 10, nil
|
return 10, nil
|
||||||
},
|
},
|
||||||
|
@ -68,7 +68,7 @@ func TestErrorWrapperUDPConnWriteToSuccess(t *testing.T) {
|
||||||
func TestErrorWrapperUDPConnWriteToFailure(t *testing.T) {
|
func TestErrorWrapperUDPConnWriteToFailure(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
expected := errors.New("mocked error")
|
||||||
quc := &errorWrapperUDPConn{
|
quc := &errorWrapperUDPConn{
|
||||||
UDPLikeConn: &mocks.QUICUDPConn{
|
UDPLikeConn: &mocks.QUICUDPLikeConn{
|
||||||
MockWriteTo: func(p []byte, addr net.Addr) (int, error) {
|
MockWriteTo: func(p []byte, addr net.Addr) (int, error) {
|
||||||
return 0, expected
|
return 0, expected
|
||||||
},
|
},
|
||||||
|
@ -88,7 +88,7 @@ func TestErrorWrapperUDPConnWriteToFailure(t *testing.T) {
|
||||||
func TestErrorWrapperUDPConnReadFromSuccess(t *testing.T) {
|
func TestErrorWrapperUDPConnReadFromSuccess(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
expected := errors.New("mocked error")
|
||||||
quc := &errorWrapperUDPConn{
|
quc := &errorWrapperUDPConn{
|
||||||
UDPLikeConn: &mocks.QUICUDPConn{
|
UDPLikeConn: &mocks.QUICUDPLikeConn{
|
||||||
MockReadFrom: func(b []byte) (int, net.Addr, error) {
|
MockReadFrom: func(b []byte) (int, net.Addr, error) {
|
||||||
return 0, nil, expected
|
return 0, nil, expected
|
||||||
},
|
},
|
||||||
|
@ -109,7 +109,7 @@ func TestErrorWrapperUDPConnReadFromSuccess(t *testing.T) {
|
||||||
|
|
||||||
func TestErrorWrapperUDPConnReadFromFailure(t *testing.T) {
|
func TestErrorWrapperUDPConnReadFromFailure(t *testing.T) {
|
||||||
quc := &errorWrapperUDPConn{
|
quc := &errorWrapperUDPConn{
|
||||||
UDPLikeConn: &mocks.QUICUDPConn{
|
UDPLikeConn: &mocks.QUICUDPLikeConn{
|
||||||
MockReadFrom: func(b []byte) (int, net.Addr, error) {
|
MockReadFrom: func(b []byte) (int, net.Addr, error) {
|
||||||
return 10, nil, nil
|
return 10, nil, nil
|
||||||
},
|
},
|
||||||
|
|
|
@ -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{}
|
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -3,6 +3,7 @@ package mocks
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Dialer is a mockable Dialer.
|
// Dialer is a mockable Dialer.
|
||||||
|
@ -20,3 +21,57 @@ func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.
|
||||||
func (d *Dialer) CloseIdleConnections() {
|
func (d *Dialer) CloseIdleConnections() {
|
||||||
d.MockCloseIdleConnections()
|
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{}
|
||||||
|
|
|
@ -5,34 +5,157 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDialerDialContext(t *testing.T) {
|
func TestDialer(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
t.Run("DialContext", func(t *testing.T) {
|
||||||
d := Dialer{
|
expected := errors.New("mocked error")
|
||||||
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
d := Dialer{
|
||||||
return nil, expected
|
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")
|
ctx := context.Background()
|
||||||
if !errors.Is(err, expected) {
|
conn, err := d.DialContext(ctx, "tcp", "8.8.8.8:53")
|
||||||
t.Fatal("not the error we expected")
|
if !errors.Is(err, expected) {
|
||||||
}
|
t.Fatal("not the error we expected")
|
||||||
if conn != nil {
|
}
|
||||||
t.Fatal("expected nil conn")
|
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) {
|
func TestConn(t *testing.T) {
|
||||||
var called bool
|
t.Run("Read", func(t *testing.T) {
|
||||||
d := &Dialer{
|
expected := errors.New("mocked error")
|
||||||
MockCloseIdleConnections: func() {
|
c := &Conn{
|
||||||
called = true
|
MockRead: func(b []byte) (int, error) {
|
||||||
},
|
return 0, expected
|
||||||
}
|
},
|
||||||
d.CloseIdleConnections()
|
}
|
||||||
if !called {
|
count, err := c.Read(make([]byte, 128))
|
||||||
t.Fatal("not called")
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,31 +8,33 @@ import (
|
||||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHTTPTransportRoundTrip(t *testing.T) {
|
func TestHTTPTransport(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
t.Run("RoundTrip", func(t *testing.T) {
|
||||||
txp := &HTTPTransport{
|
expected := errors.New("mocked error")
|
||||||
MockRoundTrip: func(req *http.Request) (*http.Response, error) {
|
txp := &HTTPTransport{
|
||||||
return nil, expected
|
MockRoundTrip: func(req *http.Request) (*http.Response, error) {
|
||||||
},
|
return nil, expected
|
||||||
}
|
},
|
||||||
resp, err := txp.RoundTrip(&http.Request{})
|
}
|
||||||
if !errors.Is(err, expected) {
|
resp, err := txp.RoundTrip(&http.Request{})
|
||||||
t.Fatal("not the error we expected", err)
|
if !errors.Is(err, expected) {
|
||||||
}
|
t.Fatal("not the error we expected", err)
|
||||||
if resp != nil {
|
}
|
||||||
t.Fatal("expected nil response here")
|
if resp != nil {
|
||||||
}
|
t.Fatal("expected nil response here")
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
func TestHTTPTransportCloseIdleConnections(t *testing.T) {
|
t.Run("CloseIdleConnections", func(t *testing.T) {
|
||||||
called := &atomicx.Int64{}
|
called := &atomicx.Int64{}
|
||||||
txp := &HTTPTransport{
|
txp := &HTTPTransport{
|
||||||
MockCloseIdleConnections: func() {
|
MockCloseIdleConnections: func() {
|
||||||
called.Add(1)
|
called.Add(1)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
txp.CloseIdleConnections()
|
txp.CloseIdleConnections()
|
||||||
if called.Load() != 1 {
|
if called.Load() != 1 {
|
||||||
t.Fatal("not called")
|
t.Fatal("not called")
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,21 +9,23 @@ import (
|
||||||
"github.com/lucas-clemente/quic-go"
|
"github.com/lucas-clemente/quic-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestQUICContextDialerDialContext(t *testing.T) {
|
func TestQUICContextDialer(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
t.Run("DialContext", func(t *testing.T) {
|
||||||
qcd := &QUICContextDialer{
|
expected := errors.New("mocked error")
|
||||||
MockDialContext: func(ctx context.Context, network string, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) {
|
qcd := &QUICContextDialer{
|
||||||
return nil, expected
|
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{}
|
ctx := context.Background()
|
||||||
quicConfig := &quic.Config{}
|
tlsConfig := &tls.Config{}
|
||||||
sess, err := qcd.DialContext(ctx, "udp", "dns.google:443", tlsConfig, quicConfig)
|
quicConfig := &quic.Config{}
|
||||||
if !errors.Is(err, expected) {
|
sess, err := qcd.DialContext(ctx, "udp", "dns.google:443", tlsConfig, quicConfig)
|
||||||
t.Fatal("not the error we expected")
|
if !errors.Is(err, expected) {
|
||||||
}
|
t.Fatal("not the error we expected")
|
||||||
if sess != nil {
|
}
|
||||||
t.Fatal("expected nil session")
|
if sess != nil {
|
||||||
}
|
t.Fatal("expected nil session")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -139,8 +139,8 @@ func (s *QUICEarlySession) ReceiveMessage() ([]byte, error) {
|
||||||
return s.MockReceiveMessage()
|
return s.MockReceiveMessage()
|
||||||
}
|
}
|
||||||
|
|
||||||
// QUICUDPConn is an UDP conn used by QUIC.
|
// QUICUDPLikeConn is an UDP conn used by QUIC.
|
||||||
type QUICUDPConn struct {
|
type QUICUDPLikeConn struct {
|
||||||
MockWriteTo func(p []byte, addr net.Addr) (int, error)
|
MockWriteTo func(p []byte, addr net.Addr) (int, error)
|
||||||
MockClose func() error
|
MockClose func() error
|
||||||
MockLocalAddr func() net.Addr
|
MockLocalAddr func() net.Addr
|
||||||
|
@ -153,54 +153,54 @@ type QUICUDPConn struct {
|
||||||
MockSetReadBuffer func(n int) error
|
MockSetReadBuffer func(n int) error
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ quicx.UDPLikeConn = &QUICUDPConn{}
|
var _ quicx.UDPLikeConn = &QUICUDPLikeConn{}
|
||||||
|
|
||||||
// WriteTo calls MockWriteTo.
|
// 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)
|
return c.MockWriteTo(p, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close calls MockClose.
|
// Close calls MockClose.
|
||||||
func (c *QUICUDPConn) Close() error {
|
func (c *QUICUDPLikeConn) Close() error {
|
||||||
return c.MockClose()
|
return c.MockClose()
|
||||||
}
|
}
|
||||||
|
|
||||||
// LocalAddr calls MockLocalAddr.
|
// LocalAddr calls MockLocalAddr.
|
||||||
func (c *QUICUDPConn) LocalAddr() net.Addr {
|
func (c *QUICUDPLikeConn) LocalAddr() net.Addr {
|
||||||
return c.MockLocalAddr()
|
return c.MockLocalAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoteAddr calls MockRemoteAddr.
|
// RemoteAddr calls MockRemoteAddr.
|
||||||
func (c *QUICUDPConn) RemoteAddr() net.Addr {
|
func (c *QUICUDPLikeConn) RemoteAddr() net.Addr {
|
||||||
return c.MockRemoteAddr()
|
return c.MockRemoteAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetDeadline calls MockSetDeadline.
|
// SetDeadline calls MockSetDeadline.
|
||||||
func (c *QUICUDPConn) SetDeadline(t time.Time) error {
|
func (c *QUICUDPLikeConn) SetDeadline(t time.Time) error {
|
||||||
return c.MockSetDeadline(t)
|
return c.MockSetDeadline(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetReadDeadline calls MockSetReadDeadline.
|
// SetReadDeadline calls MockSetReadDeadline.
|
||||||
func (c *QUICUDPConn) SetReadDeadline(t time.Time) error {
|
func (c *QUICUDPLikeConn) SetReadDeadline(t time.Time) error {
|
||||||
return c.MockSetReadDeadline(t)
|
return c.MockSetReadDeadline(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetWriteDeadline calls MockSetWriteDeadline.
|
// SetWriteDeadline calls MockSetWriteDeadline.
|
||||||
func (c *QUICUDPConn) SetWriteDeadline(t time.Time) error {
|
func (c *QUICUDPLikeConn) SetWriteDeadline(t time.Time) error {
|
||||||
return c.MockSetWriteDeadline(t)
|
return c.MockSetWriteDeadline(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadFrom calls MockReadFrom.
|
// 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)
|
return c.MockReadFrom(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SyscallConn calls MockSyscallConn.
|
// SyscallConn calls MockSyscallConn.
|
||||||
func (c *QUICUDPConn) SyscallConn() (syscall.RawConn, error) {
|
func (c *QUICUDPLikeConn) SyscallConn() (syscall.RawConn, error) {
|
||||||
return c.MockSyscallConn()
|
return c.MockSyscallConn()
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetReadBuffer calls MockSetReadBuffer.
|
// SetReadBuffer calls MockSetReadBuffer.
|
||||||
func (c *QUICUDPConn) SetReadBuffer(n int) error {
|
func (c *QUICUDPLikeConn) SetReadBuffer(n int) error {
|
||||||
return c.MockSetReadBuffer(n)
|
return c.MockSetReadBuffer(n)
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,420 +16,428 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestQUICListenerListen(t *testing.T) {
|
func TestQUICListenerListen(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
t.Run("Listen", func(t *testing.T) {
|
||||||
ql := &QUICListener{
|
expected := errors.New("mocked error")
|
||||||
MockListen: func(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
|
ql := &QUICListener{
|
||||||
return nil, expected
|
MockListen: func(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
|
||||||
},
|
return nil, expected
|
||||||
}
|
},
|
||||||
pconn, err := ql.Listen(&net.UDPAddr{})
|
}
|
||||||
if !errors.Is(err, expected) {
|
pconn, err := ql.Listen(&net.UDPAddr{})
|
||||||
t.Fatal("not the error we expected", expected)
|
if !errors.Is(err, expected) {
|
||||||
}
|
t.Fatal("not the error we expected", expected)
|
||||||
if pconn != nil {
|
}
|
||||||
t.Fatal("expected nil conn here")
|
if pconn != nil {
|
||||||
}
|
t.Fatal("expected nil conn here")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestQUICDialerDialContext(t *testing.T) {
|
func TestQUICDialer(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
t.Run("DialContext", func(t *testing.T) {
|
||||||
qcd := &QUICDialer{
|
expected := errors.New("mocked error")
|
||||||
MockDialContext: func(ctx context.Context, network string, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) {
|
qcd := &QUICDialer{
|
||||||
return nil, expected
|
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{}
|
ctx := context.Background()
|
||||||
quicConfig := &quic.Config{}
|
tlsConfig := &tls.Config{}
|
||||||
sess, err := qcd.DialContext(ctx, "udp", "dns.google:443", tlsConfig, quicConfig)
|
quicConfig := &quic.Config{}
|
||||||
if !errors.Is(err, expected) {
|
sess, err := qcd.DialContext(ctx, "udp", "dns.google:443", tlsConfig, quicConfig)
|
||||||
t.Fatal("not the error we expected")
|
if !errors.Is(err, expected) {
|
||||||
}
|
t.Fatal("not the error we expected")
|
||||||
if sess != nil {
|
}
|
||||||
t.Fatal("expected nil session")
|
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) {
|
func TestQUICEarlySession(t *testing.T) {
|
||||||
var called bool
|
t.Run("AcceptStream", func(t *testing.T) {
|
||||||
qcd := &QUICDialer{
|
expected := errors.New("mocked error")
|
||||||
MockCloseIdleConnections: func() {
|
sess := &QUICEarlySession{
|
||||||
called = true
|
MockAcceptStream: func(ctx context.Context) (quic.Stream, error) {
|
||||||
},
|
return nil, expected
|
||||||
}
|
},
|
||||||
qcd.CloseIdleConnections()
|
}
|
||||||
if !called {
|
ctx := context.Background()
|
||||||
t.Fatal("not called")
|
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) {
|
func TestQUICUDPLikeConn(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
t.Run("WriteTo", func(t *testing.T) {
|
||||||
sess := &QUICEarlySession{
|
expected := errors.New("mocked error")
|
||||||
MockAcceptStream: func(ctx context.Context) (quic.Stream, error) {
|
quc := &QUICUDPLikeConn{
|
||||||
return nil, expected
|
MockWriteTo: func(p []byte, addr net.Addr) (int, error) {
|
||||||
},
|
return 0, expected
|
||||||
}
|
},
|
||||||
ctx := context.Background()
|
}
|
||||||
stream, err := sess.AcceptStream(ctx)
|
pkt := make([]byte, 128)
|
||||||
if !errors.Is(err, expected) {
|
addr := &net.UDPAddr{}
|
||||||
t.Fatal("not the error we expected", err)
|
cnt, err := quc.WriteTo(pkt, addr)
|
||||||
}
|
if !errors.Is(err, expected) {
|
||||||
if stream != nil {
|
t.Fatal("not the error we expected", err)
|
||||||
t.Fatal("expected nil stream here")
|
}
|
||||||
}
|
if cnt != 0 {
|
||||||
}
|
t.Fatal("expected zero here")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
func TestQUICEarlySessionAcceptUniStream(t *testing.T) {
|
t.Run("ConnClose", func(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
expected := errors.New("mocked error")
|
||||||
sess := &QUICEarlySession{
|
quc := &QUICUDPLikeConn{
|
||||||
MockAcceptUniStream: func(ctx context.Context) (quic.ReceiveStream, error) {
|
MockClose: func() error {
|
||||||
return nil, expected
|
return expected
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
ctx := context.Background()
|
err := quc.Close()
|
||||||
stream, err := sess.AcceptUniStream(ctx)
|
if !errors.Is(err, expected) {
|
||||||
if !errors.Is(err, expected) {
|
t.Fatal("not the error we expected", err)
|
||||||
t.Fatal("not the error we expected", err)
|
}
|
||||||
}
|
})
|
||||||
if stream != nil {
|
|
||||||
t.Fatal("expected nil stream here")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestQUICEarlySessionOpenStream(t *testing.T) {
|
t.Run("LocalAddr", func(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
expected := &net.TCPAddr{
|
||||||
sess := &QUICEarlySession{
|
IP: net.IPv6loopback,
|
||||||
MockOpenStream: func() (quic.Stream, error) {
|
Port: 1234,
|
||||||
return nil, expected
|
}
|
||||||
},
|
c := &QUICUDPLikeConn{
|
||||||
}
|
MockLocalAddr: func() net.Addr {
|
||||||
stream, err := sess.OpenStream()
|
return expected
|
||||||
if !errors.Is(err, expected) {
|
},
|
||||||
t.Fatal("not the error we expected", err)
|
}
|
||||||
}
|
out := c.LocalAddr()
|
||||||
if stream != nil {
|
if diff := cmp.Diff(expected, out); diff != "" {
|
||||||
t.Fatal("expected nil stream here")
|
t.Fatal(diff)
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
func TestQUICEarlySessionOpenStreamSync(t *testing.T) {
|
t.Run("RemoteAddr", func(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
expected := &net.TCPAddr{
|
||||||
sess := &QUICEarlySession{
|
IP: net.IPv6loopback,
|
||||||
MockOpenStreamSync: func(ctx context.Context) (quic.Stream, error) {
|
Port: 1234,
|
||||||
return nil, expected
|
}
|
||||||
},
|
c := &QUICUDPLikeConn{
|
||||||
}
|
MockRemoteAddr: func() net.Addr {
|
||||||
ctx := context.Background()
|
return expected
|
||||||
stream, err := sess.OpenStreamSync(ctx)
|
},
|
||||||
if !errors.Is(err, expected) {
|
}
|
||||||
t.Fatal("not the error we expected", err)
|
out := c.RemoteAddr()
|
||||||
}
|
if diff := cmp.Diff(expected, out); diff != "" {
|
||||||
if stream != nil {
|
t.Fatal(diff)
|
||||||
t.Fatal("expected nil stream here")
|
}
|
||||||
}
|
})
|
||||||
}
|
|
||||||
|
|
||||||
func TestQUICEarlySessionOpenUniStream(t *testing.T) {
|
t.Run("SetDeadline", func(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
expected := errors.New("mocked error")
|
||||||
sess := &QUICEarlySession{
|
c := &QUICUDPLikeConn{
|
||||||
MockOpenUniStream: func() (quic.SendStream, error) {
|
MockSetDeadline: func(t time.Time) error {
|
||||||
return nil, expected
|
return expected
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
stream, err := sess.OpenUniStream()
|
err := c.SetDeadline(time.Time{})
|
||||||
if !errors.Is(err, expected) {
|
if !errors.Is(err, expected) {
|
||||||
t.Fatal("not the error we expected", err)
|
t.Fatal("not the error we expected", err)
|
||||||
}
|
}
|
||||||
if stream != nil {
|
})
|
||||||
t.Fatal("expected nil stream here")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestQUICEarlySessionOpenUniStreamSync(t *testing.T) {
|
t.Run("SetReadDeadline", func(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
expected := errors.New("mocked error")
|
||||||
sess := &QUICEarlySession{
|
c := &QUICUDPLikeConn{
|
||||||
MockOpenUniStreamSync: func(ctx context.Context) (quic.SendStream, error) {
|
MockSetReadDeadline: func(t time.Time) error {
|
||||||
return nil, expected
|
return expected
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
ctx := context.Background()
|
err := c.SetReadDeadline(time.Time{})
|
||||||
stream, err := sess.OpenUniStreamSync(ctx)
|
if !errors.Is(err, expected) {
|
||||||
if !errors.Is(err, expected) {
|
t.Fatal("not the error we expected", err)
|
||||||
t.Fatal("not the error we expected", err)
|
}
|
||||||
}
|
})
|
||||||
if stream != nil {
|
|
||||||
t.Fatal("expected nil stream here")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestQUICEarlySessionLocalAddr(t *testing.T) {
|
t.Run("SetWriteDeadline", func(t *testing.T) {
|
||||||
sess := &QUICEarlySession{
|
expected := errors.New("mocked error")
|
||||||
MockLocalAddr: func() net.Addr {
|
c := &QUICUDPLikeConn{
|
||||||
return &net.UDPAddr{}
|
MockSetWriteDeadline: func(t time.Time) error {
|
||||||
},
|
return expected
|
||||||
}
|
},
|
||||||
addr := sess.LocalAddr()
|
}
|
||||||
if !reflect.ValueOf(addr).Elem().IsZero() {
|
err := c.SetWriteDeadline(time.Time{})
|
||||||
t.Fatal("expected a zero address here")
|
if !errors.Is(err, expected) {
|
||||||
}
|
t.Fatal("not the error we expected", err)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
func TestQUICEarlySessionRemoteAddr(t *testing.T) {
|
t.Run("ConnReadFrom", func(t *testing.T) {
|
||||||
sess := &QUICEarlySession{
|
expected := errors.New("mocked error")
|
||||||
MockRemoteAddr: func() net.Addr {
|
quc := &QUICUDPLikeConn{
|
||||||
return &net.UDPAddr{}
|
MockReadFrom: func(b []byte) (int, net.Addr, error) {
|
||||||
},
|
return 0, nil, expected
|
||||||
}
|
},
|
||||||
addr := sess.RemoteAddr()
|
}
|
||||||
if !reflect.ValueOf(addr).Elem().IsZero() {
|
b := make([]byte, 128)
|
||||||
t.Fatal("expected a zero address here")
|
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) {
|
t.Run("SyscallConn", func(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
expected := errors.New("mocked error")
|
||||||
sess := &QUICEarlySession{
|
quc := &QUICUDPLikeConn{
|
||||||
MockCloseWithError: func(
|
MockSyscallConn: func() (syscall.RawConn, error) {
|
||||||
code quic.ApplicationErrorCode, reason string) error {
|
return nil, expected
|
||||||
return expected
|
},
|
||||||
},
|
}
|
||||||
}
|
conn, err := quc.SyscallConn()
|
||||||
err := sess.CloseWithError(0, "")
|
if !errors.Is(err, expected) {
|
||||||
if !errors.Is(err, expected) {
|
t.Fatal("not the error we expected", err)
|
||||||
t.Fatal("not the error we expected", err)
|
}
|
||||||
}
|
if conn != nil {
|
||||||
}
|
t.Fatal("expected nil here")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
func TestQUICEarlySessionContext(t *testing.T) {
|
t.Run("SetReadBuffer", func(t *testing.T) {
|
||||||
ctx := context.Background()
|
expected := errors.New("mocked error")
|
||||||
sess := &QUICEarlySession{
|
quc := &QUICUDPLikeConn{
|
||||||
MockContext: func() context.Context {
|
MockSetReadBuffer: func(n int) error {
|
||||||
return ctx
|
return expected
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
out := sess.Context()
|
err := quc.SetReadBuffer(1 << 10)
|
||||||
if !reflect.DeepEqual(ctx, out) {
|
if !errors.Is(err, expected) {
|
||||||
t.Fatal("not the context we expected")
|
t.Fatal("not the error we expected", err)
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,19 +5,21 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestReaderRead(t *testing.T) {
|
func TestReader(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
t.Run("Read", func(t *testing.T) {
|
||||||
r := &Reader{
|
expected := errors.New("mocked error")
|
||||||
MockRead: func(b []byte) (int, error) {
|
r := &Reader{
|
||||||
return 0, expected
|
MockRead: func(b []byte) (int, error) {
|
||||||
},
|
return 0, expected
|
||||||
}
|
},
|
||||||
b := make([]byte, 128)
|
}
|
||||||
count, err := r.Read(b)
|
b := make([]byte, 128)
|
||||||
if !errors.Is(err, expected) {
|
count, err := r.Read(b)
|
||||||
t.Fatal("unexpected error", err)
|
if !errors.Is(err, expected) {
|
||||||
}
|
t.Fatal("unexpected error", err)
|
||||||
if count != 0 {
|
}
|
||||||
t.Fatal("unexpected count", count)
|
if count != 0 {
|
||||||
}
|
t.Fatal("unexpected count", count)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,54 +6,56 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestResolverLookupHost(t *testing.T) {
|
func TestResolver(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
t.Run("LookupHost", func(t *testing.T) {
|
||||||
r := &Resolver{
|
expected := errors.New("mocked error")
|
||||||
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
r := &Resolver{
|
||||||
return nil, expected
|
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
||||||
},
|
return nil, expected
|
||||||
}
|
},
|
||||||
ctx := context.Background()
|
}
|
||||||
addrs, err := r.LookupHost(ctx, "dns.google")
|
ctx := context.Background()
|
||||||
if !errors.Is(err, expected) {
|
addrs, err := r.LookupHost(ctx, "dns.google")
|
||||||
t.Fatal("unexpected error", err)
|
if !errors.Is(err, expected) {
|
||||||
}
|
t.Fatal("unexpected error", err)
|
||||||
if addrs != nil {
|
}
|
||||||
t.Fatal("expected nil addr")
|
if addrs != nil {
|
||||||
}
|
t.Fatal("expected nil addr")
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
func TestResolverNetwork(t *testing.T) {
|
t.Run("Network", func(t *testing.T) {
|
||||||
r := &Resolver{
|
r := &Resolver{
|
||||||
MockNetwork: func() string {
|
MockNetwork: func() string {
|
||||||
return "antani"
|
return "antani"
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if v := r.Network(); v != "antani" {
|
if v := r.Network(); v != "antani" {
|
||||||
t.Fatal("unexpected network", v)
|
t.Fatal("unexpected network", v)
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
func TestResolverAddress(t *testing.T) {
|
t.Run("Address", func(t *testing.T) {
|
||||||
r := &Resolver{
|
r := &Resolver{
|
||||||
MockAddress: func() string {
|
MockAddress: func() string {
|
||||||
return "1.1.1.1"
|
return "1.1.1.1"
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if v := r.Address(); v != "1.1.1.1" {
|
if v := r.Address(); v != "1.1.1.1" {
|
||||||
t.Fatal("unexpected address", v)
|
t.Fatal("unexpected address", v)
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
func TestResolverCloseIdleConnections(t *testing.T) {
|
t.Run("CloseIdleConnections", func(t *testing.T) {
|
||||||
var called bool
|
var called bool
|
||||||
r := &Resolver{
|
r := &Resolver{
|
||||||
MockCloseIdleConnections: func() {
|
MockCloseIdleConnections: func() {
|
||||||
called = true
|
called = true
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
r.CloseIdleConnections()
|
r.CloseIdleConnections()
|
||||||
if !called {
|
if !called {
|
||||||
t.Fatal("not called")
|
t.Fatal("not called")
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,81 +9,87 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTLSHandshakerHandshake(t *testing.T) {
|
func TestTLSHandshaker(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
t.Run("Handshake", func(t *testing.T) {
|
||||||
conn := &Conn{}
|
expected := errors.New("mocked error")
|
||||||
ctx := context.Background()
|
conn := &Conn{}
|
||||||
config := &tls.Config{}
|
ctx := context.Background()
|
||||||
th := &TLSHandshaker{
|
config := &tls.Config{}
|
||||||
MockHandshake: func(ctx context.Context, conn net.Conn,
|
th := &TLSHandshaker{
|
||||||
config *tls.Config) (net.Conn, tls.ConnectionState, error) {
|
MockHandshake: func(ctx context.Context, conn net.Conn,
|
||||||
return nil, tls.ConnectionState{}, expected
|
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) {
|
tlsConn, connState, err := th.Handshake(ctx, conn, config)
|
||||||
t.Fatal("not the error we expected", err)
|
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 !reflect.ValueOf(connState).IsZero() {
|
||||||
}
|
t.Fatal("expected zero ConnectionState here")
|
||||||
if tlsConn != nil {
|
}
|
||||||
t.Fatal("expected nil conn here")
|
if tlsConn != nil {
|
||||||
}
|
t.Fatal("expected nil conn here")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTLSConnConnectionState(t *testing.T) {
|
func TestTLSConn(t *testing.T) {
|
||||||
state := tls.ConnectionState{Version: tls.VersionTLS12}
|
t.Run("ConnectionState", func(t *testing.T) {
|
||||||
c := &TLSConn{
|
state := tls.ConnectionState{Version: tls.VersionTLS12}
|
||||||
MockConnectionState: func() tls.ConnectionState {
|
c := &TLSConn{
|
||||||
return state
|
MockConnectionState: func() tls.ConnectionState {
|
||||||
},
|
return state
|
||||||
}
|
},
|
||||||
out := c.ConnectionState()
|
}
|
||||||
if !reflect.DeepEqual(out, state) {
|
out := c.ConnectionState()
|
||||||
t.Fatal("not the result we expected")
|
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) {
|
func TestTLSDialer(t *testing.T) {
|
||||||
expected := errors.New("mocked error")
|
t.Run("CloseIdleConnections", func(t *testing.T) {
|
||||||
c := &TLSConn{
|
var called bool
|
||||||
MockHandshakeContext: func(ctx context.Context) error {
|
td := &TLSDialer{
|
||||||
return expected
|
MockCloseIdleConnections: func() {
|
||||||
},
|
called = true
|
||||||
}
|
},
|
||||||
err := c.HandshakeContext(context.Background())
|
}
|
||||||
if !errors.Is(err, expected) {
|
td.CloseIdleConnections()
|
||||||
t.Fatal("not the error we expected", err)
|
if !called {
|
||||||
}
|
t.Fatal("not called")
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
func TestTLSDialerCloseIdleConnections(t *testing.T) {
|
t.Run("DialTLSContext", func(t *testing.T) {
|
||||||
var called bool
|
expected := errors.New("mocked error")
|
||||||
td := &TLSDialer{
|
td := &TLSDialer{
|
||||||
MockCloseIdleConnections: func() {
|
MockDialTLSContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||||
called = true
|
return nil, expected
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
td.CloseIdleConnections()
|
ctx := context.Background()
|
||||||
if !called {
|
conn, err := td.DialTLSContext(ctx, "", "")
|
||||||
t.Fatal("not called")
|
if !errors.Is(err, expected) {
|
||||||
}
|
t.Fatal("not the error we expected", err)
|
||||||
}
|
}
|
||||||
|
if conn != nil {
|
||||||
func TestTLSDialerDialTLSContext(t *testing.T) {
|
t.Fatal("expected nil conn here")
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -493,7 +493,7 @@ func TestNewSingleUseQUICDialerWorksAsIntended(t *testing.T) {
|
||||||
func TestQUICListenerErrWrapper(t *testing.T) {
|
func TestQUICListenerErrWrapper(t *testing.T) {
|
||||||
t.Run("Listen", func(t *testing.T) {
|
t.Run("Listen", func(t *testing.T) {
|
||||||
t.Run("on success", func(t *testing.T) {
|
t.Run("on success", func(t *testing.T) {
|
||||||
expectedConn := &mocks.QUICUDPConn{}
|
expectedConn := &mocks.QUICUDPLikeConn{}
|
||||||
ql := &quicListenerErrWrapper{
|
ql := &quicListenerErrWrapper{
|
||||||
QUICListener: &mocks.QUICListener{
|
QUICListener: &mocks.QUICListener{
|
||||||
MockListen: func(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
|
MockListen: func(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
|
||||||
|
@ -537,7 +537,7 @@ func TestQUICErrWrapperUDPLikeConn(t *testing.T) {
|
||||||
expectedAddr := &net.UDPAddr{}
|
expectedAddr := &net.UDPAddr{}
|
||||||
p := make([]byte, 128)
|
p := make([]byte, 128)
|
||||||
conn := &quicErrWrapperUDPLikeConn{
|
conn := &quicErrWrapperUDPLikeConn{
|
||||||
UDPLikeConn: &mocks.QUICUDPConn{
|
UDPLikeConn: &mocks.QUICUDPLikeConn{
|
||||||
MockReadFrom: func(p []byte) (n int, addr net.Addr, err error) {
|
MockReadFrom: func(p []byte) (n int, addr net.Addr, err error) {
|
||||||
return len(p), expectedAddr, nil
|
return len(p), expectedAddr, nil
|
||||||
},
|
},
|
||||||
|
@ -559,7 +559,7 @@ func TestQUICErrWrapperUDPLikeConn(t *testing.T) {
|
||||||
p := make([]byte, 128)
|
p := make([]byte, 128)
|
||||||
expectedErr := io.EOF
|
expectedErr := io.EOF
|
||||||
conn := &quicErrWrapperUDPLikeConn{
|
conn := &quicErrWrapperUDPLikeConn{
|
||||||
UDPLikeConn: &mocks.QUICUDPConn{
|
UDPLikeConn: &mocks.QUICUDPLikeConn{
|
||||||
MockReadFrom: func(p []byte) (n int, addr net.Addr, err error) {
|
MockReadFrom: func(p []byte) (n int, addr net.Addr, err error) {
|
||||||
return 0, nil, expectedErr
|
return 0, nil, expectedErr
|
||||||
},
|
},
|
||||||
|
@ -582,7 +582,7 @@ func TestQUICErrWrapperUDPLikeConn(t *testing.T) {
|
||||||
t.Run("on success", func(t *testing.T) {
|
t.Run("on success", func(t *testing.T) {
|
||||||
p := make([]byte, 128)
|
p := make([]byte, 128)
|
||||||
conn := &quicErrWrapperUDPLikeConn{
|
conn := &quicErrWrapperUDPLikeConn{
|
||||||
UDPLikeConn: &mocks.QUICUDPConn{
|
UDPLikeConn: &mocks.QUICUDPLikeConn{
|
||||||
MockWriteTo: func(p []byte, addr net.Addr) (int, error) {
|
MockWriteTo: func(p []byte, addr net.Addr) (int, error) {
|
||||||
return len(p), nil
|
return len(p), nil
|
||||||
},
|
},
|
||||||
|
@ -601,7 +601,7 @@ func TestQUICErrWrapperUDPLikeConn(t *testing.T) {
|
||||||
p := make([]byte, 128)
|
p := make([]byte, 128)
|
||||||
expectedErr := io.EOF
|
expectedErr := io.EOF
|
||||||
conn := &quicErrWrapperUDPLikeConn{
|
conn := &quicErrWrapperUDPLikeConn{
|
||||||
UDPLikeConn: &mocks.QUICUDPConn{
|
UDPLikeConn: &mocks.QUICUDPLikeConn{
|
||||||
MockWriteTo: func(p []byte, addr net.Addr) (int, error) {
|
MockWriteTo: func(p []byte, addr net.Addr) (int, error) {
|
||||||
return 0, expectedErr
|
return 0, expectedErr
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue
Block a user