Bug 1188750 - Add test to ensure NSS is initialized before the WebCrypto API tries to deserialize a key f=keeler r=khuey

This commit is contained in:
Tim Taubert 2015-08-08 16:58:39 +02:00
parent 981a809447
commit 2b0ed745b9
3 changed files with 144 additions and 0 deletions

View File

@ -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>

View File

@ -2,11 +2,13 @@
# 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]

View File

@ -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>