feat(errorsx): add error wrapper for quic (#422)
Part of https://github.com/ooni/probe/issues/1505
This commit is contained in:
parent
250a595f89
commit
30c7e2cdb3
7 changed files with 407 additions and 1 deletions
|
|
@ -7,7 +7,9 @@ import (
|
|||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
"github.com/ooni/probe-cli/v3/internal/quicx"
|
||||
)
|
||||
|
|
@ -266,3 +268,152 @@ func TestQUICEarlySessionReceiveMessage(t *testing.T) {
|
|||
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 TestQUICUDPConnReadMsgUDP(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
quc := &QUICUDPConn{
|
||||
MockReadMsgUDP: func(b, oob []byte) (int, int, int, *net.UDPAddr, error) {
|
||||
return 0, 0, 0, nil, expected
|
||||
},
|
||||
}
|
||||
b := make([]byte, 128)
|
||||
oob := make([]byte, 128)
|
||||
n, oobn, flags, addr, err := quc.ReadMsgUDP(b, oob)
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if n != 0 {
|
||||
t.Fatal("expected zero here")
|
||||
}
|
||||
if oobn != 0 {
|
||||
t.Fatal("expected zero here")
|
||||
}
|
||||
if flags != 0 {
|
||||
t.Fatal("expected zero here")
|
||||
}
|
||||
if addr != nil {
|
||||
t.Fatal("expected nil 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")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue