Bug 1255784 - u2f tests should use SpecialPowers.pushPrefEnv, r=jjones
authorAndrea Marchesini <amarchesini@mozilla.com>
Fri, 11 Mar 2016 18:20:55 +0100
changeset 339583 8836b439d0b3c324f743f1e5b59e986829d08440
parent 339582 622e8779744631d6a6a8e445ee4325717b392f6b
child 339584 80d84efa1d0c79b19a086f312a358cd5891fa198
push id12762
push userbmo:rail@mozilla.com
push dateFri, 11 Mar 2016 19:47:45 +0000
reviewersjjones
bugs1255784
milestone48.0a1
Bug 1255784 - u2f tests should use SpecialPowers.pushPrefEnv, r=jjones
dom/u2f/tests/frame_no_token.html
dom/u2f/tests/mochitest.ini
dom/u2f/tests/test_frame.html
dom/u2f/tests/test_frame_appid_facet.html
dom/u2f/tests/test_frame_appid_facet_insecure.html
dom/u2f/tests/test_frame_appid_facet_remoteload.html
dom/u2f/tests/test_frame_appid_facet_subdomain.html
dom/u2f/tests/test_frame_register.html
dom/u2f/tests/test_no_token.html
dom/u2f/tests/test_util_methods.html
new file mode 100644
--- /dev/null
+++ b/dom/u2f/tests/frame_no_token.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<meta charset=utf-8>
+<head>
+  <title>Test for FIDO Universal Second Factor No Token</title>
+  <script src="u2futil.js"></script>
+</head>
+<body>
+
+<script class="testbody" type="text/javascript">
+
+var challenge = new Uint8Array(16);
+window.crypto.getRandomValues(challenge);
+
+var regRequest = {
+  version: "U2F_V2",
+  challenge: bytesToBase64UrlSafe(challenge),
+};
+
+u2f.register(window.location.origin, [regRequest], [], function (regResponse) {
+  parent.postMessage(regResponse.errorCode, '*');
+});
+
+</script>
+
+</body>
+</html>
+
--- a/dom/u2f/tests/mochitest.ini
+++ b/dom/u2f/tests/mochitest.ini
@@ -1,10 +1,11 @@
 [DEFAULT]
 support-files =
+  frame_no_token.html
   u2futil.js
   test_frame_appid_facet.html
   test_frame_register.html
   test_frame_appid_facet_remoteload.html
   test_frame_appid_facet_insecure.html
   test_frame_appid_facet_subdomain.html
   facet/facetList.txt
   facet/facetList-good
--- a/dom/u2f/tests/test_frame.html
+++ b/dom/u2f/tests/test_frame.html
@@ -4,64 +4,61 @@
   <title>Test for AppID / FacetID behavior for FIDO Universal Second Factor</title>
   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script src="u2futil.js"></script>
 
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1231681">Mozilla Bug 1231681</a>
-<p id="display"></p>
-
-<div id="content" style="display: none"></div>
 
 <div id="framediv">
   <iframe id="testing_frame"></iframe>
 </div>
 
 <pre id="log"></pre>
 
-
 <script class="testbody" type="text/javascript">
-SpecialPowers.setBoolPref("security.webauth.u2f", true);
-SpecialPowers.setBoolPref("security.webauth.u2f.softtoken", true);
 
