blob: 9d6faa8a14af9a9f7a94c2b00929bd441e7590ff [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<script src="../resources/js-test.js"></script>
<script src="resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Tests cypto.subtle.importKey.");
jsTestIsAsync = true;
aesCbc = {name: 'aes-cbc'};
Promise.resolve(null).then(function() {
keyFormat = "raw";
data = asciiToUint8Array("raw bytes for key");
algorithm = { name: 'hmac', hash: { name: 'sha-256' } };
extractable = true;
// Note there are duplicates
keyUsages = ['encrypt', 'encrypt', 'encrypt', 'sign'];
return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages);
}).then(function(result) {
key = result;
shouldBe("key.type", "'secret'")
shouldBe("key.extractable", "true")
shouldBe("key.algorithm.name", "'HMAC'")
shouldBe("key.usages.join(',')", "'encrypt,sign'")
// Same test as above, but with an keyUsages, and AES-CBC.
keyFormat = "raw";
data = asciiToUint8Array("16 bytes of key!");
algorithm = aesCbc;
extractable = true;
keyUsages = [];
return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages);
}).then(function(result) {
key = result;
shouldBe("key.type", "'secret'")
shouldBe("key.extractable", "true")
shouldBe("key.algorithm.name", "'AES-CBC'")
shouldBe("key.usages.join(',')", "''")
// Same test as above, but with extractable = false.
keyFormat = "raw";
data = asciiToUint8Array("16 bytes of key!");
algorithm = aesCbc;
extractable = false;
keyUsages = [];
return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages);
}).then(function(result) {
key = result;
shouldBe("key.type", "'secret'")
shouldBe("key.extractable", "false")
shouldBe("key.algorithm.name", "'AES-CBC'")
shouldBe("key.usages.join(',')", "''")
// Same test as above, but with keyFormat = spki
keyFormat = "spki";
data = asciiToUint8Array("16 bytes of key!");
algorithm = aesCbc;
extractable = false;
keyUsages = [];
return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages);
}).then(undefined, function(result) {
// TODO(eroman): Only "raw" key format is supported at the moment.
debug("rejected with " + result);
// Import a "raw" key without specifying the algorithm.
keyFormat = "raw";
data = asciiToUint8Array("16 bytes of key!");
algorithm = null;
extractable = false;
keyUsages = [];
return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages);
}).then(undefined, function(result) {
debug("rejected with " + result);
keyFormat = "raw";
data = asciiToUint8Array("");
algorithm = aesCbc;
extractable = true;
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'])");
// If both the format and key usage are bogus, should complain about the
// format first.
shouldThrow("crypto.subtle.importKey('invalid format', data, algorithm, extractable, ['SIGN'])");
// Undefined key usage.
// FIXME: http://crbug.com/262383
//shouldThrow("crypto.subtle.importKey(keyFormat, data, algorithm, extractable, undefined)");
// Invalid data
shouldThrow("crypto.subtle.importKey(keyFormat, [], algorithm, extractable, keyUsages)");
shouldThrow("crypto.subtle.importKey(keyFormat, null, algorithm, extractable, keyUsages)");
// Missing hash parameter for HMAC.
shouldRejectPromiseWithNull("crypto.subtle.importKey(keyFormat, data, {name: 'hmac'}, extractable, keyUsages)");
// SHA-1 doesn't support the importKey operation.
shouldThrow("crypto.subtle.importKey(keyFormat, data, {name: 'sha-1'}, extractable, keyUsages)");
}).then(finishJSTest, failAndFinishJSTest);
</script>
</body>
</html>