Bug 1331680 - Part 4: Tests for document.cookie behaving synchronously and reflecting changes from the parent process. r=jdm

* * *
Bug 1331680 - modification part5 -- fixed try server errors. r=jdm
This commit is contained in:
Amy Chung 2017-08-03 19:02:06 +08:00
parent 238f1e2d3a
commit 3a958eaecb
12 changed files with 300 additions and 2 deletions

View File

@ -47,5 +47,6 @@ function runTest() {
// Disable third-party cookies for this test.
addEventListener('testready', function() {
SpecialPowers.pushPrefEnv({'set': [['network.cookie.cookieBehavior', 1]]}, runTest);
SpecialPowers.pushPrefEnv({'set': [['network.cookie.cookieBehavior', 1],
['network.cookie.ipc.sync', true]]}, runTest);
});

View File

@ -0,0 +1,23 @@
let {utils: Cu, classes: Cc, interfaces: Ci } = Components;
Cu.import("resource://gre/modules/Services.jsm");
let cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
var observer = {
observe: function(subject, topic, data) {
if (topic == "cookie-changed") {
let cookie = subject.QueryInterface(Ci.nsICookie2);
sendAsyncMessage("cookieName", cookie.name + "=" + cookie.value);
}
}
};
addMessageListener("createObserver" , function(e) {
Services.obs.addObserver(observer, "cookie-changed");
sendAsyncMessage("createObserver:return");
});
addMessageListener("removeObserver" , function(e) {
Services.obs.removeObserver(observer, "cookie-changed");
sendAsyncMessage("removeObserver:return");
});

View File

@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<script>
document.cookie = "if2_1=if2_val1";
document.cookie = "if2_2=if2_val2";
window.parent.postMessage(document.cookie, "*");
</script>
</html>

View File

@ -0,0 +1,6 @@
<!DOCTYPE html>
<html>
<script>
document.cookie = "if1=if1_val";
</script>
</html>

View File

@ -5,8 +5,12 @@ support-files =
rel_preconnect.sjs
user_agent.sjs
user_agent_update.sjs
set_cookie_xhr.sjs
web_packaged_app.sjs
file_loadinfo_redirectchain.sjs
file_1331680.js
file_iframe_allow_scripts.html
file_iframe_allow_same_origin.html
redirect_idn.html^headers^
redirect_idn.html
empty.html
@ -24,3 +28,6 @@ support-files =
[test_user_agent_updates_reset.html]
[test_viewsource_unlinkable.html]
[test_xhr_method_case.html]
[test_1331680.html]
[test_1331680_iframe.html]
[test_1331680_xhr.html]

View File

@ -0,0 +1,13 @@
function handleRequest(request, response)
{
var queryString = request.queryString;
switch (queryString) {
case "xhr1":
response.setHeader("Set-Cookie", "xhr1=xhr_val1; path=/", false);
break;
case "xhr2":
response.setHeader("Set-Cookie", "xhr2=xhr_val2; path=/; HttpOnly", false);
break;
}
}

View File

@ -0,0 +1,80 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1331680
-->
<head>
<title>Cookies set in content processes update immediately.</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1331680">Mozilla Bug 1331680</a>
<p id="display"></p>
<div id="content" style="display: none">
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();
let {utils: Cu, classes: Cc, interfaces: Ci } = Components;
var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('file_1331680.js'));
gScript.addMessageListener("cookieName", confirmCookieName);
gScript.addMessageListener("createObserver:return", testSetCookie);
gScript.addMessageListener("removeObserver:return", finishTest);
gScript.sendAsyncMessage('createObserver');
var testsNum = 0;
function confirmRemoveAllCookies() {
is(document.cookie, "", "Removed all cookies.");
SimpleTest.finish();
}
// Confirm the notify which represents the cookie is updating.
function confirmCookieName(name) {
testsNum++;
switch(testsNum) {
case 1:
case 3:
is(name, "cookie0=test1", "An update for the cookie named " + name + " was observed.");
break;
case 2:
is(name, "cookie2=test3", "An update for the cookie named " + name + " was observed.");
break;
case 4:
is(name, "cookie2=test3", "An update for the cookie named " + name + " was observed.");
gScript.sendAsyncMessage('removeObserver');
break;
}
}
function finishTest() {
is(document.cookie, "", "Removed all cookies from cookie-changed");
SimpleTest.finish();
}
/* Test document.cookie
* 1. Set a cookie and confirm the cookies which are processed from observer.
* 2. Set a cookie and get cookie.
*/
const COOKIE_NAMES = ["cookie0", "cookie1", "cookie2"];
function testSetCookie() {
document.cookie = COOKIE_NAMES[0] + "=test1";
document.cookie = COOKIE_NAMES[1] + "=test2; HttpOnly";
document.cookie = COOKIE_NAMES[2] + "=test3";
var confirmCookieString = COOKIE_NAMES[0] + "=test1; " + COOKIE_NAMES[2] + "=test3";
is(document.cookie, confirmCookieString, "Confirm the cookies string which be get is current.");
for (var i = 0; i < COOKIE_NAMES.length; i++) {
document.cookie = COOKIE_NAMES[i] + "=; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
}
is(document.cookie, "", "Removed all cookies.");
}
</script>
</div>
<pre id="test">
</pre>
</body>
</html>

