Bug 1188750 - Add test to ensure NSS is initialized before the WebCrypto API tries to deserialize a key f=keeler r=khuey
☠☠ backed out by 4b35236fc76e ☠ ☠
authorTim Taubert <ttaubert@mozilla.com>
Sat, 08 Aug 2015 16:58:39 +0200
changeset 257609 7bf71f3a4465c056791ef79ba73b85b14dccb1dc
parent 257608 6ec8b34e2bc62bf6b43d56212a0c95c83141b9e2
child 257610 d320f4dabfd285e37b76b5d8bc7be2e9dc5b6874
push id29223
push userkwierso@gmail.com
push dateThu, 13 Aug 2015 22:18:08 +0000
treeherdermozilla-central@4b35236fc76e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskhuey
bugs1188750
milestone43.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1188750 - Add test to ensure NSS is initialized before the WebCrypto API tries to deserialize a key f=keeler r=khuey
dom/crypto/test/file_indexedDB.html
dom/crypto/test/mochitest.ini
dom/crypto/test/test_indexedDB.html
new file mode 100644
--- /dev/null
+++ b/dom/crypto/test/file_indexedDB.html
@@ -0,0 +1,82 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Bug 1188750 - WebCrypto must ensure NSS is initialized before deserializing</title>
+</head>
+<body>
+  <script type="application/javascript;version=1.8">
+    let db;
+
+    function err(resolve) {
+      return e => resolve(e.target.error.message);
+    }
+
+    function openDatabase() {
+      return new Promise((resolve, reject) => {
+        let request = indexedDB.open("keystore", 1);
+
+        request.onerror = err(reject);
+        request.onsuccess = function (event) {
+          db = event.target.result;
+          resolve();
+        };
+
+        request.onupgradeneeded = function(event) {
+          db = event.target.result;
+          let objectStore = db.createObjectStore("keys", {autoIncrement: true});
+          objectStore.transaction.oncomplete = resolve;
+        };
+      });
+    }
+
+    function storeKey(key) {
+      return new Promise((resolve, reject) => {
+        let transaction = db.transaction("keys", "readwrite");
+        transaction.objectStore("keys").put(key, key.type);
+
+        transaction.onabort = err(reject);
+        transaction.onerror = err(reject);
+
+        transaction.oncomplete = function () {
+          resolve(key);
+        };
+      });
+    };
+
+    function retrieveKey() {
+      return new Promise((resolve, reject) => {
+        let transaction = db.transaction("keys", "readonly");
+        let cursor = transaction.objectStore("keys").openCursor();
+
+        cursor.onerror = err(reject);
+        cursor.onabort = err(reject);
+
+        cursor.onsuccess = function (event) {
+          try {
+            let result = event.target.result;
+            resolve(result && result.value);
+          } catch (e) {
+            reject(e.message);
+          }
+        };
+      });
+    }
+
+    function generateKey() {
+      let algorithm = {
+        name: "RSASSA-PKCS1-v1_5",
+        hash: "SHA-256",
+        modulusLength: 1024,
+        publicExponent: new Uint8Array([0x01, 0x00, 0x01])
+      };
+
+      return crypto.subtle.generateKey(algorithm, true, ["sign", "verify"]);
+    }
+
+    openDatabase()
+      .then(retrieveKey).then(generateKey).then(storeKey)
+      .then(() => alert("ok")).catch(alert);
+  </script>
+</body>
+</html>
--- a/dom/crypto/test/mochitest.ini
+++ b/dom/crypto/test/mochitest.ini
@@ -1,17 +1,19 @@
 [DEFAULT]
 # Bug 1010743 - Re-enable WebCrypto tests on b2g
 skip-if = (buildapp == 'b2g')
 support-files =
+  file_indexedDB.html
   test-array.js
   test-vectors.js
   test_WebCrypto.css
   util.js
 
+[test_indexedDB.html]
 [test_WebCrypto.html]
 [test_WebCrypto_DH.html]
 [test_WebCrypto_ECDH.html]
 [test_WebCrypto_ECDSA.html]
 [test_WebCrypto_JWK.html]
 [test_WebCrypto_Normalize.html]
 [test_WebCrypto_PBKDF2.html]
 [test_WebCrypto_Reject_Generating_Keys_Without_Usages.html]
new file mode 100644
--- /dev/null
+++ b/dom/crypto/test/test_indexedDB.html
@@ -0,0 +1,60 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Bug 1188750 - WebCrypto must ensure NSS is initialized before deserializing</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"?>
+</head>
+<body>
+  <script type="application/javascript;version=1.8">
+    /*
+     * Bug 1188750 - The WebCrypto API must ensure that NSS was initialized
+     * for the current process before trying to deserialize objects like
+     * CryptoKeys from e.g. IndexedDB.
+     */
+    "use strict";
+
+    const TEST_URI = "http://www.example.com/tests/" +
+                     "dom/crypto/test/file_indexedDB.html";
+
+    SimpleTest.waitForExplicitFinish();
+
+    function createMozBrowserFrame(cb) {
+      let frame = document.createElement("iframe");
+      SpecialPowers.wrap(frame).mozbrowser = true;
+      frame.src = TEST_URI;
+
+      frame.addEventListener("mozbrowsershowmodalprompt", function onPrompt(e) {
+        frame.removeEventListener("mozbrowsershowmodalprompt", onPrompt);
+        cb(frame, e.detail.message);
+      });
+
+      document.body.appendChild(frame);
+    }
+
+    function runTest() {
+      // Load the test app once, to generate and store keys.
+      createMozBrowserFrame((frame, result) => {
+        is(result, "ok", "stored keys successfully");
+        frame.remove();
+
+        // Load the test app again to retrieve stored keys.
+        createMozBrowserFrame((frame, result) => {
+          is(result, "ok", "retrieved keys successfully");
+          frame.remove();
+          SimpleTest.finish();
+        });
+      });
+    }
+
+    addEventListener("load", function () {
+      SpecialPowers.addPermission("browser", true, document);
+      SpecialPowers.pushPrefEnv({set: [
+        ["dom.ipc.browser_frames.oop_by_default", true],
+        ["dom.mozBrowserFramesEnabled", true]
+      ]}, runTest);
+    });
+  </script>
+</body>
+</html>