Bug 1397624 Add tests for privacy.firstparty.isolate.pbmode.enabled r?tanvi draft
authorTom Ritter <tom@mozilla.com>
Thu, 29 Mar 2018 13:21:35 -0500
changeset 781818 e75e200aa3fc618051c621907c76d2ec49c301cb
parent 781817 e9cbcaa2f44c6c2c5171497c116dc4fbcb3126e6
push id106421
push userbmo:tom@mozilla.com
push dateFri, 13 Apr 2018 18:10:23 +0000
reviewerstanvi
bugs1397624
milestone61.0a1
Bug 1397624 Add tests for privacy.firstparty.isolate.pbmode.enabled r?tanvi MozReview-Commit-ID: 9mHZuw9aB4x
browser/components/originattributes/test/browser/browser_firstPartyIsolation.js
--- a/browser/components/originattributes/test/browser/browser_firstPartyIsolation.js
+++ b/browser/components/originattributes/test/browser/browser_firstPartyIsolation.js
@@ -12,47 +12,50 @@ async function principal_test(browser, s
   });
 
   await ContentTask.spawn(tab.linkedBrowser, { shouldBeIsolated, firstPartyDomain: BASE_DOMAIN }, async function(attrs) {
     let assertFunc = function(b) { return attrs.shouldBeIsolated ? b : !b; };
     info("document principal: " + content.document.nodePrincipal.origin);
     Assert.ok(docShell.getOriginAttributes().firstPartyDomain == "",
                  "top-level docShell shouldn't have firstPartyDomain attribute under any circumstance.");
     Assert.ok(assertFunc(content.document.nodePrincipal.originAttributes.firstPartyDomain ==
-                 attrs.firstPartyDomain), "The document should have firstPartyDomain");
+                 attrs.firstPartyDomain), "The document should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain");
 
     for (let i = 1; i < 4; i++) {
       let iframe = content.document.getElementById("iframe" + i);
       info("iframe principal: " + iframe.contentDocument.nodePrincipal.origin);
       Assert.ok(assertFunc(iframe.frameLoader.docShell.getOriginAttributes().firstPartyDomain ==
-                   attrs.firstPartyDomain), "iframe's docshell should have firstPartyDomain");
+                   attrs.firstPartyDomain), "iframe's docshell should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain");
       Assert.ok(assertFunc(iframe.contentDocument.nodePrincipal.originAttributes.firstPartyDomain ==
-                   attrs.firstPartyDomain), "iframe should have firstPartyDomain");
+                   attrs.firstPartyDomain), "iframe should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain");
     }
   });
 
   browser.removeTab(tab);
 }
 
 /**
  * Test for the cookie jars of the top-level document and child iframe should be
  * isolated by firstPartyDomain.
+ *
+ * If we are in Private Browsing Mode, we need to provide that in the query to
+ * the cookie jar, otherwise we always get the non-PBM cookie jar.
  */