-var testList = [
-  "https://example.com/tests/dom/u2f/tests/test_frame_register.html",
-  "http://mochi.test:8888/tests/dom/u2f/tests/test_frame_appid_facet_insecure.html",
-  "https://example.com/tests/dom/u2f/tests/test_frame_appid_facet.html",
-  "https://example.com/tests/dom/u2f/tests/test_frame_appid_facet_remoteload.html",
-  "https://test1.example.com/tests/dom/u2f/tests/test_frame_appid_facet_subdomain.html"
-];
+SpecialPowers.pushPrefEnv({"set": [["security.webauth.u2f", true],
+                                   ["security.webauth.u2f.softtoken", true]]},
+function() {
+  var testList = [
+    "https://example.com/tests/dom/u2f/tests/test_frame_register.html",
+    "http://mochi.test:8888/tests/dom/u2f/tests/test_frame_appid_facet_insecure.html",
+    "https://example.com/tests/dom/u2f/tests/test_frame_appid_facet.html",
+    "https://example.com/tests/dom/u2f/tests/test_frame_appid_facet_remoteload.html",
+    "https://test1.example.com/tests/dom/u2f/tests/test_frame_appid_facet_subdomain.html"
+  ];
 
-function log(msg) {
-  document.getElementById("log").textContent += "\n" + msg;
-}
-
-function nextTest() {
-  if (testList.length < 1) {
-    SimpleTest.finish();
-    return;
+  function log(msg) {
+    document.getElementById("log").textContent += "\n" + msg;
   }
 
-  document.getElementById('testing_frame').src = testList.shift();
-}
+  function nextTest() {
+    if (testList.length < 1) {
+      SimpleTest.finish();
+      return;
+    }
+
+    document.getElementById('testing_frame').src = testList.shift();
+  }
 
-// listen for a messages from the mixed content test harness
-function receiveMessage(event) {
-  if ("test" in event.data) {
-    var summary = event.data.test + ": " + event.data.msg;
-    log(event.data.status + ": " + summary);
-    ok(event.data.status, summary);
-  } else if ("done" in event.data) {
-    nextTest();
+  // listen for a messages from the mixed content test harness
+  function receiveMessage(event) {
+    if ("test" in event.data) {
+      var summary = event.data.test + ": " + event.data.msg;
+      log(event.data.status + ": " + summary);
+      ok(event.data.status, summary);
+    } else if ("done" in event.data) {
+      nextTest();
+    }
   }
-}
+
+  window.addEventListener("message", receiveMessage, false);
+  nextTest();
+});
 
 SimpleTest.waitForExplicitFinish();
-
-window.addEventListener("message", receiveMessage, false);
-nextTest();
-
 </script>
 
   </body>
 </html>
--- a/dom/u2f/tests/test_frame_appid_facet.html
+++ b/dom/u2f/tests/test_frame_appid_facet.html
@@ -3,19 +3,16 @@
 <head>
   <script src="u2futil.js"></script>
 </head>
 <body>
 <p>Test for AppID / FacetID behavior for FIDO Universal Second Factor</p>
 <script class="testbody" type="text/javascript">
 "use strict";
 
-SpecialPowers.setBoolPref("security.webauth.u2f", true);
-SpecialPowers.setBoolPref("security.webauth.u2f.softtoken", true);
-
 local_is(window.location.origin, "https://example.com", "Is loaded correctly");
 
 var version = "U2F_V2";
 var challenge = new Uint8Array(16);
 
 u2f.register(null, [{
   version: version,
   challenge: bytesToBase64UrlSafe(challenge),
--- a/dom/u2f/tests/test_frame_appid_facet_insecure.html
+++ b/dom/u2f/tests/test_frame_appid_facet_insecure.html
@@ -3,19 +3,16 @@
 <head>
   <script src="u2futil.js"></script>
 </head>
 <body>
 <p>Test for AppID / FacetID behavior for FIDO Universal Second Factor</p>
 <script class="testbody" type="text/javascript">
 "use strict";
 
-SpecialPowers.setBoolPref("security.webauth.u2f", true);
-SpecialPowers.setBoolPref("security.webauth.u2f.softtoken", true);
-
 local_is(window.location.origin, "http://mochi.test:8888", "Is loaded correctly");
 
 var version = "U2F_V2";
 var challenge = new Uint8Array(16);
 
 u2f.register(null, [{
   version: version,
   challenge: bytesToBase64UrlSafe(challenge),
--- a/dom/u2f/tests/test_frame_appid_facet_remoteload.html
+++ b/dom/u2f/tests/test_frame_appid_facet_remoteload.html
@@ -3,19 +3,16 @@
 <head>
   <script src="u2futil.js"></script>
 </head>
 <body>
 <p>Test for Remote AppId Load behavior for FIDO Universal Second Factor</p>
 <script class="testbody" type="text/javascript">
 "use strict";
 
-SpecialPowers.setBoolPref("security.webauth.u2f", true);
-SpecialPowers.setBoolPref("security.webauth.u2f.softtoken", true);
-
 var version = "U2F_V2";
 var challenge = new Uint8Array(16);
 
 local_is(window.location.origin, "https://example.com", "Is loaded correctly");
 
 // TODO: Must support remote loads of AppID manifests first.
 //
 // u2f.register("https://test1.example.com/dom/u2f/tests/facet/facetList.txt", [{
--- a/dom/u2f/tests/test_frame_appid_facet_subdomain.html
+++ b/dom/u2f/tests/test_frame_appid_facet_subdomain.html
@@ -3,19 +3,16 @@
 <head>
   <script src="u2futil.js"></script>
 </head>
 <body>
 <p>Test for AppID / FacetID behavior for FIDO Universal Second Factor</p>
 <script class="testbody" type="text/javascript">
 "use strict";
 
-SpecialPowers.setBoolPref("security.webauth.u2f", true);
-SpecialPowers.setBoolPref("security.webauth.u2f.softtoken", true);
-
 var version = "U2F_V2";
 var challenge = new Uint8Array(16);
 
 local_is(window.location.origin, "https://test1.example.com", "Is loaded correctly");
 
 // eTLD+1 check
 u2f.register("https://example.com/appId", [{
   version: version,
--- a/dom/u2f/tests/test_frame_register.html
+++ b/dom/u2f/tests/test_frame_register.html
@@ -3,19 +3,16 @@
 <head>
   <script src="u2futil.js"></script>
 </head>
 <body>
 <p>Test for Register behavior for FIDO Universal Second Factor</p>
 <script class="testbody" type="text/javascript">
 "use strict";
 
-SpecialPowers.setBoolPref("security.webauth.u2f", true);
-SpecialPowers.setBoolPref("security.webauth.u2f.softtoken", true);
-
 var version = "U2F_V2";
 var challenge = new Uint8Array(16);
 
 local_is(window.location.origin, "https://example.com", "Is loaded correctly");
 
 // eTLD+1 check
 u2f.register("https://example.com/appId", [{
   version: version,
--- a/dom/u2f/tests/test_no_token.html
+++ b/dom/u2f/tests/test_no_token.html
@@ -3,36 +3,34 @@
 <head>
   <title>Test for FIDO Universal Second Factor No Token</title>
   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script src="u2futil.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1231681">Mozilla Bug 1231681</a>
-<p id="display"></p>
-<div id="content" style="display: none">
+
+<div id="framediv">
+  <iframe id="testing_frame"></iframe>
 </div>
-<pre id="test">
+
 <script class="testbody" type="text/javascript">
+
 SimpleTest.waitForExplicitFinish();
-SpecialPowers.setBoolPref("security.webauth.u2f", true);
-SpecialPowers.setBoolPref("security.webauth.u2f.softtoken", false);
-SpecialPowers.setBoolPref("security.webauth.u2f.usbtoken", false);
 
-var challenge = new Uint8Array(16);
-window.crypto.getRandomValues(challenge);
+SpecialPowers.pushPrefEnv({"set": [["security.webauth.u2f", true],
+                                   ["security.webauth.u2f.softtoken", false],
+                                   ["security.webauth.u2f.usbtoken", false]]},
+function() {
+  onmessage = function(event) {
+    //event.data is the response.errorCode
+    isnot(event.data, 0, "The registration should be rejected.");
+    SimpleTest.finish();
+  }
 
-var regRequest = {
-  version: "U2F_V2",
-  challenge: bytesToBase64UrlSafe(challenge),
-};
-
-u2f.register(window.location.origin, [regRequest], [], function (regResponse) {
-  isnot(regResponse.errorCode, 0, "The registration should be rejected.");
-
-  SimpleTest.finish();
+  document.getElementById('testing_frame').src = 'frame_no_token.html';
 });
 
 </script>
-</pre>
+
 </body>
 </html>
--- a/dom/u2f/tests/test_util_methods.html
+++ b/dom/u2f/tests/test_util_methods.html
@@ -8,50 +8,53 @@
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1231681">Mozilla Bug 1231681</a>
 <p id="display"></p>
 <div id="content" style="display: none">
 </div>
 <pre id="test">
 <script class="testbody" type="text/javascript">
+
 SimpleTest.waitForExplicitFinish();
-SpecialPowers.setBoolPref("security.webauth.u2f", true);
-SpecialPowers.setBoolPref("security.webauth.u2f.softtoken", true);
-SpecialPowers.setBoolPref("security.webauth.u2f.usbtoken", false);
 
-// Example from:
-// https://fidoalliance.org/specs/fido-u2f-v1.0-nfc-bt-amendment-20150514/fido-u2f-raw-message-formats.html
-//
-// Run this example from the console to check that the u2futil methods work
-var pubKey = hexDecode("04d368f1b665bade3c33a20f1e429c7750d5033660c019119d29aa4ba7abc04aa7c80a46bbe11ca8cb5674d74f31f8a903f6bad105fb6ab74aefef4db8b0025e1d");
-var appId = "https://gstatic.com/securitykey/a/example.com";
-var clientData = string2buffer('{"typ":"navigator.id.getAssertion","challenge":"opsXqUifDriAAmWclinfbS0e-USY0CgyJHe_Otd7z8o","cid_pubkey":{"kty":"EC","crv":"P-256","x":"HzQwlfXX7Q4S5MtCCnZUNBw3RMzPO9tOyWjBqRl4tJ8","y":"XVguGFLIZx1fXg3wNqfdbn75hi4-_7-BxhMljw42Ht4"},"origin":"http://example.com"}');
-var presenceAndCounter = hexDecode("0100000001");
-var signature = hexDecode("304402204b5f0cd17534cedd8c34ee09570ef542a353df4436030ce43d406de870b847780220267bb998fac9b7266eb60e7cb0b5eabdfd5ba9614f53c7b22272ec10047a923f");
+SpecialPowers.pushPrefEnv({"set": [["security.webauth.u2f", true],
+                                   ["security.webauth.u2f.softtoken", true],
+                                   ["security.webauth.u2f.usbtoken", false]]},
+function() {
+  // Example from:
+  // https://fidoalliance.org/specs/fido-u2f-v1.0-nfc-bt-amendment-20150514/fido-u2f-raw-message-formats.html
+  //
+  // Run this example from the console to check that the u2futil methods work
+  var pubKey = hexDecode("04d368f1b665bade3c33a20f1e429c7750d5033660c019119d29aa4ba7abc04aa7c80a46bbe11ca8cb5674d74f31f8a903f6bad105fb6ab74aefef4db8b0025e1d");
+  var appId = "https://gstatic.com/securitykey/a/example.com";
+  var clientData = string2buffer('{"typ":"navigator.id.getAssertion","challenge":"opsXqUifDriAAmWclinfbS0e-USY0CgyJHe_Otd7z8o","cid_pubkey":{"kty":"EC","crv":"P-256","x":"HzQwlfXX7Q4S5MtCCnZUNBw3RMzPO9tOyWjBqRl4tJ8","y":"XVguGFLIZx1fXg3wNqfdbn75hi4-_7-BxhMljw42Ht4"},"origin":"http://example.com"}');
+  var presenceAndCounter = hexDecode("0100000001");
+  var signature = hexDecode("304402204b5f0cd17534cedd8c34ee09570ef542a353df4436030ce43d406de870b847780220267bb998fac9b7266eb60e7cb0b5eabdfd5ba9614f53c7b22272ec10047a923f");
 
-// Import the key
-// Assemble the client data
-// Verify
-Promise.all([
-  importPublicKey(pubKey),
-  assembleSignedData(appId, presenceAndCounter, clientData)
-])
-.then(function(results) {
-  var importedKey = results[0];
-  var signedData = new Uint8Array(results[1]);
-  return verifySignature(importedKey, signedData, signature);
-})
-.then(function(verified) {
-  console.log("verified:", verified);
-  ok(true, "Utility methods work")
-  SimpleTest.finish();
-})
-.catch(function(err) {
-  console.log("error:", err);
-  ok(false, "Utility methods failed")
-  SimpleTest.finish();
+  // Import the key
+  // Assemble the client data
+  // Verify
+  Promise.all([
+    importPublicKey(pubKey),
+    assembleSignedData(appId, presenceAndCounter, clientData)
+  ])
+  .then(function(results) {
+    var importedKey = results[0];
+    var signedData = new Uint8Array(results[1]);
+    return verifySignature(importedKey, signedData, signature);
+  })
+  .then(function(verified) {
+    console.log("verified:", verified);
+    ok(true, "Utility methods work")
+    SimpleTest.finish();
+  })
+  .catch(function(err) {
+    console.log("error:", err);
+    ok(false, "Utility methods failed")
+    SimpleTest.finish();
+  });
 });
 
 </script>
 </pre>
 </body>
 </html>