WebCrypto Key.usages should be ordered alphabetically
[WebKit-https.git] / LayoutTests / crypto / subtle / jwk-import-use-values.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../resources/js-test-pre.js"></script>
5 <script src="resources/common.js"></script>
6 </head>
7 <body>
8 <p id="description"></p>
9 <div id="console"></div>
10
11 <script>
12 description("Test importing keys with various uses from JWK.");
13
14 jsTestIsAsync = true;
15
16 var extractable = true;
17
18 var aesKeyAsJSON = {
19     "alg": "A192CBC",
20     "ext": true,
21     "kty": "oct",
22     "k": "jnOw99oOZFLIEPMrgJB55WL46tJSLGt7"
23 };
24
25 var hmacKeyAsJSON = {
26     "alg": "HS256",
27     "ext": true,
28     "kty": "oct",
29     "k": "ahjkn-_387fgnsibf23qsvahjkn-_387fgnsibf23qs"
30 };
31
32 function testWithAESCBC(expectedUsages, use)
33 {
34     aesKeyAsJSON.use = use;
35     return crypto.subtle.importKey("jwk", asciiToUint8Array(JSON.stringify(aesKeyAsJSON)), "AES-CBC", extractable, ["encrypt", "decrypt", "wrapKey", "unwrapKey"]).then(function(result) {
36         key = result;
37         debug(use + ":");
38         shouldBe("key.usages", JSON.stringify(expectedUsages));
39         debug("");
40     });
41 }
42
43 function testWithHMAC(expectedUsages, use)
44 {
45     hmacKeyAsJSON.use = use;
46     return crypto.subtle.importKey("jwk", asciiToUint8Array(JSON.stringify(hmacKeyAsJSON)), {name: 'hmac', hash: {name: 'sha-256'}}, extractable, ["sign", "verify"]).then(function(result) {
47         key = result;
48         debug(use + ":");
49         shouldBe("key.usages", JSON.stringify(expectedUsages));
50         debug("");
51     });
52 }
53
54 Promise.all([
55     testWithAESCBC(["encrypt"], "enconly"),
56     testWithAESCBC(["decrypt"], "deconly"),
57     testWithAESCBC(["decrypt", "encrypt"], "enconly,deconly"),
58     testWithAESCBC(["wrapKey"], "wrap"),
59     testWithAESCBC(["unwrapKey"], "unwrap"),
60     testWithAESCBC(["unwrapKey", "wrapKey"], "wrap,unwrap"),
61     testWithAESCBC(["decrypt", "encrypt", "wrapKey"], "enconly,deconly,wrap"),
62     testWithAESCBC(["decrypt", "encrypt", "unwrapKey", "wrapKey"], "enc"),
63     testWithHMAC(["sign"], "sigonly"),
64     testWithHMAC(["verify"], "vfyonly"),
65     testWithHMAC(["sign", "verify"], "sig"),
66
67 // Incorrect use strings. There is currently no spec for what to do, but ignoring unknown uses seems right.
68     testWithAESCBC([], "'enconly'"),
69     testWithAESCBC([], "enconly "),
70     testWithAESCBC([], "EncOnly"),
71     testWithAESCBC(["encrypt"], "enconly, deconly"),
72     testWithAESCBC(["decrypt","encrypt"], "enconly,,deconly"),
73
74 ]).then(function() { finishJSTest(); } );
75 </script>
76
77 <script src="../../resources/js-test-post.js"></script>
78 </body>
79 </html>