-async function cookie_test(browser, shouldBeIsolated) {
+async function cookie_test(browser, pbm, shouldBeIsolated) {
   let assertFunc = function(b) { return shouldBeIsolated ? b : !b; };
   let tab = BrowserTestUtils.addTab(browser, BASE_URL + "test_firstParty_cookie.html");
   await BrowserTestUtils.browserLoaded(tab.linkedBrowser, true);
 
-  let iter = Services.cookies.enumerator;
+  let iter = Services.cookies.getCookiesWithOriginAttributes(JSON.stringify({privateBrowsingId: pbm ? 1 : 0}));
   let count = 0;
   while (iter.hasMoreElements()) {
     count++;
     let cookie = iter.getNext().QueryInterface(Ci.nsICookie2);
     Assert.equal(cookie.value, "foo", "Cookie value should be foo");
-    Assert.ok(assertFunc(cookie.originAttributes.firstPartyDomain == BASE_DOMAIN), "Cookie's origin attributes should be " + BASE_DOMAIN);
+    Assert.ok(assertFunc(cookie.originAttributes.firstPartyDomain == BASE_DOMAIN), "Cookie's origin attributes should " + (assertFunc(true) ? "" : "not") + " be " + BASE_DOMAIN);
   }
 
   // one cookie is from requesting test.js from top-level doc, and the other from
   // requesting test2.js from iframe test2.html.
   Assert.equal(count, 2, "Should have two cookies");
 
   browser.removeTab(tab);
 }
@@ -68,33 +71,33 @@ async function redirect_test(browser, sh
   await ContentTask.spawn(tab.linkedBrowser, { shouldBeIsolated, firstPartyDomain: "example.com" }, async function(attrs) {
     let assertFunc = function(b) { return attrs.shouldBeIsolated ? b : !b; };
     info("document principal: " + content.document.nodePrincipal.origin);
     info("document uri: " + content.document.documentURI);
 
     Assert.equal(content.document.documentURI, "http://example.com/browser/browser/components/originattributes/test/browser/dummy.html",
                  "The page should have been redirected to http://example.com/browser/browser/components/originattributes/test/browser/dummy.html");
     Assert.ok(assertFunc(content.document.nodePrincipal.originAttributes.firstPartyDomain ==
-                 attrs.firstPartyDomain), "The document should have firstPartyDomain");
+                 attrs.firstPartyDomain), "The document should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain");
   });
 
   // Since this is a HTML redirect, we wait until the final page is loaded.
   let tab2 = BrowserTestUtils.addTab(browser, BASE_URL + "test_firstParty_html_redirect.html");
   await BrowserTestUtils.browserLoaded(tab2.linkedBrowser, false, function(url) {
     return url == "http://example.com/";
   });
 
   await ContentTask.spawn(tab2.linkedBrowser, { shouldBeIsolated, firstPartyDomain: "example.com" }, async function(attrs) {
     let assertFunc = function(b) { return attrs.shouldBeIsolated ? b : !b; };
     info("2nd tab document principal: " + content.document.nodePrincipal.origin);
     info("2nd tab document uri: " + content.document.documentURI);
     Assert.equal(content.document.documentURI, "http://example.com/",
                  "The page should have been redirected to http://example.com");
     Assert.ok(assertFunc(content.document.nodePrincipal.originAttributes.firstPartyDomain ==
-                 attrs.firstPartyDomain), "The document should have firstPartyDomain");
+                 attrs.firstPartyDomain), "The document should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain");
   });
 
   let tab3 = BrowserTestUtils.addTab(browser, BASE_URL + "test_firstParty_iframe_http_redirect.html");
   await BrowserTestUtils.browserLoaded(tab3.linkedBrowser, true, function(url) {
     return url == (BASE_URL + "test_firstParty_iframe_http_redirect.html");
   });
 
   // This redirect happens on the iframe, so unlike the two redirect tests above,
@@ -104,40 +107,39 @@ async function redirect_test(browser, sh
     let assertFunc = function(b) { return attrs.shouldBeIsolated ? b : !b; };
     let iframe = content.document.getElementById("iframe1");
     info("iframe document principal: " + iframe.contentDocument.nodePrincipal.origin);
     info("iframe document uri: " + iframe.contentDocument.documentURI);
 
     Assert.equal(iframe.contentDocument.documentURI, "http://example.com/browser/browser/components/originattributes/test/browser/dummy.html",
                  "The page should have been redirected to http://example.com/browser/browser/components/originattributes/test/browser/dummy.html");
     Assert.ok(assertFunc(iframe.contentDocument.nodePrincipal.originAttributes.firstPartyDomain ==
-                 attrs.firstPartyDomain), "The iframe should have firstPartyDomain: " + attrs.firstPartyDomain);
+                 attrs.firstPartyDomain), "The iframe should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain: " + attrs.firstPartyDomain);
   });
 
   browser.removeTab(tab);
   browser.removeTab(tab2);
   browser.removeTab(tab3);
 }
 
 /**
  * Test for postMessage between document and iframe.
  */
