-
Notifications
You must be signed in to change notification settings - Fork 323
/
lt_cred_test.go
125 lines (102 loc) · 3.13 KB
/
lt_cred_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
// +build !js
package turn
import (
"net"
"testing"
"time"
"github.com/pion/logging"
"github.com/stretchr/testify/assert"
)
func TestLtCredMech(t *testing.T) {
username := "1599491771"
sharedSecret := "foobar"
expectedPassword := "Tpz/nKkyvX/vMSLKvL4sbtBt8Vs=" //nolint:gosec
actualPassword, _ := longTermCredentials(username, sharedSecret)
if expectedPassword != actualPassword {
t.Errorf("Expected %q, got %q", expectedPassword, actualPassword)
}
}
func TestNewLongTermAuthHandler(t *testing.T) {
const sharedSecret = "HELLO_WORLD"
serverListener, err := net.ListenPacket("udp4", "0.0.0.0:3478")
assert.NoError(t, err)
server, err := NewServer(ServerConfig{
AuthHandler: NewLongTermAuthHandler(sharedSecret, nil),
PacketConnConfigs: []PacketConnConfig{
{
PacketConn: serverListener,
RelayAddressGenerator: &RelayAddressGeneratorStatic{
RelayAddress: net.ParseIP("127.0.0.1"),
Address: "0.0.0.0",
},
},
},
Realm: "pion.ly",
LoggerFactory: logging.NewDefaultLoggerFactory(),
})
assert.NoError(t, err)
conn, err := net.ListenPacket("udp4", "0.0.0.0:0")
assert.NoError(t, err)
username, password, err := GenerateLongTermCredentials(sharedSecret, time.Minute)
assert.NoError(t, err)
addr := "127.0.0.1:3478"
client, err := NewClient(&ClientConfig{
STUNServerAddr: addr,
TURNServerAddr: addr,
Conn: conn,
Username: username,
Password: password,
LoggerFactory: logging.NewDefaultLoggerFactory(),
})
assert.NoError(t, err)
assert.NoError(t, client.Listen())
relayConn, err := client.Allocate()
assert.NoError(t, err)
client.Close()
assert.NoError(t, relayConn.Close())
assert.NoError(t, conn.Close())
assert.NoError(t, server.Close())
}
func TestLongTermTURNRESTAuthHandler(t *testing.T) {
const sharedSecret = "HELLO_WORLD"
serverListener, err := net.ListenPacket("udp4", "0.0.0.0:3478")
assert.NoError(t, err)
server, err := NewServer(ServerConfig{
AuthHandler: LongTermTURNRESTAuthHandler(sharedSecret, nil),
PacketConnConfigs: []PacketConnConfig{
{
PacketConn: serverListener,
RelayAddressGenerator: &RelayAddressGeneratorStatic{
RelayAddress: net.ParseIP("127.0.0.1"),
Address: "0.0.0.0",
},
},
},
Realm: "pion.ly",
LoggerFactory: logging.NewDefaultLoggerFactory(),
})
assert.NoError(t, err)
conn, err := net.ListenPacket("udp4", "0.0.0.0:0")
assert.NoError(t, err)
username, password, err := GenerateLongTermTURNRESTCredentials(sharedSecret, "testuser", time.Minute)
assert.NoError(t, err)
client, err := NewClient(&ClientConfig{
STUNServerAddr: "0.0.0.0:3478",
TURNServerAddr: "0.0.0.0:3478",
Conn: conn,
Username: username,
Password: password,
LoggerFactory: logging.NewDefaultLoggerFactory(),
})
assert.NoError(t, err)
assert.NoError(t, client.Listen())
relayConn, err := client.Allocate()
assert.NoError(t, err)
client.Close()
assert.NoError(t, relayConn.Close())
assert.NoError(t, conn.Close())
assert.NoError(t, server.Close())
}