Bug 722861 - Tests for imgLoader privacy-respecting changes.
authorJosh Matthews <josh@joshmatthews.net>
Tue, 26 Jun 2012 00:04:59 -0400
changeset 105630 c3333e6e37aa746ccf694a9faa8091d5112988ab
parent 105629 ceb5b26840e0855b4cd4e6596243741f7cbb26b8
child 105631 6b4ab96903f2b3144f5598e2813a3237a515206e
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
bugs722861
milestone17.0a1
Bug 722861 - Tests for imgLoader privacy-respecting changes.
image/test/unit/test_private_channel.js
image/test/unit/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/image/test/unit/test_private_channel.js
@@ -0,0 +1,113 @@
+do_load_httpd_js();
+
+var server = new nsHttpServer();
+server.registerPathHandler('/image.png', imageHandler);
+server.start(8088);
+
+load('image_load_helpers.js');
+
+var gHits = 0;
+
+var gIoService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);  
+var gLoader = Cc["@mozilla.org/image/loader;1"].getService(Ci.imgILoader);
+
+function imageHandler(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);
+}
+
+var requests = [];
+var listeners = [];
+
+function NotificationCallbacks(isPrivate) {
+  this.usePrivateBrowsing = isPrivate;
+}
+
+NotificationCallbacks.prototype = {
+  QueryInterface: function (iid) {
+    if (iid.equals(Ci.nsISupports) ||
+        iid.equals(Ci.nsILoadContext))
+      return this;
+    throw Cr.NS_ERROR_NO_INTERFACE;
+  },
+  getInterface: function(iid) {
+    if (iid.equals(Ci.nsILoadContext))
+      return this;
+    throw Cr.NS_ERROR_NO_INTERFACE;
+  }
+};
+
+var gImgPath = 'http://localhost:8088/image.png';
+
+function setup_chan(path, isPrivate, callback) {
+  var uri = gIoService.newURI(gImgPath, null, null);
+  var chan = gIoService.newChannelFromURI(uri);
+  chan.notificationCallbacks = new NotificationCallbacks(isPrivate);
+  var channelListener = new ChannelListener();
+  chan.asyncOpen(channelListener, null);
+  
+  var listener = new ImageListener(null, callback);
+  listeners.push(listener);
+  var outlistener = {};
+  requests.push(gLoader.loadImageWithChannel(chan, listener, null, outlistener));
+  channelListener.outputListener = outlistener.value;
+  listener.synchronous = false;
+}
+
+function loadImage(isPrivate, callback) {
+  var listener = new ImageListener(null, callback);
+  var uri = gIoService.newURI(gImgPath, null, null);
+  var loadGroup = Cc["@mozilla.org/network/load-group;1"].createInstance(Ci.nsILoadGroup);
+  loadGroup.notificationCallbacks = new NotificationCallbacks(isPrivate);
+  requests.push(gLoader.loadImage(uri, null, null, null, loadGroup, listener, null, 0, null, null, null));
+  listener.synchronous = false;  
+}
+
+function run_loadImage_tests() {
+  clearAllImageCaches();
+  let cs = Cc["@mozilla.org/network/cache-service;1"].getService(Ci.nsICacheService);
+  cs.evictEntries(Ci.nsICache.STORE_ANYWHERE);
+
+  gHits = 0;
+  loadImage(false, function() {
+    loadImage(false, function() {
+      loadImage(true, function() {
+        loadImage(true, function() {
+          do_check_eq(gHits, 2);
+          do_test_finished();
+        });
+      });
+    });
+  });
+}
+
+function clearAllImageCaches() {
+  gLoader.QueryInterface(Ci.imgICache);
+  gLoader.clearCache(false, false);
+  gLoader.clearCache(false, true);
+}
+
+function run_test() {
+  clearAllImageCaches();
+  do_test_pending();
+ 
+  // We create a public channel that loads an image, then an identical
+  // one that should cause a cache read. We then create a private channel
+  // and load the same image, and do that a second time to ensure a cache
+  // read. In total, we should cause two separate http responses to occur,
+  // since the private channels shouldn't be able to use the public cache.
+  setup_chan('/image.png', false, function() {
+    setup_chan('/image.png', false, function() {
+      setup_chan('/image.png', true, function() {
+        setup_chan('/image.png', true, function() {
+          do_check_eq(gHits, 2);
+          run_loadImage_tests();
+        });
+      });
+    });
+  });
+}
\ No newline at end of file
--- a/image/test/unit/xpcshell.ini
+++ b/image/test/unit/xpcshell.ini
@@ -6,8 +6,9 @@ tail =
 [test_async_notification_404.js]
 [test_async_notification_animated.js]
 [test_encoder_apng.js]
 [test_encoder_png.js]
 [test_imgtools.js]
 # Bug 676968: test fails consistently on Android
 fail-if = os == "android"
 [test_moz_icon_uri.js]
+[test_private_channel.js]
\ No newline at end of file