summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/tests/mochitests/test_peerConnection_gatherWithSetConfiguration.html
blob: 6710e628aad88a4a3dd6aada2153ef47224dc391 (plain)
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
<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="pc.js"></script>
  <script type="application/javascript" src="iceTestUtils.js"></script>
  <script type="application/javascript" src="helpers_from_wpt/sdp.js"></script></head>
<body>
<pre id="test">
<script type="application/javascript">
  createHTML({
    bug: "1253706",
    title: "Test ICE gathering when setConfiguration is used to change the ICE config"
  });

  const tests = [
    async function baselineV4Cases() {
      await checkSrflx([{urls:[`stun:${turnAddressV4}`]}]);
      await checkRelayUdp([{urls:[`turn:${turnAddressV4}`], username, credential}]);
      await checkRelayTcp([{urls:[`turn:${turnAddressV4}?transport=tcp`], username, credential}]);
      await checkRelayUdpTcp([{urls:[`turn:${turnAddressV4}`, `turn:${turnAddressV4}?transport=tcp`], username, credential}]);
      await checkNoSrflx();
      await checkNoRelay();
    },

    async function addStunServerBeforeOffer() {
      const pc = new RTCPeerConnection();
      try {
        pc.setConfiguration({iceServers: [{urls:[`stun:${turnAddressV4}`]}]});
        const candidates = await gatherWithTimeout(pc, 32000, `just a stun server`);
        ok(candidates.some(c => c.candidate.includes("srflx")), "Should get at least one srflx candidate");
        ok(!candidates.some(c => c.candidate.includes("relay")), "Should not get any relay candidates");
      } finally {
        pc.close();
      }
    },

    async function addTurnServerBeforeOffer() {
      const pc = new RTCPeerConnection();
      try {
        pc.setConfiguration({iceServers: [{urls:[`turn:${turnAddressV4}`], username, credential}]});
        const candidates = await gatherWithTimeout(pc, 32000, `a turn (udp) server`);
        ok(candidates.some(c => c.candidate.includes("srflx")), "Should get at least one srflx candidate");
        ok(candidates.some(c => c.candidate.includes("relay")), "Should get at least one relay candidate");
      } finally {
        pc.close();
      }
    },

    async function addTurnTcpServerBeforeOffer() {
      const pc = new RTCPeerConnection();
      try {
        pc.setConfiguration({iceServers: [{urls:[`turn:${turnAddressV4}?transport=tcp`], username, credential}]});
        const candidates = await gatherWithTimeout(pc, 32000, `a turn (tcp) server`);
        ok(!candidates.some(c => c.candidate.includes("srflx")), "Should not get any srflx candidates");
        ok(candidates.some(c => c.candidate.includes("relay")), "Should get at least one relay candidate");
      } finally {
        pc.close();
      }
    },

    async function addStunServerAfterOffer() {
      const pc = new RTCPeerConnection();
      try {
        const candidates1 = await gatherWithTimeout(pc, 32000, `no ICE servers`);
        ok(!candidates1.some(c => c.candidate.includes("srflx")), "Should not get any srflx candidates");
        ok(!candidates1.some(c => c.candidate.includes("relay")), "Should not get any relay candidates");
        await pc.setLocalDescription({type: "rollback"});

        pc.setConfiguration({iceServers: [{urls:[`stun:${turnAddressV4}`]}]});
        const candidates2 = await gatherWithTimeout(pc, 32000, `just a stun server`);
        ok(candidates2.some(c => c.candidate.includes("srflx")), "Should get at least one srflx candidate");
        ok(!candidates2.some(c => c.candidate.includes("relay")), "Should not get any relay candidates");
      } finally {
        pc.close();
      }
    },

    async function addTurnServerAfterOffer() {
      const pc = new RTCPeerConnection();
      try {
        const candidates1 = await gatherWithTimeout(pc, 32000, `no ICE servers`);
        ok(!candidates1.some(c => c.candidate.includes("srflx")), "Should not get any srflx candidates");
        ok(!candidates1.some(c => c.candidate.includes("relay")), "Should not get any relay candidates");
        await pc.setLocalDescription({type: "rollback"});

        pc.setConfiguration({iceServers: [{urls:[`turn:${turnAddressV4}`], username, credential}]});
        const candidates2 = await gatherWithTimeout(pc, 32000, `a turn (udp) server`);
        ok(candidates2.some(c => c.candidate.includes("srflx")), "Should get at least one srflx candidate");
        ok(candidates2.some(c => c.candidate.includes("relay")), "Should get at least one relay candidate");
      } finally {
        pc.close();
      }
    },

    async function addTurnTcpServerAfterOffer() {
      const pc = new RTCPeerConnection();
      try {
        const candidates1 = await gatherWithTimeout(pc, 32000, `no ICE servers`);
        ok(!candidates1.some(c => c.candidate.includes("srflx")), "Should not get any srflx candidates");
        ok(!candidates1.some(c => c.candidate.includes("relay")), "Should not get any relay candidates");
        await pc.setLocalDescription({type: "rollback"});

        pc.setConfiguration({iceServers: [{urls:[`turn:${turnAddressV4}?transport=tcp`], username, credential}]});
        const candidates2 = await gatherWithTimeout(pc, 32000, `a turn (tcp) server`);
        ok(!candidates2.some(c => c.candidate.includes("srflx")), "Should get no srflx candidates");
        ok(candidates2.some(c => c.candidate.includes("relay")), "Should get at least one relay candidate");
      } finally {
        pc.close();
      }
    },

    async function removeStunServerBeforeOffer() {
      const pc = new RTCPeerConnection({iceServers: [{urls:[`stun:${turnAddressV4}`]}]});
      try {
        pc.setConfiguration({});
        const candidates = await gatherWithTimeout(pc, 32000, `no ICE servers`);
        ok(!candidates.some(c => c.candidate.includes("srflx")), "Should not get any srflx candidates");
        ok(!candidates.some(c => c.candidate.includes("relay")), "Should not get any relay candidates");
      } finally {
        pc.close();
      }
    },

    async function removeTurnServerBeforeOffer() {
      const pc = new RTCPeerConnection({iceServers: [{urls:[`turn:${turnAddressV4}`], username, credential}]});
      try {
        pc.setConfiguration({});
        const candidates = await gatherWithTimeout(pc, 32000, `no ICE servers`);
        ok(!candidates.some(c => c.candidate.includes("srflx")), "Should not get any srflx candidates");
        ok(!candidates.some(c => c.candidate.includes("relay")), "Should not get any relay candidates");
      } finally {
        pc.close();
      }
    },

    async function removeTurnTcpServerBeforeOffer() {
      const pc = new RTCPeerConnection({iceServers: [{urls:[`turn:${turnAddressV4}?transport=tcp`], username, credential}]});
      try {
        pc.setConfiguration({});
        const candidates = await gatherWithTimeout(pc, 32000, `no ICE servers`);
        ok(!candidates.some(c => c.candidate.includes("srflx")), "Should not get any srflx candidates");
        ok(!candidates.some(c => c.candidate.includes("relay")), "Should not get any relay candidates");
      } finally {
        pc.close();
      }
    },

    async function removeStunServerAfterOffer() {
      const pc = new RTCPeerConnection({iceServers: [{urls:[`stun:${turnAddressV4}`]}]});
      try {
        const candidates1 = await gatherWithTimeout(pc, 32000, `just a stun server`);
        ok(candidates1.some(c => c.candidate.includes("srflx")), "Should get at least one srflx candidate");
        ok(!candidates1.some(c => c.candidate.includes("relay")), "Should not get any relay candidates");
        await pc.setLocalDescription({type: "rollback"});

        pc.setConfiguration({});
        const candidates2 = await gatherWithTimeout(pc, 32000, `no ICE servers`);
        ok(!candidates2.some(c => c.candidate.includes("srflx")), "Should not get any srflx candidates");
        ok(!candidates2.some(c => c.candidate.includes("relay")), "Should not get any relay candidates");
      } finally {
        pc.close();
      }
    },

    async function removeTurnServerAfterOffer() {
      const pc = new RTCPeerConnection({iceServers: [{urls:[`turn:${turnAddressV4}`], username, credential}]});
      try {
        const candidates1 = await gatherWithTimeout(pc, 32000, `a turn (udp) server`);
        ok(candidates1.some(c => c.candidate.includes("srflx")), "Should get at least one srflx candidate");
        ok(candidates1.some(c => c.candidate.includes("relay")), "Should get at least one relay candidate");
        await pc.setLocalDescription({type: "rollback"});

        pc.setConfiguration({});
        const candidates2 = await gatherWithTimeout(pc, 32000, `no ICE servers`);
        ok(!candidates2.some(c => c.candidate.includes("srflx")), "Should not get any srflx candidates");
        ok(!candidates2.some(c => c.candidate.includes("relay")), "Should not get any relay candidates");
      } finally {
        pc.close();
      }
    },

    async function removeTurnTcpServerAfterOffer() {
      const pc = new RTCPeerConnection({iceServers: [{urls:[`turn:${turnAddressV4}?transport=tcp`], username, credential}]});
      try {
        const candidates1 = await gatherWithTimeout(pc, 32000, `a turn (tcp) server`);
        ok(!candidates1.some(c => c.candidate.includes("srflx")), "Should get no srflx candidates");
        ok(candidates1.some(c => c.candidate.includes("relay")), "Should get at least one relay candidate");
        await pc.setLocalDescription({type: "rollback"});

        pc.setConfiguration({});
        const candidates2 = await gatherWithTimeout(pc, 32000, `no ICE servers`);
        ok(!candidates2.some(c => c.candidate.includes("srflx")), "Should not get any srflx candidates");
        ok(!candidates2.some(c => c.candidate.includes("relay")), "Should not get any relay candidates");
      } finally {
        pc.close();
      }
    },

    async function addStunServerAfterNegotiation() {
      const offerer = new RTCPeerConnection();
      const answerer = new RTCPeerConnection({iceServers: [{urls:[`stun:${turnAddressV4}`]}]});
      try {
        const candidatePromise = trickleIce(offerer);
        await connect(offerer, answerer, 32000, `no ICE servers`);
        const candidates = await candidatePromise;
        const ufrags = Array.from(new Set(candidates.map(c => c.usernameFragment)));
        is(ufrags.length, 1, "Should have one ufrag in candidate set");

        offerer.setConfiguration({iceServers: [{urls:[`stun:${turnAddressV4}`]}]});
        const candidates2 = await gatherWithTimeout(offerer, 32000, `just a stun server`);
        ok(candidates2.some(c => c.candidate.includes("srflx")), "Should get at least one srflx candidate");
        ok(!candidates2.some(c => c.candidate.includes("relay")), "Should not get any relay candidates");
        const ufrags2 = Array.from(new Set(candidates2.map(c => c.usernameFragment)));
        is(ufrags2.length, 1, "Should have one ufrag in candidate set");
        isnot(ufrags[0], ufrags2[0], "ufrag should change, because setConfiguration should have triggered an ICE restart");
      } finally {
        offerer.close();
        answerer.close();
      }
    },

    async function addTurnServerAfterNegotiation() {
      const offerer = new RTCPeerConnection();
      const answerer = new RTCPeerConnection({iceServers: [{urls:[`stun:${turnAddressV4}`]}]});
      try {
        const candidatePromise = trickleIce(offerer);
        await connect(offerer, answerer, 32000, `no ICE servers`);
        const candidates = await candidatePromise;
        const ufrags = Array.from(new Set(candidates.map(c => c.usernameFragment)));
        is(ufrags.length, 1, "Should have one ufrag in candidate set");

        offerer.setConfiguration({iceServers: [{urls:[`turn:${turnAddressV4}`], username, credential}]});
        const candidates2 = await gatherWithTimeout(offerer, 32000, `a turn (udp) server`);
        ok(candidates2.some(c => c.candidate.includes("srflx")), "Should get at least one srflx candidate");
        ok(candidates2.some(c => c.candidate.includes("relay")), "Should get at least one relay candidate");
        const ufrags2 = Array.from(new Set(candidates2.map(c => c.usernameFragment)));
        is(ufrags2.length, 1, "Should have one ufrag in candidate set");
        isnot(ufrags[0], ufrags2[0], "ufrag should change, because setConfiguration should have triggered an ICE restart");
      } finally {
        offerer.close();
        answerer.close();
      }
    },

    async function addTurnTcpServerAfterNegotiation() {
      const offerer = new RTCPeerConnection();
      const answerer = new RTCPeerConnection({iceServers: [{urls:[`stun:${turnAddressV4}`]}]});
      try {
        const candidatePromise = trickleIce(offerer);
        await connect(offerer, answerer, 32000, `no ICE servers`);
        const candidates = await candidatePromise;
        const ufrags = Array.from(new Set(candidates.map(c => c.usernameFragment)));
        is(ufrags.length, 1, "Should have one ufrag in candidate set");

        offerer.setConfiguration({iceServers: [{urls:[`turn:${turnAddressV4}?transport=tcp`], username, credential}]});
        const candidates2 = await gatherWithTimeout(offerer, 32000, `a turn (tcp) server`);
        ok(!candidates2.some(c => c.candidate.includes("srflx")), "Should not get any srflx candidates");
        ok(candidates2.some(c => c.candidate.includes("relay")), "Should get at least one relay candidate");
        const ufrags2 = Array.from(new Set(candidates2.map(c => c.usernameFragment)));
        is(ufrags2.length, 1, "Should have one ufrag in candidate set");
        isnot(ufrags[0], ufrags2[0], "ufrag should change, because setConfiguration should have triggered an ICE restart");
      } finally {
        offerer.close();
        answerer.close();
      }
    },

    async function addStunServerBeforeCreateAnswer() {
      const offerer = new RTCPeerConnection();
      const answerer = new RTCPeerConnection();

      try {
        await answerer.setRemoteDescription(await offerer.createOffer({offerToReceiveAudio: true}));

        answerer.setConfiguration({iceServers: [{urls:[`stun:${turnAddressV4}`]}]});
        const candidates = await gatherWithTimeout(answerer, 32000, `just a stun server`);
        ok(candidates.some(c => c.candidate.includes("srflx")), "Should get at least one srflx candidate");
        ok(!candidates.some(c => c.candidate.includes("relay")), "Should not get any relay candidates");
      } finally {
        offerer.close();
        answerer.close();
      }
    },

    async function addTurnServerBeforeCreateAnswer() {
      const offerer = new RTCPeerConnection();
      const answerer = new RTCPeerConnection();

      try {
        await answerer.setRemoteDescription(await offerer.createOffer({offerToReceiveAudio: true}));

        answerer.setConfiguration({iceServers: [{urls:[`turn:${turnAddressV4}`], username, credential}]});
        const candidates = await gatherWithTimeout(answerer, 32000, `a turn (udp) server`);
        ok(candidates.some(c => c.candidate.includes("srflx")), "Should get at least one srflx candidate");
        ok(candidates.some(c => c.candidate.includes("relay")), "Should get at least one relay candidate");
      } finally {
        offerer.close();
        answerer.close();
      }
    },

    async function addTurnTcpServerBeforeCreateAnswer() {
      const offerer = new RTCPeerConnection();
      const answerer = new RTCPeerConnection();

      try {
        await answerer.setRemoteDescription(await offerer.createOffer({offerToReceiveAudio: true}));

        answerer.setConfiguration({iceServers: [{urls:[`turn:${turnAddressV4}?transport=tcp`], username, credential}]});
        const candidates = await gatherWithTimeout(answerer, 32000, `a turn (tcp) server`);
        ok(!candidates.some(c => c.candidate.includes("srflx")), "Should not get any srflx candidates");
        ok(candidates.some(c => c.candidate.includes("relay")), "Should get at least one relay candidate");
      } finally {
        offerer.close();
        answerer.close();
      }
    },

    async function relayPolicyPreventsSrflx() {
      const pc = new RTCPeerConnection();
      try {
        pc.setConfiguration({iceServers: [{urls:[`turn:${turnAddressV4}`], username, credential}], iceTransportPolicy: "relay"});
        const candidates = await gatherWithTimeout(pc, 32000, `a turn (udp) server`);
        ok(!candidates.some(c => c.candidate.includes("srflx")), "Should not get a srflx candidate");
        ok(candidates.some(c => c.candidate.includes("relay")), "Should get at least one relay candidate");
      } finally {
        pc.close();
      }
    },

    async function addOffererStunServerAllowsIceToConnect() {
      const offerer = new RTCPeerConnection();
      const answerer = new RTCPeerConnection();

      try {
        try {
          // Both ends are behind a simulated endpoint-independent NAT, which
          // requires at least one side to have a srflx candidate to work.
          await connect(offerer, answerer, 2000, `no ICE servers`);
          ok(false, "ICE should either have failed, or timed out!");
        } catch (e) {
          if (!(e instanceof Error)) throw e;
          ok(true, "ICE should either have failed, or timed out!");
        }

        offerer.setConfiguration({iceServers: [{urls:[`stun:${turnAddressV4}`]}]});
        await connect(offerer, answerer, 32000, `just a STUN server`);
      } finally {
        offerer.close();
        answerer.close();
      }
    },

    async function addAnswererStunServerDoesNotAllowIceToConnect() {
      const offerer = new RTCPeerConnection();
      const answerer = new RTCPeerConnection();

      try {
        try {
          // Both ends are behind a simulated endpoint-independent NAT, which
          // requires at least one side to have a srflx candidate to work.
          await connect(offerer, answerer, 2000, `no ICE servers`);
          ok(false, "ICE should either have failed, or timed out!");
        } catch (e) {
          if (!(e instanceof Error)) throw e;
          ok(true, "ICE should either have failed, or timed out!");
        }

        // This _won't_ help, because the answerer does not get to decide to
        // trigger an ICE restart.
        answerer.setConfiguration({iceServers: [{urls:[`stun:${turnAddressV4}`]}]});
        try {
          await connectNoTrickleWait(offerer, answerer, 2000, `no ICE servers`);
          ok(false, "ICE should either have failed, or timed out!");
        } catch (e) {
          if (!(e instanceof Error)) throw e;
          ok(true, "ICE should either have failed, or timed out!");
        }
      } finally {
        offerer.close();
        answerer.close();
      }
    },

    async function addOffererTurnServerAllowsIceToConnect() {
      await pushPrefs(
          ['media.peerconnection.nat_simulator.filtering_type', 'PORT_DEPENDENT'],
          ['media.peerconnection.nat_simulator.mapping_type', 'PORT_DEPENDENT']);

      const offerer = new RTCPeerConnection({iceServers: [{urls:[`stun:${turnAddressV4}`]}]});
      const answerer = new RTCPeerConnection({iceServers: [{urls:[`stun:${turnAddressV4}`]}]});

      try {
        try {
          // Both ends are behind a simulated port-dependent NAT, which
          // requires at least one side to have a relay candidate to work.
          await connect(offerer, answerer, 2000, `just a STUN server`);
          ok(false, "ICE should either have failed, or timed out!");
        } catch (e) {
          if (!(e instanceof Error)) throw e;
          ok(true, "ICE should either have failed, or timed out!");
        }

        offerer.setConfiguration({iceServers: [{urls:[`turn:${turnAddressV4}`], username, credential}]});
        await connect(offerer, answerer, 32000, `a TURN (udp) server`);
      } finally {
        offerer.close();
        answerer.close();
        await SpecialPowers.popPrefEnv();
      }
    },

  ];

  runNetworkTest(async () => {
    const turnServer = iceServersArray.find(server => "username" in server);
    username = turnServer.username;
    credential = turnServer.credential;
    // Just use the first url. It might make sense to look for TURNS first,
    // since that will always use a hostname, but on CI we don't have TURNS
    // support anyway (see bug 1323439).
    const turnHostname = getTurnHostname(turnServer.urls[0]);
    turnAddressV4 = await dnsLookupV4(turnHostname);

    await pushPrefs(
        ['media.peerconnection.ice.obfuscate_host_addresses', false],
        ['media.peerconnection.nat_simulator.filtering_type', 'ENDPOINT_INDEPENDENT'],
        ['media.peerconnection.nat_simulator.mapping_type', 'ENDPOINT_INDEPENDENT'],
        ['media.peerconnection.ice.loopback', true],
        ['media.getusermedia.insecure.enabled', true]);

    for (const test of tests) {
      info(`Running test: ${test.name}`);
      try {
        await test();
      } catch (e) {
        ok(false, `Caught ${e.name}: ${e.message} ${e.stack}`);
      }
      info(`Done running test: ${test.name}`);
      // Make sure we don't build up a pile of GC work, and also get PCImpl to
      // print their timecards.
      await new Promise(r => SpecialPowers.exactGC(r));
    }

    await SpecialPowers.popPrefEnv();
  }, { useIceServer: true });
</script>
</pre>
</body>
</html>