Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html> |
michael@0 | 3 | <!-- |
michael@0 | 4 | https://bugzilla.mozilla.org/show_bug.cgi?id=338583 |
michael@0 | 5 | --> |
michael@0 | 6 | <head> |
michael@0 | 7 | <meta http-equiv='Content-Type' content='text/html; charset=utf-8'> |
michael@0 | 8 | <title>Test for Bug 338583</title> |
michael@0 | 9 | <script type="text/javascript" src="/MochiKit/MochiKit.js"></script> |
michael@0 | 10 | <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 11 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
michael@0 | 12 | |
michael@0 | 13 | </head> |
michael@0 | 14 | <body bgColor=white> |
michael@0 | 15 | <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=338583">Mozilla Bug 338583</a> |
michael@0 | 16 | <p id="display"></p> |
michael@0 | 17 | <div id="content" style="display: none"> |
michael@0 | 18 | |
michael@0 | 19 | </div> |
michael@0 | 20 | <pre id="test"> |
michael@0 | 21 | <script class="testbody" type="text/javascript"> |
michael@0 | 22 | /** Tests for Bug 338583 **/ |
michael@0 | 23 | |
michael@0 | 24 | // we test: |
michael@0 | 25 | // 1) the EventSource behaviour |
michael@0 | 26 | // 2) if the events are trusted |
michael@0 | 27 | // 3) possible invalid eventsources |
michael@0 | 28 | // 4) the close method when the object is just been used |
michael@0 | 29 | // 5) access-control |
michael@0 | 30 | // 6) the data parameter |
michael@0 | 31 | // 7) delayed server responses |
michael@0 | 32 | |
michael@0 | 33 | // -- |
michael@0 | 34 | |
michael@0 | 35 | var gTestsHaveFinished = []; |
michael@0 | 36 | |
michael@0 | 37 | function setTestHasFinished(test_id) |
michael@0 | 38 | { |
michael@0 | 39 | if (gTestsHaveFinished[test_id]) { |
michael@0 | 40 | return; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | gTestsHaveFinished[test_id] = true; |
michael@0 | 44 | for (var i=0; i < gTestsHaveFinished.length; ++i) { |
michael@0 | 45 | if (!gTestsHaveFinished[i]) { |
michael@0 | 46 | return; |
michael@0 | 47 | } |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | SimpleTest.finish(); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | function runAllTests() { |
michael@0 | 54 | // these tests run asynchronously, and they will take 8000 ms |
michael@0 | 55 | var all_tests = [ |
michael@0 | 56 | doTest1, doTest1_e, doTest1_f, doTest2, doTest3, doTest3_b, doTest3_c, doTest3_d, |
michael@0 | 57 | doTest3_e, doTest3_f, doTest3_g, doTest3_h, doTest4, doTest4_b, |
michael@0 | 58 | doTest5, doTest5_b, doTest5_c, doTest5_e, doTest6, doTest7 |
michael@0 | 59 | ]; |
michael@0 | 60 | |
michael@0 | 61 | for (var test_id=0; test_id < all_tests.length; ++test_id) { |
michael@0 | 62 | gTestsHaveFinished[test_id] = false; |
michael@0 | 63 | var fn = all_tests[test_id]; |
michael@0 | 64 | fn(test_id); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | setTimeout(function() { |
michael@0 | 68 | for (var test_id=0; test_id < all_tests.length; ++test_id) { |
michael@0 | 69 | if (!gTestsHaveFinished[test_id]) { |
michael@0 | 70 | ok(false, "Test " + test_id + " took too long"); |
michael@0 | 71 | setTestHasFinished(test_id); |
michael@0 | 72 | } |
michael@0 | 73 | } |
michael@0 | 74 | }, 60000 * stress_factor); // all tests together are supposed to take less than 1 minute |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | function fn_onmessage(e) { |
michael@0 | 78 | if (e.currentTarget == e.target && e.target.hits != null) |
michael@0 | 79 | e.target.hits['fn_onmessage']++; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | function fn_event_listener_message(e) { |
michael@0 | 83 | if (e.currentTarget == e.target && e.target.hits != null) |
michael@0 | 84 | e.target.hits['fn_event_listener_message']++; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | function fn_other_event_name(e) { |
michael@0 | 88 | if (e.currentTarget == e.target && e.target.hits != null) |
michael@0 | 89 | e.target.hits['fn_other_event_name']++; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | var gEventSourceObj1 = null, gEventSourceObj1_e, gEventSourceObj1_f; |
michael@0 | 93 | var gEventSourceObj2 = null; |
michael@0 | 94 | var gEventSourceObj3_a = null, gEventSourceObj3_b = null, |
michael@0 | 95 | gEventSourceObj3_c = null, gEventSourceObj3_d = null, |
michael@0 | 96 | gEventSourceObj3_e = null, gEventSourceObj3_f = null, |
michael@0 | 97 | gEventSourceObj3_g = null, gEventSourceObj3_h = null; |
michael@0 | 98 | var gEventSourceObj4_a = null, gEventSourceObj4_b = null; |
michael@0 | 99 | var gEventSourceObj5_a = null, gEventSourceObj5_b = null, |
michael@0 | 100 | gEventSourceObj5_c = null, gEventSourceObj5_d = null, |
michael@0 | 101 | gEventSourceObj5_e = null, gEventSourceObj5_f = null; |
michael@0 | 102 | var gEventSourceObj6 = null; |
michael@0 | 103 | var gEventSourceObj7 = null; |
michael@0 | 104 | var stress_factor; // used in the setTimeouts in order to help |
michael@0 | 105 | // the test when running in slow machines |
michael@0 | 106 | |
michael@0 | 107 | function hasBeenHitFor1And2(obj, min) { |
michael@0 | 108 | if (obj.hits['fn_onmessage'] < min || |
michael@0 | 109 | obj.hits['fn_event_listener_message'] < min || |
michael@0 | 110 | obj.hits['fn_other_event_name'] < min) |
michael@0 | 111 | return false; |
michael@0 | 112 | return true; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | // in order to test (1): |
michael@0 | 116 | // a) if the EventSource constructor parameter is equal to its url attribute |
michael@0 | 117 | // b) let its fn_onmessage, fn_event_listener_message, and fn_other_event_name functions listeners be hit four times each |
michael@0 | 118 | // c) the close method (we expect readyState == CLOSED) |
michael@0 | 119 | // d) the close method (we expect no message events anymore) |
michael@0 | 120 | // e) use the default for withCredentials when passing dictionary arguments that don't explicitly set it |
michael@0 | 121 | // f) if a 204 HTTP response closes (interrupts) connections. See bug 869432. |
michael@0 | 122 | |
michael@0 | 123 | function doTest1(test_id) { |
michael@0 | 124 | gEventSourceObj1 = new EventSource("eventsource.resource"); |
michael@0 | 125 | ok(gEventSourceObj1.url == "http://mochi.test:8888/tests/content/base/test/eventsource.resource", "Test 1.a failed."); |
michael@0 | 126 | ok(gEventSourceObj1.readyState == 0 || gEventSourceObj1.readyState == 1, "Test 1.a failed."); |
michael@0 | 127 | |
michael@0 | 128 | doTest1_b(test_id); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | function doTest1_b(test_id) { |
michael@0 | 132 | gEventSourceObj1.hits = []; |
michael@0 | 133 | gEventSourceObj1.hits['fn_onmessage'] = 0; |
michael@0 | 134 | gEventSourceObj1.onmessage = fn_onmessage; |
michael@0 | 135 | gEventSourceObj1.hits['fn_event_listener_message'] = 0; |
michael@0 | 136 | gEventSourceObj1.addEventListener('message', fn_event_listener_message, true); |
michael@0 | 137 | gEventSourceObj1.hits['fn_other_event_name'] = 0; |
michael@0 | 138 | gEventSourceObj1.addEventListener('other_event_name', fn_other_event_name, true); |
michael@0 | 139 | |
michael@0 | 140 | // the eventsources.res always use a retry of 0.5 second, so for four hits a timeout of 6 seconds is enough |
michael@0 | 141 | setTimeout(function(){ |
michael@0 | 142 | bhits = hasBeenHitFor1And2(gEventSourceObj1, 4); |
michael@0 | 143 | ok(bhits, "Test 1.b failed."); |
michael@0 | 144 | |
michael@0 | 145 | doTest1_c(test_id); |
michael@0 | 146 | }, parseInt(6000*stress_factor)); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | function doTest1_c(test_id) { |
michael@0 | 150 | gEventSourceObj1.close(); |
michael@0 | 151 | ok(gEventSourceObj1.readyState == 2, "Test 1.c failed."); |
michael@0 | 152 | |
michael@0 | 153 | doTest1_d(test_id); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | function doTest1_d(test_id) { |
michael@0 | 157 | gEventSourceObj1.hits['fn_onmessage'] = 0; |
michael@0 | 158 | gEventSourceObj1.hits['fn_event_listener_message'] = 0; |
michael@0 | 159 | gEventSourceObj1.hits['fn_other_event_name'] = 0; |
michael@0 | 160 | |
michael@0 | 161 | setTimeout(function(){ |
michael@0 | 162 | bhits = hasBeenHitFor1And2(gEventSourceObj1, 1); |
michael@0 | 163 | ok(!bhits, "Test 1.d failed."); |
michael@0 | 164 | gEventSourceObj1.close(); |
michael@0 | 165 | setTestHasFinished(test_id); |
michael@0 | 166 | }, parseInt(2000*stress_factor)); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | function doTest1_e(test_id) { |
michael@0 | 170 | try { |
michael@0 | 171 | for (var options of [null, undefined, {}]) { |
michael@0 | 172 | gEventSourceObj1_e = new EventSource("eventsource.resource", options); |
michael@0 | 173 | is(gEventSourceObj1_e.withCredentials, false, "withCredentials should default to false"); |
michael@0 | 174 | gEventSourceObj1_e.close(); |
michael@0 | 175 | } |
michael@0 | 176 | } catch (e) { |
michael@0 | 177 | ok(false, "Test 1.e failed"); |
michael@0 | 178 | } |
michael@0 | 179 | setTestHasFinished(test_id); |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | function doTest1_f(test_id) { |
michael@0 | 183 | var called_on_error = false; |
michael@0 | 184 | |
michael@0 | 185 | gEventSourceObj1_f = new EventSource("file_bug869432.eventsource"); |
michael@0 | 186 | gEventSourceObj1_f.onopen = function(e) { |
michael@0 | 187 | ok(false, "Test 1.f failed: onopen was called"); |
michael@0 | 188 | }; |
michael@0 | 189 | gEventSourceObj1_f.onmessage = function(e) { |
michael@0 | 190 | ok(false, "Test 1.f failed: onmessage was called"); |
michael@0 | 191 | }; |
michael@0 | 192 | gEventSourceObj1_f.onerror = function(e) { |
michael@0 | 193 | if (called_on_error) { |
michael@0 | 194 | ok(false, "Test 1.f failed: onerror was called twice"); |
michael@0 | 195 | } |
michael@0 | 196 | called_on_error = true; |
michael@0 | 197 | ok(gEventSourceObj1_f.readyState == 2, "Test 1.f failed: onerror was called with readyState = " + gEventSourceObj1_f.readyState); |
michael@0 | 198 | }; |
michael@0 | 199 | |
michael@0 | 200 | setTimeout(function() { // just to clean... |
michael@0 | 201 | ok(called_on_error, "Test 1.f failed: onerror was not called"); |
michael@0 | 202 | setTestHasFinished(test_id); |
michael@0 | 203 | }, parseInt(5000*stress_factor)); |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | // in order to test (2) |
michael@0 | 207 | // a) set a eventsource that give the dom events messages |
michael@0 | 208 | // b) expect trusted events |
michael@0 | 209 | |
michael@0 | 210 | function doTest2(test_id) { |
michael@0 | 211 | var func = function(e) { |
michael@0 | 212 | ok(e.isTrusted, "Test 2 failed"); |
michael@0 | 213 | gEventSourceObj2.close(); |
michael@0 | 214 | }; |
michael@0 | 215 | |
michael@0 | 216 | gEventSourceObj2 = new EventSource("eventsource.resource"); |
michael@0 | 217 | gEventSourceObj2.onmessage = func; |
michael@0 | 218 | |
michael@0 | 219 | setTimeout(function() { // just to clean... |
michael@0 | 220 | gEventSourceObj2.close(); |
michael@0 | 221 | setTestHasFinished(test_id); |
michael@0 | 222 | }, parseInt(5000*stress_factor)); |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | // in order to test (3) |
michael@0 | 226 | // a) XSite domain error test |
michael@0 | 227 | // b) protocol file:// test |
michael@0 | 228 | // c) protocol javascript: test |
michael@0 | 229 | // d) wrong Content-Type test |
michael@0 | 230 | // e) bad http response code test |
michael@0 | 231 | // f) message eventsource without a data test |
michael@0 | 232 | // g) DNS error |
michael@0 | 233 | // h) EventSource which last message doesn't end with an empty line. See bug 710546 |
michael@0 | 234 | |
michael@0 | 235 | function doTest3(test_id) { |
michael@0 | 236 | gEventSourceObj3_a = new EventSource("http://example.org/tests/content/base/test/eventsource.resource"); |
michael@0 | 237 | |
michael@0 | 238 | gEventSourceObj3_a.onmessage = fn_onmessage; |
michael@0 | 239 | gEventSourceObj3_a.hits = []; |
michael@0 | 240 | gEventSourceObj3_a.hits['fn_onmessage'] = 0; |
michael@0 | 241 | |
michael@0 | 242 | setTimeout(function() { |
michael@0 | 243 | ok(gEventSourceObj3_a.hits['fn_onmessage'] == 0, "Test 3.a failed"); |
michael@0 | 244 | gEventSourceObj3_a.close(); |
michael@0 | 245 | setTestHasFinished(test_id); |
michael@0 | 246 | }, parseInt(1500*stress_factor)); |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | function doTest3_b(test_id) { |
michael@0 | 250 | // currently no support yet for local files for b2g/Android mochitest, see bug 838726 |
michael@0 | 251 | if (navigator.appVersion.indexOf("Android") != -1 || SpecialPowers.Services.appinfo.name == "B2G") { |
michael@0 | 252 | setTestHasFinished(test_id); |
michael@0 | 253 | return; |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | var xhr = new XMLHttpRequest; |
michael@0 | 257 | xhr.open("GET", "/dynamic/getMyDirectory.sjs", false); |
michael@0 | 258 | xhr.send(); |
michael@0 | 259 | var basePath = xhr.responseText; |
michael@0 | 260 | |
michael@0 | 261 | gEventSourceObj3_b = new EventSource("file://" + basePath + "eventsource.resource"); |
michael@0 | 262 | |
michael@0 | 263 | gEventSourceObj3_b.onmessage = fn_onmessage; |
michael@0 | 264 | gEventSourceObj3_b.hits = []; |
michael@0 | 265 | gEventSourceObj3_b.hits['fn_onmessage'] = 0; |
michael@0 | 266 | |
michael@0 | 267 | setTimeout(function() { |
michael@0 | 268 | ok(gEventSourceObj3_b.hits['fn_onmessage'] == 0, "Test 3.b failed"); |
michael@0 | 269 | gEventSourceObj3_b.close(); |
michael@0 | 270 | setTestHasFinished(test_id); |
michael@0 | 271 | }, parseInt(1500*stress_factor)); |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | function jsEvtSource() |
michael@0 | 275 | { |
michael@0 | 276 | return "event: message\n" + |
michael@0 | 277 | "data: 1\n\n"; |
michael@0 | 278 | } |
michael@0 | 279 | |
michael@0 | 280 | function doTest3_c(test_id) { |
michael@0 | 281 | gEventSourceObj3_c = new EventSource("javascript: return jsEvtSource()"); |
michael@0 | 282 | |
michael@0 | 283 | gEventSourceObj3_c.onmessage = fn_onmessage; |
michael@0 | 284 | gEventSourceObj3_c.hits = []; |
michael@0 | 285 | gEventSourceObj3_c.hits['fn_onmessage'] = 0; |
michael@0 | 286 | |
michael@0 | 287 | setTimeout(function() { |
michael@0 | 288 | ok(gEventSourceObj3_c.hits['fn_onmessage'] == 0, "Test 3.c failed"); |
michael@0 | 289 | gEventSourceObj3_c.close(); |
michael@0 | 290 | setTestHasFinished(test_id); |
michael@0 | 291 | }, parseInt(1500*stress_factor)); |
michael@0 | 292 | } |
michael@0 | 293 | |
michael@0 | 294 | function doTest3_d(test_id) { |
michael@0 | 295 | gEventSourceObj3_d = new EventSource("badContentType.eventsource"); |
michael@0 | 296 | |
michael@0 | 297 | gEventSourceObj3_d.onmessage = fn_onmessage; |
michael@0 | 298 | gEventSourceObj3_d.hits = []; |
michael@0 | 299 | gEventSourceObj3_d.hits['fn_onmessage'] = 0; |
michael@0 | 300 | |
michael@0 | 301 | setTimeout(function() { |
michael@0 | 302 | ok(gEventSourceObj3_d.hits['fn_onmessage'] == 0, "Test 3.d failed"); |
michael@0 | 303 | gEventSourceObj3_d.close(); |
michael@0 | 304 | setTestHasFinished(test_id); |
michael@0 | 305 | }, parseInt(1500*stress_factor)); |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | function doTest3_e(test_id) { |
michael@0 | 309 | gEventSourceObj3_e = new EventSource("badHTTPResponseCode.eventsource"); |
michael@0 | 310 | |
michael@0 | 311 | gEventSourceObj3_e.onmessage = fn_onmessage; |
michael@0 | 312 | gEventSourceObj3_e.hits = []; |
michael@0 | 313 | gEventSourceObj3_e.hits['fn_onmessage'] = 0; |
michael@0 | 314 | |
michael@0 | 315 | setTimeout(function() { |
michael@0 | 316 | ok(gEventSourceObj3_e.hits['fn_onmessage'] == 0, "Test 3.e failed"); |
michael@0 | 317 | gEventSourceObj3_e.close(); |
michael@0 | 318 | setTestHasFinished(test_id); |
michael@0 | 319 | }, parseInt(1500*stress_factor)); |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | function doTest3_f(test_id) { |
michael@0 | 323 | gEventSourceObj3_f = new EventSource("badMessageEvent.eventsource"); |
michael@0 | 324 | |
michael@0 | 325 | gEventSourceObj3_f.onmessage = fn_onmessage; |
michael@0 | 326 | gEventSourceObj3_f.hits = []; |
michael@0 | 327 | gEventSourceObj3_f.hits['fn_onmessage'] = 0; |
michael@0 | 328 | |
michael@0 | 329 | setTimeout(function() { |
michael@0 | 330 | ok(gEventSourceObj3_f.hits['fn_onmessage'] == 0, "Test 3.f failed"); |
michael@0 | 331 | gEventSourceObj3_f.close(); |
michael@0 | 332 | setTestHasFinished(test_id); |
michael@0 | 333 | }, parseInt(1500*stress_factor)); |
michael@0 | 334 | } |
michael@0 | 335 | |
michael@0 | 336 | function fnInvalidNCName() { |
michael@0 | 337 | fnInvalidNCName.hits++; |
michael@0 | 338 | } |
michael@0 | 339 | |
michael@0 | 340 | function doTest3_g(test_id) { |
michael@0 | 341 | gEventSourceObj3_g = new EventSource("http://hdfskjghsbg.jtiyoejowe.dafsgbhjab.com"); |
michael@0 | 342 | |
michael@0 | 343 | gEventSourceObj3_g.onmessage = fn_onmessage; |
michael@0 | 344 | gEventSourceObj3_g.hits = []; |
michael@0 | 345 | gEventSourceObj3_g.hits['fn_onmessage'] = 0; |
michael@0 | 346 | |
michael@0 | 347 | setTimeout(function() { |
michael@0 | 348 | ok(gEventSourceObj3_g.hits['fn_onmessage'] == 0, "Test 3.g failed"); |
michael@0 | 349 | gEventSourceObj3_g.close(); |
michael@0 | 350 | setTestHasFinished(test_id); |
michael@0 | 351 | }, parseInt(1500*stress_factor)); |
michael@0 | 352 | } |
michael@0 | 353 | |
michael@0 | 354 | function fnMessageListenerTest3h(e) { |
michael@0 | 355 | fnMessageListenerTest3h.msg_ok = (fnMessageListenerTest3h.msg_ok && e.data == "ok"); |
michael@0 | 356 | fnMessageListenerTest3h.id_ok = (fnMessageListenerTest3h.msg_ok && e.lastEventId == ""); |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | function doTest3_h(test_id) { |
michael@0 | 360 | gEventSourceObj3_h = new EventSource("badMessageEvent2.eventsource"); |
michael@0 | 361 | |
michael@0 | 362 | gEventSourceObj3_h.addEventListener('message event', fnMessageListenerTest3h, true); |
michael@0 | 363 | fnMessageListenerTest3h.msg_ok = true; |
michael@0 | 364 | fnMessageListenerTest3h.id_ok = true; |
michael@0 | 365 | |
michael@0 | 366 | gEventSourceObj3_h.onmessage = fn_onmessage; |
michael@0 | 367 | gEventSourceObj3_h.hits = []; |
michael@0 | 368 | gEventSourceObj3_h.hits['fn_onmessage'] = 0; |
michael@0 | 369 | |
michael@0 | 370 | setTimeout(function() { |
michael@0 | 371 | ok(gEventSourceObj3_h.hits['fn_onmessage'] > 1, "Test 3.h.1 failed"); |
michael@0 | 372 | if (gEventSourceObj3_h.hits['fn_onmessage'] > 1) { |
michael@0 | 373 | ok(fnMessageListenerTest3h.msg_ok, "Test 3.h.2 failed"); |
michael@0 | 374 | ok(fnMessageListenerTest3h.id_ok, "Test 3.h.3 failed"); |
michael@0 | 375 | } |
michael@0 | 376 | gEventSourceObj3_h.close(); |
michael@0 | 377 | setTestHasFinished(test_id); |
michael@0 | 378 | }, parseInt(3000*stress_factor)); |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | // in order to test (4) |
michael@0 | 382 | // a) close the object when it is in use, which is being processed and that is expected |
michael@0 | 383 | // to dispatch more eventlisteners |
michael@0 | 384 | // b) remove an eventlistener in use |
michael@0 | 385 | |
michael@0 | 386 | function fn_onmessage4_a(e) |
michael@0 | 387 | { |
michael@0 | 388 | if (e.data > gEventSourceObj4_a.lastData) |
michael@0 | 389 | gEventSourceObj4_a.lastData = e.data; |
michael@0 | 390 | if (e.data == 2) |
michael@0 | 391 | gEventSourceObj4_a.close(); |
michael@0 | 392 | } |
michael@0 | 393 | |
michael@0 | 394 | function fn_onmessage4_b(e) |
michael@0 | 395 | { |
michael@0 | 396 | if (e.data > gEventSourceObj4_b.lastData) |
michael@0 | 397 | gEventSourceObj4_b.lastData = e.data; |
michael@0 | 398 | if (e.data == 2) |
michael@0 | 399 | gEventSourceObj4_b.removeEventListener('message', fn_onmessage4_b, true); |
michael@0 | 400 | } |
michael@0 | 401 | |
michael@0 | 402 | function doTest4(test_id) { |
michael@0 | 403 | gEventSourceObj4_a = new EventSource("forRemoval.resource"); |
michael@0 | 404 | gEventSourceObj4_a.lastData = 0; |
michael@0 | 405 | gEventSourceObj4_a.onmessage = fn_onmessage4_a; |
michael@0 | 406 | |
michael@0 | 407 | setTimeout(function() { |
michael@0 | 408 | ok(gEventSourceObj4_a.lastData == 2, "Test 4.a failed"); |
michael@0 | 409 | gEventSourceObj4_a.close(); |
michael@0 | 410 | setTestHasFinished(test_id); |
michael@0 | 411 | }, parseInt(3000*stress_factor)); |
michael@0 | 412 | } |
michael@0 | 413 | |
michael@0 | 414 | function doTest4_b(test_id) |
michael@0 | 415 | { |
michael@0 | 416 | gEventSourceObj4_b = new EventSource("forRemoval.resource"); |
michael@0 | 417 | gEventSourceObj4_b.lastData = 0; |
michael@0 | 418 | gEventSourceObj4_b.addEventListener('message', fn_onmessage4_b, true); |
michael@0 | 419 | |
michael@0 | 420 | setTimeout(function() { |
michael@0 | 421 | ok(gEventSourceObj4_b.lastData == 2, "Test 4.b failed"); |
michael@0 | 422 | gEventSourceObj4_b.close(); |
michael@0 | 423 | setTestHasFinished(test_id); |
michael@0 | 424 | }, parseInt(3000*stress_factor)); |
michael@0 | 425 | } |
michael@0 | 426 | |
michael@0 | 427 | // in order to test (5) |
michael@0 | 428 | // a) valid access-control xsite request |
michael@0 | 429 | // b) invalid access-control xsite request |
michael@0 | 430 | // c) valid access-control xsite request on a restricted page with credentials |
michael@0 | 431 | // d) valid access-control xsite request on a restricted page without credentials |
michael@0 | 432 | // e) valid access-control xsite request on a restricted page when the parameter withCredentials is a getter |
michael@0 | 433 | // f) valid access-control xsite request on a restricted page when the parameter withCredentials is missing |
michael@0 | 434 | |
michael@0 | 435 | function doTest5(test_id) |
michael@0 | 436 | { |
michael@0 | 437 | gEventSourceObj5_a = new EventSource("http://example.org/tests/content/base/test/accesscontrol.resource"); |
michael@0 | 438 | |
michael@0 | 439 | gEventSourceObj5_a.onmessage = fn_onmessage; |
michael@0 | 440 | gEventSourceObj5_a.hits = []; |
michael@0 | 441 | gEventSourceObj5_a.hits['fn_onmessage'] = 0; |
michael@0 | 442 | |
michael@0 | 443 | setTimeout(function() { |
michael@0 | 444 | ok(gEventSourceObj5_a.hits['fn_onmessage'] != 0, "Test 5.a failed"); |
michael@0 | 445 | gEventSourceObj5_a.close(); |
michael@0 | 446 | setTestHasFinished(test_id); |
michael@0 | 447 | }, parseInt(3000*stress_factor)); |
michael@0 | 448 | } |
michael@0 | 449 | |
michael@0 | 450 | function doTest5_b(test_id) |
michael@0 | 451 | { |
michael@0 | 452 | gEventSourceObj5_b = new EventSource("http://example.org/tests/content/base/test/invalid_accesscontrol.resource"); |
michael@0 | 453 | |
michael@0 | 454 | gEventSourceObj5_b.onmessage = fn_onmessage; |
michael@0 | 455 | gEventSourceObj5_b.hits = []; |
michael@0 | 456 | gEventSourceObj5_b.hits['fn_onmessage'] = 0; |
michael@0 | 457 | |
michael@0 | 458 | setTimeout(function() { |
michael@0 | 459 | ok(gEventSourceObj5_b.hits['fn_onmessage'] == 0, "Test 5.b failed"); |
michael@0 | 460 | gEventSourceObj5_b.close(); |
michael@0 | 461 | setTestHasFinished(test_id); |
michael@0 | 462 | }, parseInt(3000*stress_factor)); |
michael@0 | 463 | } |
michael@0 | 464 | |
michael@0 | 465 | function doTest5_c(test_id) |
michael@0 | 466 | { |
michael@0 | 467 | // credentials using the auth cache |
michael@0 | 468 | var xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true}); |
michael@0 | 469 | xhr.withCredentials = true; |
michael@0 | 470 | // also, test mixed mode UI |
michael@0 | 471 | xhr.open("GET", "https://example.com/tests/content/base/test/file_restrictedEventSource.sjs?test=user1_xhr", true, "user 1", "password 1"); |
michael@0 | 472 | xhr.send(); |
michael@0 | 473 | xhr.onloadend = function() { |
michael@0 | 474 | ok(xhr.status == 200, "Failed to set credentials in test 5.c"); |
michael@0 | 475 | |
michael@0 | 476 | gEventSourceObj5_c = new EventSource("https://example.com/tests/content/base/test/file_restrictedEventSource.sjs?test=user1_evtsrc", |
michael@0 | 477 | { withCredentials: true } ); |
michael@0 | 478 | ok(gEventSourceObj5_c.withCredentials, "Wrong withCredentials in test 5.c"); |
michael@0 | 479 | |
michael@0 | 480 | gEventSourceObj5_c.onmessage = function(e) { |
michael@0 | 481 | ok(e.origin == "https://example.com", "Wrong Origin in test 5.c"); |
michael@0 | 482 | fn_onmessage(e); |
michael@0 | 483 | }; |
michael@0 | 484 | gEventSourceObj5_c.hits = []; |
michael@0 | 485 | gEventSourceObj5_c.hits['fn_onmessage'] = 0; |
michael@0 | 486 | |
michael@0 | 487 | setTimeout(function() { |
michael@0 | 488 | ok(gEventSourceObj5_c.hits['fn_onmessage'] > 0, "Test 5.c failed"); |
michael@0 | 489 | gEventSourceObj5_c.close(); |
michael@0 | 490 | doTest5_d(test_id); |
michael@0 | 491 | }, parseInt(3000*stress_factor)); |
michael@0 | 492 | }; |
michael@0 | 493 | } |
michael@0 | 494 | |
michael@0 | 495 | function doTest5_d(test_id) |
michael@0 | 496 | { |
michael@0 | 497 | var xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true}); |
michael@0 | 498 | xhr.withCredentials = true; |
michael@0 | 499 | xhr.open("GET", "https://example.com/tests/content/base/test/file_restrictedEventSource.sjs?test=user2_xhr", true, "user 2", "password 2"); |
michael@0 | 500 | xhr.send(); |
michael@0 | 501 | xhr.onloadend = function() { |
michael@0 | 502 | ok(xhr.status == 200, "Failed to set credentials in test 5.d"); |
michael@0 | 503 | |
michael@0 | 504 | gEventSourceObj5_d = new EventSource("https://example.com/tests/content/base/test/file_restrictedEventSource.sjs?test=user2_evtsrc"); |
michael@0 | 505 | ok(!gEventSourceObj5_d.withCredentials, "Wrong withCredentials in test 5.d"); |
michael@0 | 506 | |
michael@0 | 507 | gEventSourceObj5_d.onmessage = function(e) { |
michael@0 | 508 | ok(e.origin == "https://example.com", "Wrong Origin in test 5.d"); |
michael@0 | 509 | fn_onmessage(e); |
michael@0 | 510 | }; |
michael@0 | 511 | gEventSourceObj5_d.hits = []; |
michael@0 | 512 | gEventSourceObj5_d.hits['fn_onmessage'] = 0; |
michael@0 | 513 | |
michael@0 | 514 | setTimeout(function() { |
michael@0 | 515 | ok(gEventSourceObj5_d.hits['fn_onmessage'] == 0, "Test 5.d failed"); |
michael@0 | 516 | gEventSourceObj5_d.close(); |
michael@0 | 517 | setTestHasFinished(test_id); |
michael@0 | 518 | }, parseInt(3000*stress_factor)); |
michael@0 | 519 | }; |
michael@0 | 520 | } |
michael@0 | 521 | |
michael@0 | 522 | function doTest5_e(test_id) |
michael@0 | 523 | { |
michael@0 | 524 | // credentials using the auth cache |
michael@0 | 525 | var xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true}); |
michael@0 | 526 | xhr.withCredentials = true; |
michael@0 | 527 | xhr.open("GET", "http://example.org/tests/content/base/test/file_restrictedEventSource.sjs?test=user1_xhr", true, "user 1", "password 1"); |
michael@0 | 528 | xhr.send(); |
michael@0 | 529 | xhr.onloadend = function() { |
michael@0 | 530 | ok(xhr.status == 200, "Failed to set credentials in test 5.e"); |
michael@0 | 531 | |
michael@0 | 532 | gEventSourceObj5_e = new EventSource("http://example.org/tests/content/base/test/file_restrictedEventSource.sjs?test=user1_evtsrc", |
michael@0 | 533 | { get withCredentials() { return true; } } ); |
michael@0 | 534 | ok(gEventSourceObj5_e.withCredentials, "Wrong withCredentials in test 5.e"); |
michael@0 | 535 | |
michael@0 | 536 | gEventSourceObj5_e.onmessage = function(e) { |
michael@0 | 537 | ok(e.origin == "http://example.org", "Wrong Origin in test 5.e"); |
michael@0 | 538 | fn_onmessage(e); |
michael@0 | 539 | }; |
michael@0 | 540 | gEventSourceObj5_e.hits = []; |
michael@0 | 541 | gEventSourceObj5_e.hits['fn_onmessage'] = 0; |
michael@0 | 542 | |
michael@0 | 543 | setTimeout(function() { |
michael@0 | 544 | ok(gEventSourceObj5_e.hits['fn_onmessage'] > 0, "Test 5.e failed"); |
michael@0 | 545 | gEventSourceObj5_e.close(); |
michael@0 | 546 | doTest5_f(test_id); |
michael@0 | 547 | }, parseInt(5000*stress_factor)); |
michael@0 | 548 | }; |
michael@0 | 549 | } |
michael@0 | 550 | |
michael@0 | 551 | function doTest5_f(test_id) |
michael@0 | 552 | { |
michael@0 | 553 | var xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true}); |
michael@0 | 554 | xhr.withCredentials = true; |
michael@0 | 555 | xhr.open("GET", "http://example.org/tests/content/base/test/file_restrictedEventSource.sjs?test=user2_xhr", true, "user 2", "password 2"); |
michael@0 | 556 | xhr.send(); |
michael@0 | 557 | xhr.onloadend = function() { |
michael@0 | 558 | ok(xhr.status == 200, "Failed to set credentials in test 5.f"); |
michael@0 | 559 | |
michael@0 | 560 | gEventSourceObj5_f = new EventSource("http://example.org/tests/content/base/test/file_restrictedEventSource.sjs?test=user2_evtsrc", |
michael@0 | 561 | { }); |
michael@0 | 562 | ok(!gEventSourceObj5_f.withCredentials, "Wrong withCredentials in test 5.f"); |
michael@0 | 563 | |
michael@0 | 564 | gEventSourceObj5_f.onmessage = function(e) { |
michael@0 | 565 | ok(e.origin == "http://example.org", "Wrong Origin in test 5.f"); |
michael@0 | 566 | fn_onmessage(e); |
michael@0 | 567 | }; |
michael@0 | 568 | gEventSourceObj5_f.hits = []; |
michael@0 | 569 | gEventSourceObj5_f.hits['fn_onmessage'] = 0; |
michael@0 | 570 | |
michael@0 | 571 | setTimeout(function() { |
michael@0 | 572 | ok(gEventSourceObj5_f.hits['fn_onmessage'] == 0, "Test 5.f failed"); |
michael@0 | 573 | gEventSourceObj5_f.close(); |
michael@0 | 574 | setTestHasFinished(test_id); |
michael@0 | 575 | }, parseInt(3000*stress_factor)); |
michael@0 | 576 | }; |
michael@0 | 577 | } |
michael@0 | 578 | |
michael@0 | 579 | function doTest6(test_id) |
michael@0 | 580 | { |
michael@0 | 581 | gEventSourceObj6 = new EventSource("somedatas.resource"); |
michael@0 | 582 | var fn_somedata = function(e) { |
michael@0 | 583 | if (fn_somedata.expected == 0) { |
michael@0 | 584 | ok(e.data == "123456789\n123456789123456789\n123456789123456789123456789123456789\n 123456789123456789123456789123456789123456789123456789123456789123456789\nçãá\"\'@`~Ý Ḿyyyy", |
michael@0 | 585 | "Test 6.a failed"); |
michael@0 | 586 | } else if (fn_somedata.expected == 1) { |
michael@0 | 587 | ok(e.data == " :xxabcdefghij\nçãá\"\'@`~Ý Ḿyyyy : zz", |
michael@0 | 588 | "Test 6.b failed"); |
michael@0 | 589 | gEventSourceObj6.close(); |
michael@0 | 590 | } else { |
michael@0 | 591 | ok(false, "Test 6 failed (unexpected message event)"); |
michael@0 | 592 | } |
michael@0 | 593 | fn_somedata.expected++; |
michael@0 | 594 | } |
michael@0 | 595 | fn_somedata.expected = 0; |
michael@0 | 596 | gEventSourceObj6.onmessage = fn_somedata; |
michael@0 | 597 | |
michael@0 | 598 | setTimeout(function() { |
michael@0 | 599 | gEventSourceObj6.close(); |
michael@0 | 600 | setTestHasFinished(test_id); |
michael@0 | 601 | }, parseInt(2500*stress_factor)); |
michael@0 | 602 | } |
michael@0 | 603 | |
michael@0 | 604 | function doTest7(test_id) |
michael@0 | 605 | { |
michael@0 | 606 | gEventSourceObj7 = new EventSource("delayedServerEvents.sjs"); |
michael@0 | 607 | gEventSourceObj7.msg_received = []; |
michael@0 | 608 | gEventSourceObj7.onmessage = function(e) |
michael@0 | 609 | { |
michael@0 | 610 | e.target.msg_received.push(e.data); |
michael@0 | 611 | } |
michael@0 | 612 | |
michael@0 | 613 | setTimeout(function() { |
michael@0 | 614 | gEventSourceObj7.close(); |
michael@0 | 615 | |
michael@0 | 616 | ok(gEventSourceObj7.msg_received[0] == "" && |
michael@0 | 617 | gEventSourceObj7.msg_received[1] == "delayed1" && |
michael@0 | 618 | gEventSourceObj7.msg_received[2] == "delayed2", "Test 7 failed"); |
michael@0 | 619 | |
michael@0 | 620 | document.getElementById('waitSpan').innerHTML = ''; |
michael@0 | 621 | setTestHasFinished(test_id); |
michael@0 | 622 | }, parseInt(8000*stress_factor)); |
michael@0 | 623 | } |
michael@0 | 624 | |
michael@0 | 625 | function doTest() |
michael@0 | 626 | { |
michael@0 | 627 | // Allow all cookies, then run the actual test |
michael@0 | 628 | SpecialPowers.pushPrefEnv({"set": [["network.cookie.cookieBehavior", 0], ["dom.server-events.enabled", true]]}, function() { SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], doTestCallback);}); |
michael@0 | 629 | } |
michael@0 | 630 | |
michael@0 | 631 | function doTestCallback() |
michael@0 | 632 | { |
michael@0 | 633 | |
michael@0 | 634 | // we get a good stress_factor by testing 10 setTimeouts and some float |
michael@0 | 635 | // arithmetic taking my machine as stress_factor==1 (time=589) |
michael@0 | 636 | |
michael@0 | 637 | var begin_time = (new Date()).getTime(); |
michael@0 | 638 | |
michael@0 | 639 | var f = function() { |
michael@0 | 640 | for (var j=0; j<f.i; ++j) |
michael@0 | 641 | eval("Math.log(Math.atan(Math.sqrt(Math.pow(3.1415, 13.1415))/0.0007))"); |
michael@0 | 642 | if (f.i < 10) { |
michael@0 | 643 | f.i++; |
michael@0 | 644 | setTimeout(f, 10 + 10*f.i); |
michael@0 | 645 | } else { |
michael@0 | 646 | stress_factor = ((new Date()).getTime()-begin_time)*1/589; |
michael@0 | 647 | stress_factor *= 1.10; // also, a margin of 10% |
michael@0 | 648 | |
michael@0 | 649 | runAllTests(); |
michael@0 | 650 | } |
michael@0 | 651 | } |
michael@0 | 652 | f.i = 0; |
michael@0 | 653 | |
michael@0 | 654 | setTimeout(f, 10); |
michael@0 | 655 | } |
michael@0 | 656 | |
michael@0 | 657 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 658 | addLoadEvent(doTest); |
michael@0 | 659 | |
michael@0 | 660 | </script> |
michael@0 | 661 | </pre> |
michael@0 | 662 | <span id=waitSpan>Wait please...</span> |
michael@0 | 663 | </body> |
michael@0 | 664 | </html> |
michael@0 | 665 |