-
Notifications
You must be signed in to change notification settings - Fork 160
/
transport_vnet_test.go
91 lines (75 loc) · 2.31 KB
/
transport_vnet_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
// +build !js
package ice
import (
"fmt"
"net"
"testing"
"time"
"github.com/pion/stun/v3"
"github.com/pion/transport/v3/test"
"github.com/pion/transport/v3/vnet"
"github.com/stretchr/testify/require"
)
func TestRemoteLocalAddr(t *testing.T) {
// Check for leaking routines
defer test.CheckRoutines(t)()
// Limit runtime in case of deadlocks
defer test.TimeOut(time.Second * 20).Stop()
// Agent0 is behind 1:1 NAT
natType0 := &vnet.NATType{Mode: vnet.NATModeNAT1To1}
// Agent1 is behind 1:1 NAT
natType1 := &vnet.NATType{Mode: vnet.NATModeNAT1To1}
v, errVnet := buildVNet(natType0, natType1)
require.NoError(t, errVnet, "should succeed")
defer v.close()
stunServerURL := &stun.URI{
Scheme: stun.SchemeTypeSTUN,
Host: vnetSTUNServerIP,
Port: vnetSTUNServerPort,
Proto: stun.ProtoTypeUDP,
}
t.Run("Disconnected Returns nil", func(t *testing.T) {
disconnectedAgent, err := NewAgent(&AgentConfig{})
require.NoError(t, err)
disconnectedConn := Conn{agent: disconnectedAgent}
require.Nil(t, disconnectedConn.RemoteAddr())
require.Nil(t, disconnectedConn.LocalAddr())
require.NoError(t, disconnectedConn.Close())
})
t.Run("Remote/Local Pair Match between Agents", func(t *testing.T) {
ca, cb := pipeWithVNet(v,
&agentTestConfig{
urls: []*stun.URI{stunServerURL},
},
&agentTestConfig{
urls: []*stun.URI{stunServerURL},
},
)
defer closePipe(t, ca, cb)
aRAddr := ca.RemoteAddr()
aLAddr := ca.LocalAddr()
bRAddr := cb.RemoteAddr()
bLAddr := cb.LocalAddr()
// Assert that nothing is nil
require.NotNil(t, aRAddr)
require.NotNil(t, aLAddr)
require.NotNil(t, bRAddr)
require.NotNil(t, bLAddr)
// Assert addresses
require.Equal(t, aLAddr.String(),
fmt.Sprintf("%s:%d", vnetLocalIPA, bRAddr.(*net.UDPAddr).Port), //nolint:forcetypeassert
)
require.Equal(t, bLAddr.String(),
fmt.Sprintf("%s:%d", vnetLocalIPB, aRAddr.(*net.UDPAddr).Port), //nolint:forcetypeassert
)
require.Equal(t, aRAddr.String(),
fmt.Sprintf("%s:%d", vnetGlobalIPB, bLAddr.(*net.UDPAddr).Port), //nolint:forcetypeassert
)
require.Equal(t, bRAddr.String(),
fmt.Sprintf("%s:%d", vnetGlobalIPA, aLAddr.(*net.UDPAddr).Port), //nolint:forcetypeassert
)
})
}