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
|
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=postMessage
-->
<head>
<title>postMessage from about:blank, data URLs</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="browserFu.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=postMessage">Mozilla Bug 387706</a>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test">
<script class="testbody" type="application/javascript"><![CDATA[
/** Test for Bug 387706 **/
SimpleTest.waitForExplicitFinish();
var B64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/**
* Encodes an array of bytes into a string using the base 64 encoding scheme.
*
* @param bytes
* An array of bytes to encode.
*/
function b64(str)
{
var byteArray = new Array(str.length);
for (var i = 0, sz = str.length; i < sz; i++)
byteArray[i] = str.charCodeAt(i);
var index = 0;
function get3Bytes()
{
if (byteArray.length - index < 3)
return null; // Less than three bytes remaining
// Return the next three bytes in the array, and increment index for our
// next invocation
return byteArray.slice(index, index += 3);
}
var out = "";
var bytes = null;
while ((bytes = get3Bytes()))
{
var bits = 0;
for (var i = 0; i < 3; i++)
bits = (bits << 8) | bytes[i];
for (var j = 18; j >= 0; j -= 6)
out += B64_CHARS[(bits>>j) & 0x3F];
}
// Get the remaining bytes
bytes = byteArray.slice(index);
switch (bytes.length)
{
case 2:
out += B64_CHARS[(bytes[0]>>2) & 0x3F] +
B64_CHARS[((bytes[0] & 0x03) << 4) | ((bytes[1] >> 4) & 0x0F)] +
B64_CHARS[((bytes[1] & 0x0F) << 2)] +
"=";
break;
case 1:
out += B64_CHARS[(bytes[0]>>2) & 0x3F] +
B64_CHARS[(bytes[0] & 0x03) << 4] +
"==";
break;
}
return out;
}
var aboutBlankWindow = null;
var aboutBlank2Window = null;
var dataWindow = null;
/** Convert a nullable string to a pretty representation */
function sourceify(v)
{
if (typeof v == "string")
return "'" + v + "'";
return String(v);
}
/** Receives MessageEvents to this window. */
function messageReceiver(evt)
{
// It's not clear what the security model is for data: URLs and whether they
// can access their parents; WebKit denies access, while Gecko currently
// allows it. We work around this problem by using postMessage (surprise!)
// to start the round of tests when each iframe loads.
if (evt.data === "next-test")
{
setTimeout(nextTest, 0);
return;
}
try
{
ok(evt instanceof MessageEvent, "umm, how did we get this?");
is(evt.type, "message", "expected events of type 'message'");
if (isMozilla)
{
ok(evt.isTrusted === false, "shouldn't have been a trusted event");
}
if (evt.data === "about:blank-response")
{
// This isn't clarified in HTML5 yet, but the origin for a document which
// has been open()ed is the origin of the calling code, somewhat loosely
// speaking. For the specific case of about:blank it's also possible
// that the origin is determined by the code that opens the window. It's
// not codified yet which of these two causes the identifier tokens on
// the event generated by the new window to be those of this window, but
// in either case this is what they should be.
is(evt.origin, "http://mochi.test:8888",
"wrong origin for event from about:blank");
is(evt.source, aboutBlankWindow, "wrong source");
// ...and onto the next test
setupBlank2();
}
else if (evt.data === "about:blank2-response")
{
is(evt.origin, "http://mochi.test:8888",
"wrong origin for event from about:blank #2");
is(evt.source, aboutBlank2Window, "wrong source");
setupData();
}
else if (evt.data === "data-response")
{
// HTML5 defines the origin of a data: URI as the origin of the window or
// script that opened the data: URI.
is(evt.origin, "http://mochi.test:8888",
"wrong origin for event from data URL (should be the origin of the " +
"window/script that opened the URL, in this case the origin of this " +
"file)");
is(evt.source, dataWindow, "wrong source");
finish();
}
else
{
ok(false, "unexpected message: " + evt.data);
}
}
catch (e)
{
ok(false, "error processing event with data '" + evt.data + "': " + e);
}
}
function getContents(description, responseText)
{
var contents =
"<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
" <title>about:blank</title>\n" +
" <script type='application/javascript'>\n" +
"function receive(evt)\n" +
"{\n" +
" var response = '" + responseText + "';\n" +
"\n" +
" if (evt.source !== window.parent)\n" +
" response += ' wrong-source';\n" +
" if (evt.origin !== 'http://mochi.test:8888')\n" +
" response += ' wrong-origin(' + evt.origin + ')';\n" +
" if (evt.data !== 'from-opener')\n" +
" response += ' wrong-data(' + evt.data + ')';\n" +
"\n" +
" window.parent.postMessage(response, 'http://mochi.test:8888');\n" +
"}\n" +
"\n" +
"function ready()\n" +
"{\n" +
" window.parent.postMessage('next-test', 'http://mochi.test:8888');\n" +
"}\n" +
"\n" +
"window.addEventListener('load', ready, false);\n" +
"window.addEventListener('message', receive, false);\n" +
" </script>\n" +
"</head>\n" +
"<body><p>" + description + "</p></body>\n" +
"</html>";
return contents;
}
function finish()
{
SimpleTest.finish();
}
var xhtmlns = "http://www.w3.org/1999/xhtml";
function insert(el)
{
var content = $("content");
content.parentNode.insertBefore(el, content);
}
function setupBlank()
{
var aboutBlankFrame = document.createElementNS(xhtmlns, "iframe");
aboutBlankFrame.setAttribute("src", "about:blank");
insert(aboutBlankFrame);
aboutBlankWindow = aboutBlankFrame.contentWindow;
var doc = aboutBlankWindow.document;
doc.open();
doc.write(getContents("This was about:blank #1", "about:blank-response"));
doc.close();
// I don't believe anything guarantees sync parsing, so we have to wait for
// the new window to poke us to actually do the test. :-\
}
function setupBlank2()
{
var aboutBlank2Frame = document.createElementNS(xhtmlns, "iframe");
aboutBlank2Frame.addEventListener("load", nextTest, false);
aboutBlank2Frame.setAttribute("src", "about:blank");
insert(aboutBlank2Frame);
}
// Could use window.btoa here, but that's not standardized, and we want to be
// able to run these tests against browsers that don't support it.
var dataURI = "data:text/html;base64," +
b64(getContents("A data: URL", "data-response"));
function setupData()
{
var dataFrame = document.createElementNS(xhtmlns, "iframe");
dataFrame.setAttribute("src", dataURI);
insert(dataFrame);
dataWindow = dataFrame.contentWindow;
// ...and wait again for the window to load...
}
var count = 0;
function nextTest()
{
switch (count++)
{
case 0:
testBlank();
break;
case 1:
testBlank2();
break;
case 2:
testData();
break;
default:
ok(false, "unreached");
break;
}
}
function testBlank()
{
aboutBlankWindow.postMessage("from-opener", "http://mochi.test:8888");
}
function testBlank2()
{
// For some reason we can't access this across browsers prior to the iframe
// loading, so set its value here.
aboutBlank2Window = window.frames[1];
var doc = aboutBlank2Window.document;
doc.body.textContent = "This was about:blank #2";
var script = doc.createElement("script");
script.textContent =
"window.parent.postMessage('about:blank2-response', " +
" 'http://mochi.test:8888');";
doc.body.appendChild(script);
}
function testData()
{
dataWindow.postMessage("from-opener", "http://mochi.test:8888");
}
window.addEventListener("message", messageReceiver, false);
addLoadEvent(setupBlank);
]]></script>
</pre>
</body>
</html>
|