Bug 1424341 Add tests for privacy.reduceTimerPrecision that confirm the default timer resolution works draft
authorTom Ritter <tom@mozilla.com>
Fri, 08 Dec 2017 15:24:11 -0600
changeset 718834 8f02e33c813cb77d408649ee8c09f3ace7929654
parent 718833 70c3c08937cdc3d303256278ec1143b18de7d052
child 718835 33d81b8b54039ca55625b5a98ec87002f804ac8f
push id95052
push userbmo:tom@mozilla.com
push dateWed, 10 Jan 2018 21:56:43 +0000
bugs1424341
milestone59.0a1
Bug 1424341 Add tests for privacy.reduceTimerPrecision that confirm the default timer resolution works MozReview-Commit-ID: 1tGb0kedqSP
browser/components/resistfingerprinting/test/mochitest/file_animation_api.html
browser/components/resistfingerprinting/test/mochitest/test_animation_api.html
browser/components/resistfingerprinting/test/mochitest/test_reduce_time_precision.html
--- a/browser/components/resistfingerprinting/test/mochitest/file_animation_api.html
+++ b/browser/components/resistfingerprinting/test/mochitest/file_animation_api.html
@@ -32,24 +32,24 @@
     const testDiv = document.getElementById("testDiv");
     const animation = testDiv.animate({ opacity: [0, 1] }, 100000);
     animation.play();
 
     waitForCondition(
       () => animation.currentTime > 100,
         () => {
           opener.ok(isRounded(animation.startTime),
-             "animation.startTime is rounded");
+             "pref: " + opener.currentPref + " - animation.startTime is not rounded");
           opener.ok(isRounded(animation.currentTime),
-             "animation.currentTime is rounded");
+             "pref: " + opener.currentPref + " - animation.currentTime is not rounded");
           opener.ok(isRounded(animation.timeline.currentTime),
-             "animation.timeline.currentTime is rounded");
+             "pref: " + opener.currentPref + " - animation.timeline.currentTime is not rounded");
           if (document.timeline) {
             opener.ok(isRounded(document.timeline.currentTime),
-               "document.timeline.currentTime is rounded");
+               "pref: " + opener.currentPref + " - document.timeline.currentTime is not rounded");
           }
           opener.done();
           window.close();
         },
         "animation failed to start");
   }
 </script>
 </head>
--- a/browser/components/resistfingerprinting/test/mochitest/test_animation_api.html
+++ b/browser/components/resistfingerprinting/test/mochitest/test_animation_api.html
@@ -8,30 +8,56 @@ https://bugzilla.mozilla.org/show_bug.cg
   <title>Test for Bug 1382545</title>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript">
 
   /** Test for Bug 1382545 **/
   SimpleTest.waitForExplicitFinish();
 
+  var currentPref = "";
   window.onload = () => {
+    currentPref = "privacy.resistFingerprinting";
     SpecialPowers.pushPrefEnv({"set":
       [
         ["privacy.resistFingerprinting", true],
-        ["dom.animations-api.core.enabled", true]
+        ["dom.animations-api.core.enabled", true],
+        ["privacy.reduceTimerPrecision", false]
       ]
     }, runTest);
   };
 
   function runTest() {
     window.open("file_animation_api.html");
   }
 
+  let completed = 0;
+  const numTests = 2;
   function done() {
-    SimpleTest.finish();
+    completed++;
+    if (completed == numTests) {
+      SimpleTest.finish();
+    } else {
+      nextTest();
+    }
   }
 
+  function nextTest() {
+    if (completed == 1) {
+      currentPref = "privacy.reduceTimerPrecision";
+      SpecialPowers.pushPrefEnv({"set":
+        [
+          ["privacy.resistFingerprinting", false],
+          ["dom.animations-api.core.enabled", true],
+          ["privacy.reduceTimerPrecision", true]
+        ]
+      }, runTest);
+    } else {
+      ok(false, "I seem to have asked for " + numTests +
+         " tests, but don't know how to run them all.");
+      SimpleTest.finish();
+    }
+  }
   </script>
 </head>
 <body>
 </body>
 </html>
--- a/browser/components/resistfingerprinting/test/mochitest/test_reduce_time_precision.html
+++ b/browser/components/resistfingerprinting/test/mochitest/test_reduce_time_precision.html
@@ -1,23 +1,24 @@
 <!DOCTYPE HTML>
 <html>
 <!--
 Tor bug
 https://trac.torproject.org/projects/tor/ticket/1517
 -->
 <head>
   <meta charset="utf-8">
-  <title>Test for Tor Bug 1517</title>
+  <title>Test for Tor Bug 1517 and Mozilla Bug 1424341</title>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script type="application/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
 </head>
 <body>
 <a target="_blank" href="https://trac.torproject.org/projects/tor/ticket/1517">Tor Bug 1517</a>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1424341">Mozilla Bug 1424341</a>
 
 <!-- Canvas for testing 'currentTime' -->
 <canvas id="test-canvas" width="100" height="100"></canvas>
 
 <!-- The main testing script -->
 <script type="application/javascript">
   SimpleTest.requestFlakyTimeout("testing JS time-based fingerprinting");
 
