summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/registration.https.html
blob: ae9f85fb27c5e9fa7b4e89d3a9f2518429e342c3 (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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<!DOCTYPE html>
<title>Service Worker: Registration</title>
<script src="/resources/testharness.js"></script>
<script src="resources/testharness-helpers.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>

promise_test(function(t) {
    var script = 'resources/registration-worker.js';
    var scope = 'resources/registration/normal';
    return navigator.serviceWorker.register(script, {scope: scope})
      .then(function(registration) {
          assert_true(registration instanceof ServiceWorkerRegistration,
                      'Successfully registered.');
          service_worker_unregister_and_done(t, scope);
        })
  }, 'Registering normal scope');

promise_test(function(t) {
    var script = 'resources/registration-worker.js';
    var scope = 'resources/registration/scope-with-fragment#ref';
    return navigator.serviceWorker.register(script, {scope: scope})
      .then(function(registration) {
          assert_true(
            registration instanceof ServiceWorkerRegistration,
            'Successfully registered.');
          assert_equals(
            registration.scope,
            normalizeURL('resources/registration/scope-with-fragment'),
            'A fragment should be removed from scope')
          service_worker_unregister_and_done(t, scope);
        })
  }, 'Registering scope with fragment');

promise_test(function(t) {
    var script = 'resources/registration-worker.js';
    var scope = 'resources/';
    return navigator.serviceWorker.register(script, {scope: scope})
      .then(function(registration) {
          assert_true(registration instanceof ServiceWorkerRegistration,
                      'Successfully registered.');
          service_worker_unregister_and_done(t, scope);
        })
  }, 'Registering same scope as the script directory');

promise_test(function(t) {
    var script = 'resources/registration-worker.js';
    var scope = 'resources';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        'SecurityError',
        'Registering same scope as the script directory without the last ' +
            'slash should fail with SecurityError.');
  }, 'Registering same scope as the script directory without the last slash');

promise_test(function(t) {
    var script = 'resources/registration-worker.js';
    var scope = 'different-directory/';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        'SecurityError',
        'Registration scope outside the script directory should fail ' +
            'with SecurityError.');
  }, 'Registration scope outside the script directory');

promise_test(function(t) {
    var script = 'resources/registration-worker.js';
    var scope = 'http://example.com/';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        'SecurityError',
        'Registration scope outside domain should fail with SecurityError.');
  }, 'Registering scope outside domain');

promise_test(function(t) {
    var script = 'http://example.com/worker.js';
    var scope = 'http://example.com/scope/';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        'SecurityError',
        'Registration script outside domain should fail with SecurityError.');
  }, 'Registering script outside domain');

promise_test(function(t) {
    var script = 'resources/no-such-worker.js';
    var scope = 'resources/scope/no-such-worker';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        new TypeError(),
        'Registration of non-existent script should fail.');
  }, 'Registering non-existent script');

promise_test(function(t) {
    var script = 'resources/invalid-chunked-encoding.py';
    var scope = 'resources/scope/invalid-chunked-encoding/';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        new TypeError(),
        'Registration of invalid chunked encoding script should fail.');
  }, 'Registering invalid chunked encoding script');

promise_test(function(t) {
    var script = 'resources/invalid-chunked-encoding-with-flush.py';
    var scope = 'resources/scope/invalid-chunked-encoding-with-flush/';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        new TypeError(),
        'Registration of invalid chunked encoding script should fail.');
  }, 'Registering invalid chunked encoding script with flush');

promise_test(function(t) {
    var script = 'resources/mime-type-worker.py';
    var scope = 'resources/scope/no-mime-type-worker/';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        'SecurityError',
        'Registration of no MIME type script should fail.');
  }, 'Registering script with no MIME type');

promise_test(function(t) {
    var script = 'resources/mime-type-worker.py?mime=text/plain';
    var scope = 'resources/scope/bad-mime-type-worker/';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        'SecurityError',
        'Registration of plain text script should fail.');
  }, 'Registering script with bad MIME type');

