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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 | package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
"github.com/gorilla/websocket"
)
const testRoom = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" // 43 chars b64url
func peerID(i int) string { return fmt.Sprintf("%032x", i) }
func testServer(t *testing.T) (*httptest.Server, *Hub) {
t.Helper()
cfg := loadConfig()
cfg.AuthSecret = "testsecret"
hub := newHub(cfg)
srv := httptest.NewServer(newMux(cfg, hub))
t.Cleanup(srv.Close)
return srv, hub
}
func dial(t *testing.T, srv *httptest.Server) *websocket.Conn {
t.Helper()
url := "ws" + strings.TrimPrefix(srv.URL, "http") + "/ws"
ws, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ws.Close() })
return ws
}
func send(t *testing.T, ws *websocket.Conn, v any) {
t.Helper()
if err := ws.WriteJSON(v); err != nil {
t.Fatal(err)
}
}
func recv(t *testing.T, ws *websocket.Conn) map[string]any {
t.Helper()
ws.SetReadDeadline(time.Now().Add(2 * time.Second))
var m map[string]any
if err := ws.ReadJSON(&m); err != nil {
t.Fatal(err)
}
return m
}
func join(t *testing.T, ws *websocket.Conn, peer string) map[string]any {
t.Helper()
send(t, ws, map[string]any{"t": "join", "room": testRoom, "peer": peer})
m := recv(t, ws)
if m["t"] != "peers" {
t.Fatalf("expected peers reply, got %v", m)
}
return m
}
func TestJoinAndSignalForwarding(t *testing.T) {
srv, _ := testServer(t)
a, b := dial(t, srv), dial(t, srv)
m := join(t, a, peerID(1))
if n := len(m["peers"].([]any)); n != 0 {
t.Fatalf("first joiner should see empty room, got %d", n)
}
m = join(t, b, peerID(2))
if got := m["peers"].([]any)[0]; got != peerID(1) {
t.Fatalf("second joiner should see peer 1, got %v", got)
}
if m = recv(t, a); m["t"] != "peer-joined" || m["peer"] != peerID(2) {
t.Fatalf("a should see peer-joined 2, got %v", m)
}
// b signals a; server must stamp from=b and pass payload verbatim.
payload := map[string]any{"v": 1, "iv": "abc", "ct": "def"}
send(t, b, map[string]any{"t": "signal", "to": peerID(1), "payload": payload})
m = recv(t, a)
if m["t"] != "signal" || m["from"] != peerID(2) {
t.Fatalf("bad forward: %v", m)
}
if p := m["payload"].(map[string]any); p["ct"] != "def" {
t.Fatalf("payload mangled: %v", p)
}
// spoofed from must be ignored (server overwrites).
send(t, b, map[string]any{"t": "signal", "to": peerID(1), "from": peerID(9), "payload": payload})
if m = recv(t, a); m["from"] != peerID(2) {
t.Fatalf("from spoofable! got %v", m)
}
// leave → a gets peer-left.
send(t, b, map[string]any{"t": "leave"})
if m = recv(t, a); m["t"] != "peer-left" || m["peer"] != peerID(2) {
t.Fatalf("expected peer-left 2, got %v", m)
}
}
func TestRoomFullAndValidation(t *testing.T) {
srv, hub := testServer(t)
hub.cfg.MaxRoomPeers = 2
a, b, c := dial(t, srv), dial(t, srv), dial(t, srv)
join(t, a, peerID(1))
join(t, b, peerID(2))
recv(t, a) // peer-joined 2
send(t, c, map[string]any{"t": "join", "room": testRoom, "peer": peerID(3)})
if m := recv(t, c); m["code"] != "room-full" {
t.Fatalf("expected room-full, got %v", m)
}
d := dial(t, srv)
send(t, d, map[string]any{"t": "join", "room": "short", "peer": peerID(4)})
if m := recv(t, d); m["code"] != "bad-message" {
t.Fatalf("expected bad-message for invalid room, got %v", m)
}
e := dial(t, srv)
send(t, e, map[string]any{"t": "signal", "to": peerID(1), "payload": map[string]any{"x": 1}})
if m := recv(t, e); m["code"] != "not-joined" {
t.Fatalf("expected not-joined, got %v", m)
}
}
func TestDuplicatePeerReplaces(t *testing.T) {
srv, _ := testServer(t)
a := dial(t, srv)
join(t, a, peerID(1))
a2 := dial(t, srv)
m := join(t, a2, peerID(1)) // same peerId: replaces the zombie
if n := len(m["peers"].([]any)); n != 0 {
t.Fatalf("replacement join should see empty room, got %d", n)
}
// old socket should be closed by the server
a.SetReadDeadline(time.Now().Add(2 * time.Second))
for {
if _, _, err := a.ReadMessage(); err != nil {
break // closed as expected
}
}
}
func TestTurnCredentials(t *testing.T) {
srv, hub := testServer(t)
resp, err := http.Get(srv.URL + "/turn-credentials")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
var body struct {
Username string `json:"username"`
Credential string `json:"credential"`
TTL int `json:"ttl"`
URLs []string `json:"urls"`
}
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
t.Fatal(err)
}
exp, err := strconv.ParseInt(body.Username, 10, 64)
if err != nil || exp < time.Now().Unix()+3500 {
t.Fatalf("bad expiry username %q", body.Username)
}
mac := hmac.New(sha1.New, []byte(hub.cfg.AuthSecret))
mac.Write([]byte(body.Username))
if body.Credential != base64.StdEncoding.EncodeToString(mac.Sum(nil)) {
t.Fatal("credential is not HMAC-SHA1(secret, username)")
}
if len(body.URLs) == 0 {
t.Fatal("no ICE urls")
}
}
|