summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/tests/mochitests/iceTestUtils.js
blob: 23237f563b693e945ae16180681ab857a456fcd6 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

// This is mostly so test_peerConnection_gatherWithStun300.html and
// test_peerConnection_gatherWithStun300IPv6 can share this code. I would have
// put the ipv6 test code in the same file, but our ipv6 tester support is
// inconsistent enough that we need to be able to track the ipv6 test
// separately.

async function findStatsRelayCandidates(pc, protocol) {
  const stats = await pc.getStats();
  return [...stats.values()].filter(
    v =>
      v.type == "local-candidate" &&
      v.candidateType == "relay" &&
      v.relayProtocol == protocol
  );
}

// Trickles candidates if pcDst is set, and resolves the candidate list
async function trickleIce(pc, pcDst) {
  const candidates = [],
    addCandidatePromises = [];
  while (true) {
    const { candidate } = await new Promise(r =>
      pc.addEventListener("icecandidate", r, { once: true })
    );
    if (!candidate) {
      break;
    }
    candidates.push(candidate);
    if (pcDst) {
      addCandidatePromises.push(pcDst.addIceCandidate(candidate));
    }
  }
  await Promise.all(addCandidatePromises);
  return candidates;
}

async function gather(pc) {
  if (pc.signalingState == "stable") {
    await pc.setLocalDescription(
      await pc.createOffer({ offerToReceiveAudio: true })
    );
  } else if (pc.signalingState == "have-remote-offer") {
    await pc.setLocalDescription();
  }

  return trickleIce(pc);
}

async function gatherWithTimeout(pc, timeout, context) {
  const throwOnTimeout = async () => {
    await wait(timeout);
    throw new Error(
      `Gathering did not complete within ${timeout} ms with ${context}`
    );
  };

  return Promise.race([gather(pc), throwOnTimeout()]);
}

async function iceConnected(pc) {
  return new Promise((resolve, reject) => {
    pc.addEventListener("iceconnectionstatechange", () => {
      if (["connected", "completed"].includes(pc.iceConnectionState)) {
        resolve();
      } else if (pc.iceConnectionState == "failed") {
        reject(new Error(`ICE failed`));
      }
    });
  });
}

async function dtlsConnected(pc) {
  return new Promise((resolve, reject) => {
    pc.addEventListener("connectionstatechange", () => {
      if (["connected", "completed"].includes(pc.connectionState)) {
        resolve();
      } else if (pc.connectionState == "failed") {
        reject(new Error(`Connection failed`));
      }
    });
  });
}

// Set up trickle, but does not wait for it to complete. Can be used by itself
// in cases where we do not expect any new candidates, but want to still set up
// the signal handling in case new candidates _do_ show up.
async function connectNoTrickleWait(offerer, answerer, timeout, context) {
  return connect(offerer, answerer, timeout, context, true);
}

async function connect(
  offerer,
  answerer,
  timeout,
  context,
  noTrickleWait = false,
  waitForDtls = false
) {
  const trickle1 = trickleIce(offerer, answerer);
  const trickle2 = trickleIce(answerer, offerer);
  try {
    const offer = await offerer.createOffer({ offerToReceiveAudio: true });
    await offerer.setLocalDescription(offer);
    await answerer.setRemoteDescription(offer);
    const answer = await answerer.createAnswer();
    await Promise.all([
      offerer.setRemoteDescription(answer),
      answerer.setLocalDescription(answer),
    ]);

    const throwOnTimeout = async () => {
      if (timeout) {
        await wait(timeout);
        throw new Error(
          `ICE did not complete within ${timeout} ms with ${context}`
        );
      }
    };

    const connectionPromises = waitForDtls
      ? [dtlsConnected(offerer), dtlsConnected(answerer)]
      : [iceConnected(offerer), iceConnected(answerer)];

    await Promise.race([
      Promise.all(connectionPromises),
      throwOnTimeout(timeout, context),
    ]);
  } finally {
    if (!noTrickleWait) {
      // TODO(bug 1751509): For now, we need to let gathering finish before we
      // proceed, because there are races in ICE restart wrt gathering state.
      await Promise.all([trickle1, trickle2]);
    }
  }
}

function isV6HostCandidate(candidate) {
  const fields = candidate.candidate.split(" ");
  const type = fields[7];
  const ipAddress = fields[4];
  return type == "host" && ipAddress.includes(":");
}

async function ipv6Supported() {
  const pc = new RTCPeerConnection();
  const candidates = await gatherWithTimeout(pc, 8000);
  info(`baseline candidates: ${JSON.stringify(candidates)}`);
  pc.close();
  return candidates.some(isV6HostCandidate);
}