promise_test(function(t) {
    var script = 'resources/redirect.py?Redirect=' +
                  encodeURIComponent('/resources/registration-worker.js');
    var scope = 'resources/scope/redirect/';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        'SecurityError',
        'Registration of redirected script should fail.');
  }, 'Registering redirected script');

promise_test(function(t) {
    var script = 'resources/malformed-worker.py?parse-error';
    var scope = 'resources/scope/parse-error';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        new TypeError(),
        'Registration of script including parse error should fail.');
  }, 'Registering script including parse error');

promise_test(function(t) {
    var script = 'resources/malformed-worker.py?undefined-error';
    var scope = 'resources/scope/undefined-error';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        new TypeError(),
        'Registration of script including undefined error should fail.');
  }, 'Registering script including undefined error');

promise_test(function(t) {
    var script = 'resources/malformed-worker.py?uncaught-exception';
    var scope = 'resources/scope/uncaught-exception';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        new TypeError(),
        'Registration of script including uncaught exception should fail.');
  }, 'Registering script including uncaught exception');

promise_test(function(t) {
    var script = 'resources/malformed-worker.py?caught-exception';
    var scope = 'resources/scope/caught-exception';
    return navigator.serviceWorker.register(script, {scope: scope})
        .then(function(registration) {
            assert_true(registration instanceof ServiceWorkerRegistration,
                        'Successfully registered.');
            service_worker_unregister_and_done(t, scope);
          })
  }, 'Registering script including caught exception');

promise_test(function(t) {
    var script = 'resources/malformed-worker.py?import-malformed-script';
    var scope = 'resources/scope/import-malformed-script';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        new TypeError(),
        'Registration of script importing malformed script should fail.');
  }, 'Registering script importing malformed script');

promise_test(function(t) {
    var script = 'resources/malformed-worker.py?import-no-such-script';
    var scope = 'resources/scope/import-no-such-script';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        new TypeError(),
        'Registration of script importing non-existent script should fail.');
  }, 'Registering script importing non-existent script');

promise_test(function(t) {
    // URL-encoded full-width 'scope'.
    var name = '%ef%bd%93%ef%bd%83%ef%bd%8f%ef%bd%90%ef%bd%85';
    var script = 'resources/empty-worker.js';
    var scope = 'resources/' + name + '/escaped-multibyte-character-scope';
    return navigator.serviceWorker.register(script, {scope: scope})
      .then(function(registration) {
          assert_equals(
            registration.scope,
            normalizeURL(scope),
            'URL-encoded multibyte characters should be available.');
          service_worker_unregister_and_done(t, scope);
        });
  }, 'Scope including URL-encoded multibyte characters');

promise_test(function(t) {
    // Non-URL-encoded full-width "scope".
    var name = String.fromCodePoint(0xff53, 0xff43, 0xff4f, 0xff50, 0xff45);
    var script = 'resources/empty-worker.js';
    var scope = 'resources/' + name  + '/non-escaped-multibyte-character-scope';
    return navigator.serviceWorker.register(script, {scope: scope})
      .then(function(registration) {
          assert_equals(
            registration.scope,
            normalizeURL(scope),
            'Non-URL-encoded multibyte characters should be available.');
          service_worker_unregister_and_done(t, scope);
        });
  }, 'Scope including non-escaped multibyte characters');

promise_test(function(t) {
    var script = 'resources%2fempty-worker.js';
    var scope = 'resources/scope/encoded-slash-in-script-url';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        new TypeError(),
        'URL-encoded slash in the script URL should be rejected.');
  }, 'Script URL including URL-encoded slash');

promise_test(function(t) {
    var script = 'resources/empty-worker.js';
    var scope = 'resources/scope%2fencoded-slash-in-scope';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        new TypeError(),
        'URL-encoded slash in the scope should be rejected.');
  }, 'Scope including URL-encoded slash');

promise_test(function(t) {
    var script = 'resources%5cempty-worker.js';
    var scope = 'resources/scope/encoded-slash-in-script-url';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        new TypeError(),
        'URL-encoded backslash in the script URL should be rejected.');
  }, 'Script URL including URL-encoded backslash');

promise_test(function(t) {
    var script = 'resources/empty-worker.js';
    var scope = 'resources/scope%5cencoded-slash-in-scope';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        new TypeError(),
        'URL-encoded backslash in the scope should be rejected.');
  }, 'Scope including URL-encoded backslash');

promise_test(function(t) {
    var script = 'resources/././empty-worker.js';
    var scope = 'resources/scope/parent-reference-in-script-url';
    return navigator.serviceWorker.register(script, {scope: scope})
      .then(function(registration) {
          assert_equals(
            registration.installing.scriptURL,
            normalizeURL('resources/empty-worker.js'),
            'Script URL including self-reference should be normalized.');
          service_worker_unregister_and_done(t, scope);
        });
  }, 'Script URL including self-reference');

promise_test(function(t) {
    var script = 'resources/empty-worker.js';
    var scope = 'resources/././scope/self-reference-in-scope';
    return navigator.serviceWorker.register(script, {scope: scope})
      .then(function(registration) {
          assert_equals(
            registration.scope,
            normalizeURL('resources/scope/self-reference-in-scope'),
            'Scope including self-reference should be normalized.');
          service_worker_unregister_and_done(t, scope);
        });
  }, 'Scope including self-reference');

promise_test(function(t) {
    var script = 'resources/../resources/empty-worker.js';
    var scope = 'resources/scope/parent-reference-in-script-url';
    return navigator.serviceWorker.register(script, {scope: scope})
      .then(function(registration) {
          assert_equals(
            registration.installing.scriptURL,
            normalizeURL('resources/empty-worker.js'),
            'Script URL including parent-reference should be normalized.');
          service_worker_unregister_and_done(t, scope);
        });
  }, 'Script URL including parent-reference');

promise_test(function(t) {
    var script = 'resources/empty-worker.js';
    var scope = 'resources/../resources/scope/parent-reference-in-scope';
    return navigator.serviceWorker.register(script, {scope: scope})
      .then(function(registration) {
          assert_equals(
            registration.scope,
            normalizeURL('resources/scope/parent-reference-in-scope'),
            'Scope including parent-reference should be normalized.');
          service_worker_unregister_and_done(t, scope);
        });
  }, 'Scope including parent-reference');

promise_test(function(t) {
    var script = 'resources/empty-worker.js';
    var scope = 'resources/../scope/parent-reference-in-scope';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        'SecurityError',
        'Scope not under the script directory should be rejected.');
  }, 'Scope including parent-reference and not under the script directory');

promise_test(function(t) {
    var script = 'resources////empty-worker.js';
    var scope = 'resources/scope/consecutive-slashes-in-script-url';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        'SecurityError',
        'Consecutive slashes in the script url should not be unified.');
  }, 'Script URL including consecutive slashes');

promise_test(function(t) {
    var script = 'resources/empty-worker.js';
    var scope = 'resources/scope////consecutive-slashes-in-scope';
    return navigator.serviceWorker.register(script, {scope: scope})
      .then(function(registration) {
          // Although consecutive slashes in the scope are not unified, the
          // scope is under the script directory and registration should
          // succeed.
          assert_equals(
            registration.scope,
            normalizeURL(scope),
            'Should successfully be registered.');
          service_worker_unregister_and_done(t, scope);
        })
  }, 'Scope including consecutive slashes');

promise_test(function(t) {
    var script = 'filesystem:' + normalizeURL('resources/empty-worker.js');
    var scope = 'resources/scope/filesystem-script-url';
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        'SecurityError',
        'Registering a script which has same-origin filesystem: URL should ' +
            'fail with SecurityError.');
  }, 'Script URL is same-origin filesystem: URL');

promise_test(function(t) {
    var script = 'resources/empty-worker.js';
    var scope = 'filesystem:' + normalizeURL('resources/scope/filesystem-scope-url');
    return assert_promise_rejects(
        navigator.serviceWorker.register(script, {scope: scope}),
        'SecurityError',
        'Registering with the scope that has same-origin filesystem: URL ' +
            'should fail with SecurityError.');
  }, 'Scope URL is same-origin filesystem: URL');

</script>