chore(natpmp): more robust tests with longer connection durations

This commit is contained in:
Quentin McGaw
2023-07-06 06:54:01 +00:00
parent 075a1e2a80
commit 9c0f187a12
4 changed files with 49 additions and 45 deletions

View File

@@ -28,14 +28,14 @@ func Test_Client_ExternalAddress(t *testing.T) {
"failure": {
ctx: canceledCtx,
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
initialConnDuration: time.Millisecond,
initialConnDuration: initialConnectionDuration,
err: context.Canceled,
errMessage: "executing remote procedure call: reading from udp connection: context canceled",
},
"success": {
ctx: context.Background(),
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
initialConnDuration: time.Millisecond,
initialConnDuration: initialConnectionDuration,
exchanges: []udpExchange{{
request: []byte{0, 0},
response: []byte{0x0, 0x80, 0x0, 0x0, 0x0, 0x13, 0xf2, 0x4f, 0x49, 0x8c, 0x36, 0x9a},

View File

@@ -4,11 +4,15 @@ import (
"errors"
"net"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// enough for slow machines for local UDP server.
const initialConnectionDuration = 3 * time.Second
type udpExchange struct {
request []byte
response []byte

View File

@@ -19,7 +19,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
internalPort uint16
requestedExternalPort uint16
lifetime time.Duration
initialConnDuration time.Duration
initialConnectionDuration time.Duration
exchanges []udpExchange
durationSinceStartOfEpoch time.Duration
assignedInternalPort uint16
@@ -46,7 +46,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
internalPort: 123,
requestedExternalPort: 456,
lifetime: 1200 * time.Second,
initialConnDuration: time.Millisecond,
initialConnectionDuration: time.Millisecond,
exchanges: []udpExchange{{close: true}},
err: ErrConnectionTimeout,
errMessage: "executing remote procedure call: connection timeout: after 1ms",
@@ -58,7 +58,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
internalPort: 123,
requestedExternalPort: 456,
lifetime: 1200 * time.Second,
initialConnDuration: time.Second,
initialConnectionDuration: initialConnectionDuration,
exchanges: []udpExchange{{
request: []byte{0x0, 0x1, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
response: []byte{0x0, 0x81, 0x0, 0x0, 0x0, 0x13, 0xfe, 0xff, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
@@ -75,7 +75,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
internalPort: 123,
requestedExternalPort: 456,
lifetime: 1200 * time.Second,
initialConnDuration: time.Second,
initialConnectionDuration: initialConnectionDuration,
exchanges: []udpExchange{{
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
response: []byte{0x0, 0x82, 0x0, 0x0, 0x0, 0x14, 0x3, 0x21, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
@@ -90,7 +90,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
protocol: "udp",
internalPort: 123,
initialConnDuration: time.Second,
initialConnectionDuration: initialConnectionDuration,
exchanges: []udpExchange{{
request: []byte{0x0, 0x1, 0x0, 0x0, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
response: []byte{0x0, 0x81, 0x0, 0x0, 0x0, 0x14, 0x3, 0xd5, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
@@ -103,7 +103,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
protocol: "tcp",
internalPort: 123,
initialConnDuration: time.Second,
initialConnectionDuration: initialConnectionDuration,
exchanges: []udpExchange{{
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
response: []byte{0x0, 0x82, 0x0, 0x0, 0x0, 0x14, 0x4, 0x96, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
@@ -122,7 +122,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
client := Client{
serverPort: uint16(remoteAddress.Port),
initialConnectionDuration: testCase.initialConnDuration,
initialConnectionDuration: testCase.initialConnectionDuration,
maxRetries: 1,
}

View File

@@ -32,7 +32,7 @@ func Test_Client_rpc(t *testing.T) {
"request_too_small": {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
request: []byte{0},
initialConnectionDuration: time.Second,
initialConnectionDuration: time.Nanosecond, // doesn't matter
err: ErrRequestSizeTooSmall,
errMessage: `checking request: message size is too small: ` +
`need at least 2 bytes and got 1 byte\(s\)`,
@@ -60,7 +60,7 @@ func Test_Client_rpc(t *testing.T) {
ctx: context.Background(),
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
request: []byte{0, 0},
initialConnectionDuration: time.Second,
initialConnectionDuration: initialConnectionDuration,
exchanges: []udpExchange{{
request: []byte{0, 0},
response: []byte{1},
@@ -74,7 +74,7 @@ func Test_Client_rpc(t *testing.T) {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
responseSize: 5,
initialConnectionDuration: time.Second,
initialConnectionDuration: initialConnectionDuration,
exchanges: []udpExchange{{
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
response: []byte{0, 1, 2, 3}, // size 4
@@ -88,7 +88,7 @@ func Test_Client_rpc(t *testing.T) {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
responseSize: 16,
initialConnectionDuration: time.Second,
initialConnectionDuration: initialConnectionDuration,
exchanges: []udpExchange{{
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
response: []byte{0x1, 0x82, 0x0, 0x0, 0x0, 0x14, 0x4, 0x96, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
@@ -101,7 +101,7 @@ func Test_Client_rpc(t *testing.T) {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
responseSize: 16,
initialConnectionDuration: time.Second,
initialConnectionDuration: initialConnectionDuration,
exchanges: []udpExchange{{
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
response: []byte{0x0, 0x88, 0x0, 0x0, 0x0, 0x14, 0x4, 0x96, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
@@ -114,7 +114,7 @@ func Test_Client_rpc(t *testing.T) {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
responseSize: 16,
initialConnectionDuration: time.Second,
initialConnectionDuration: initialConnectionDuration,
exchanges: []udpExchange{{
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
response: []byte{0x0, 0x82, 0x0, 0x11, 0x0, 0x14, 0x4, 0x96, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
@@ -127,7 +127,7 @@ func Test_Client_rpc(t *testing.T) {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
responseSize: 16,
initialConnectionDuration: time.Second,
initialConnectionDuration: initialConnectionDuration,
exchanges: []udpExchange{{
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
response: []byte{0x0, 0x82, 0x0, 0x0, 0x0, 0x0, 0x4, 0x96, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},