function makeContextString(iceServers) {
  const currentRedirectAddress = SpecialPowers.getCharPref(
    "media.peerconnection.nat_simulator.redirect_address",
    ""
  );
  const currentRedirectTargets = SpecialPowers.getCharPref(
    "media.peerconnection.nat_simulator.redirect_targets",
    ""
  );
  return `redirect rule: ${currentRedirectAddress}=>${currentRedirectTargets} iceServers: ${JSON.stringify(
    iceServers
  )}`;
}

async function checkSrflx(iceServers) {
  const context = makeContextString(iceServers);
  info(`checkSrflx ${context}`);
  const pc = new RTCPeerConnection({
    iceServers,
    bundlePolicy: "max-bundle", // Avoids extra candidates
  });
  const candidates = await gatherWithTimeout(pc, 8000, context);
  const srflxCandidates = candidates.filter(c => c.candidate.includes("srflx"));
  info(`candidates: ${JSON.stringify(srflxCandidates)}`);
  // TODO(bug 1339203): Once we support rtcpMuxPolicy, set it to "require" to
  // result in a single srflx candidate
  is(
    srflxCandidates.length,
    2,
    `Should have two srflx candidates with ${context}`
  );
  pc.close();
}

async function checkNoSrflx(iceServers) {
  const context = makeContextString(iceServers);
  info(`checkNoSrflx ${context}`);
  const pc = new RTCPeerConnection({
    iceServers,
    bundlePolicy: "max-bundle", // Avoids extra candidates
  });
  const candidates = await gatherWithTimeout(pc, 8000, context);
  const srflxCandidates = candidates.filter(c => c.candidate.includes("srflx"));
  info(`candidates: ${JSON.stringify(srflxCandidates)}`);
  is(
    srflxCandidates.length,
    0,
    `Should have no srflx candidates with ${context}`
  );
  pc.close();
}

async function checkRelayUdp(iceServers) {
  const context = makeContextString(iceServers);
  info(`checkRelayUdp ${context}`);
  const pc = new RTCPeerConnection({
    iceServers,
    bundlePolicy: "max-bundle", // Avoids extra candidates
  });
  const candidates = await gatherWithTimeout(pc, 8000, context);
  const relayCandidates = candidates.filter(c => c.candidate.includes("relay"));
  info(`candidates: ${JSON.stringify(relayCandidates)}`);
  // TODO(bug 1339203): Once we support rtcpMuxPolicy, set it to "require" to
  // result in a single relay candidate
  is(
    relayCandidates.length,
    2,
    `Should have two relay candidates with ${context}`
  );
  // It would be nice if RTCIceCandidate had a field telling us what the
  // "related protocol" is (similar to relatedAddress and relatedPort).
  // Because there is no such thing, we need to go through the stats API,
  // which _does_ have that information.
  is(
    (await findStatsRelayCandidates(pc, "tcp")).length,
    0,
    `No TCP relay candidates should be present with ${context}`
  );
  pc.close();
}

async function checkRelayTcp(iceServers) {
  const context = makeContextString(iceServers);
  info(`checkRelayTcp ${context}`);
  const pc = new RTCPeerConnection({
    iceServers,
    bundlePolicy: "max-bundle", // Avoids extra candidates
  });
  const candidates = await gatherWithTimeout(pc, 8000, context);
  const relayCandidates = candidates.filter(c => c.candidate.includes("relay"));
  info(`candidates: ${JSON.stringify(relayCandidates)}`);
  // TODO(bug 1339203): Once we support rtcpMuxPolicy, set it to "require" to
  // result in a single relay candidate
  is(
    relayCandidates.length,
    2,
    `Should have two relay candidates with ${context}`
  );
  // It would be nice if RTCIceCandidate had a field telling us what the
  // "related protocol" is (similar to relatedAddress and relatedPort).
  // Because there is no such thing, we need to go through the stats API,
  // which _does_ have that information.
  is(
    (await findStatsRelayCandidates(pc, "udp")).length,
    0,
    `No UDP relay candidates should be present with ${context}`
  );
  pc.close();
}

async function checkRelayUdpTcp(iceServers) {
  const context = makeContextString(iceServers);
  info(`checkRelayUdpTcp ${context}`);
  const pc = new RTCPeerConnection({
    iceServers,
    bundlePolicy: "max-bundle", // Avoids extra candidates
  });
  const candidates = await gatherWithTimeout(pc, 8000, context);
  const relayCandidates = candidates.filter(c => c.candidate.includes("relay"));
  info(`candidates: ${JSON.stringify(relayCandidates)}`);
  // TODO(bug 1339203): Once we support rtcpMuxPolicy, set it to "require" to
  // result in a single relay candidate each for UDP and TCP
  is(
    relayCandidates.length,
    4,
    `Should have two relay candidates for each protocol with ${context}`
  );
  // It would be nice if RTCIceCandidate had a field telling us what the
  // "related protocol" is (similar to relatedAddress and relatedPort).
  // Because there is no such thing, we need to go through the stats API,
  // which _does_ have that information.
  is(
    (await findStatsRelayCandidates(pc, "udp")).length,
    2,
    `Two UDP relay candidates should be present with ${context}`
  );
  // TODO(bug 1705563): This is 1 because of bug 1705563
  is(
    (await findStatsRelayCandidates(pc, "tcp")).length,
    1,
    `One TCP relay candidates should be present with ${context}`
  );
  pc.close();
}

