chore(natpmp): more robust tests with longer connection durations
This commit is contained in:
@@ -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},
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -46,7 +46,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
|
|||||||
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",
|
||||||
@@ -58,7 +58,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
|
|||||||
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},
|
||||||
@@ -75,7 +75,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
|
|||||||
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},
|
||||||
@@ -90,7 +90,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
|
|||||||
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},
|
||||||
@@ -103,7 +103,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
|
|||||||
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,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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},
|
||||||
|
|||||||
Reference in New Issue
Block a user