From db4df95d702b7c8b20d7ac7f911ea5f59d4a7ff1 Mon Sep 17 00:00:00 2001 From: Blake Kaplan Date: Thu, 21 Jan 2016 15:15:41 -0800 Subject: [PATCH] Bug 1241557 - Make cookie tests pass in e10s. r=mccr8 --HG-- extra : transplant_source : %98%27%3E%7D%9C%C6NA%C1%D0w%F1%F7%AA%5Bm%E21q%8D --- dom/html/test/mochitest.ini | 3 +- dom/html/test/test_non-ascii-cookie.html | 54 ++++--- extensions/cookie/test/file_chromecommon.js | 15 ++ extensions/cookie/test/file_testcommon.js | 48 +++--- extensions/cookie/test/file_testloadflags.js | 149 +++++------------- .../test/file_testloadflags_chromescript.js | 112 +++++++++++++ extensions/cookie/test/mochitest.ini | 4 +- 7 files changed, 220 insertions(+), 165 deletions(-) create mode 100644 extensions/cookie/test/file_chromecommon.js create mode 100644 extensions/cookie/test/file_testloadflags_chromescript.js diff --git a/dom/html/test/mochitest.ini b/dom/html/test/mochitest.ini index 70bd4c62ffc8d..45982ec4781e6 100644 --- a/dom/html/test/mochitest.ini +++ b/dom/html/test/mochitest.ini @@ -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 diff --git a/dom/html/test/test_non-ascii-cookie.html b/dom/html/test/test_non-ascii-cookie.html index fc307265a659e..ab96ac07c2dc4 100644 --- a/dom/html/test/test_non-ascii-cookie.html +++ b/dom/html/test/test_non-ascii-cookie.html @@ -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(); +}); diff --git a/extensions/cookie/test/file_chromecommon.js b/extensions/cookie/test/file_chromecommon.js new file mode 100644 index 0000000000000..4383876b25b5e --- /dev/null +++ b/extensions/cookie/test/file_chromecommon.js @@ -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(); diff --git a/extensions/cookie/test/file_testcommon.js b/extensions/cookie/test/file_testcommon.js index 0058f308ea439..3ee53c52cb56b 100644 --- a/extensions/cookie/test/file_testcommon.js +++ b/extensions/cookie/test/file_testcommon.js @@ -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(); @@ -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"); } diff --git a/extensions/cookie/test/file_testloadflags.js b/extensions/cookie/test/file_testloadflags.js index 1d21b50e17413..9cf4a15a6bded 100644 --- a/extensions/cookie/test/file_testloadflags.js +++ b/extensions/cookie/test/file_testloadflags.js @@ -1,3 +1,5 @@ +const SCRIPT_URL = SimpleTest.getTestFileURL('file_testloadflags_chromescript.js'); + var gExpectedCookies; var gExpectedHeaders; var gExpectedLoads; @@ -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. */ @@ -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); @@ -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"); } diff --git a/extensions/cookie/test/file_testloadflags_chromescript.js b/extensions/cookie/test/file_testloadflags_chromescript.js new file mode 100644 index 0000000000000..ad9b0fe53e230 --- /dev/null +++ b/extensions/cookie/test/file_testloadflags_chromescript.js @@ -0,0 +1,112 @@ +let { classes: Cc, interfaces: Ci } = Components; + +var gObs; + +function info(s) { + sendAsyncMessage("info", { str: String(s) }); +} + +function ok(c, m) { + sendAsyncMessage("ok", { c, m }); +} + +function is(a, b, m) { + ok(Object.is(a, b), m + " (" + a + " === " + b + ")"); +} + +// Count headers. +function obs() { + info("adding observer"); + + this.os = Cc["@mozilla.org/observer-service;1"] + .getService(Ci.nsIObserverService); + this.os.addObserver(this, "http-on-modify-request", false); +} + +obs.prototype = { + observe(theSubject, theTopic, theData) { + info("theSubject " + theSubject); + info("theTopic " + theTopic); + info("theData " + theData); + + var channel = theSubject.QueryInterface(Ci.nsIHttpChannel); + info("channel " + channel); + try { + info("channel.URI " + channel.URI); + info("channel.URI.spec " + channel.URI.spec); + channel.visitRequestHeaders({ + visitHeader: function(aHeader, aValue) { + info(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) { + info("ignoring this one"); + return; + } + + sendAsyncMessage("observer:gotCookie", + { cookie: channel.getRequestHeader("Cookie"), + uri: channel.URI.spec }); + }, + + remove() { + info("removing observer"); + + this.os.removeObserver(this, "http-on-modify-request"); + this.os = null; + } +} + +function getCookieCount(cs) { + let count = 0; + let list = cs.enumerator; + while (list.hasMoreElements()) { + let cookie = list.getNext().QueryInterface(Ci.nsICookie); + info("cookie: " + cookie); + info("cookie host " + cookie.host + " path " + cookie.path + " name " + cookie.name + + " value " + cookie.value + " isSecure " + cookie.isSecure + " expires " + cookie.expires); + ++count; + } + + return count; +} + +addMessageListener("init", ({ domain }) => { + let cs = Cc["@mozilla.org/cookiemanager;1"] + .getService(Ci.nsICookieManager2); + + info("we are going to remove these cookies"); + + let count = getCookieCount(cs); + info(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); + + gObs = new obs(); + sendAsyncMessage("init:return"); +}); + +addMessageListener("getCookieCount", () => { + let cs = Cc["@mozilla.org/cookiemanager;1"] + .getService(Ci.nsICookieManager); + let count = getCookieCount(cs); + + cs.removeAll(); + sendAsyncMessage("getCookieCount:return", { count }); +}); + +addMessageListener("shutdown", () => { + gObs.remove(); + + let cs = Cc["@mozilla.org/cookiemanager;1"] + .getService(Ci.nsICookieManager2); + cs.removeAll(); + sendAsyncMessage("shutdown:return"); +}); diff --git a/extensions/cookie/test/mochitest.ini b/extensions/cookie/test/mochitest.ini index 794dce7d52e09..5253274f188cb 100644 --- a/extensions/cookie/test/mochitest.ini +++ b/extensions/cookie/test/mochitest.ini @@ -1,10 +1,11 @@ [DEFAULT] -skip-if = buildapp == 'b2g' || e10s +skip-if = buildapp == 'b2g' support-files = beltzner.jpg beltzner.jpg^headers^ damonbowling.jpg damonbowling.jpg^headers^ + file_chromecommon.js file_domain_hierarchy_inner.html file_domain_hierarchy_inner_inner.html file_domain_hierarchy_inner_inner_inner.html @@ -18,6 +19,7 @@ support-files = file_subdomain_inner.html file_testcommon.js file_testloadflags.js + file_testloadflags_chromescript.js image1.png image1.png^headers^ image2.png