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
|
/* 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/. */
const ORIGIN = "https://example.com";
async function tryPeerConnection(browser, expectedError = null) {
let errtype = await SpecialPowers.spawn(browser, [], async function () {
let pc = new content.RTCPeerConnection();
try {
await pc.createOffer({ offerToReceiveAudio: true });
return null;
} catch (err) {
return err.name;
}
});
let detail = expectedError
? `createOffer() threw a ${expectedError}`
: "createOffer() succeeded";
is(errtype, expectedError, detail);
}
// Helper for tests that use the peer-request-allowed and -blocked events.
// A test that expects some of those events does the following:
// - call Events.on() before the test to setup event handlers
// - call Events.expect(name) after a specific event is expected to have
// occured. This will fail if the event didn't occur, and will return
// the details passed to the handler for furhter checking.
// - call Events.off() at the end of the test to clean up. At this point, if
// any events were triggered that the test did not expect, the test fails.
const Events = {
events: ["peer-request-allowed", "peer-request-blocked"],
details: new Map(),
handlers: new Map(),
on() {
for (let event of this.events) {
let handler = data => {
if (this.details.has(event)) {
ok(false, `Got multiple ${event} events`);
}
this.details.set(event, data);
};
webrtcUI.on(event, handler);
this.handlers.set(event, handler);
}
},
expect(event) {
let result = this.details.get(event);
isnot(result, undefined, `${event} event was triggered`);
this.details.delete(event);
// All events should have a good origin
is(result.origin, ORIGIN, `${event} event has correct origin`);
return result;
},
off() {
for (let event of this.events) {
webrtcUI.off(event, this.handlers.get(event));
this.handlers.delete(event);
}
for (let [event] of this.details) {
ok(false, `Got unexpected event ${event}`);
}
},
};
var gTests = [
{
desc: "Basic peer-request-allowed event",
run: async function testPeerRequestEvent(browser) {
Events.on();
await tryPeerConnection(browser);
let details = Events.expect("peer-request-allowed");
isnot(
details.callID,
undefined,
"peer-request-allowed event includes callID"
);
isnot(
details.windowID,
undefined,
"peer-request-allowed event includes windowID"
);
Events.off();
},
},
{
desc: "Immediate peer connection blocker can allow",
run: async function testBlocker(browser) {
Events.on();
let blockerCalled = false;
let blocker = params => {
is(
params.origin,
ORIGIN,
"Peer connection blocker origin parameter is correct"
);
blockerCalled = true;
return "allow";
};
webrtcUI.addPeerConnectionBlocker(blocker);
await tryPeerConnection(browser);
is(blockerCalled, true, "Blocker was called");
Events.expect("peer-request-allowed");
webrtcUI.removePeerConnectionBlocker(blocker);
Events.off();
},
},
{
desc: "Deferred peer connection blocker can allow",
run: async function testDeferredBlocker(browser) {
Events.on();
let blocker = params => Promise.resolve("allow");
webrtcUI.addPeerConnectionBlocker(blocker);
await tryPeerConnection(browser);
Events.expect("peer-request-allowed");
webrtcUI.removePeerConnectionBlocker(blocker);
Events.off();
},
},
{
desc: "Immediate peer connection blocker can deny",
run: async function testBlockerDeny(browser) {
Events.on();
let blocker = params => "deny";
webrtcUI.addPeerConnectionBlocker(blocker);
await tryPeerConnection(browser, "NotAllowedError");
Events.expect("peer-request-blocked");
webrtcUI.removePeerConnectionBlocker(blocker);
Events.off();
},
},
{
desc: "Multiple blockers work (both allow)",
run: async function testMultipleAllowBlockers(browser) {
Events.on();
let blocker1Called = false,
blocker1 = params => {
blocker1Called = true;
return "allow";
};
webrtcUI.addPeerConnectionBlocker(blocker1);
let blocker2Called = false,
blocker2 = params => {
blocker2Called = true;
return "allow";
};
webrtcUI.addPeerConnectionBlocker(blocker2);
await tryPeerConnection(browser);
Events.expect("peer-request-allowed");
ok(blocker1Called, "First blocker was called");
ok(blocker2Called, "Second blocker was called");
webrtcUI.removePeerConnectionBlocker(blocker1);
webrtcUI.removePeerConnectionBlocker(blocker2);
Events.off();
},
},
{
desc: "Multiple blockers work (allow then deny)",
run: async function testAllowDenyBlockers(browser) {
Events.on();
let blocker1Called = false,
blocker1 = params => {
blocker1Called = true;
return "allow";
};
webrtcUI.addPeerConnectionBlocker(blocker1);
let blocker2Called = false,
blocker2 = params => {
blocker2Called = true;
return "deny";
};
webrtcUI.addPeerConnectionBlocker(blocker2);
await tryPeerConnection(browser, "NotAllowedError");
Events.expect("peer-request-blocked");
ok(blocker1Called, "First blocker was called");
ok(blocker2Called, "Second blocker was called");
webrtcUI.removePeerConnectionBlocker(blocker1);
webrtcUI.removePeerConnectionBlocker(blocker2);
Events.off();
},
},
{
desc: "Multiple blockers work (deny first)",
run: async function testDenyAllowBlockers(browser) {
Events.on();
let blocker1Called = false,
blocker1 = params => {
blocker1Called = true;
return "deny";
};
webrtcUI.addPeerConnectionBlocker(blocker1);
let blocker2Called = false,
blocker2 = params => {
blocker2Called = true;
return "allow";
};
webrtcUI.addPeerConnectionBlocker(blocker2);
await tryPeerConnection(browser, "NotAllowedError");
Events.expect("peer-request-blocked");
ok(blocker1Called, "First blocker was called");
ok(
!blocker2Called,
"Peer connection blocker after a deny is not invoked"
);
webrtcUI.removePeerConnectionBlocker(blocker1);
webrtcUI.removePeerConnectionBlocker(blocker2);
Events.off();
},
},
{
desc: "Blockers may be removed",
run: async function testRemoveBlocker(browser) {
Events.on();
let blocker1Called = false,
blocker1 = params => {
blocker1Called = true;
return "allow";
};
webrtcUI.addPeerConnectionBlocker(blocker1);
let blocker2Called = false,
blocker2 = params => {
blocker2Called = true;
return "allow";
};
webrtcUI.addPeerConnectionBlocker(blocker2);
webrtcUI.removePeerConnectionBlocker(blocker1);
await tryPeerConnection(browser);
Events.expect("peer-request-allowed");
ok(!blocker1Called, "Removed peer connection blocker is not invoked");
ok(blocker2Called, "Second peer connection blocker was invoked");
webrtcUI.removePeerConnectionBlocker(blocker2);
Events.off();
},
},
{
desc: "Blocker that throws is ignored",
run: async function testBlockerThrows(browser) {
Events.on();
let blocker1Called = false,
blocker1 = params => {
blocker1Called = true;
throw new Error("kaboom");
};
webrtcUI.addPeerConnectionBlocker(blocker1);
let blocker2Called = false,
blocker2 = params => {
blocker2Called = true;
return "allow";
};
webrtcUI.addPeerConnectionBlocker(blocker2);
await tryPeerConnection(browser);
Events.expect("peer-request-allowed");
ok(blocker1Called, "First blocker was invoked");
ok(blocker2Called, "Second blocker was invoked");
webrtcUI.removePeerConnectionBlocker(blocker1);
webrtcUI.removePeerConnectionBlocker(blocker2);
Events.off();
},
},
{
desc: "Cancel peer request",
run: async function testBlockerCancel(browser) {
let blocker,
blockerPromise = new Promise(resolve => {
blocker = params => {
resolve();
// defer indefinitely
return new Promise(innerResolve => {});
};
});
webrtcUI.addPeerConnectionBlocker(blocker);
await SpecialPowers.spawn(browser, [], async function () {
new content.RTCPeerConnection().createOffer({
offerToReceiveAudio: true,
});
});
await blockerPromise;
let eventPromise = new Promise(resolve => {
webrtcUI.on("peer-request-cancel", function listener(details) {
resolve(details);
webrtcUI.off("peer-request-cancel", listener);
});
});
await SpecialPowers.spawn(browser, [], async function () {
content.location.reload();
});
let details = await eventPromise;
isnot(
details.callID,
undefined,
"peer-request-cancel event includes callID"
);
is(
details.origin,
ORIGIN,
"peer-request-cancel event has correct origin"
);
webrtcUI.removePeerConnectionBlocker(blocker);
},
},
];
add_task(async function test() {
await runTests(gTests, {
skipObserverVerification: true,
cleanup() {
is(
webrtcUI.peerConnectionBlockers.size,
0,
"Peer connection blockers list is empty"
);
},
});
});
|