-async function postMessage_test(browser, shouldBeIsolated) {
+async function postMessage_test(browser) {
   let tab = BrowserTestUtils.addTab(browser, BASE_URL + "test_firstParty_postMessage.html");
 
   // The top-level page will post a message to its child iframe, and wait for
   // another message from the iframe, once it receives the message, it will
   // create another iframe, dummy.html.
   // So we wait until dummy.html is loaded
   await BrowserTestUtils.browserLoaded(tab.linkedBrowser, true, function(url) {
     return url == BASE_URL + "dummy.html";
   });
 
-  await ContentTask.spawn(tab.linkedBrowser, {shouldBeIsolated}, async function(attrs) {
-    let assertFunc = function(b) { return attrs.shouldBeIsolated ? b : !b; };
+  await ContentTask.spawn(tab.linkedBrowser, {}, async function() {
     info("document principal: " + content.document.nodePrincipal.origin);
     let value = content.document.getElementById("message").textContent;
     Assert.equal(value, "OK", "We should have gotten an OK from postMesage");
   });
 
   browser.removeTab(tab);
 }
 
@@ -152,25 +154,25 @@ async function openWindow_test(browser, 
   });
 
   let tab = BrowserTestUtils.addTab(browser, BASE_URL + "window.html");
   let win = await BrowserTestUtils.waitForNewWindow();
 
   await ContentTask.spawn(win.gBrowser.selectedBrowser, { shouldBeIsolated, firstPartyDomain: "mochi.test" }, async function(attrs) {
     let assertFunc = function(b) { return attrs.shouldBeIsolated ? b : !b; };
     Assert.ok(assertFunc(docShell.getOriginAttributes().firstPartyDomain == attrs.firstPartyDomain),
-                 "window.open() should have firstPartyDomain attribute");
+                 "window.open() should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain attribute");
     Assert.ok(assertFunc(content.document.nodePrincipal.originAttributes.firstPartyDomain ==
-                 attrs.firstPartyDomain), "The document should have firstPartyDomain");
+                 attrs.firstPartyDomain), "The document should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain");
 
     let iframe = content.document.getElementById("iframe1");
     Assert.ok(assertFunc(iframe.frameLoader.docShell.getOriginAttributes().firstPartyDomain ==
-                 attrs.firstPartyDomain), "iframe's docshell should have firstPartyDomain");
+                 attrs.firstPartyDomain), "iframe's docshell should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain");
     Assert.ok(assertFunc(iframe.contentDocument.nodePrincipal.originAttributes.firstPartyDomain ==
-                 attrs.firstPartyDomain), "iframe should have firstPartyDomain");
+                 attrs.firstPartyDomain), "iframe should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain");
   });
 
   browser.removeTab(tab);
   await BrowserTestUtils.closeWindow(win);
 }
 
 /**
  * When the web page calls window.open, the top-level docshell in the new
@@ -184,19 +186,19 @@ async function window_open_redirect_test
 
   let tab = BrowserTestUtils.addTab(browser, BASE_URL + "window_redirect.html");
   let win = await BrowserTestUtils.waitForNewWindow();
   await BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser);
 
   await ContentTask.spawn(win.gBrowser.selectedBrowser, { shouldBeIsolated, firstPartyDomain: "mochi.test" }, async function(attrs) {
     let assertFunc = function(b) { return attrs.shouldBeIsolated ? b : !b; };
     Assert.ok(assertFunc(docShell.getOriginAttributes().firstPartyDomain == attrs.firstPartyDomain),
-                 "window.open() should have firstPartyDomain attribute");
+                 "window.open() should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain attribute");
     Assert.ok(assertFunc(content.document.nodePrincipal.originAttributes.firstPartyDomain ==
-                 attrs.firstPartyDomain), "The document should have firstPartyDomain");
+                 attrs.firstPartyDomain), "The document should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain");
   });
 
   browser.removeTab(tab);
   await BrowserTestUtils.closeWindow(win);
 }
 
 /**
  * When the web page calls window.open, the top-level docshell in the new
@@ -212,45 +214,45 @@ async function window_open_iframe_test(b
 
   let tab = BrowserTestUtils.addTab(browser, BASE_URL + "window2.html");
   let win = await BrowserTestUtils.waitForNewWindow();
   await BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser, true);
 
   await ContentTask.spawn(win.gBrowser.selectedBrowser, { shouldBeIsolated, firstPartyDomain: "mochi.test" }, async function(attrs) {
     let assertFunc = function(b) { return attrs.shouldBeIsolated ? b : !b; };
     Assert.ok(assertFunc(docShell.getOriginAttributes().firstPartyDomain == attrs.firstPartyDomain),
-                 "window.open() should have firstPartyDomain attribute");
+                 "window.open() should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain attribute");
 
     // The document is http://example.com/browser/browser/components/originattributes/test/browser/test_firstParty.html
     // so the firstPartyDomain will be overriden to 'example.com'.
     Assert.ok(assertFunc(content.document.nodePrincipal.originAttributes.firstPartyDomain ==
-                 "example.com"), "The document should have firstPartyDomain");
+                 "example.com"), "The document should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain equal to example.com");
 
     let iframe = content.document.getElementById("iframe1");
     Assert.ok(assertFunc(iframe.frameLoader.docShell.getOriginAttributes().firstPartyDomain ==
-                 "example.com"), "iframe's docshell should have firstPartyDomain");
+                 "example.com"), "iframe's docshell should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain equal to example.com");
     Assert.ok(assertFunc(iframe.contentDocument.nodePrincipal.originAttributes.firstPartyDomain ==
-                 "example.com"), "iframe should have firstPartyDomain");
+                 "example.com"), "iframe should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain equal to example.com");
   });
 
   browser.removeTab(tab);
   await BrowserTestUtils.closeWindow(win);
 }
 
 /**
  * Test for the loadInfo->TriggeringPrincipal is the document itself.
  */
 async function form_test(browser, shouldBeIsolated) {
   let tab = BrowserTestUtils.addTab(browser, BASE_URL + "test_form.html");
   await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
 
   await ContentTask.spawn(tab.linkedBrowser, { shouldBeIsolated, firstPartyDomain: "mochi.test" }, async function(attrs) {
     let assertFunc = function(b) { return attrs.shouldBeIsolated ? b : !b; };
     Assert.ok(assertFunc(content.document.nodePrincipal.originAttributes.firstPartyDomain ==
-                 attrs.firstPartyDomain), "The document should have firstPartyDomain");
+                 attrs.firstPartyDomain), "The document should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain");
 
     let submit = content.document.getElementById("submit");
     submit.click();
   });
 
   browser.removeTab(tab);
 }
 
@@ -265,19 +267,19 @@ async function window_open_form_test(bro
 
   let tab = BrowserTestUtils.addTab(browser, BASE_URL + "window3.html");
   let win = await BrowserTestUtils.waitForNewWindow();
   await BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser, true);
 
   await ContentTask.spawn(win.gBrowser.selectedBrowser, { shouldBeIsolated, firstPartyDomain: "mochi.test" }, async function(attrs) {
     let assertFunc = function(b) { return attrs.shouldBeIsolated ? b : !b; };
     Assert.ok(assertFunc(docShell.getOriginAttributes().firstPartyDomain == attrs.firstPartyDomain),
-                 "window.open() should have firstPartyDomain attribute");
+                 "window.open() should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain attribute");
     Assert.ok(assertFunc(content.document.nodePrincipal.originAttributes.firstPartyDomain ==
-                 "example.com"), "The document should have firstPartyDomain");
+                 "example.com"), "The document should " + (assertFunc(true) ? "" : "not") + " have firstPartyDomain");
 
     let submit = content.document.getElementById("submit");
     submit.click();
   });
 
   browser.removeTab(tab);
   await BrowserTestUtils.closeWindow(win);
 }
@@ -285,24 +287,57 @@ async function window_open_form_test(bro
 // =================================================================================
 // Now we can actually perform the tests ===========================================
 // =================================================================================
 
 add_task(async function setup() {
   Services.prefs.setBoolPref("privacy.firstparty.isolate", true);
   registerCleanupFunction(function() {
     Services.prefs.clearUserPref("privacy.firstparty.isolate");
+    Services.prefs.clearUserPref("privacy.firstparty.isolate.pbmode.enabled");
     Services.cookies.removeAll();
   });
 });
 
 
 add_task(async function enableEverywhereTests() {
-  await cookie_test(gBrowser, true);
   await principal_test(gBrowser, true);
+  await cookie_test(gBrowser, false, true);
   await redirect_test(gBrowser, true);
-  await postMessage_test(gBrowser, true);
+  await postMessage_test(gBrowser);
   await openWindow_test(gBrowser, true);
   await window_open_redirect_test(gBrowser, true);
   await window_open_iframe_test(gBrowser, true);
   await form_test(gBrowser, true);
   await window_open_form_test(gBrowser, true);
-});
\ No newline at end of file
+});
+
+add_task(async function resetup() {
+  Services.prefs.setBoolPref("privacy.firstparty.isolate", false);
+  Services.prefs.setBoolPref("privacy.firstparty.isolate.pbmode.enabled", true);
+  Services.cookies.removeAll();
+});
+
+add_task(async function enableInPBOnlyTests() {
+  await principal_test(gBrowser, false);
+  await cookie_test(gBrowser, false, false);
+  await redirect_test(gBrowser, false);
+  await postMessage_test(gBrowser);
+  await openWindow_test(gBrowser, false);
+  await window_open_redirect_test(gBrowser, false);
+  await window_open_iframe_test(gBrowser, false);
+  await form_test(gBrowser, false);
+  await window_open_form_test(gBrowser, false);
+
+  Services.cookies.removeAll();
+
+  let pBrowser = await BrowserTestUtils.openNewBrowserWindow({private: true});
+  await principal_test(pBrowser.gBrowser, true);
+  await cookie_test(pBrowser.gBrowser, true, true);
+  await redirect_test(pBrowser.gBrowser, true);
+  await postMessage_test(pBrowser.gBrowser);
+  await openWindow_test(pBrowser.gBrowser, true);
+  await window_open_redirect_test(pBrowser.gBrowser, true);
+  await window_open_iframe_test(pBrowser.gBrowser, true);
+  await form_test(pBrowser.gBrowser, true);
+  await window_open_form_test(pBrowser.gBrowser, true);
+  await BrowserTestUtils.closeWindow(pBrowser);
+});