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": { "failure": {
ctx: canceledCtx, ctx: canceledCtx,
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
initialConnDuration: time.Millisecond, initialConnDuration: initialConnectionDuration,
err: context.Canceled, err: context.Canceled,
errMessage: "executing remote procedure call: reading from udp connection: context canceled", errMessage: "executing remote procedure call: reading from udp connection: context canceled",
}, },
"success": { "success": {
ctx: context.Background(), ctx: context.Background(),
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
initialConnDuration: time.Millisecond, initialConnDuration: initialConnectionDuration,
exchanges: []udpExchange{{ exchanges: []udpExchange{{
request: []byte{0, 0}, request: []byte{0, 0},
response: []byte{0x0, 0x80, 0x0, 0x0, 0x0, 0x13, 0xf2, 0x4f, 0x49, 0x8c, 0x36, 0x9a}, response: []byte{0x0, 0x80, 0x0, 0x0, 0x0, 0x13, 0xf2, 0x4f, 0x49, 0x8c, 0x36, 0x9a},

View File

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

View File

@@ -19,7 +19,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
internalPort uint16 internalPort uint16
requestedExternalPort uint16 requestedExternalPort uint16
lifetime time.Duration lifetime time.Duration
initialConnDuration time.Duration initialConnectionDuration time.Duration
exchanges []udpExchange exchanges []udpExchange
durationSinceStartOfEpoch time.Duration durationSinceStartOfEpoch time.Duration
assignedInternalPort uint16 assignedInternalPort uint16
@@ -40,25 +40,25 @@ func Test_Client_AddPortMapping(t *testing.T) {
errMessage: "network protocol is unknown: xyz", errMessage: "network protocol is unknown: xyz",
}, },
"rpc_error": { "rpc_error": {
ctx: context.Background(), ctx: context.Background(),
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
protocol: "udp", protocol: "udp",
internalPort: 123, internalPort: 123,
requestedExternalPort: 456, requestedExternalPort: 456,
lifetime: 1200 * time.Second, lifetime: 1200 * time.Second,
initialConnDuration: time.Millisecond, initialConnectionDuration: time.Millisecond,
exchanges: []udpExchange{{close: true}}, exchanges: []udpExchange{{close: true}},
err: ErrConnectionTimeout, err: ErrConnectionTimeout,
errMessage: "executing remote procedure call: connection timeout: after 1ms", errMessage: "executing remote procedure call: connection timeout: after 1ms",
}, },
"add_udp": { "add_udp": {
ctx: context.Background(), ctx: context.Background(),
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
protocol: "udp", protocol: "udp",
internalPort: 123, internalPort: 123,
requestedExternalPort: 456, requestedExternalPort: 456,
lifetime: 1200 * time.Second, lifetime: 1200 * time.Second,
initialConnDuration: time.Second, initialConnectionDuration: initialConnectionDuration,
exchanges: []udpExchange{{ exchanges: []udpExchange{{
request: []byte{0x0, 0x1, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0}, 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}, response: []byte{0x0, 0x81, 0x0, 0x0, 0x0, 0x13, 0xfe, 0xff, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
@@ -69,13 +69,13 @@ func Test_Client_AddPortMapping(t *testing.T) {
assignedLifetime: 0x4b0 * time.Second, assignedLifetime: 0x4b0 * time.Second,
}, },
"add_tcp": { "add_tcp": {
ctx: context.Background(), ctx: context.Background(),
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
protocol: "tcp", protocol: "tcp",
internalPort: 123, internalPort: 123,
requestedExternalPort: 456, requestedExternalPort: 456,
lifetime: 1200 * time.Second, lifetime: 1200 * time.Second,
initialConnDuration: time.Second, initialConnectionDuration: initialConnectionDuration,
exchanges: []udpExchange{{ exchanges: []udpExchange{{
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0}, 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}, response: []byte{0x0, 0x82, 0x0, 0x0, 0x0, 0x14, 0x3, 0x21, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
@@ -86,11 +86,11 @@ func Test_Client_AddPortMapping(t *testing.T) {
assignedLifetime: 0x4b0 * time.Second, assignedLifetime: 0x4b0 * time.Second,
}, },
"remove_udp": { "remove_udp": {
ctx: context.Background(), ctx: context.Background(),
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
protocol: "udp", protocol: "udp",
internalPort: 123, internalPort: 123,
initialConnDuration: time.Second, initialConnectionDuration: initialConnectionDuration,
exchanges: []udpExchange{{ exchanges: []udpExchange{{
request: []byte{0x0, 0x1, 0x0, 0x0, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, 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}, response: []byte{0x0, 0x81, 0x0, 0x0, 0x0, 0x14, 0x3, 0xd5, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
@@ -99,11 +99,11 @@ func Test_Client_AddPortMapping(t *testing.T) {
assignedInternalPort: 0x7b, assignedInternalPort: 0x7b,
}, },
"remove_tcp": { "remove_tcp": {
ctx: context.Background(), ctx: context.Background(),
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
protocol: "tcp", protocol: "tcp",
internalPort: 123, internalPort: 123,
initialConnDuration: time.Second, initialConnectionDuration: initialConnectionDuration,
exchanges: []udpExchange{{ exchanges: []udpExchange{{
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, 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}, 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{ client := Client{
serverPort: uint16(remoteAddress.Port), serverPort: uint16(remoteAddress.Port),
initialConnectionDuration: testCase.initialConnDuration, initialConnectionDuration: testCase.initialConnectionDuration,
maxRetries: 1, maxRetries: 1,
} }

View File

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