Skip to content

Commit

Permalink
Migrate cookie-test.js's httpRedirectCookieTest to test_driver.delete…
Browse files Browse the repository at this point in the history
…_all_cookies

Now that we have a JS function within testdriver to delete all cookies
we should use it instead of the one-off helpers.

Bug: 1233305
Change-Id: Ib0ee77a51a3b1bea9d48549a309fdba8116461bb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3116120
Reviewed-by: Steven Bingler <bingler@chromium.org>
Auto-Submit: Ari Chivukula <arichiv@chromium.org>
Commit-Queue: Ari Chivukula <arichiv@chromium.org>
Cr-Commit-Position: refs/heads/main@{#951023}
  • Loading branch information
arichiv authored and foolip committed Dec 21, 2021
1 parent c60806c commit 71cac4e
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 46 deletions.
3 changes: 2 additions & 1 deletion cookies/attributes/domain.sub.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/cookies/resources/cookie-test.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
</head>
<body>
<script>
Expand Down
2 changes: 2 additions & 0 deletions cookies/attributes/path-redirect.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/cookies/resources/cookie-test.js"></script>
</head>
<body>
Expand Down
2 changes: 2 additions & 0 deletions cookies/attributes/resources/domain-child.sub.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<meta name=help href="https://tools.ietf.org/html/rfc6265#section-5.2.3">
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/cookies/resources/cookie-test.js"></script>
</head>
<body>
Expand Down
29 changes: 11 additions & 18 deletions cookies/attributes/resources/path-redirect-shared.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
function expireCookie(cookie) {
const cookies = Array.isArray(cookie) ? cookie : [cookie];
for (let c of cookies) {
document.cookie = c += "; max-age=0";
}
}

function getCookies() {
return document.cookie;
}

// Note: this function has a dependency on testdriver.js. Any test files calling
// it should include testdriver.js and testdriver-vendor.js
window.addEventListener("message", (e) => {
if (e.data == "getCookies") {
const cookies = getCookies();
e.source.postMessage({"cookies": cookies}, '*');
let test_window = window.top;
while (test_window.opener && !test_window.opener.closed) {
test_window = test_window.opener.top;
}

if (typeof e.data == "object" && 'expireCookie' in e.data) {
expireCookie(e.data.expireCookie);
e.source.postMessage("expired", '*');
test_driver.set_test_context(test_window);
if (e.data == "getAndExpireCookiesForRedirectTest") {
const cookies = document.cookie;
test_driver.delete_all_cookies().then(() => {
e.source.postMessage({"cookies": cookies}, '*');
});
}
});
2 changes: 2 additions & 0 deletions cookies/attributes/resources/path.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<meta name=help href="http://tools.ietf.org/html/rfc6265#section-5.1.4">
</head>
<body>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/cookies/attributes/resources/path-redirect-shared.js"></script>
</body>
</html>
2 changes: 2 additions & 0 deletions cookies/attributes/resources/path/one.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<meta name=help href="http://tools.ietf.org/html/rfc6265#section-5.1.4">
</head>
<body>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/cookies/attributes/resources/path-redirect-shared.js"></script>
</body>
</html>
2 changes: 2 additions & 0 deletions cookies/attributes/resources/path/three.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<meta name=help href="http://tools.ietf.org/html/rfc6265#section-5.1.4">
</head>
<body>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/cookies/attributes/resources/path-redirect-shared.js"></script>
</body>
</html>
2 changes: 2 additions & 0 deletions cookies/attributes/resources/path/two.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<meta name=help href="http://tools.ietf.org/html/rfc6265#section-5.1.4">
</head>
<body>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/cookies/attributes/resources/path-redirect-shared.js"></script>
</body>
</html>
2 changes: 2 additions & 0 deletions cookies/attributes/resources/pathfakeout.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<meta name=help href="http://tools.ietf.org/html/rfc6265#section-5.1.4">
</head>
<body>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/cookies/attributes/resources/path-redirect-shared.js"></script>
</body>
</html>
2 changes: 2 additions & 0 deletions cookies/attributes/resources/pathfakeout/one.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<meta name=help href="http://tools.ietf.org/html/rfc6265#section-5.1.4">
</head>
<body>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/cookies/attributes/resources/path-redirect-shared.js"></script>
</body>
</html>
3 changes: 2 additions & 1 deletion cookies/ordering/ordering.sub.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/cookies/resources/cookie-test.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
</head>
</head>
<body>
Expand Down
2 changes: 2 additions & 0 deletions cookies/ordering/resources/ordering-child.sub.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<meta name=help href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-07#section-5.5">
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/cookies/resources/cookie-test.js"></script>
</head>
<body>
Expand Down
45 changes: 19 additions & 26 deletions cookies/resources/cookie-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,25 @@ async function getAndExpireCookiesForDefaultPathTest() {
});
}

// getRedirectedCookies is a helper method to get and delete cookies that
// were set from a Location header redirect.
async function getRedirectedCookies(location, cookie) {
// getAndExpireCookiesForRedirectTest is a helper method to get and delete
// cookies that were set from a Location header redirect.
async function getAndExpireCookiesForRedirectTest(location) {
return new Promise((resolve, reject) => {
try {
const iframe = document.createElement('iframe');
iframe.style = 'display: none';
iframe.src = location;

const listener = (e) => {
if (typeof e.data == 'object' && 'cookies' in e.data) {
window.removeEventListener('message', listener);
document.documentElement.removeChild(iframe);
resolve(e.data.cookies);
}
};
window.addEventListener('message', listener);
iframe.addEventListener('load', (e) => {
const win = e.target.contentWindow;
let iframeCookie;
// go ask for the cookie
win.postMessage('getCookies', '*');

// once we get it, send a message to delete on the other
// side, then resolve the cookie back to httpRedirectCookieTest
window.addEventListener('message', (e) => {
if (typeof e.data == 'object' && 'cookies' in e.data) {
iframeCookie = e.data.cookies;
e.source.postMessage({'expireCookie': cookie}, '*');
}

// wait on the iframe to tell us it deleted the cookies before
// resolving, to avoid any state race conditions.
if (e.data == 'expired') {
resolve(iframeCookie);
}
});
e.target.contentWindow.postMessage('getAndExpireCookiesForRedirectTest', '*');
}, {once: true});

document.documentElement.appendChild(iframe);
} catch (e) {
reject(e);
Expand Down Expand Up @@ -96,21 +84,26 @@ function httpCookieTest(cookie, expectedValue, name, defaultPath = true) {
// This is a variation on httpCookieTest, where a redirect happens via
// the Location header and we check to see if cookies are sent via
// getRedirectedCookies
//
// Note: the locations targeted by this function have a dependency on
// path-redirect-shared.js and should be sure to include it.
function httpRedirectCookieTest(cookie, expectedValue, name, location) {
return promise_test(async (t) => {
// The result is ignored as we're expiring cookies for cleaning here.
await getAndExpireCookiesForRedirectTest(location);

const encodedCookie = encodeURIComponent(JSON.stringify(cookie));
const encodedLocation = encodeURIComponent(location);
const setParams = `?set=${encodedCookie}&location=${encodedLocation}`;
await fetch(`/cookies/resources/cookie.py${setParams}`);
// for the tests where a redirect happens, we need to head
// to that URI to get the cookies (and then delete them there)
const cookies = await getRedirectedCookies(location, cookie);
const cookies = await getAndExpireCookiesForRedirectTest(location);
if (Boolean(expectedValue)) {
assert_equals(cookies, expectedValue, 'The cookie was set as expected.');
} else {
assert_equals(cookies, expectedValue, 'The cookie was rejected.');
}
await fetch(`/cookies/resources/cookie.py?drop=${encodedCookie}`);
}, name);
}

Expand Down

0 comments on commit 71cac4e

Please sign in to comment.