OLD | NEW |
(Empty) | |
| 1 function registration_tests(register_method, check_error_types) { |
| 2 // The navigator.serviceWorker.register() method guarantees that the newly |
| 3 // installing worker is available as registration.installing when its promise |
| 4 // resolves. However these tests are also used to test installation using a |
| 5 // <link> element where it is possible for the installing worker to have |
| 6 // already become the waiting or active worker. So This method is used to get |
| 7 // the newest worker when these tests need access to the ServiceWorker itself. |
| 8 function get_newest_worker(registration) { |
| 9 if (registration.installing) |
| 10 return registration.installing; |
| 11 if (registration.waiting) |
| 12 return registration.waiting; |
| 13 if (registration.active) |
| 14 return registration.active; |
| 15 } |
| 16 |
| 17 promise_test(function(t) { |
| 18 var script = 'resources/registration-worker.js'; |
| 19 var scope = 'resources/registration/normal'; |
| 20 return register_method(script, {scope: scope}) |
| 21 .then(function(registration) { |
| 22 assert_equals(registration.constructor.name, |
| 23 'ServiceWorkerRegistration', |
| 24 'Successfully registered.'); |
| 25 service_worker_unregister_and_done(t, scope); |
| 26 }) |
| 27 }, 'Registering normal scope'); |
| 28 |
| 29 promise_test(function(t) { |
| 30 var script = 'resources/registration-worker.js'; |
| 31 var scope = 'resources/registration/scope-with-fragment#ref'; |
| 32 return register_method(script, {scope: scope}) |
| 33 .then(function(registration) { |
| 34 assert_equals(registration.constructor.name, |
| 35 'ServiceWorkerRegistration', |
| 36 'Successfully registered.'); |
| 37 assert_equals( |
| 38 registration.scope, |
| 39 normalizeURL('resources/registration/scope-with-fragment'), |
| 40 'A fragment should be removed from scope') |
| 41 service_worker_unregister_and_done(t, scope); |
| 42 }) |
| 43 }, 'Registering scope with fragment'); |
| 44 |
| 45 promise_test(function(t) { |
| 46 var script = 'resources/registration-worker.js'; |
| 47 var scope = 'resources/'; |
| 48 return register_method(script, {scope: scope}) |
| 49 .then(function(registration) { |
| 50 assert_equals(registration.constructor.name, |
| 51 'ServiceWorkerRegistration', |
| 52 'Successfully registered.'); |
| 53 service_worker_unregister_and_done(t, scope); |
| 54 }) |
| 55 }, 'Registering same scope as the script directory'); |
| 56 |
| 57 promise_test(function(t) { |
| 58 var script = 'resources/registration-worker.js'; |
| 59 var scope = 'resources'; |
| 60 return assert_promise_rejects( |
| 61 register_method(script, {scope: scope}), |
| 62 check_error_types ? 'SecurityError' : null, |
| 63 'Registering same scope as the script directory without the last ' + |
| 64 'slash should fail with SecurityError.'); |
| 65 }, 'Registering same scope as the script directory without the last slash'); |
| 66 |
| 67 promise_test(function(t) { |
| 68 var script = 'resources/registration-worker.js'; |
| 69 var scope = 'different-directory/'; |
| 70 return assert_promise_rejects( |
| 71 register_method(script, {scope: scope}), |
| 72 check_error_types ? 'SecurityError' : null, |
| 73 'Registration scope outside the script directory should fail ' + |
| 74 'with SecurityError.'); |
| 75 }, 'Registration scope outside the script directory'); |
| 76 |
| 77 promise_test(function(t) { |
| 78 var script = 'resources/registration-worker.js'; |
| 79 var scope = 'http://example.com/'; |
| 80 return assert_promise_rejects( |
| 81 register_method(script, {scope: scope}), |
| 82 check_error_types ? 'SecurityError' : null, |
| 83 'Registration scope outside domain should fail with SecurityError.'); |
| 84 }, 'Registering scope outside domain'); |
| 85 |
| 86 promise_test(function(t) { |
| 87 var script = 'http://example.com/worker.js'; |
| 88 var scope = 'http://example.com/scope/'; |
| 89 return assert_promise_rejects( |
| 90 register_method(script, {scope: scope}), |
| 91 check_error_types ? 'SecurityError' : null, |
| 92 'Registration script outside domain should fail with SecurityError.'); |
| 93 }, 'Registering script outside domain'); |
| 94 |
| 95 promise_test(function(t) { |
| 96 var script = 'resources/no-such-worker.js'; |
| 97 var scope = 'resources/scope/no-such-worker'; |
| 98 return assert_promise_rejects( |
| 99 register_method(script, {scope: scope}), |
| 100 check_error_types ? 'NetworkError' : null, |
| 101 navigator.serviceWorker.register(script, {scope: scope}), |
| 102 'Registration of non-existent script should fail.'); |
| 103 }, 'Registering non-existent script'); |
| 104 |
| 105 promise_test(function(t) { |
| 106 var script = 'resources/invalid-chunked-encoding.php'; |
| 107 var scope = 'resources/scope/invalid-chunked-encoding/'; |
| 108 return assert_promise_rejects( |
| 109 register_method(script, {scope: scope}), |
| 110 check_error_types ? 'NetworkError' : null, |
| 111 'Registration of invalid chunked encoding script should fail.'); |
| 112 }, 'Registering invalid chunked encoding script'); |
| 113 |
| 114 promise_test(function(t) { |
| 115 var script = 'resources/invalid-chunked-encoding-with-flush.php'; |
| 116 var scope = 'resources/scope/invalid-chunked-encoding-with-flush/'; |
| 117 return assert_promise_rejects( |
| 118 register_method(script, {scope: scope}), |
| 119 check_error_types ? 'NetworkError' : null, |
| 120 'Registration of invalid chunked encoding script should fail.'); |
| 121 }, 'Registering invalid chunked encoding script with flush'); |
| 122 |
| 123 promise_test(function(t) { |
| 124 var script = 'resources/mime-type-worker.php'; |
| 125 var scope = 'resources/scope/no-mime-type-worker/'; |
| 126 return assert_promise_rejects( |
| 127 register_method(script, {scope: scope}), |
| 128 check_error_types ? 'SecurityError' : null, |
| 129 'Registration of no MIME type script should fail.'); |
| 130 }, 'Registering script with no MIME type'); |
| 131 |
| 132 promise_test(function(t) { |
| 133 var script = 'resources/mime-type-worker.php?mime=text/plain'; |
| 134 var scope = 'resources/scope/bad-mime-type-worker/'; |
| 135 return assert_promise_rejects( |
| 136 register_method(script, {scope: scope}), |
| 137 check_error_types ? 'SecurityError' : null, |
| 138 'Registration of plain text script should fail.'); |
| 139 }, 'Registering script with bad MIME type'); |
| 140 |
| 141 promise_test(function(t) { |
| 142 var script = 'resources/redirect.php?Redirect=' + |
| 143 encodeURIComponent('/resources/registration-worker.js'); |
| 144 var scope = 'resources/scope/redirect/'; |
| 145 return assert_promise_rejects( |
| 146 register_method(script, {scope: scope}), |
| 147 check_error_types ? 'SecurityError' : null, |
| 148 'Registration of redirected script should fail.'); |
| 149 }, 'Registering redirected script'); |
| 150 |
| 151 promise_test(function(t) { |
| 152 var script = 'resources/malformed-worker.php?parse-error'; |
| 153 var scope = 'resources/scope/parse-error'; |
| 154 return assert_promise_rejects( |
| 155 register_method(script, {scope: scope}), |
| 156 check_error_types ? 'AbortError' : null, |
| 157 'Registration of script including parse error should fail.'); |
| 158 }, 'Registering script including parse error'); |
| 159 |
| 160 promise_test(function(t) { |
| 161 var script = 'resources/malformed-worker.php?undefined-error'; |
| 162 var scope = 'resources/scope/undefined-error'; |
| 163 return assert_promise_rejects( |
| 164 register_method(script, {scope: scope}), |
| 165 check_error_types ? 'AbortError' : null, |
| 166 'Registration of script including undefined error should fail.'); |
| 167 }, 'Registering script including undefined error'); |
| 168 |
| 169 promise_test(function(t) { |
| 170 var script = 'resources/malformed-worker.php?uncaught-exception'; |
| 171 var scope = 'resources/scope/uncaught-exception'; |
| 172 return assert_promise_rejects( |
| 173 register_method(script, {scope: scope}), |
| 174 check_error_types ? 'AbortError' : null, |
| 175 'Registration of script including uncaught exception should fail.'); |
| 176 }, 'Registering script including uncaught exception'); |
| 177 |
| 178 promise_test(function(t) { |
| 179 var script = 'resources/malformed-worker.php?caught-exception'; |
| 180 var scope = 'resources/scope/caught-exception'; |
| 181 return register_method(script, {scope: scope}) |
| 182 .then(function(registration) { |
| 183 assert_equals(registration.constructor.name, |
| 184 'ServiceWorkerRegistration', |
| 185 'Successfully registered.'); |
| 186 service_worker_unregister_and_done(t, scope); |
| 187 }) |
| 188 }, 'Registering script including caught exception'); |
| 189 |
| 190 promise_test(function(t) { |
| 191 var script = 'resources/malformed-worker.php?import-malformed-script'; |
| 192 var scope = 'resources/scope/import-malformed-script'; |
| 193 return assert_promise_rejects( |
| 194 register_method(script, {scope: scope}), |
| 195 check_error_types ? 'AbortError' : null, |
| 196 'Registration of script importing malformed script should fail.'); |
| 197 }, 'Registering script importing malformed script'); |
| 198 |
| 199 promise_test(function(t) { |
| 200 var script = 'resources/malformed-worker.php?import-no-such-script'; |
| 201 var scope = 'resources/scope/import-no-such-script'; |
| 202 return assert_promise_rejects( |
| 203 register_method(script, {scope: scope}), |
| 204 check_error_types ? 'AbortError' : null, |
| 205 'Registration of script importing non-existent script should fail.'); |
| 206 }, 'Registering script importing non-existent script'); |
| 207 |
| 208 promise_test(function(t) { |
| 209 // URL-encoded full-width 'scope'. |
| 210 var name = '%ef%bd%93%ef%bd%83%ef%bd%8f%ef%bd%90%ef%bd%85'; |
| 211 var script = 'resources/empty-worker.js'; |
| 212 var scope = 'resources/' + name + '/escaped-multibyte-character-scope'; |
| 213 return register_method(script, {scope: scope}) |
| 214 .then(function(registration) { |
| 215 assert_equals( |
| 216 registration.scope, |
| 217 normalizeURL(scope), |
| 218 'URL-encoded multibyte characters should be available.'); |
| 219 service_worker_unregister_and_done(t, scope); |
| 220 }); |
| 221 }, 'Scope including URL-encoded multibyte characters'); |
| 222 |
| 223 promise_test(function(t) { |
| 224 // Non-URL-encoded full-width "scope". |
| 225 var name = String.fromCodePoint(0xff53, 0xff43, 0xff4f, 0xff50, 0xff45); |
| 226 var script = 'resources/empty-worker.js'; |
| 227 var scope = 'resources/' + name + '/non-escaped-multibyte-character-scope
'; |
| 228 return register_method(script, {scope: scope}) |
| 229 .then(function(registration) { |
| 230 assert_equals( |
| 231 registration.scope, |
| 232 normalizeURL(scope), |
| 233 'Non-URL-encoded multibyte characters should be available.'); |
| 234 service_worker_unregister_and_done(t, scope); |
| 235 }); |
| 236 }, 'Scope including non-escaped multibyte characters'); |
| 237 |
| 238 promise_test(function(t) { |
| 239 var script = 'resources%2fempty-worker.js'; |
| 240 var scope = 'resources/scope/encoded-slash-in-script-url'; |
| 241 return assert_promise_rejects( |
| 242 register_method(script, {scope: scope}), |
| 243 check_error_types ? new TypeError : null, |
| 244 'URL-encoded slash in the script URL should be rejected.'); |
| 245 }, 'Script URL including URL-encoded slash'); |
| 246 |
| 247 promise_test(function(t) { |
| 248 var script = 'resources%2Fempty-worker.js'; |
| 249 var scope = 'resources/scope/encoded-slash-in-script-url'; |
| 250 return assert_promise_rejects( |
| 251 register_method(script, {scope: scope}), |
| 252 check_error_types ? new TypeError : null, |
| 253 'URL-encoded slash in the script URL should be rejected.'); |
| 254 }, 'Script URL including uppercase URL-encoded slash'); |
| 255 |
| 256 promise_test(function(t) { |
| 257 var script = 'resources/empty-worker.js'; |
| 258 var scope = 'resources/scope%2fencoded-slash-in-scope'; |
| 259 return assert_promise_rejects( |
| 260 register_method(script, {scope: scope}), |
| 261 check_error_types ? new TypeError : null, |
| 262 'URL-encoded slash in the scope should be rejected.'); |
| 263 }, 'Scope including URL-encoded slash'); |
| 264 |
| 265 promise_test(function(t) { |
| 266 var script = 'resources%5cempty-worker.js'; |
| 267 var scope = 'resources/scope/encoded-slash-in-script-url'; |
| 268 return assert_promise_rejects( |
| 269 register_method(script, {scope: scope}), |
| 270 check_error_types ? new TypeError : null, |
| 271 'URL-encoded backslash in the script URL should be rejected.'); |
| 272 }, 'Script URL including URL-encoded backslash'); |
| 273 |
| 274 promise_test(function(t) { |
| 275 var script = 'resources%5Cempty-worker.js'; |
| 276 var scope = 'resources/scope/encoded-slash-in-script-url'; |
| 277 return assert_promise_rejects( |
| 278 register_method(script, {scope: scope}), |
| 279 check_error_types ? new TypeError : null, |
| 280 'URL-encoded backslash in the script URL should be rejected.'); |
| 281 }, 'Script URL including uppercase URL-encoded backslash'); |
| 282 |
| 283 promise_test(function(t) { |
| 284 var script = 'resources/empty-worker.js'; |
| 285 var scope = 'resources/scope%5cencoded-slash-in-scope'; |
| 286 return assert_promise_rejects( |
| 287 register_method(script, {scope: scope}), |
| 288 check_error_types ? new TypeError : null, |
| 289 'URL-encoded backslash in the scope should be rejected.'); |
| 290 }, 'Scope including URL-encoded backslash'); |
| 291 |
| 292 promise_test(function(t) { |
| 293 var script = 'resources/././empty-worker.js'; |
| 294 var scope = 'resources/scope/parent-reference-in-script-url'; |
| 295 return register_method(script, {scope: scope}) |
| 296 .then(function(registration) { |
| 297 assert_equals( |
| 298 get_newest_worker(registration).scriptURL, |
| 299 normalizeURL('resources/empty-worker.js'), |
| 300 'Script URL including self-reference should be normalized.'); |
| 301 service_worker_unregister_and_done(t, scope); |
| 302 }); |
| 303 }, 'Script URL including self-reference'); |
| 304 |
| 305 promise_test(function(t) { |
| 306 var script = 'resources/empty-worker.js'; |
| 307 var scope = 'resources/././scope/self-reference-in-scope'; |
| 308 return register_method(script, {scope: scope}) |
| 309 .then(function(registration) { |
| 310 assert_equals( |
| 311 registration.scope, |
| 312 normalizeURL('resources/scope/self-reference-in-scope'), |
| 313 'Scope including self-reference should be normalized.'); |
| 314 service_worker_unregister_and_done(t, scope); |
| 315 }); |
| 316 }, 'Scope including self-reference'); |
| 317 |
| 318 promise_test(function(t) { |
| 319 var script = 'resources/../resources/empty-worker.js'; |
| 320 var scope = 'resources/scope/parent-reference-in-script-url'; |
| 321 return register_method(script, {scope: scope}) |
| 322 .then(function(registration) { |
| 323 assert_equals( |
| 324 get_newest_worker(registration).scriptURL, |
| 325 normalizeURL('resources/empty-worker.js'), |
| 326 'Script URL including parent-reference should be normalized.'); |
| 327 service_worker_unregister_and_done(t, scope); |
| 328 }); |
| 329 }, 'Script URL including parent-reference'); |
| 330 |
| 331 promise_test(function(t) { |
| 332 var script = 'resources/empty-worker.js'; |
| 333 var scope = 'resources/../resources/scope/parent-reference-in-scope'; |
| 334 return register_method(script, {scope: scope}) |
| 335 .then(function(registration) { |
| 336 assert_equals( |
| 337 registration.scope, |
| 338 normalizeURL('resources/scope/parent-reference-in-scope'), |
| 339 'Scope including parent-reference should be normalized.'); |
| 340 service_worker_unregister_and_done(t, scope); |
| 341 }); |
| 342 }, 'Scope including parent-reference'); |
| 343 |
| 344 promise_test(function(t) { |
| 345 var script = 'resources/empty-worker.js'; |
| 346 var scope = 'resources/../scope/parent-reference-in-scope'; |
| 347 return assert_promise_rejects( |
| 348 register_method(script, {scope: scope}), |
| 349 check_error_types ? 'SecurityError' : null, |
| 350 'Scope not under the script directory should be rejected.'); |
| 351 }, 'Scope including parent-reference and not under the script directory'); |
| 352 |
| 353 promise_test(function(t) { |
| 354 var script = 'resources////empty-worker.js'; |
| 355 var scope = 'resources/scope/consecutive-slashes-in-script-url'; |
| 356 return assert_promise_rejects( |
| 357 register_method(script, {scope: scope}), |
| 358 check_error_types ? 'SecurityError' : null, |
| 359 'Consecutive slashes in the script url should not be unified.'); |
| 360 }, 'Script URL including consecutive slashes'); |
| 361 |
| 362 promise_test(function(t) { |
| 363 var script = 'resources/empty-worker.js'; |
| 364 var scope = 'resources/scope////consecutive-slashes-in-scope'; |
| 365 return register_method(script, {scope: scope}) |
| 366 .then(function(registration) { |
| 367 // Although consecutive slashes in the scope are not unified, the |
| 368 // scope is under the script directory and registration should |
| 369 // succeed. |
| 370 assert_equals( |
| 371 registration.scope, |
| 372 normalizeURL(scope), |
| 373 'Should successfully be registered.'); |
| 374 service_worker_unregister_and_done(t, scope); |
| 375 }) |
| 376 }, 'Scope including consecutive slashes'); |
| 377 |
| 378 promise_test(function(t) { |
| 379 var script = 'filesystem:' + normalizeURL('resources/empty-worker.js'); |
| 380 var scope = 'resources/scope/filesystem-script-url'; |
| 381 return assert_promise_rejects( |
| 382 register_method(script, {scope: scope}), |
| 383 check_error_types ? 'SecurityError' : null, |
| 384 'Registering a script which has same-origin filesystem: URL should ' + |
| 385 'fail with SecurityError.'); |
| 386 }, 'Script URL is same-origin filesystem: URL'); |
| 387 |
| 388 promise_test(function(t) { |
| 389 var script = 'resources/empty-worker.js'; |
| 390 var scope = 'filesystem:' + normalizeURL('resources/scope/filesystem-scope
-url'); |
| 391 return assert_promise_rejects( |
| 392 register_method(script, {scope: scope}), |
| 393 check_error_types ? 'SecurityError' : null, |
| 394 'Registering with the scope that has same-origin filesystem: URL ' + |
| 395 'should fail with SecurityError.'); |
| 396 }, 'Scope URL is same-origin filesystem: URL'); |
| 397 } |
OLD | NEW |