Bug 1341343 - Test for overwriting alt-data r=michal draft
authorValentin Gosu <valentin.gosu@gmail.com>
Mon, 13 Mar 2017 11:38:38 +0100
changeset 497484 d7f412e7aaf563b6083c0f5811f52605ccb4c5da
parent 496313 35398cae65c1526ce45c23a5f8b5568c5ada4762
child 497485 4c026325572e60aae5b63258f05763dccb49d12e
push id48926
push uservalentin.gosu@gmail.com
push dateMon, 13 Mar 2017 13:01:30 +0000
reviewersmichal
bugs1341343
milestone55.0a1
Bug 1341343 - Test for overwriting alt-data r=michal MozReview-Commit-ID: HljsfPNtN42
netwerk/test/unit/test_alt-data_overwrite.js
netwerk/test/unit/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/netwerk/test/unit/test_alt-data_overwrite.js
@@ -0,0 +1,205 @@
+/**
+ * Test for overwriting the alternative data in a cache entry.
+ *
+ * - run_test loads a new channel
+ * - readServerContent checks the content, and saves alt-data
+ * - cacheFlushObserver creates a new channel with "text/binary" alt-data type
+ * - readAltContent checks that it gets back alt-data and creates a channel with the dummy/null alt-data type
+ * - readServerContent2 checks that it gets regular content, from the cache and tries to overwrite the alt-data with the same representation
+ * - cacheFlushObserver2 creates a new channel with "text/binary" alt-data type
+ * - readAltContent2 checks that it gets back alt-data, and tries to overwrite with a kind of alt-data
+ * - cacheFlushObserver3 creates a new channel with "text/binary2" alt-data type
+ * - readAltContent3 checks that it gets back the newly saved alt-data
+ */
+
+Cu.import("resource://testing-common/httpd.js");
+Cu.import("resource://gre/modules/NetUtil.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
+
+XPCOMUtils.defineLazyGetter(this, "URL", function() {
+  return "http://localhost:" + httpServer.identity.primaryPort + "/content";
+});
+
+let httpServer = null;
+
+function make_and_open_channel(url, altContentType, callback) {
+  let chan = NetUtil.newChannel({uri: url, loadUsingSystemPrincipal: true});
+  if (altContentType) {
+    let cc = chan.QueryInterface(Ci.nsICacheInfoChannel);
+    cc.preferAlternativeDataType(altContentType);
+  }
+  chan.asyncOpen2(new ChannelListener(callback, null));
+}
+
+const responseContent = "response body";
+const altContent = "!@#$%^&*()";
+const altContentType = "text/binary";
+const altContent2 = "abc";
+const altContentType2 = "text/binary2";
+
+let servedNotModified = false;
+
+function contentHandler(metadata, response)
+{
+  response.setHeader("Content-Type", "text/plain");
+  response.setHeader("Cache-Control", "no-cache");
+  response.setHeader("ETag", "test-etag1");
+  let etag = ""
+  try {
+    etag = metadata.getHeader("If-None-Match");
+  } catch(ex) {
+    etag = "";
+  }
+
+  if (etag == "test-etag1") {
+    response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
+    servedNotModified = true;
+  } else {
+    servedNotModified = false;
+    response.bodyOutputStream.write(responseContent, responseContent.length);
+  }
+}
+
+function run_test()
+{
+  do_get_profile();
+  httpServer = new HttpServer();
+  httpServer.registerPathHandler("/content", contentHandler);
+  httpServer.start(-1);
+
+  do_test_pending();
+  make_and_open_channel(URL, altContentType, readServerContent);
+}
+
+function readServerContent(request, buffer)
+{
+  let cc = request.QueryInterface(Ci.nsICacheInfoChannel);
+
+  do_check_eq(buffer, responseContent);
+  do_check_eq(cc.alternativeDataType, "");
+
+  do_execute_soon(() => {
+    let os = cc.openAlternativeOutputStream(altContentType);
+    os.write(altContent, altContent.length);
+    os.close();
+
+    do_execute_soon(flushAndOpenAltChannel);
+  });
+}
+
+function flushAndOpenAltChannel()
+{
+  // We need to do a GC pass to ensure the cache entry has been freed.
+  gc();
+  Services.cache2.QueryInterface(Ci.nsICacheTesting).flush(cacheFlushObserver);
+}
+
+// needs to be rooted
+let cacheFlushObserver = { observe: function() {
+  if (!cacheFlushObserver) {
+    do_print("ignoring cacheFlushObserver\n");
+    return;
+  }
+  cacheFlushObserver = null;
+  gc();
+  make_and_open_channel(URL, altContentType, readAltContent);
+}};
+
+function readAltContent(request, buffer, closure, fromCache)
+{
+  gc();
+  let cc = request.QueryInterface(Ci.nsICacheInfoChannel);
+
+  do_check_eq(fromCache || servedNotModified, true);
+  do_check_eq(cc.alternativeDataType, altContentType);
+  do_check_eq(buffer, altContent);
+
+  make_and_open_channel(URL, "dummy/null", readServerContent2);
+}
+
+function readServerContent2(request, buffer, closure, fromCache)
+{
+  gc();
+  let cc = request.QueryInterface(Ci.nsICacheInfoChannel);
+
+  do_check_eq(fromCache || servedNotModified, true);
+  do_check_eq(buffer, responseContent);
+  do_check_eq(cc.alternativeDataType, "");
+
+  do_execute_soon(() => {
+    let os = cc.openAlternativeOutputStream(altContentType);
+    os.write(altContent, altContent.length);
+    os.close();
+
+    do_execute_soon(flushAndOpenAltChannel2);
+  });
+}
+
+function flushAndOpenAltChannel2()
+{
+  // We need to do a GC pass to ensure the cache entry has been freed.
+  gc();
+  Services.cache2.QueryInterface(Ci.nsICacheTesting).flush(cacheFlushObserver2);
+}
+
+// needs to be rooted
+let cacheFlushObserver2 = { observe: function() {
+  if (!cacheFlushObserver2) {
+    do_print("ignoring cacheFlushObserver2\n");
+    return;
+  }
+  cacheFlushObserver2 = null;
+  gc();
+  make_and_open_channel(URL, altContentType, readAltContent2);
+}};
+
+function readAltContent2(request, buffer, closure, fromCache)
+{
+  gc();
+  let cc = request.QueryInterface(Ci.nsICacheInfoChannel);
+
+  do_check_eq(servedNotModified || fromCache, true);
+  do_check_eq(cc.alternativeDataType, altContentType);
+  do_check_eq(buffer, altContent);
+
+  do_execute_soon(() => {
+    gc();
+    do_print("writing other content\n");
+    let os = cc.openAlternativeOutputStream(altContentType2);
+    os.write(altContent2, altContent2.length);
+    os.close();
+
+    do_execute_soon(flushAndOpenAltChannel3);
+  });
+}
+
+function flushAndOpenAltChannel3()
+{
+  // We need to do a GC pass to ensure the cache entry has been freed.
+  gc();
+  Services.cache2.QueryInterface(Ci.nsICacheTesting).flush(cacheFlushObserver3);
+}
+
+// needs to be rooted
+let cacheFlushObserver3 = { observe: function() {
+  if (!cacheFlushObserver3) {
+    do_print("ignoring cacheFlushObserver3\n");
+    return;
+  }
+
+  cacheFlushObserver3 = null;
+  gc();
+  make_and_open_channel(URL, altContentType2, readAltContent3);
+}};
+
+
+function readAltContent3(request, buffer, closure, fromCache)
+{
+  let cc = request.QueryInterface(Ci.nsICacheInfoChannel);
+
+  do_check_eq(servedNotModified || fromCache, true);
+  do_check_eq(cc.alternativeDataType, altContentType2);
+  do_check_eq(buffer, altContent2);
+
+  httpServer.stop(do_test_finished);
+}
--- a/netwerk/test/unit/xpcshell.ini
+++ b/netwerk/test/unit/xpcshell.ini
@@ -367,16 +367,17 @@ skip-if = os == "android"
 [test_dns_disable_ipv6.js]
 [test_bug1195415.js]
 [test_cookie_blacklist.js]
 [test_getHost.js]
 [test_bug412457.js]
 [test_bug464591.js]
 [test_alt-data_simple.js]
 [test_alt-data_stream.js]
+[test_alt-data_overwrite.js]
 [test_cache-control_request.js]
 [test_bug1279246.js]
 [test_throttlequeue.js]
 [test_throttlechannel.js]
 [test_throttling.js]
 [test_separate_connections.js]
 [test_rusturl.js]
 [test_trackingProtection_annotateChannels.js]