Bug 1639807 - Isolate image cache per first-party when privacy.partition.network_state is set to true - part 3 - tests, r=timhuang
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 21 May 2020 11:53:49 +0000
changeset 531424 7a3e41165d8970875a9275693db32c1482d47310
parent 531423 c904f37eb5aded2df116d0457e31521e28f224ea
child 531425 2eca3c8ca51ffbd17b0bd6d7f1b866cced131a45
push id37439
push userbtara@mozilla.com
push dateThu, 21 May 2020 21:49:34 +0000
treeherdermozilla-central@92c11f0bf14b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstimhuang
bugs1639807
milestone78.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1639807 - Isolate image cache per first-party when privacy.partition.network_state is set to true - part 3 - tests, r=timhuang Differential Revision: https://phabricator.services.mozilla.com/D76284
toolkit/components/antitracking/test/xpcshell/test_staticPartition_image.js
toolkit/components/antitracking/test/xpcshell/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/toolkit/components/antitracking/test/xpcshell/test_staticPartition_image.js
@@ -0,0 +1,85 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+const { CookieXPCShellUtils } = ChromeUtils.import(
+  "resource://testing-common/CookieXPCShellUtils.jsm"
+);
+
+CookieXPCShellUtils.init(this);
+
+let gHits = 0;
+
+add_task(async function() {
+  do_get_profile();
+
+  info("Disable predictor and accept all");
+  Services.prefs.setBoolPref("network.predictor.enabled", false);
+  Services.prefs.setBoolPref("network.predictor.enable-prefetch", false);
+  Services.prefs.setBoolPref("network.http.rcwn.enabled", false);
+  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
+
+  const server = CookieXPCShellUtils.createServer({
+    hosts: ["example.org", "foo.com"],
+  });
+  server.registerPathHandler("/image.png", (metadata, response) => {
+    gHits++;
+    response.setHeader("Cache-Control", "max-age=10000", false);
+    response.setStatusLine(metadata.httpVersion, 200, "OK");
+    response.setHeader("Content-Type", "image/png", false);
+    var body =
+      "iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAIAAADZSiLoAAAAEUlEQVQImWP4z8AAQTAamQkAhpcI+DeMzFcAAAAASUVORK5CYII=";
+    response.bodyOutputStream.write(body, body.length);
+  });
+
+  server.registerPathHandler("/image", (metadata, response) => {
+    response.setStatusLine(metadata.httpVersion, 200, "OK");
+    response.setHeader("Content-Type", "text/html", false);
+    var body = `<img src="http://example.org/image.png">`;
+    response.bodyOutputStream.write(body, body.length);
+  });
+
+  const tests = [
+    {
+      prefValue: true,
+      hitsCount: 2,
+    },
+    {
+      prefValue: false,
+      hitsCount: 1,
+    },
+  ];
+
+  for (let test of tests) {
+    info("Clear image and network caches");
+    let imageCache = Cc["@mozilla.org/image/tools;1"]
+      .getService(Ci.imgITools)
+      .getImgCacheForDocument(null);
+    imageCache.clearCache(true); // true=chrome
+    imageCache.clearCache(false); // false=content
+    Services.cache2.clear();
+
+    info("Reset the hits count");
+    gHits = 0;
+
+    info("Enabling network state partitioning");
+    Services.prefs.setBoolPref(
+      "privacy.partition.network_state",
+      test.prefValue
+    );
+
+    info("Let's load a page with origin A");
+    let contentPage = await CookieXPCShellUtils.loadContentPage(
+      "http://example.org/image"
+    );
+    await contentPage.close();
+
+    info("Let's load a page with origin B");
+    contentPage = await CookieXPCShellUtils.loadContentPage(
+      "http://foo.com/image"
+    );
+    await contentPage.close();
+
+    Assert.equal(gHits, test.hitsCount, "The number of hits match");
+  }
+});
--- a/toolkit/components/antitracking/test/xpcshell/xpcshell.ini
+++ b/toolkit/components/antitracking/test/xpcshell/xpcshell.ini
@@ -1,8 +1,10 @@
 [DEFAULT]
 head = head.js ../../../../components/url-classifier/tests/unit/head_urlclassifier.js
 
 [test_cookie_behavior.js]
 [test_purge_trackers.js]
 [test_tracking_db_service.js]
 [test_rejectForeignAllowList.js]
 skip-if = toolkit == 'android' # Bug 1567341
+[test_staticPartition_image.js]
+skip-if = toolkit == 'android' # Bug 1567341