async function checkNoRelay(iceServers) {
  const context = makeContextString(iceServers);
  info(`checkNoRelay ${context}`);
  const pc = new RTCPeerConnection({
    iceServers,
    bundlePolicy: "max-bundle", // Avoids extra candidates
  });
  const candidates = await gatherWithTimeout(pc, 8000, context);
  const relayCandidates = candidates.filter(c => c.candidate.includes("relay"));
  info(`candidates: ${JSON.stringify(relayCandidates)}`);
  is(
    relayCandidates.length,
    0,
    `Should have no relay candidates with ${context}`
  );
  pc.close();
}

function gatheringStateReached(object, state) {
  if (object instanceof RTCIceTransport) {
    return new Promise(r =>
      object.addEventListener("gatheringstatechange", function listener() {
        if (object.gatheringState == state) {
          object.removeEventListener("gatheringstatechange", listener);
          r(state);
        }
      })
    );
  } else if (object instanceof RTCPeerConnection) {
    return new Promise(r =>
      object.addEventListener("icegatheringstatechange", function listener() {
        if (object.iceGatheringState == state) {
          object.removeEventListener("icegatheringstatechange", listener);
          r(state);
        }
      })
    );
  } else {
    throw "First parameter is neither an RTCIceTransport nor an RTCPeerConnection";
  }
}

function nextGatheringState(object) {
  if (object instanceof RTCIceTransport) {
    return new Promise(resolve =>
      object.addEventListener(
        "gatheringstatechange",
        () => resolve(object.gatheringState),
        { once: true }
      )
    );
  } else if (object instanceof RTCPeerConnection) {
    return new Promise(resolve =>
      object.addEventListener(
        "icegatheringstatechange",
        () => resolve(object.iceGatheringState),
        { once: true }
      )
    );
  } else {
    throw "First parameter is neither an RTCIceTransport nor an RTCPeerConnection";
  }
}

function emptyCandidate(pc) {
  return new Promise(r =>
    pc.addEventListener("icecandidate", function listener(e) {
      if (e.candidate && e.candidate.candidate == "") {
        pc.removeEventListener("icecandidate", listener);
        r(e);
      }
    })
  );
}

function nullCandidate(pc) {
  return new Promise(r =>
    pc.addEventListener("icecandidate", function listener(e) {
      if (!e.candidate) {
        pc.removeEventListener("icecandidate", listener);
        r(e);
      }
    })
  );
}

function connectionStateReached(object, state) {
  if (object instanceof RTCIceTransport || object instanceof RTCDtlsTransport) {
    return new Promise(resolve =>
      object.addEventListener("statechange", function listener() {
        if (object.state == state) {
          object.removeEventListener("statechange", listener);
          resolve(state);
        }
      })
    );
  } else if (object instanceof RTCPeerConnection) {
    return new Promise(resolve =>
      object.addEventListener("connectionstatechange", function listener() {
        if (object.connectionState == state) {
          object.removeEventListener("connectionstatechange", listener);
          resolve(state);
        }
      })
    );
  } else {
    throw "First parameter is neither an RTCIceTransport, an RTCDtlsTransport, nor an RTCPeerConnection";
  }
}

function nextConnectionState(object) {
  if (object instanceof RTCIceTransport || object instanceof RTCDtlsTransport) {
    return new Promise(resolve =>
      object.addEventListener("statechange", () => resolve(object.state), {
        once: true,
      })
    );
  } else if (object instanceof RTCPeerConnection) {
    return new Promise(resolve =>
      object.addEventListener(
        "connectionstatechange",
        () => resolve(object.connectionState),
        { once: true }
      )
    );
  } else {
    throw "First parameter is neither an RTCIceTransport, an RTCDtlsTransport, nor an RTCPeerConnection";
  }
}

function nextIceConnectionState(pc) {
  if (pc instanceof RTCPeerConnection) {
    return new Promise(resolve =>
      pc.addEventListener(
        "iceconnectionstatechange",
        () => resolve(pc.iceConnectionState),
        { once: true }
      )
    );
  } else {
    throw "First parameter is not an RTCPeerConnection";
  }
}