@@ -35,26 +36,27 @@ https://trac.torproject.org/projects/tor
     "new Date().getTime()",
     "new Event(\"\").timeStamp",
     "new File([], \"\").lastModified",
     "new File([], \"\").lastModifiedDate.getTime()",
   ];
 
   const kExpectedResolution = 100;
 
-  function* checkWorker(worker) {
+  function* checkWorker(worker, prefname) {
     // The child worker will send the results back.
     let checkTimeStamps = () => new Promise(function(resolve) {
       let onMessage = function(event) {
         worker.removeEventListener("message", onMessage);
 
         let timeStamps = event.data;
         for (let i = 0; i < timeStampCodes.length; i++) {
           let timeStamp = timeStamps[i];
           is(timeStamp % kExpectedResolution, 0,
+            "pref: " + prefname + " - '" +
              "'" + timeStampCodes[i] +
              "' should be rounded to nearest 100 ms in workers; saw " +
              timeStamp);
         }
         resolve();
       };
       worker.addEventListener("message", onMessage);
     });
@@ -65,44 +67,85 @@ https://trac.torproject.org/projects/tor
     // First, check the child's results.
     yield checkTimeStamps();
     // Then, check the grandchild's results.
     yield checkTimeStamps();
 
     worker.terminate();
   }
 
-  add_task(async function testWorker() {
+  add_task(async function testWorkerRFP() {
     // Create one worker before setting the pref, and one after, in order to
     // check that the resolution is updated whether or not the worker was
     // already started
     let worker1 = new Worker("worker_child.js");
     await SpecialPowers.pushPrefEnv({
-      "set": [["privacy.resistFingerprinting", true]]});
+      "set": [["privacy.resistFingerprinting", true],
+              ["privacy.reduceTimerPrecision", false]]});
     let worker2 = new Worker("worker_child.js");
     // Allow ~550 ms to elapse, so we can get non-zero
     // time values for all elements.
     await new Promise(resolve => window.setTimeout(resolve, 550));
-    await checkWorker(worker1);
-    await checkWorker(worker2);
+    await checkWorker(worker1, "privacy.resistFingerprinting");
+    await checkWorker(worker2, "privacy.resistFingerprinting");
   });
 
-  add_task(async function testDOM() {
+  add_task(async function testWorkerRTP() {
+    // Create one worker before setting the pref, and one after, in order to
+    // check that the resolution is updated whether or not the worker was
+    // already started
+    let worker1 = new Worker("worker_child.js");
+    await SpecialPowers.pushPrefEnv({
+      "set": [["privacy.resistFingerprinting", false],
+              ["privacy.reduceTimerPrecision", true]]});
+    let worker2 = new Worker("worker_child.js");
+    // Allow ~550 ms to elapse, so we can get non-zero
+    // time values for all elements.
+    await new Promise(resolve => window.setTimeout(resolve, 550));
+    await checkWorker(worker1, "privacy.reduceTimerPrecision");
+    await checkWorker(worker2, "privacy.reduceTimerPrecision");
+  });
+
+  add_task(async function testDOMRFP() {
+    await SpecialPowers.pushPrefEnv({
+      "set": [["privacy.resistFingerprinting", true],
+              ["privacy.reduceTimerPrecision", false]]});
     let timeStampCodesDOM = timeStampCodes.concat([
       "audioContext.currentTime * 1000",
       "canvasStream.currentTime * 1000",
     ]);
     // Loop through each timeStampCode, evaluate it,
     // and check if it is rounded to the nearest 100 ms.
     for (let timeStampCode of timeStampCodesDOM) {
       let timeStamp = eval(timeStampCode);
       is(timeStamp % kExpectedResolution, 0,
+        "pref: privacy.resistFingerprinting - '" +
          "'" + timeStampCode +
          "' should be rounded to nearest 100 ms; saw " +
          timeStamp);
     }
   });
 
+  add_task(async function testDOMRTP() {
+    await SpecialPowers.pushPrefEnv({
+      "set": [["privacy.resistFingerprinting", false],
+              ["privacy.reduceTimerPrecision", true]]});
+    let timeStampCodesDOM = timeStampCodes.concat([
+      "audioContext.currentTime * 1000",
+      "canvasStream.currentTime * 1000",
+    ]);
+    // Loop through each timeStampCode, evaluate it,
+    // and check if it is rounded to the nearest 100 ms.
+    for (let timeStampCode of timeStampCodesDOM) {
+      let timeStamp = eval(timeStampCode);
+      is(timeStamp % kExpectedResolution, 0,
+         "pref: privacy.reduceTimerPrecision - '" +
+         timeStampCode +
+         "' should be rounded to nearest 100 ms; saw " +
+         timeStamp);
+    }
+  });
+
 </script>
 
 
 </body>
 </html>