Skip to content

Commit

Permalink
Bug 1241557 - Make cookie tests pass in e10s. r=mccr8
Browse files Browse the repository at this point in the history
--HG--
extra : transplant_source : %98%27%3E%7D%9C%C6NA%C1%D0w%F1%F7%AA%5Bm%E21q%8D
  • Loading branch information
mrbkap committed Jan 21, 2016
1 parent 43bfcd9 commit db4df95
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 165 deletions.
3 changes: 2 additions & 1 deletion dom/html/test/mochitest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,8 @@ skip-if = toolkit == 'android'
[test_bug741266.html]
skip-if = buildapp == "mulet" || buildapp == "b2g" || toolkit == "android" || toolkit == "windows" || e10s # b2g(needs control of popup window size) b2g-debug(needs control of popup window size) b2g-desktop(needs control of popup window size) windows(bug 1234520)
[test_non-ascii-cookie.html]
skip-if = buildapp == 'b2g' || e10s
skip-if = buildapp == 'b2g'
support-files = file_cookiemanager.js
[test_bug765780.html]
[test_bug871161.html]
skip-if = (buildapp == 'b2g' && toolkit != 'gonk') #Bug 931116, b2g desktop specific, initial triage
Expand Down
54 changes: 28 additions & 26 deletions dom/html/test/test_non-ascii-cookie.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,40 @@

/** Test for non-ASCII cookie values **/

var [Cc, Ci] = [SpecialPowers.Cc, SpecialPowers.Ci];
SimpleTest.waitForExplicitFinish();

var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL("file_cookiemanager.js"));

var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager);
function getCookieFromManager() {
var values = [];
var host = location.hostname;
var path = location.pathname;
path = path.substring(0, path.lastIndexOf("/") + 1);
var e = cm.enumerator;
while (e.hasMoreElements()) {
var cookie = e.getNext().QueryInterface(Ci.nsICookie);
if (!cookie) {
break;
}
if (host != cookie.host || path != cookie.path) {
continue;
}
values.push(cookie.name + "=" + cookie.value);
}
return values.join("; ");
return new Promise(resolve => {
gScript.addMessageListener("getCookieFromManager:return", function gcfm({ cookie }) {
gScript.removeMessageListener("getCookieFromManager:return", gcfm);
resolve(cookie);
});
gScript.sendAsyncMessage("getCookieFromManager", { host: location.hostname, path: location.pathname });
});
}

var c = document.cookie;
is(document.cookie, 'abc=012©ABC\ufffdDEF', "document.cookie should be decoded as UTF-8");
is(getCookieFromManager(), document.cookie, "nsICookieManager should be consistent with document.cookie");
var newCookie = 'def=∼≩≭≧∯≳≲≣∽≸≸∺≸∠≯≮≥≲≲≯≲∽≡≬≥≲≴∨∱∩∾';
document.cookie = newCookie;
is(document.cookie, c + '; ' + newCookie, "document.cookie should be encoded as UTF-8");
is(getCookieFromManager(), document.cookie, "nsICookieManager should be consistent with document.cookie");
var date1 = new Date();
date1.setTime(0);
document.cookie = newCookie + 'def=;expires=' + date1.toGMTString();

var newCookie;

getCookieFromManager().then((cookie) => {
is(cookie, document.cookie, "nsICookieManager should be consistent with document.cookie");
newCookie = 'def=∼≩≭≧∯≳≲≣∽≸≸∺≸∠≯≮≥≲≲≯≲∽≡≬≥≲≴∨∱∩∾';
document.cookie = newCookie;
is(document.cookie, c + '; ' + newCookie, "document.cookie should be encoded as UTF-8");

return getCookieFromManager();
}).then((cookie) => {
is(cookie, document.cookie, "nsICookieManager should be consistent with document.cookie");
var date1 = new Date();
date1.setTime(0);
document.cookie = newCookie + 'def=;expires=' + date1.toGMTString();
gScript.destroy();
SimpleTest.finish();
});
</script>
</pre>
</body>
Expand Down
15 changes: 15 additions & 0 deletions extensions/cookie/test/file_chromecommon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let { classes: Cc, utils: Cu, interfaces: Ci } = Components;

let cs = Cc["@mozilla.org/cookiemanager;1"]
.getService(Ci.nsICookieManager2);

addMessageListener("getCookieCountAndClear", () => {
let count = 0;
for (let list = cs.enumerator; list.hasMoreElements(); list.getNext())
++count;
cs.removeAll();

sendAsyncMessage("getCookieCountAndClear:return", { count });
});

cs.removeAll();
48 changes: 21 additions & 27 deletions extensions/cookie/test/file_testcommon.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
const SCRIPT_URL = SimpleTest.getTestFileURL("file_chromecommon.js");

var gExpectedCookies;
var gExpectedLoads;

