forked from web-platform-tests/wpt
/
broadcast-channel.tentative.window.js
139 lines (116 loc) · 5.38 KB
/
broadcast-channel.tentative.window.js
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
// META: timeout=long
// META: script=/common/get-host-info.sub.js
// META: script=/common/utils.js
// META: script=/common/dispatcher/dispatcher.js
// META: script=../credentialless/resources/common.js
// META: script=./resources/common.js
// A script listening using a BroadcastChannel.
const listen_script = (key, done, onmessage) => `
const bc = new BroadcastChannel("${key}");
bc.onmessage = event => send("${onmessage}", event.data);
send("${done}", "registered");
`;
const emit_script = (key, message) => `
const bc = new BroadcastChannel("${key}");
bc.postMessage("${message}");
`;
// For incorrect web browser implementations, some tests are failing by timing
// out. Calling this function turns timeout into errors. This informs developers
// about which precise test is failing.
const errorOnTimeout = test =>
test.step_timeout(test.unreached_func("Timeout"), 9000);
promise_test_parallel(async test => {
errorOnTimeout(test);
const origin = get_host_info().HTTPS_REMOTE_ORIGIN;
const key_1 = token();
const key_2 = token();
// 2 actors: An anonymous iframe and a normal one.
const iframe_anonymous = newAnonymousIframe(origin);
const iframe_normal = newIframe(origin);
const queue_1 = token();
const queue_2 = token();
const unexpected_queue = token();
// Listen using the two keys from both sides:
send(iframe_anonymous , listen_script(key_1, queue_1, queue_1));
send(iframe_anonymous , listen_script(key_2, queue_1, unexpected_queue));
send(iframe_normal, listen_script(key_2, queue_2, queue_2));
send(iframe_normal, listen_script(key_1, queue_2, unexpected_queue));
assert_equals(await receive(queue_1), "registered");
assert_equals(await receive(queue_1), "registered");
assert_equals(await receive(queue_2), "registered");
assert_equals(await receive(queue_2), "registered");
// Emit from both sides. It must work, and work without crossing the
// anonymous/non-anonymous border.
receive(unexpected_queue).then(test.unreached_func(
"BroadcastChannel shouldn't cross the anonymous/normal border"));
send(iframe_anonymous , emit_script(key_1, "msg_1"));
send(iframe_normal, emit_script(key_2, "msg_2"));
assert_equals(await receive(queue_1), "msg_1");
assert_equals(await receive(queue_2), "msg_2");
// Wait a bit to let bad things the opportunity to show up. This is done by
// repeating the previous operation.
send(iframe_anonymous , emit_script(key_1, "msg_3"));
send(iframe_normal, emit_script(key_2, "msg_4"));
assert_equals(await receive(queue_1), "msg_3");
assert_equals(await receive(queue_2), "msg_4");
}, "Anonymous iframe and normal iframe aren't in the same partition")
promise_test_parallel(async test => {
errorOnTimeout(test);
const origin = get_host_info().HTTPS_REMOTE_ORIGIN;
const key = token();
const iframe_anonymous_1 = newAnonymousIframe(origin);
const iframe_anonymous_2 = newAnonymousIframe(origin);
const queue = token();
send(iframe_anonymous_1 , listen_script(key, queue, queue));
assert_equals(await receive(queue), "registered");
send(iframe_anonymous_2, emit_script(key, "msg"));
assert_equals(await receive(queue), "msg");
}, "Two sibling same-origin anonymous iframes are in the same partition");
promise_test_parallel(async test => {
errorOnTimeout(test);
const origin = get_host_info().HTTPS_REMOTE_ORIGIN;
const key = token();
const queue = token();
const iframe_anonymous_1 = newAnonymousIframe(origin);
send(iframe_anonymous_1, `
const importScript = ${importScript};
await importScript("/common/utils.js");
await importScript("/html/cross-origin-embedder-policy/credentialless" +
"/resources/common.js");
const newAnonymousIframe = ${newAnonymousIframe};
const iframe_anonymous_2 = newAnonymousIframe("${origin}");
send("${queue}", iframe_anonymous_2);
`);
const iframe_anonymous_2 = await receive(queue);
send(iframe_anonymous_1 , listen_script(key, queue, queue));
assert_equals(await receive(queue), "registered");
send(iframe_anonymous_2, emit_script(key, "msg"));
assert_equals(await receive(queue), "msg");
}, "Nested same-origin anonymous iframe are in the same partition");
promise_test_parallel(async test => {
errorOnTimeout(test);
const origin = get_host_info().HTTPS_REMOTE_ORIGIN;
const key = token();
const queue = token();
const iframe_anonymous_1 = newAnonymousIframe(origin);
const popup = newPopup(origin);
send(popup, `
const importScript = ${importScript};
await importScript("/common/utils.js");
await importScript("/html/cross-origin-embedder-policy/credentialless" +
"/resources/common.js");
const newAnonymousIframe = ${newAnonymousIframe};
send("${queue}", newAnonymousIframe("${origin}"));
`);
const iframe_anonymous_2 = await receive(queue);
const unexpected_queue = token();
receive(unexpected_queue).then(test.unreached_func(
"Two same-origin anonymous iframe in different windows shouldn't be able " +
"to communicate using BroadcastChannel"));
send(iframe_anonymous_1 , listen_script(key, queue, unexpected_queue));
assert_equals(await receive(queue), "registered");
await send(iframe_anonymous_2, emit_script(key, "msg"));
// Wait a bit to give the opportunity for unexpected message to be received.
await new Promise(r => test.step_timeout(r, 500));
}, "Two anonymous iframes in different windows do not share the same " +
"partition");