View File

@ -0,0 +1,67 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=643051
-->
<head>
<title>Cookies set from iframe in content process</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1331680">Mozilla Bug 1331680</a>
<p id="display"></p>
<div id="content" style="display: none">
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();
const IFRAME_COOKIE_NAMES = ["if1", "if2_1", "if2_2"];
const ID = ["if_1", "if_2", "if_3"];
let {utils: Cu, classes: Cc, interfaces: Ci } = Components;
var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('file_1331680.js'));
/* Test iframe
* 1. Create three iframes, and one of the iframe will create two cookies.
* 2. Confirm the cookies can be proessed from observer.
* 3. Confirm document.cookie can get cookies "if2_1" and "if2_2".
* 4. Confirm the iframe whose source is "about:blank" can get parent's cookies.
*/
function createIframe(id, src, sandbox_flags) {
return new Promise(resolve => {
var ifr = document.createElement("iframe");
ifr.id = id;
ifr.src = src;
ifr.sandbox = sandbox_flags;
ifr.addEventListener("load", resolve);
document.body.appendChild(ifr);
});
};
function confirmCookies(id) {
is(document.cookie, "if2_1=if2_val1; if2_2=if2_val2", "Confirm the cookies can get after iframe was deleted");
var new_ifr = document.getElementById(id);
is(new_ifr.contentDocument.cookie, document.cookie, "Confirm the inner document.cookie = parent document.cookie");
document.cookie = IFRAME_COOKIE_NAMES[1] + "=; expires=Thu, 01-Jan-1970 00:00:01 GMT";
document.cookie = IFRAME_COOKIE_NAMES[2] + "=; expires=Thu, 01-Jan-1970 00:00:01 GMT";
is(document.cookie, "", "Removed all cookies");
SimpleTest.finish();
}
addEventListener("message", function(event) {
is(event.data, document.cookie, "Confirm the iframe 2 can communicate with iframe");
});
Promise.resolve()
.then(_ => createIframe(ID[0], "file_iframe_allow_scripts.html", "allow-scripts"))
.then(_ => createIframe(ID[1], "file_iframe_allow_same_origin.html", "allow-scripts allow-same-origin"))
.then(_ => createIframe(ID[2], "about:blank", "allow-scripts allow-same-origin"))
.then(_ => confirmCookies(ID[2]));
</script>
</div>
<pre id="test">
</pre>
</body>
</html>

View File

@ -0,0 +1,89 @@
<!DOCTYPE HTML>
<html>
<!--
-->
<head>
<title>Cookie changes from XHR requests are observed in content processes.</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<script type="text/javascript">
SimpleTest.waitForExplicitFinish();
let {utils: Cu, classes: Cc, interfaces: Ci } = Components;
const XHR_COOKIE_NAMES = ["xhr1", "xhr2"];
var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('file_1331680.js'));
gScript.addMessageListener("cookieName", confirmCookieName);
gScript.addMessageListener("removeObserver:return", finishTest);
gScript.sendAsyncMessage('createObserver');
// Confirm the notify which represents the cookie is updating.
var testsNum = 0;
function confirmCookieName(name) {
testsNum++;
switch(testsNum) {
case 1:
is(name, "xhr1=xhr_val1", "The cookie which names " + name + " is update to db");
break;
case 2:
is(document.cookie, "xhr1=xhr_val1", "Confirm the cookie string");
for (var i = 0; i < XHR_COOKIE_NAMES.length; i++) {
document.cookie = XHR_COOKIE_NAMES[i] + "=; path=/; expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
break;
case 3:
is(document.cookie, "", "Confirm the cookie string");
gScript.sendAsyncMessage('removeObserver');
break;
}
}
function finishTest() {
SimpleTest.finish();
}
function createXHR(url) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true); // async request
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send();
});
}
/* Test XHR
* 1. Create two XHR.
* 2. One of the XHR create a cookie names "xhr1", and other one create a http-only cookie names "xhr2".
* 3. Child process only set xhr1 to cookies hash table.
* 4. Child process only can get the xhr1 cookie from cookies hash table.
*/
Promise.resolve()
.then(_ => createXHR('set_cookie_xhr.sjs?xhr1'))
.then(_ => createXHR('set_cookie_xhr.sjs?xhr2'));
</script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test">
</pre>
</body>
</html>

View File

@ -1,6 +1,7 @@
Cu.import("resource://gre/modules/Services.jsm");
function run_test() {
Services.prefs.setBoolPref("network.cookie.ipc.sync", true);
Services.prefs.setIntPref("network.cookie.cookieBehavior", 1);
run_test_in_child("../unit/test_bug528292.js");
}

View File

@ -7,5 +7,6 @@ Cu.import("resource://gre/modules/Services.jsm");
function run_test() {
// Allow all cookies.
Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
Services.prefs.setBoolPref("network.cookie.ipc.sync", true);
run_test_in_child("../unit/test_cookie_header.js");
}

View File

@ -1,3 +1,5 @@
Cu.import("resource://gre/modules/Services.jsm");
function run_test() {
Services.prefs.setBoolPref("network.cookie.ipc.sync", true);
run_test_in_child("../unit/test_multipart_streamconv.js");
}