Commit 33f4894a authored by Kenichi Ishibashi's avatar Kenichi Ishibashi Committed by Commit Bot

Rewrite addressspace-serviceworker-basic test

The previous version of this test had some issues and were the
source of flakiness:
* It used non-secure contexts and register() seemed not working.
* It used a mock object for service_worker_unregister_and_register()
  and the mock object silently consumed rejections of register().
* It didn't wait for iframe loading completion.
* It didn't clean up iframes.
* It didn't handle error cases correctly and the test passed
  in an unexpected way.

This CL rewrites the test to fix above issues:
* Use secure contexts.
* Use existing test helpers which provide better error handlings.

As a consequence we stop using addressspace-test.js in the test
but I think the new test is easier to read and understand.

This CL should de-flake the test.

Bug: 988074
Change-Id: I3d16c61742d819c08cadcff20a017b7336508efd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1877876
Commit-Queue: Kenichi Ishibashi <bashi@chromium.org>
Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Cr-Commit-Position: refs/heads/master@{#709384}
parent 1b16e7f2
......@@ -5474,7 +5474,6 @@ crbug.com/988432 virtual/webrtc-wpt-plan-b/external/wpt/webrtc/RTCRtpReceiver-ge
# Sheriff 2019-07-29
crbug.com/937811 [ Win Release ] http/tests/devtools/elements/shadow/elements-panel-shadow-selection-on-refresh-3.js [ Pass Failure Timeout ]
crbug.com/988074 [ Linux ] virtual/cors-rfc1918/http/tests/security/cors-rfc1918/addressspace-serviceworker-basic.html [ Pass Failure ]
# Pending enabling navigation feature
crbug.com/705583 external/wpt/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_navigate_history_go_back.html [ Skip ]
......@@ -5499,7 +5498,6 @@ crbug.com/989860 [ Mac10.13 ] http/tests/devtools/indexeddb/live-update-indexedd
crbug.com/989860 [ Linux ] http/tests/devtools/indexeddb/live-update-indexeddb-list.js [ Pass Failure ]
crbug.com/989860 [ Win ] http/tests/devtools/indexeddb/live-update-indexeddb-list.js [ Pass Failure ]
crbug.com/989717 [ Fuchsia ] http/tests/preload/avoid_delaying_onload_link_preload.html [ Pass Failure ]
crbug.com/988074 [ Linux ] virtual/omt-worker-fetch-cors-rfc1918/http/tests/security/cors-rfc1918/addressspace-serviceworker-basic.html [ Pass Failure ]
# Sheriff 2019-08-05
crbug.com/991243 [ Linux ] external/wpt/workers/semantics/multiple-workers/003.html [ Pass Timeout ]
......
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="./resources/addressspace-test.js"></script>
<script>
window.onload = function () {
addressSpaceTest("http://localhost:8000", "serviceworker", "local");
addressSpaceTest("http://127.0.0.1:8000", "serviceworker", "local");
};
</script>
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/serviceworker/resources/test-helpers.js"></script>
<body>
<script>
async function receiveAddressSpaceMessageFromIFrame(frame) {
return new Promise((resolve) => {
window.addEventListener("message", e => {
// The iframe uses promise_test() and it sends messages which are
// related to test state. Ignore these messages.
if (e.data.messageType === "AddressSpaceMessage")
resolve(e.data);
});
});
}
async function addressSpaceServiceWorkerTest(t, origin, expected) {
const url = origin + "/security/cors-rfc1918/resources/post-addressspace-from-serviceworker.html";
const frame = await with_iframe(url);
t.add_cleanup(_ => frame.remove());
const message = await receiveAddressSpaceMessageFromIFrame(frame);
assert_equals(message.origin, origin, "origin");
assert_equals(message.addressSpace, expected, "addressSpace");
}
promise_test(async t => {
const origin = "https://localhost:8443";
const expected = "local";
await addressSpaceServiceWorkerTest(t, origin, expected);
}, "addressspace: service worker localhost");
promise_test(async t => {
const origin = "https://127.0.0.1:8443";
const expected = "local";
await addressSpaceServiceWorkerTest(t, origin, expected);
}, "addressspace: service worker 127.0.0.1");
</script>
</body>
\ No newline at end of file
<script src='/serviceworker/resources/test-helpers.js'></script>
<script src="/resources/testharness.js"></script>
<script src="/serviceworker/resources/test-helpers.js"></script>
<script>
function postDataToParent(e) {
window.parent.postMessage(e.data, "*");
}
var script = "/security/cors-rfc1918/resources/post-addressspace-to-owner-serviceworker.js";
var scope = "/security/cors-rfc1918/resources/post-addressspace-from-serviceworker.html";
var registration;
async function receiveMessageFromServiceWorker(worker) {
return new Promise((resolve) => {
// Use a dedicated message channel to avoid unexpected messages.
const channel = new MessageChannel();
channel.port1.onmessage = e => resolve(e.data);
worker.postMessage("Don't care", [channel.port2]);
});
}
promise_test(async t => {
const script = "/security/cors-rfc1918/resources/post-addressspace-to-owner-serviceworker.js";
const scope = "/security/cors-rfc1918/resources/post-addressspace-from-serviceworker.html";
const registration = await service_worker_unregister_and_register(
t, script, scope);
t.add_cleanup(_ => registration.unregister());
await wait_for_state(t, registration.installing, "activated");
const message = await receiveMessageFromServiceWorker(registration.active);
window.parent.postMessage(message, "*");
}, "Address space tests for service worker");
var t = { step_func: f => f};
service_worker_unregister_and_register(t, script, scope)
.then(r => {
if (!r) {
throw({ name: "WTF" });
}
registration = r;
return wait_for_state(t, registration.installing, 'activated');
})
.then(_ => {
return new Promise(resolve => {
var ports = new MessageChannel();
ports.port1.onmessage = e => {
postDataToParent(e);
resolve();
};
registration.active.postMessage("go", [ports.port2]);
});
})
.then(_ => service_worker_unregister(t, scope))
.catch(e => window.parent.postMessage({ "origin": e.name }, "*"));
</script>
self.addEventListener('message', e => {
e.ports[0].postMessage({
"origin": self.location.origin,
"addressSpace": self.addressSpace
});
self.registration.active.postMessage({
"messageType": "AddressSpaceMessage",
"origin": self.location.origin,
"addressSpace": self.addressSpace
});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment