fix(netx): make sure we save quic udp conn events (#423)
https://github.com/ooni/probe-cli/pull/421 was wrong because we need a more rich interface for quic-go to call ReadMsgUDP. With this commit, we use such an interface: OOBCapablePacketConn. Still part of https://github.com/ooni/probe/issues/1505.
This commit is contained in:
parent
30c7e2cdb3
commit
ceb2aa8a8d
8 changed files with 82 additions and 49 deletions
|
|
@ -4,19 +4,19 @@ import (
|
|||
"context"
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
"github.com/ooni/probe-cli/v3/internal/quicx"
|
||||
)
|
||||
|
||||
// QUICListener is a mockable netxlite.QUICListener.
|
||||
type QUICListener struct {
|
||||
MockListen func(addr *net.UDPAddr) (quicx.UDPConn, error)
|
||||
MockListen func(addr *net.UDPAddr) (quic.OOBCapablePacketConn, error)
|
||||
}
|
||||
|
||||
// Listen calls MockListen.
|
||||
func (ql *QUICListener) Listen(addr *net.UDPAddr) (quicx.UDPConn, error) {
|
||||
func (ql *QUICListener) Listen(addr *net.UDPAddr) (quic.OOBCapablePacketConn, error) {
|
||||
return ql.MockListen(addr)
|
||||
}
|
||||
|
||||
|
|
@ -140,9 +140,11 @@ type QUICUDPConn struct {
|
|||
MockSetReadDeadline func(t time.Time) error
|
||||
MockSetWriteDeadline func(t time.Time) error
|
||||
MockReadFrom func(p []byte) (n int, addr net.Addr, err error)
|
||||
MockSyscallConn func() (syscall.RawConn, error)
|
||||
MockWriteMsgUDP func(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error)
|
||||
}
|
||||
|
||||
var _ net.PacketConn = &QUICUDPConn{}
|
||||
var _ quic.OOBCapablePacketConn = &QUICUDPConn{}
|
||||
|
||||
// WriteTo calls MockWriteTo.
|
||||
func (c *QUICUDPConn) WriteTo(p []byte, addr net.Addr) (int, error) {
|
||||
|
|
@ -188,3 +190,13 @@ func (c *QUICUDPConn) SetWriteDeadline(t time.Time) error {
|
|||
func (c *QUICUDPConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||
return c.MockReadFrom(b)
|
||||
}
|
||||
|
||||
// SyscallConn calls MockSyscallConn.
|
||||
func (c *QUICUDPConn) SyscallConn() (syscall.RawConn, error) {
|
||||
return c.MockSyscallConn()
|
||||
}
|
||||
|
||||
// WriteMsgUDP calls MockReadMsgUDP.
|
||||
func (c *QUICUDPConn) WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error) {
|
||||
return c.MockWriteMsgUDP(b, oob, addr)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,18 +6,18 @@ import (
|
|||
"errors"
|
||||
"net"
|
||||
"reflect"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
"github.com/ooni/probe-cli/v3/internal/quicx"
|
||||
)
|
||||
|
||||
func TestQUICListenerListen(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
ql := &QUICListener{
|
||||
MockListen: func(addr *net.UDPAddr) (quicx.UDPConn, error) {
|
||||
MockListen: func(addr *net.UDPAddr) (quic.OOBCapablePacketConn, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
|
|
@ -417,3 +417,41 @@ func TestQUICUDPConnReadFrom(t *testing.T) {
|
|||
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 TestQUICUDPConnWriteMsgUDP(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
quc := &QUICUDPConn{
|
||||
MockWriteMsgUDP: func(b, oob []byte, addr *net.UDPAddr) (n int, oobn int, err error) {
|
||||
return 0, 0, expected
|
||||
},
|
||||
}
|
||||
b := make([]byte, 128)
|
||||
oob := make([]byte, 128)
|
||||
addr := &net.UDPAddr{}
|
||||
n, oobn, err := quc.WriteMsgUDP(b, oob, addr)
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if n != 0 {
|
||||
t.Fatal("expected 0 here")
|
||||
}
|
||||
if oobn != 0 {
|
||||
t.Fatal("expected 0 here")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue