Bug 1264572 - Test the Isolate the Image Cache per url bar domain. r=baku
authorDave Huseby <dhuseby@mozilla.com>
Thu, 15 Sep 2016 10:02:00 -0400
changeset 357834 32bb28f7009253b367e4e9bbcda234f0f92c1e58
parent 357833 a4754f5f3a007f666f5319046ece2c18c6b546e3
child 357835 c6876f222943810536b0970db21053ceafda3097
push id1324
push usermtabara@mozilla.com
push dateMon, 16 Jan 2017 13:07:44 +0000
treeherdermozilla-release@a01c49833940 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1264572
milestone51.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 1264572 - Test the Isolate the Image Cache per url bar domain. r=baku
browser/components/originattributes/test/browser/browser.ini
browser/components/originattributes/test/browser/browser_imageCacheIsolation.js
browser/components/originattributes/test/browser/head.js
--- a/browser/components/originattributes/test/browser/browser.ini
+++ b/browser/components/originattributes/test/browser/browser.ini
@@ -20,8 +20,9 @@ support-files =
   test_firstParty_postMessage.html
   window.html
   worker_blobify.js
   worker_deblobify.js
 
 [browser_firstPartyIsolation.js]
 [browser_localStorageIsolation.js]
 [browser_blobURLIsolation.js]
+[browser_imageCacheIsolation.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/originattributes/test/browser/browser_imageCacheIsolation.js
@@ -0,0 +1,78 @@
+/*
+ * Bug 1264572 - A test case for image cache isolation.
+ */
+
+let Cu = Components.utils;
+let {HttpServer} = Cu.import("resource://testing-common/httpd.js", {});
+
+const NUM_ISOLATION_LOADS = 2;
+const NUM_CACHED_LOADS = 1;
+
+let gHits = 0;
+
+let server = new HttpServer();
+server.registerPathHandler('/image.png', imageHandler);
+server.registerPathHandler('/file.html', fileHandler);
+server.start(-1);
+
+registerCleanupFunction(() => {
+  server.stop(() => {
+    server = null;
+  });
+});
+
+let BASE_URI = 'http://localhost:' + server.identity.primaryPort;
+let IMAGE_URI = BASE_URI + '/image.png';
+let FILE_URI = BASE_URI + '/file.html';
+
+function imageHandler(metadata, response) {
+  info('XXX: loading image from server');
+  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);
+}
+
+function fileHandler(metadata, response) {
+  response.setStatusLine(metadata.httpVersion, 200, "OK");
+  response.setHeader("Content-Type", "text/html", false);
+  let body = `<html><body><image src=${IMAGE_URI}></body></html>`;
+  response.bodyOutputStream.write(body, body.length);
+}
+
+function doBefore() {
+  // reset hit counter
+  info('XXX resetting gHits');
+  gHits = 0;
+  info('XXX clearing image cache');
+  let imageCache = Cc["@mozilla.org/image/tools;1"]
+                      .getService(Ci.imgITools)
+                      .getImgCacheForDocument(null);
+  imageCache.clearCache(true);
+  imageCache.clearCache(false);
+  info('XXX clearning network cache');
+  let networkCache = Cc["@mozilla.org/netwerk/cache-storage-service;1"]
+                        .getService(Ci.nsICacheStorageService);
+  networkCache.clear();
+}
+
+// the test function does nothing on purpose.
+function doTest(aBrowser) {
+  return 0;
+}
+
+// the check function
+function doCheck(shouldIsolate, a, b) {
+  // if we're doing first party isolation and the image cache isolation is
+  // working, then gHits should be 2 because the image would have been loaded
+  // one per first party domain.  if first party isolation is disabled, then
+  // gHits should be 1 since there would be one image load from the server and
+  // one load from the image cache.
+  info(`XXX check: gHits == ${gHits}, shouldIsolate == ${shouldIsolate}`);
+  return shouldIsolate ? gHits == NUM_ISOLATION_LOADS
+                       : gHits == NUM_CACHED_LOADS;
+}
+
+IsolationTestTools.runTests(FILE_URI, doTest, doCheck, doBefore);
--- a/browser/components/originattributes/test/browser/head.js
+++ b/browser/components/originattributes/test/browser/head.js
@@ -265,18 +265,21 @@ this.IsolationTestTools = {
    *    Each of these functions will be provided the browser object of the tab,
    *    that allows modifying or fetchings results from the page content.
    * @param aCompareResultFunc
    *    An optional function which allows modifying the way how does framework
    *    check results. This function will be provided a boolean to indicate
    *    the isolation is no or off and two results. This function should return
    *    a boolean to tell that whether isolation is working. If this function
    *    is not given, the framework will take case checking by itself.
+   * @param aBeforeFunc
+   *    An optional function which is called before any tabs are created so
+   *    that the test case can set up/reset local state.
    */
-  runTests(aURL, aGetResultFuncs, aCompareResultFunc) {
+  runTests(aURL, aGetResultFuncs, aCompareResultFunc, aBeforeFunc) {
     let pageURL;
     let firstFrameSetting;
     let secondFrameSetting;
 
     if (typeof aURL === "string") {
       pageURL = aURL;
     } else if (typeof aURL === "object") {
       pageURL = aURL.url;
@@ -292,16 +295,21 @@ this.IsolationTestTools = {
                         { firstPartyDomain: "http://example.com", userContextId: 1},
                         { firstPartyDomain: "http://example.org", userContextId: 2}
                       ];
 
     this._add_task(function* (aMode) {
       let tabSettingA = 0;
 
       for (let tabSettingB of [0, 1]) {
+        // Give the test a chance to set up before each case is run.
+        if (aBeforeFunc) {
+          yield aBeforeFunc();
+        }
+
         // Create Tabs.
         let tabInfoA = yield IsolationTestTools._addTab(aMode,
                                                         pageURL,
                                                         tabSettings[tabSettingA],
                                                         firstFrameSetting);
         let tabInfoB = yield IsolationTestTools._addTab(aMode,
                                                         pageURL,
                                                         tabSettings[tabSettingB],