var gPopup;

var gScript;

var gLoads = 0;

function setupTest(uri, cookies, loads) {
SimpleTest.waitForExplicitFinish();

SpecialPowers.Cc["@mozilla.org/preferences-service;1"]
.getService(SpecialPowers.Ci.nsIPrefBranch)
.setIntPref("network.cookie.cookieBehavior", 1);

var cs = SpecialPowers.Cc["@mozilla.org/cookiemanager;1"]
.getService(SpecialPowers.Ci.nsICookieManager2);
cs.removeAll();
var prefSet = new Promise(resolve => {
SpecialPowers.pushPrefEnv({ set: [["network.cookie.cookieBehavior", 1]] }, resolve);
});

gScript = SpecialPowers.loadChromeScript(SCRIPT_URL);
gExpectedCookies = cookies;
gExpectedLoads = loads;

// Listen for MessageEvents.
window.addEventListener("message", messageReceiver, false);

// load a window which contains an iframe; each will attempt to set
// cookies from their respective domains.
gPopup = window.open(uri, 'hai', 'width=100,height=100');
prefSet.then(() => {
// load a window which contains an iframe; each will attempt to set
// cookies from their respective domains.
gPopup = window.open(uri, 'hai', 'width=100,height=100');
});
}

function finishTest()
{
SpecialPowers.Cc["@mozilla.org/preferences-service;1"]
.getService(SpecialPowers.Ci.nsIPrefBranch)
.clearUserPref("network.cookie.cookieBehavior");

function finishTest() {
gScript.destroy();
SimpleTest.finish();
}

/** Receives MessageEvents to this window. */
// Count and check loads.
function messageReceiver(evt)
{
function messageReceiver(evt) {
is(evt.data, "message", "message data received from popup");
if (evt.data != "message") {
gPopup.close();
Expand All @@ -64,13 +62,9 @@ function runTest() {
// set a cookie from a domain of "localhost"
document.cookie = "oh=hai";

var cs = SpecialPowers.Cc["@mozilla.org/cookiemanager;1"]
.getService(SpecialPowers.Ci.nsICookieManager);
var count = 0;
for(var list = cs.enumerator; list.hasMoreElements(); list.getNext())
++count;
is(count, gExpectedCookies, "total number of cookies");
cs.removeAll();

finishTest();
gScript.addMessageListener("getCookieCountAndClear:return", ({ count }) => {
is(count, gExpectedCookies, "total number of cookies");
finishTest();
});
gScript.sendAsyncMessage("getCookieCountAndClear");
}
149 changes: 39 additions & 110 deletions extensions/cookie/test/file_testloadflags.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const SCRIPT_URL = SimpleTest.getTestFileURL('file_testloadflags_chromescript.js');

var gExpectedCookies;
var gExpectedHeaders;
var gExpectedLoads;
Expand All @@ -10,113 +12,50 @@ var gLoads = 0;

// setupTest() is run from 'onload='.
function setupTest(uri, domain, cookies, loads, headers) {
ok(true, "setupTest uri: " + uri + " domain: " + domain + " cookies: " + cookies +
" loads: " + loads + " headers: " + headers);
info("setupTest uri: " + uri + " domain: " + domain + " cookies: " + cookies +
" loads: " + loads + " headers: " + headers);

SimpleTest.waitForExplicitFinish();

SpecialPowers.Cc["@mozilla.org/preferences-service;1"]
.getService(SpecialPowers.Ci.nsIPrefBranch)
.setIntPref("network.cookie.cookieBehavior", 1);

var cs = SpecialPowers.Cc["@mozilla.org/cookiemanager;1"]
.getService(SpecialPowers.Ci.nsICookieManager2);

ok(true, "we are going to remove these cookies");
var count = 0;
var list = cs.enumerator;
while (list.hasMoreElements()) {
var cookie = list.getNext().QueryInterface(SpecialPowers.Ci.nsICookie);
ok(true, "cookie: " + cookie);
ok(true, "cookie host " + cookie.host + " path " + cookie.path + " name " + cookie.name +
" value " + cookie.value + " isSecure " + cookie.isSecure + " expires " + cookie.expires);
++count;
}
ok(true, count + " cookies");

cs.removeAll();
cs.add(domain, "", "oh", "hai", false, false, true, Math.pow(2, 62));
is(cs.countCookiesFromHost(domain), 1, "number of cookies for domain " + domain);
var prefSet = new Promise(resolve => {
SpecialPowers.pushPrefEnv({ set: [["network.cookie.cookieBehavior", 1]] }, resolve);
});

gExpectedCookies = cookies;
gExpectedLoads = loads;
gExpectedHeaders = headers;

gObs = new obs();
gScript = SpecialPowers.loadChromeScript(SCRIPT_URL);
gScript.addMessageListener("info", ({ str }) => info(str));
gScript.addMessageListener("ok", ({ c, m }) => ok(c, m));
gScript.addMessageListener("observer:gotCookie", ({ cookie, uri }) => {
isnot(cookie.indexOf("oh=hai"), -1,
"cookie 'oh=hai' is in header for " + uri);
++gHeaders;
});

var scriptReady = new Promise(resolve => {
gScript.addMessageListener("init:return", resolve);
gScript.sendAsyncMessage("init", { domain });
});

// Listen for MessageEvents.
window.addEventListener("message", messageReceiver, false);

// load a window which contains an iframe; each will attempt to set
// cookies from their respective domains.
gPopup = window.open(uri, 'hai', 'width=100,height=100');
Promise.all([ prefSet, scriptReady ]).then(() => {
// load a window which contains an iframe; each will attempt to set
// cookies from their respective domains.
gPopup = window.open(uri, 'hai', 'width=100,height=100');
});
}

function finishTest()
{
gObs.remove();

SpecialPowers.Cc["@mozilla.org/preferences-service;1"]
.getService(SpecialPowers.Ci.nsIPrefBranch)
.clearUserPref("network.cookie.cookieBehavior");

SimpleTest.finish();
}

// Count headers.
function obs () {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

ok(true, "adding observer");

this.window = window;
this.os = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
.getService(SpecialPowers.Ci.nsIObserverService);
this.os.addObserver(this, "http-on-modify-request", false);
}

obs.prototype = {
observe: function obs_observe (theSubject, theTopic, theData)
{
ok(true, "theSubject " + theSubject);
ok(true, "theTopic " + theTopic);
ok(true, "theData " + theData);

var channel = theSubject.QueryInterface(
this.window.SpecialPowers.Ci.nsIHttpChannel);
ok(true, "channel " + channel);
try {
ok(true, "channel.URI " + channel.URI);
ok(true, "channel.URI.spec " + channel.URI.spec);
channel.visitRequestHeaders({
visitHeader: function(aHeader, aValue) {
ok(true, aHeader + ": " + aValue);
}});
} catch (err) {
ok(false, "catch error " + err);
}

// Ignore notifications we don't care about (like favicons)
if (channel.URI.spec.indexOf(
"http://example.org/tests/extensions/cookie/test/") == -1) {
ok(true, "ignoring this one");
return;
}

this.window.isnot(channel.getRequestHeader("Cookie").indexOf("oh=hai"), -1,
"cookie 'oh=hai' is in header for " + channel.URI.spec);
++gHeaders;
},

remove: function obs_remove()
{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

ok(true, "removing observer");

this.os.removeObserver(this, "http-on-modify-request");
this.os = null;
this.window = null;
}
gScript.addMessageListener("shutdown:return", () => {
gScript.destroy();
SimpleTest.finish();
});
gScript.sendAsyncMessage("shutdown");
}

/** Receives MessageEvents to this window. */
Expand All @@ -126,13 +65,13 @@ function messageReceiver(evt)
ok(evt.data == "f_lf_i msg data img" || evt.data == "f_lf_i msg data page",
"message data received from popup");
if (evt.data == "f_lf_i msg data img") {
ok(true, "message data received from popup for image");
info("message data received from popup for image");
}
if (evt.data == "f_lf_i msg data page") {
ok(true, "message data received from popup for page");
info("message data received from popup for page");
}
if (evt.data != "f_lf_i msg data img" && evt.data != "f_lf_i msg data page") {
ok(true, "got this message but don't know what it is " + evt.data);
info("got this message but don't know what it is " + evt.data);
gPopup.close();
window.removeEventListener("message", messageReceiver, false);

Expand All @@ -156,20 +95,10 @@ function runTest() {
document.cookie = "o=noes";

is(gHeaders, gExpectedHeaders, "number of observed request headers");
gScript.addMessageListener("getCookieCount:return", ({ count }) => {
is(count, gExpectedCookies, "total number of cookies");
finishTest();
});

var cs = SpecialPowers.Cc["@mozilla.org/cookiemanager;1"]
.getService(SpecialPowers.Ci.nsICookieManager);
var count = 0;
var list = cs.enumerator;
while (list.hasMoreElements()) {
var cookie = list.getNext().QueryInterface(SpecialPowers.Ci.nsICookie);
ok(true, "cookie: " + cookie);
ok(true, "cookie host " + cookie.host + " path " + cookie.path + " name " + cookie.name +
" value " + cookie.value + " isSecure " + cookie.isSecure + " expires " + cookie.expires);
++count;
}
is(count, gExpectedCookies, "total number of cookies");
cs.removeAll();

finishTest();
gScript.sendAsyncMessage("getCookieCount");
}
Loading

0 comments on commit db4df95

Please sign in to comment.