Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse helpers; stop running iframe tests concurrently; set test context #39275

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions storage-access-api/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ function RunTestsInNestedIFrame(sourceURL) {
}, true);
}

function RunRequestStorageAccessInDetachedFrame() {
function CreateDetachedFrame() {
const frame = document.createElement('iframe');
document.body.append(frame);
const inner_doc = frame.contentDocument;
frame.remove();
return inner_doc.requestStorageAccess();
return inner_doc;
}

function RunRequestStorageAccessViaDomParser() {
function CreateDocumentViaDOMParser() {
const parser = new DOMParser();
const doc = parser.parseFromString('<html></html>', 'text/html');
return doc.requestStorageAccess();
return doc;
}

function RunCallbackWithGesture(callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ if (topLevelDocument) {
promise_test(t => {
const description = "document.requestStorageAccess() call in a detached frame";
// Can't use `promise_rejects_dom` here, since the error comes from the wrong global.
return RunRequestStorageAccessInDetachedFrame()
return CreateDetachedFrame().requestStorageAccess()
.then(t.unreached_func("Should have rejected: " + description), (e) => {
assert_equals(e.name, 'InvalidStateError', description);
});
}, "[non-fully-active] document.requestStorageAccess() should reject when run in a detached frame");

promise_test(t => {
return promise_rejects_dom(t, 'InvalidStateError', RunRequestStorageAccessViaDomParser(),
return promise_rejects_dom(t, 'InvalidStateError', CreateDocumentViaDOMParser().requestStorageAccess(),
"document.requestStorageAccess() in a detached DOMParser result");
}, "[non-fully-active] document.requestStorageAccess() should reject when run in a detached DOMParser document");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'use strict';

promise_test(t => {
const promise = RunRequestStorageAccessInDetachedFrame();
const promise = CreateDetachedFrame().requestStorageAccess();
const description = "document.requestStorageAccess() call in a detached frame";
// Can't use `promise_rejects_dom` here, since the error comes from the wrong global.
return promise.then(t.unreached_func("Should have rejected: " + description), (e) => {
Expand All @@ -13,6 +13,6 @@ promise_test(t => {
}, "[non-fully-active] document.requestStorageAccess() should not resolve when run in a detached frame");

promise_test(t => {
return promise_rejects_dom(t, 'InvalidStateError', RunRequestStorageAccessViaDomParser(),
return promise_rejects_dom(t, 'InvalidStateError', CreateDocumentViaDOMParser().requestStorageAccess(),
"document.requestStorageAccess() in a detached DOMParser result");
}, "[non-fully-active] document.requestStorageAccess() should not resolve when run in a detached DOMParser document");
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,9 @@
// Some tests are run at the top-level, and an iframe is added to validate API
// behavior in that context.

// Prefix each test case with an indicator so we know what context they are run
// in if they are used in multiple iframes.
let testPrefix = 'insecure-context';

// Keep track of if we run these tests in a nested context, we don't want to
// recurse forever.
let topLevelDocument = true;

// The query string allows derivation of test conditions, like whether the tests
// are running in a top-level context.
const queryParams = window.location.search.substring(1).split('&');
queryParams.forEach((param) => {
if (param.toLowerCase() == 'rootdocument=false') {
topLevelDocument = false;
} else if (param.split('=')[0].toLowerCase() == 'testcase') {
testPrefix = param.split('=')[1];
}
});

// TODO(crbug.com/1410556): when/if requestStorageAccessFor is standardized,
// we should consider upstreaming these helpers.
function RunRequestStorageAccessForInDetachedFrame(origin) {
const nestedFrame = document.createElement('iframe');
document.body.append(nestedFrame);
const inner_doc = nestedFrame.contentDocument;
nestedFrame.remove();
return inner_doc.requestStorageAccessFor(origin);
}

function RunRequestStorageAccessForViaDomParser(origin) {
const parser = new DOMParser();
const doc = parser.parseFromString('<html></html>', 'text/html');
return doc.requestStorageAccessFor(origin);
const {testPrefix, topLevelDocument} = processQueryParams();
if (!topLevelDocument) {
test_driver.set_test_context(window.top);
}

// Common tests to run in all frames.
Expand All @@ -66,7 +36,7 @@ if (topLevelDocument) {
const description =
'document.requestStorageAccessFor() call in a detached frame';
// Can't use promise_rejects_dom here because the exception is from the wrong global.
return RunRequestStorageAccessForInDetachedFrame('https://foo.com')
return CreateDetachedFrame().requestStorageAccessFor('https://foo.com')
.then(t.unreached_func('Should have rejected: ' + description))
.catch((e) => {
assert_equals(e.name, 'InvalidStateError', description);
Expand All @@ -76,7 +46,7 @@ if (topLevelDocument) {
promise_test(async t => {
const description =
'document.requestStorageAccessFor() in a detached DOMParser result';
return RunRequestStorageAccessForViaDomParser('https://foo.com')
return CreateDocumentViaDOMParser().requestStorageAccessFor('https://foo.com')
.then(t.unreached_func('Should have rejected: ' + description))
.catch((e) => {
assert_equals(e.name, 'InvalidStateError', description);
Expand All @@ -86,8 +56,10 @@ if (topLevelDocument) {
// Create a test with a single-child same-origin iframe.
// This will validate that calls to requestStorageAccessFor are rejected
// in non-top-level contexts.
RunTestsInIFrame(
'./resources/requestStorageAccessFor-iframe.html?testCase=frame-on-insecure-page&rootdocument=false');
promise_test(() => {
return RunTestsInIFrame(
'./resources/requestStorageAccessFor-iframe.html?testCase=frame-on-insecure-page');
});

promise_test(
async t => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,12 @@
// Some tests are run at the top-level, and an iframe is added to validate API
// behavior in that context.

// Prefix each test case with an indicator so we know what context they are run
// in if they are used in multiple iframes.
let testPrefix = 'top-level-context';

// Keep track of if we run these tests in a nested context, we don't want to
// recurse forever.
let topLevelDocument = true;

// The query string allows derivation of test conditions, like whether the tests
// are running in a top-level context.
const queryParams = window.location.search.substring(1).split('&');
queryParams.forEach((param) => {
if (param.toLowerCase() == 'rootdocument=false') {
topLevelDocument = false;
} else if (param.split('=')[0].toLowerCase() == 'testcase') {
testPrefix = param.split('=')[1];
}
});

const requestedOrigin = 'https://foo.com';

// TODO(crbug.com/1351540): when/if requestStorageAccessFor is standardized,
// upstream with the Storage Access API helpers file.
function RunRequestStorageAccessForInDetachedFrame(origin) {
const nestedFrame = document.createElement('iframe');
document.body.append(nestedFrame);
const inner_doc = nestedFrame.contentDocument;
nestedFrame.remove();
return inner_doc.requestStorageAccessFor(origin);
const {testPrefix, topLevelDocument} = processQueryParams();
if (!topLevelDocument) {
test_driver.set_test_context(window.top);
}

function RunRequestStorageAccessForViaDomParser(origin) {
const parser = new DOMParser();
const doc = parser.parseFromString('<html></html>', 'text/html');
return doc.requestStorageAccessFor(origin);
}
const requestedOrigin = 'https://foo.com';

// Common tests to run in all frames.
test(
Expand Down Expand Up @@ -84,7 +54,7 @@ if (topLevelDocument) {
const description =
'document.requestStorageAccessFor() call in a detached frame';
// Can't use promise_rejects_dom here because the exception is from the wrong global.
return RunRequestStorageAccessForInDetachedFrame(requestedOrigin)
return CreateDetachedFrame().requestStorageAccessFor(requestedOrigin)
.then(t.unreached_func('Should have rejected: ' + description))
.catch((e) => {
assert_equals(e.name, 'InvalidStateError', description);
Expand All @@ -94,7 +64,7 @@ if (topLevelDocument) {
promise_test(async t => {
const description =
'document.requestStorageAccessFor() in a detached DOMParser result';
return RunRequestStorageAccessForViaDomParser(requestedOrigin)
return CreateDocumentViaDOMParser().requestStorageAccessFor(requestedOrigin)
.then(t.unreached_func('Should have rejected: ' + description))
.catch((e) => {
assert_equals(e.name, 'InvalidStateError', description);
Expand Down Expand Up @@ -125,20 +95,22 @@ if (topLevelDocument) {
'granted');

await RunCallbackWithGesture(() => {
document.requestStorageAccessFor(altOrigin).then(() => {
RunTestsInIFrame(
return document.requestStorageAccessFor(altOrigin).then(() => {
return RunTestsInIFrame(
'https://{{hosts[alt][www]}}:{{ports[https][0]}}/top-level-storage-access-api/tentative/resources/requestStorageAccess-integration-iframe.https.html');
});
});
},
'[' + testPrefix +
'] document.requestStorageAccess() should be resolved without a user gesture after a successful requestStorageAccessFor() call');

promise_test(() => {
// Create a test with a single-child same-origin iframe.
// This will validate that calls to requestStorageAccessFor are rejected
// in non-top-level contexts.
RunTestsInIFrame(
'./resources/requestStorageAccessFor-iframe.https.html?testCase=same-origin-frame&rootdocument=false');
return RunTestsInIFrame(
'./resources/requestStorageAccessFor-iframe.https.html?testCase=same-origin-frame');
});

promise_test(
async t => {
Expand Down