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
|
// META: script=/common/utils.js
// META: script=resources/support.sub.js
//
// Spec: https://wicg.github.io/private-network-access/#integration-fetch
//
// These tests check that initial `ServiceWorker` script fetches are subject to
// Private Network Access checks, just like a regular `fetch()`.
//
// See also: worker.https.window.js
// Results that may be expected in tests.
const TestResult = {
SUCCESS: {
register: { loaded: true },
unregister: { unregistered: true },
},
FAILURE: {
register: { error: "TypeError" },
unregister: { unregistered: false, error: "no registration" },
},
};
async function makeTest(t, { source, target, expected }) {
const sourceUrl = resolveUrl("resources/service-worker-bridge.html",
sourceResolveOptions(source));
const targetUrl = preflightUrl(target);
targetUrl.searchParams.append("body", "undefined");
targetUrl.searchParams.append("mime-type", "application/javascript");
const scope = resolveUrl(`resources/${token()}`, {...target.server}).href;
const iframe = await appendIframe(t, document, sourceUrl);
{
const reply = futureMessage();
const message = {
action: "register",
url: targetUrl.href,
options: { scope },
};
iframe.contentWindow.postMessage(message, "*");
const { error, loaded } = await reply;
assert_equals(error, expected.register.error, "register error");
assert_equals(loaded, expected.register.loaded, "response loaded");
}
{
const reply = futureMessage();
iframe.contentWindow.postMessage({ action: "unregister", scope }, "*");
const { error, unregistered } = await reply;
assert_equals(error, expected.unregister.error, "unregister error");
assert_equals(
unregistered, expected.unregister.unregistered, "worker unregistered");
}
}
promise_test(t => makeTest(t, {
source: {
server: Server.HTTPS_LOCAL,
treatAsPublic: true,
},
target: { server: Server.HTTPS_LOCAL },
expected: TestResult.FAILURE,
}), "treat-as-public to local: failed preflight.");
promise_test(t => makeTest(t, {
source: {
server: Server.HTTPS_LOCAL,
treatAsPublic: true,
},
target: {
server: Server.HTTPS_LOCAL,
behavior: { preflight: PreflightBehavior.serviceWorkerSuccess(token()) },
},
expected: TestResult.SUCCESS,
}), "treat-as-public to local: success.");
promise_test(t => makeTest(t, {
source: {
server: Server.HTTPS_PRIVATE,
treatAsPublic: true,
},
target: { server: Server.HTTPS_PRIVATE },
expected: TestResult.FAILURE,
}), "treat-as-public to private: failed preflight.");
promise_test(t => makeTest(t, {
source: {
server: Server.HTTPS_PRIVATE,
treatAsPublic: true,
},
target: {
server: Server.HTTPS_PRIVATE,
behavior: { preflight: PreflightBehavior.serviceWorkerSuccess(token()) },
},
expected: TestResult.SUCCESS,
}), "treat-as-public to private: success.");
promise_test(t => makeTest(t, {
source: { server: Server.HTTPS_PUBLIC },
target: { server: Server.HTTPS_PUBLIC },
expected: TestResult.SUCCESS,
}), "public to public: success.");
|