{{ message }}
Repository navigation
Expand file tree
/
Copy pathtest-signaling.js
More file actions
467 lines (392 loc) · 13 KB
/
Copy pathtest-signaling.js
File metadata and controls
467 lines (392 loc) · 13 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/**
* Test script for WebSocket signaling protocol
*
* Tests:
* 1. Peer registration
* 2. Offer relay from peer A to peer B
* 3. Answer relay from peer B to peer A
* 4. ICE candidate relay
* 5. Error cases (malformed messages, unregistered peers, duplicate IDs, spoofed from)
*/
import WebSocket from 'ws';
import { randomUUID } from 'node:crypto';
const SERVER_URL = 'ws://localhost:6736';
const TIMEOUT = 5000; // 5 second timeout per test
// Test results
const results = {
passed: 0,
failed: 0,
tests: []
};
// Every test gets fresh peer IDs. Tests used to share a fixed pair, so a test
// could try to register an ID while the previous test's socket was still being
// torn down server-side -- the server correctly answered "already registered"
// and the test timed out waiting for "registered". Unique IDs remove the
// collision entirely rather than relying on cleanup winning the race.
function newPeerId() {
return randomUUID();
}
// ws.close() only *starts* the closing handshake. Waiting for the socket to
// actually reach CLOSED keeps sockets from piling up across tests.
function closeAll(...sockets) {
return Promise.all(sockets.map((ws) => new Promise((resolve) => {
if (!ws || ws.readyState === WebSocket.CLOSED) {
resolve();
return;
}
const done = setTimeout(resolve, TIMEOUT); // never hang teardown
ws.once('close', () => {
clearTimeout(done);
resolve();
});
ws.close();
})));
}
// Utility: Create WebSocket connection and wait for it to be ready
async function createConnection() {
const ws = new WebSocket(SERVER_URL);
// Set up message listener immediately (before connection opens)
const welcomePromise = new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error('Timeout waiting for welcome message'));
}, TIMEOUT);
function messageHandler(data) {
try {
const message = JSON.parse(data.toString());
if (message.type === 'welcome') {
clearTimeout(timeout);
ws.off('message', messageHandler);
resolve(message);
}
} catch (error) {
// Ignore parse errors, wait for next message
}
}
ws.on('message', messageHandler);
});
// Wait for connection to open
await new Promise((resolve, reject) => {
if (ws.readyState === WebSocket.OPEN) {
resolve();
return;
}
const timeout = setTimeout(() => {
reject(new Error('Connection timeout'));
}, TIMEOUT);
ws.once('open', () => {
clearTimeout(timeout);
resolve();
});
ws.once('error', (error) => {
clearTimeout(timeout);
reject(error);
});
});
// Wait for welcome message
await welcomePromise;
return ws;
}
// Utility: Wait for specific message type
function waitForMessage(ws, expectedType, timeoutMs = TIMEOUT) {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error(`Timeout waiting for message type: ${expectedType}`));
}, timeoutMs);
function messageHandler(data) {
try {
const message = JSON.parse(data.toString());
if (message.type === expectedType) {
clearTimeout(timeout);
ws.off('message', messageHandler);
resolve(message);
}
} catch (error) {
// Ignore parse errors, wait for next message
}
}
ws.on('message', messageHandler);
});
}
// Utility: Send message and wait for response
function sendAndWaitFor(ws, message, expectedType) {
const promise = waitForMessage(ws, expectedType);
ws.send(JSON.stringify(message));
return promise;
}
// Test runner
async function runTest(name, testFn) {
try {
console.log(`\n🧪 TEST: ${name}`);
await testFn();
console.log(`✅ PASS: ${name}`);
results.passed++;
results.tests.push({ name, status: 'PASS' });
} catch (error) {
console.log(`❌ FAIL: ${name}`);
console.log(` Error: ${error.message}`);
results.failed++;
results.tests.push({ name, status: 'FAIL', error: error.message });
}
}
// ============================================================================
// TEST CASES
// ============================================================================
// Test 1: Peer registration
async function testPeerRegistration() {
const id = newPeerId();
const ws = await createConnection();
// Register peer
const response = await sendAndWaitFor(ws, {
type: 'register',
peerId: id
}, 'registered');
if (response.peerId !== id) {
throw new Error('Peer ID mismatch in response');
}
await closeAll(ws);
}
// Test 2: Duplicate peer ID rejection
async function testDuplicatePeerIdRejection() {
const id = newPeerId();
const [ws1, ws2] = await Promise.all([
createConnection(),
createConnection()
]);
// Register first peer
await sendAndWaitFor(ws1, {
type: 'register',
peerId: id
}, 'registered');
// Try to register second peer with same ID
const errorResponse = await sendAndWaitFor(ws2, {
type: 'register',
peerId: id
}, 'error');
if (!errorResponse.message.includes('already registered')) {
throw new Error('Expected error message about duplicate peer ID');
}
await closeAll(ws1, ws2);
}
// Test 3: Offer relay from peer A to peer B
async function testOfferRelay() {
const idA = newPeerId();
const idB = newPeerId();
const [peerA, peerB] = await Promise.all([
createConnection(),
createConnection()
]);
// Register both peers
await Promise.all([
sendAndWaitFor(peerA, { type: 'register', peerId: idA }, 'registered'),
sendAndWaitFor(peerB, { type: 'register', peerId: idB }, 'registered')
]);
// Send offer from A to B (peerB should receive it)
const offerMessage = {
type: 'offer',
from: idA,
to: idB,
sdp: 'v=0\r\no=- 123456 2 IN IP4 127.0.0.1\r\n...'
};
// Set up listener on peerB before sending
const receivedOfferPromise = waitForMessage(peerB, 'offer');
peerA.send(JSON.stringify(offerMessage));
const receivedOffer = await receivedOfferPromise;
// Verify peer B received the exact offer
if (receivedOffer.from !== idA) {
throw new Error('Offer "from" field mismatch');
}
if (receivedOffer.to !== idB) {
throw new Error('Offer "to" field mismatch');
}
if (receivedOffer.sdp !== offerMessage.sdp) {
throw new Error('Offer SDP mismatch');
}
await closeAll(peerA, peerB);
}
// Test 4: Answer relay from peer B to peer A
async function testAnswerRelay() {
const idA = newPeerId();
const idB = newPeerId();
const [peerA, peerB] = await Promise.all([
createConnection(),
createConnection()
]);
// Register both peers
await Promise.all([
sendAndWaitFor(peerA, { type: 'register', peerId: idA }, 'registered'),
sendAndWaitFor(peerB, { type: 'register', peerId: idB }, 'registered')
]);
// Send answer from B to A (peerA should receive it)
const answerMessage = {
type: 'answer',
from: idB,
to: idA,
sdp: 'v=0\r\no=- 789012 2 IN IP4 127.0.0.1\r\n...'
};
// Set up listener on peerA before sending
const receivedAnswerPromise = waitForMessage(peerA, 'answer');
peerB.send(JSON.stringify(answerMessage));
const receivedAnswer = await receivedAnswerPromise;
// Verify peer A received the exact answer
if (receivedAnswer.from !== idB) {
throw new Error('Answer "from" field mismatch');
}
if (receivedAnswer.to !== idA) {
throw new Error('Answer "to" field mismatch');
}
if (receivedAnswer.sdp !== answerMessage.sdp) {
throw new Error('Answer SDP mismatch');
}
await closeAll(peerA, peerB);
}
// Test 5: ICE candidate relay
async function testIceCandidateRelay() {
const idA = newPeerId();
const idB = newPeerId();
const [peerA, peerB] = await Promise.all([
createConnection(),
createConnection()
]);
// Register both peers
await Promise.all([
sendAndWaitFor(peerA, { type: 'register', peerId: idA }, 'registered'),
sendAndWaitFor(peerB, { type: 'register', peerId: idB }, 'registered')
]);
// Send ICE candidate from A to B (peerB should receive it)
const candidateMessage = {
type: 'ice-candidate',
from: idA,
to: idB,
candidate: {
candidate: 'candidate:1 1 UDP 2130706431 192.168.1.100 54321 typ host',
sdpMLineIndex: 0,
sdpMid: 'audio'
}
};
// Set up listener on peerB before sending
const receivedCandidatePromise = waitForMessage(peerB, 'ice-candidate');
peerA.send(JSON.stringify(candidateMessage));
const receivedCandidate = await receivedCandidatePromise;
// Verify peer B received the candidate
if (receivedCandidate.from !== idA) {
throw new Error('Candidate "from" field mismatch');
}
if (receivedCandidate.to !== idB) {
throw new Error('Candidate "to" field mismatch');
}
if (JSON.stringify(receivedCandidate.candidate) !== JSON.stringify(candidateMessage.candidate)) {
throw new Error('Candidate data mismatch');
}
await closeAll(peerA, peerB);
}
// Test 6: Unregistered peer cannot send offer
async function testUnregisteredPeerRejection() {
const idA = newPeerId();
const idB = newPeerId();
const ws = await createConnection();
// Try to send offer without registering
const errorResponse = await sendAndWaitFor(ws, {
type: 'offer',
from: idA,
to: idB,
sdp: 'v=0...'
}, 'error');
if (!errorResponse.message.includes('must register')) {
throw new Error('Expected error about peer not being registered');
}
await closeAll(ws);
}
// Test 7: Target peer not found
async function testTargetPeerNotFound() {
const idA = newPeerId();
const peerA = await createConnection();
// Register peer A
await sendAndWaitFor(peerA, { type: 'register', peerId: idA }, 'registered');
// Send offer to non-existent peer
const errorResponse = await sendAndWaitFor(peerA, {
type: 'offer',
from: idA,
to: 'non-existent-peer',
sdp: 'v=0...'
}, 'error');
if (!errorResponse.message.includes('not connected')) {
throw new Error('Expected error about target peer not connected');
}
await closeAll(peerA);
}
// Test 8: Spoofed "from" field rejection
async function testSpoofedFromRejection() {
const idA = newPeerId();
const idB = newPeerId();
const peerA = await createConnection();
// Register peer A
await sendAndWaitFor(peerA, { type: 'register', peerId: idA }, 'registered');
// Try to send offer with spoofed "from" field
const errorResponse = await sendAndWaitFor(peerA, {
type: 'offer',
from: idB, // Spoofed!
to: 'peer-c',
sdp: 'v=0...'
}, 'error');
if (!errorResponse.message.includes('must match registered peer ID')) {
throw new Error('Expected error about "from" field not matching peer ID');
}
await closeAll(peerA);
}
// Test 9: Malformed message (missing required fields)
async function testMalformedMessage() {
const idA = newPeerId();
const idB = newPeerId();
const ws = await createConnection();
// Register peer
await sendAndWaitFor(ws, { type: 'register', peerId: idA }, 'registered');
// Send malformed offer (missing sdp)
const errorResponse = await sendAndWaitFor(ws, {
type: 'offer',
from: idA,
to: idB
// Missing: sdp
}, 'error');
if (!errorResponse.message.includes('sdp')) {
throw new Error('Expected error about missing sdp field');
}
await closeAll(ws);
}
// ============================================================================
// RUN ALL TESTS
// ============================================================================
async function runAllTests() {
console.log('========================================');
console.log('WebSocket Signaling Protocol Tests');
console.log('========================================');
await runTest('1. Peer registration', testPeerRegistration);
await runTest('2. Duplicate peer ID rejection', testDuplicatePeerIdRejection);
await runTest('3. Offer relay from peer A to peer B', testOfferRelay);
await runTest('4. Answer relay from peer B to peer A', testAnswerRelay);
await runTest('5. ICE candidate relay', testIceCandidateRelay);
await runTest('6. Unregistered peer cannot send offer', testUnregisteredPeerRejection);
await runTest('7. Target peer not found error', testTargetPeerNotFound);
await runTest('8. Spoofed "from" field rejection', testSpoofedFromRejection);
await runTest('9. Malformed message rejection', testMalformedMessage);
// Print summary
console.log('\n========================================');
console.log('Test Summary');
console.log('========================================');
console.log(`Total: ${results.passed + results.failed}`);
console.log(`✅ Passed: ${results.passed}`);
console.log(`❌ Failed: ${results.failed}`);
if (results.failed > 0) {
console.log('\nFailed tests:');
results.tests
.filter(t => t.status === 'FAIL')
.forEach(t => console.log(` - ${t.name}: ${t.error}`));
}
console.log('========================================\n');
// Exit with appropriate code
process.exit(results.failed > 0 ? 1 : 0);
}
// Run tests
runAllTests().catch((error) => {
console.error('Fatal error running tests:', error);
process.exit(1);
});
You can’t perform that action at this time.
