From 9c0f187a122b3e0c974785537e38058608c4056c Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Thu, 6 Jul 2023 06:54:01 +0000 Subject: [PATCH] chore(natpmp): more robust tests with longer connection durations --- internal/natpmp/externaladdress_test.go | 4 +- internal/natpmp/helpers_test.go | 4 ++ internal/natpmp/portmapping_test.go | 72 ++++++++++++------------- internal/natpmp/rpc_test.go | 14 ++--- 4 files changed, 49 insertions(+), 45 deletions(-) diff --git a/internal/natpmp/externaladdress_test.go b/internal/natpmp/externaladdress_test.go index eb64965e..526f5b49 100644 --- a/internal/natpmp/externaladdress_test.go +++ b/internal/natpmp/externaladdress_test.go @@ -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}, diff --git a/internal/natpmp/helpers_test.go b/internal/natpmp/helpers_test.go index 0aff9f30..8cd735b5 100644 --- a/internal/natpmp/helpers_test.go +++ b/internal/natpmp/helpers_test.go @@ -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 diff --git a/internal/natpmp/portmapping_test.go b/internal/natpmp/portmapping_test.go index 2905c9cc..c2500619 100644 --- a/internal/natpmp/portmapping_test.go +++ b/internal/natpmp/portmapping_test.go @@ -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 @@ -40,25 +40,25 @@ func Test_Client_AddPortMapping(t *testing.T) { errMessage: "network protocol is unknown: xyz", }, "rpc_error": { - ctx: context.Background(), - gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), - protocol: "udp", - internalPort: 123, - requestedExternalPort: 456, - lifetime: 1200 * time.Second, - initialConnDuration: time.Millisecond, - exchanges: []udpExchange{{close: true}}, - err: ErrConnectionTimeout, - errMessage: "executing remote procedure call: connection timeout: after 1ms", + ctx: context.Background(), + gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), + protocol: "udp", + internalPort: 123, + requestedExternalPort: 456, + lifetime: 1200 * time.Second, + initialConnectionDuration: time.Millisecond, + exchanges: []udpExchange{{close: true}}, + err: ErrConnectionTimeout, + errMessage: "executing remote procedure call: connection timeout: after 1ms", }, "add_udp": { - ctx: context.Background(), - gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), - protocol: "udp", - internalPort: 123, - requestedExternalPort: 456, - lifetime: 1200 * time.Second, - initialConnDuration: time.Second, + ctx: context.Background(), + gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), + protocol: "udp", + internalPort: 123, + requestedExternalPort: 456, + lifetime: 1200 * 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}, @@ -69,13 +69,13 @@ func Test_Client_AddPortMapping(t *testing.T) { assignedLifetime: 0x4b0 * time.Second, }, "add_tcp": { - ctx: context.Background(), - gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), - protocol: "tcp", - internalPort: 123, - requestedExternalPort: 456, - lifetime: 1200 * time.Second, - initialConnDuration: time.Second, + ctx: context.Background(), + gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), + protocol: "tcp", + internalPort: 123, + requestedExternalPort: 456, + lifetime: 1200 * 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}, @@ -86,11 +86,11 @@ func Test_Client_AddPortMapping(t *testing.T) { assignedLifetime: 0x4b0 * time.Second, }, "remove_udp": { - ctx: context.Background(), - gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), - protocol: "udp", - internalPort: 123, - initialConnDuration: time.Second, + ctx: context.Background(), + gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), + protocol: "udp", + internalPort: 123, + 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}, @@ -99,11 +99,11 @@ func Test_Client_AddPortMapping(t *testing.T) { assignedInternalPort: 0x7b, }, "remove_tcp": { - ctx: context.Background(), - gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), - protocol: "tcp", - internalPort: 123, - initialConnDuration: time.Second, + ctx: context.Background(), + gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}), + protocol: "tcp", + internalPort: 123, + 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, } diff --git a/internal/natpmp/rpc_test.go b/internal/natpmp/rpc_test.go index 4ffbad2e..7ff534ee 100644 --- a/internal/natpmp/rpc_test.go +++ b/internal/natpmp/rpc_test.go @@ -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},