summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webmessaging/broadcastchannel/opaque-origin.html
blob: e09d935244b8b6d01e2b3ca62d16198e3f8af6d9 (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
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<meta name="timeout" content="long">
<title></title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
</head>
<body>
<script>
<!--
promise_test(t => {
  return new Promise((resolve) => {
    let ifr = document.createElement("iframe");
    ifr.src =
      "data:text/html,<script> let bc = new BroadcastChannel(\"test\");" +
      "bc.onmessage = (e) => {" +
      "  if (e.data == \"ping\") bc.postMessage('pong');"+
      "  else parent.postMessage({workerMessageOrigin: e.data, messageOrigin: e.origin}, \"*\"); };" +
      "new Worker(URL.createObjectURL(new Blob([\"" +
      "let bc2 = new BroadcastChannel('test'); bc2.postMessage('ping'); " +
      "bc2.onmessage = e => bc2.postMessage(e.origin);" +
      "\"], {type: 'text/javascript'}))); </script>";
    window.addEventListener("message", t.step_func(e => {
        assert_equals(e.data.workerMessageOrigin, "null");
        assert_equals(e.data.messageOrigin, "null");
        resolve();
      }), {once: true});
    t.add_cleanup(() => { document.body.removeChild(ifr) });
    document.body.appendChild(ifr);
    });
}, "Opaque origin should be serialized to \"null\"");


const iframe_src = (channel_name, iframe_name) => `data:text/html,<script>
let bc2 = new BroadcastChannel("${channel_name}");
bc2.onmessage = (e) => {
  if (e.data == "from-${iframe_name}") {
    parent.postMessage("${iframe_name}-done", "*");
  } else {
    parent.postMessage("fail", "*");
  }
};
let bc3 = new BroadcastChannel("${channel_name}");
bc3.postMessage("from-${iframe_name}");
</script>`;

promise_test(t => {
  return new Promise((resolve, reject) => {
    const channel_name = "opaque-origin-test-2";
    const bc1 = new BroadcastChannel(channel_name);
    bc1.onmessage = t.unreached_func("Received message from an opaque origin");

    // We'll create an iframe and have it send a BroadcastChannel message
    // between two instances. Once the message is received, it will postMessage
    // back and we'll repeat this with another iframe. If the first
    // BroadcastChannel message is received by `bc1`, or if the second
    // BroadcastChannel message is received by `bc1` or `bc2` in the first
    // iframe, then the test should fail.

    window.addEventListener("message", e => {
      if(e.data == "iframe1-done") {
        let iframe2 = document.createElement("iframe");
        iframe2.src = iframe_src(channel_name, "iframe2");
        t.add_cleanup(() => { document.body.removeChild(iframe2) });
        document.body.appendChild(iframe2);
      } else if(e.data == "iframe2-done") {
        resolve();
      } else if(e.data == "fail") {
        reject("One opaque origin received a message from the other");
      } else {
        reject("An unexpected error occurred");
      }
    });

    let iframe1 = document.createElement("iframe");
    iframe1.src = iframe_src(channel_name, "iframe1");
    t.add_cleanup(() => { document.body.removeChild(iframe1) });
    document.body.appendChild(iframe1);
    });
}, "BroadcastChannel messages from opaque origins should be self-contained");

const data_url_worker_src = (channel_name, worker_name) => {
  const source = `
const handler = (reply) => {
  let bc2 = new BroadcastChannel("${channel_name}");
  bc2.onmessage = (e) => {
    if (e.data == "from-${worker_name}") {
      reply("${worker_name}-done");
    } else {
      reply("fail");
    }
  };
  let bc3 = new BroadcastChannel("${channel_name}");
  bc3.postMessage("from-${worker_name}");
};
// For dedicated workers:
self.addEventListener("message", () => handler(self.postMessage));
// For shared workers:
self.addEventListener("connect", (e) => {
  var port = e.ports[0];
  port.onmessage = () => handler(msg => port.postMessage(msg));
  port.start();

});
`;
  return "data:,".concat(encodeURIComponent(source));
}

promise_test(t => {
  return new Promise((resolve, reject) => {
    const channel_name = "opaque-origin-test-3";
    const bc1 = new BroadcastChannel(channel_name);
    bc1.onmessage = e => { reject("Received message from an opaque origin"); };

    // Same as the previous test but with data URL dedicated workers (which
    // should have opaque origins per the HTML spec).
    const worker_name_prefix = "data-url-dedicated-worker";
    const worker_1_name = `${worker_name_prefix}-1`;
    const worker_2_name = `${worker_name_prefix}-2`;

    const handler = e => {
      if(e.data == `${worker_1_name}-done`) {
        const worker2 = new Worker(data_url_worker_src(channel_name, worker_2_name));
        t.add_cleanup(() => worker2.terminate());
        worker2.addEventListener("message", handler);
        worker2.postMessage("go!");
      } else if(e.data == `${worker_2_name}-done`) {
        resolve();
      } else if(e.data == "fail") {
        reject("One opaque origin received a message from the other");
      } else {
        reject("An unexpected error occurred");
      }
    };

    let worker1 = new Worker(data_url_worker_src(channel_name, worker_1_name));
    t.add_cleanup(() => worker1.terminate());
    worker1.addEventListener("message", handler);
    worker1.postMessage("go!");
    });
}, "BroadcastChannel messages from data URL dedicated workers should be self-contained");

promise_test(() => {
  return new Promise((resolve, reject) => {
    const channel_name = "opaque-origin-test-4";
    const bc1 = new BroadcastChannel(channel_name);

    // Same as the previous test but with data URL shared workers (which
    // should have opaque origins per the HTML spec).
    const worker_name_prefix = "data-url-shared-worker";
    const worker_1_name = `${worker_name_prefix}-1`;
    const worker_2_name = `${worker_name_prefix}-2`;

    const handler = e => {
      if (e.data == `${worker_1_name}-done`) {
        const worker_script = data_url_worker_src(channel_name, worker_2_name);
        const worker2 = new SharedWorker(worker_script, worker_2_name);
        worker2.port.addEventListener("message", handler);
        worker2.port.start();
        worker2.port.postMessage("go!");
      } else if(e.data == `${worker_2_name}-done`) {
        resolve();
      } else if(e.data == "fail") {
        reject("One opaque origin received a message from the other");
      } else {
        reject("An unexpected error occurred");
      }
    };

    bc1.onmessage = e => {
      if (e.data == "go!") {
        const worker_script = data_url_worker_src(channel_name, worker_1_name);
        const worker1 = new SharedWorker(worker_script, worker_1_name);
        worker1.port.addEventListener("message", handler);
        worker1.port.start();
        worker1.port.postMessage("go!");
      } else {
        reject("Received message from an opaque origin");
      }
    };

    // Ensure that the BroadcastChannel instance above can receive messages
    // before we create the first shared worker.
    const bc2 = new BroadcastChannel(channel_name);
    bc2.postMessage("go!");
    });
}, "BroadcastChannel messages from data URL shared workers should be self-contained");
//-->
</script>
</body>
</html>