Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(696)

Unified Diff: LayoutTests/crypto/importKey.html

Issue 19885002: WebCrypto: Add interfaces for importKey(). (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Move test stuff into MockWebCrypto Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: LayoutTests/crypto/importKey.html
diff --git a/LayoutTests/crypto/importKey.html b/LayoutTests/crypto/importKey.html
new file mode 100644
index 0000000000000000000000000000000000000000..d66506f8f825c8d18903db2cd0b903d432444588
--- /dev/null
+++ b/LayoutTests/crypto/importKey.html
@@ -0,0 +1,219 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src="../fast/js/resources/js-test-pre.js"></script>
+</head>
+<body>
+<p id="description"></p>
+<div id="console"></div>
+
+<script>
+description("Tests cypto.subtle.importKey.");
+
+jsTestIsAsync = true;
+
+function asciiToArrayBuffer(str)
+{
+ var chars = [];
+ for (var i = 0; i < str.length; ++i)
+ chars.push(str.charCodeAt(i));
+ return new Uint8Array(chars);
+}
+
+// Each sub-test run in this file is asynchronous. Chaining them together
+// manually leads to very unreadable code, due to having closures within
+// closures within closures. Instead of doing that, each subtest calls
+// "startNextTest()" once it has completed.
+
+currentTestIndex = 0;
+
+function startNextTest()
+{
+ var currentTest = allTests[currentTestIndex++];
+
+ if (!currentTest) {
+ finishJSTest();
+ return;
+ }
+
+ currentTest();
+}
+
+function rejectHandler(value)
+{
+ debug(" rejected with value of " + value);
+ startNextTest();
+}
+
+function failHandler(value)
+{
+ testFailed(value);
+ startNextTest();
+}
+
+allTests = [
+ function()
+ {
+ keyFormat = "raw";
+ data = asciiToArrayBuffer("private");
+ algorithm = {name: "Sha-256"};
+ extractable = true;
+ // Note there are duplicates
+ keyUsages = ['encrypt', 'encrypt', 'encrypt', 'sign'];
+
+ crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages).then(
+ function(value) {
+ key = value;
+ shouldBe("key.type", "'private'")
+ shouldBe("key.extractable", "true")
+ shouldBe("key.algorithm.name", "'SHA-256'")
+ shouldBe("key.usages.join(',')", "'encrypt,sign'")
+
+ startNextTest();
+ }, failHandler);
+ },
+
+ // Same test as above, but with an keyUsages.
+ function()
+ {
+ keyFormat = "raw";
+ data = asciiToArrayBuffer("private");
+ algorithm = {name: "Sha-256"};
+ extractable = true;
+ keyUsages = [];
+
+ crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages).then(
+ function(value) {
+ key = value;
+ shouldBe("key.type", "'private'")
+ shouldBe("key.extractable", "true")
+ shouldBe("key.algorithm.name", "'SHA-256'")
+ shouldBe("key.usages.join(',')", "''")
+
+ startNextTest();
+ }, failHandler);
+ },
+
+ // Same test as above, but with extractable = false.
+ function()
+ {
+ keyFormat = "raw";
+ data = asciiToArrayBuffer("private");
+ algorithm = {name: "Sha-256"};
+ extractable = false;
+ keyUsages = [];
+
+ crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages).then(
+ function(value) {
+ key = value;
+ shouldBe("key.type", "'private'")
+ shouldBe("key.extractable", "false")
+ shouldBe("key.algorithm.name", "'SHA-256'")
+ shouldBe("key.usages.join(',')", "''")
+
+ startNextTest();
+ }, failHandler);
+ },
+
+ // Same test as above, but with key.type of public.
+ function()
+ {
+ keyFormat = "raw";
+ data = asciiToArrayBuffer("public");
+ algorithm = {name: "Sha-256"};
+ extractable = false;
+ keyUsages = [];
+
+ crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages).then(
+ function(value) {
+ key = value;
+ shouldBe("key.type", "'public'")
+ shouldBe("key.extractable", "false")
+ shouldBe("key.algorithm.name", "'SHA-256'")
+ shouldBe("key.usages.join(',')", "''")
+
+ startNextTest();
+ }, failHandler);
+ },
+
+ // Same test as above, but with keyFormat = spki
+ function()
+ {
+ keyFormat = "spki";
+ data = asciiToArrayBuffer("public");
+ algorithm = {name: "Sha-256"};
+ extractable = false;
+ keyUsages = [];
+
+ crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages).then(
+ function(value) {
+ key = value;
+ shouldBe("key.type", "'public'")
+ shouldBe("key.extractable", "false")
+ shouldBe("key.algorithm.name", "'SHA-256'")
+ shouldBe("key.usages.join(',')", "''")
+
+ startNextTest();
+ }, failHandler);
+ },
+
+ function()
+ {
+ keyFormat = "spki";
+ data = asciiToArrayBuffer("reject");
+ algorithm = {name: "Sha-256"};
+ extractable = false;
+ keyUsages = [];
+
+ crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages).then(
+ failHandler,
+ function(value) {
+ debug("rejected with " + value);
+ startNextTest();
+ });
+ },
+
+ function()
+ {
+ keyFormat = "spki";
+ data = asciiToArrayBuffer("throw");
+ algorithm = {name: "Sha-256"};
+ extractable = false;
+ keyUsages = [];
+
+ shouldThrow("crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages)");
+ startNextTest();
+ },
+
+ function()
+ {
+ keyFormat = "raw";
+ data = asciiToArrayBuffer("private");
+ algorithm = {name: 'sha-256'};
+ extractable = true;
+
+ // Note contains duplicates and invalid entries.
+ keyUsages = [];
+
+ // Invalid format.
+ shouldThrow("crypto.subtle.importKey('invalid format', data, algorithm, extractable, keyUsages)");
+
+ // Invalid key usage.
+ shouldThrow("crypto.subtle.importKey(keyFormat, data, algorithm, extractable, ['SIGN'])");
+
+ // Undefined key usage.
+ // FIXME: http://crbug.com/262383
+ //shouldThrow("crypto.subtle.importKey(keyFormat, data, algorithm, extractable, undefined)");
+
+ startNextTest();
+ },
+
+];
+
+// Begin!
+startNextTest();
+</script>
+
+<script src="../fast/js/resources/js-test-post.js"></script>
+</body>
+</html>

Powered by Google App Engine
This is Rietveld 408576698