Bug 1141814 - Part 3: Unit test. r=honzab
authorKershaw Chang <kechang@mozilla.com>
Wed, 30 Nov 2016 23:13:00 -0500
changeset 325023 33de2e2e2cf3876d74c33e4ca335090ee5224c2c
parent 325022 fb18b507a9124611562fb652ebfc993eb7a30e39
child 325024 362a85a199376e27508d4f819bb3411b90c8f106
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewershonzab
bugs1141814
milestone53.0a1
Bug 1141814 - Part 3: Unit test. r=honzab
netwerk/test/unit/test_trackingProtection_annotateChannels.js
netwerk/test/unit/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/netwerk/test/unit/test_trackingProtection_annotateChannels.js
@@ -0,0 +1,128 @@
+Cu.import("resource://gre/modules/NetUtil.jsm");
+Cu.import("resource://testing-common/UrlClassifierTestUtils.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://testing-common/httpd.js");
+
+do_get_profile();
+
+var Ci = Components.interfaces;
+
+function listener(priority, nextTest) {
+  this._priority = priority;
+  this._nextTest = nextTest;
+}
+listener.prototype = {
+  onStartRequest: function(request, context) {
+    do_check_eq(request.QueryInterface(Ci.nsISupportsPriority).priority,
+                this._priority);
+    this._nextTest();
+  },
+  onDataAvailable: function(request, context, stream, offset, count) {
+  },
+  onStopRequest: function(request, context, status) {
+  }
+};
+
+var httpServer;
+var origin;
+var testPriorityMap;
+var currentTest;
+
+function setup_test() {
+  httpServer = new HttpServer();
+  httpServer.start(-1);
+  httpServer.identity.setPrimary("http", "tracking.example.com", httpServer.identity.primaryPort);
+  origin = "http://tracking.example.com:" + httpServer.identity.primaryPort;
+
+  runTests();
+}
+
+function doPriorityTest() {
+  if (!testPriorityMap.length) {
+    runTests();
+    return;
+  }
+
+  currentTest = testPriorityMap.shift();
+  var channel = makeChannel(currentTest.path);
+  channel.asyncOpen2(new listener(currentTest.expectedPriority, doPriorityTest));
+}
+
+function makeChannel(path) {
+  var chan = NetUtil.newChannel({
+    uri: path,
+    loadUsingSystemPrincipal: true
+  });
+  chan.QueryInterface(Ci.nsIHttpChannel);
+  chan.requestMethod = "GET";
+  chan.loadFlags |= Ci.nsIChannel.LOAD_CLASSIFY_URI;
+  return chan;
+}
+
+var tests =[
+  // Create the HTTP server.
+  setup_test,
+
+  // Add the test table into tracking protection table.
+  function addTestTrackers() {
+    UrlClassifierTestUtils.addTestTrackers().then(() => {
+      runTests();
+    });
+  },
+
+  // With the pref off, the priority of channel should be normal.
+  function setupNormalPriority() {
+    Services.prefs.setBoolPref("privacy.trackingprotection.annotate_channels", false);
+    testPriorityMap = [
+      {
+        path: origin + "/evil.css",
+        expectedPriority: Ci.nsISupportsPriority.PRIORITY_NORMAL
+      },
+      {
+        path: origin + "/evil.js",
+        expectedPriority: Ci.nsISupportsPriority.PRIORITY_NORMAL
+      },
+    ];
+    runTests();
+  },
+  doPriorityTest,
+
+  // With the pref on, the priority of channel should be lowest.
+  function setupLowestPriority() {
+    Services.prefs.setBoolPref("privacy.trackingprotection.annotate_channels", true);
+    testPriorityMap = [
+      {
+        path: origin + "/evil.css",
+        expectedPriority: Ci.nsISupportsPriority.PRIORITY_LOWEST
+      },
+      {
+        path: origin + "/evil.js",
+        expectedPriority: Ci.nsISupportsPriority.PRIORITY_LOWEST
+      },
+    ];
+    runTests();
+  },
+  doPriorityTest,
+
+  function cleanUp() {
+    httpServer.stop(do_test_finished);
+    UrlClassifierTestUtils.cleanupTestTrackers();
+    runTests();
+  }
+];
+
+function runTests()
+{
+  if (!tests.length) {
+    do_test_finished();
+    return;
+  }
+
+  var test = tests.shift();
+  test();
+}
+
+function run_test() {
+  runTests();
+  do_test_pending();
+}
--- a/netwerk/test/unit/xpcshell.ini
+++ b/netwerk/test/unit/xpcshell.ini
@@ -365,8 +365,9 @@ skip-if = os == "android"
 [test_alt-data_stream.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]