Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / crypto / jwk-import-use-values.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../resources/js-test.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": "A128CBC",
20     "ext": true,
21     "kty": "oct",
22     "k": "jnOw99oOZFLIEPMrgJB55Q"
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, jwkUsages, importUsages)
33 {
34     if (jwkUsages.key_ops) {
35         aesKeyAsJSON.key_ops = jwkUsages.key_ops;
36         delete aesKeyAsJSON.use;
37     } else {
38         delete aesKeyAsJSON.key_ops;
39         aesKeyAsJSON.use = jwkUsages.use;
40     }
41
42     return crypto.subtle.importKey("jwk", aesKeyAsJSON, {name: "AES-CBC"}, extractable, importUsages).then(function(result) {
43         key = result;
44         debug(JSON.stringify(jwkUsages) + ":");
45         shouldBe("key.usages", JSON.stringify(expectedUsages));
46         debug("");
47     }, function(result) {
48         debug(JSON.stringify(jwkUsages) + ":");
49         debug("Failed importing with " + importUsages + ": " + result);
50     });
51 }
52
53 function testWithHMAC(expectedUsages, jwkUsages, importUsages)
54 {
55     if (jwkUsages.key_ops) {
56         hmacKeyAsJSON.key_ops = jwkUsages.key_ops;
57         delete hmacKeyAsJSON.use;
58     } else {
59         delete hmacKeyAsJSON.key_ops;
60         hmacKeyAsJSON.use = jwkUsages.use;
61     }
62
63     return crypto.subtle.importKey("jwk", hmacKeyAsJSON, {name: 'hmac', hash: {name: 'sha-256'}}, extractable, importUsages).then(function(result) {
64         key = result;
65         debug(JSON.stringify(jwkUsages) + ":");
66         shouldBe("key.usages", JSON.stringify(expectedUsages));
67         debug("");
68     }, function(result) {
69         debug(JSON.stringify(jwkUsages) + ":");
70         debug("Failed importing with " + importUsages + ": " + result);
71     });
72 }
73
74 debug("");
75
76
77 Promise.all([
78     // Duplicates are not allowed.
79     testWithAESCBC(null, {key_ops: ["encrypt", "encrypt"]}, ["encrypt"]),
80
81     testWithAESCBC(["encrypt"], {key_ops: ["encrypt"]}, ["encrypt"]),
82     testWithAESCBC(null, {key_ops: ["encrypt"]}, ["decrypt"]),
83
84     testWithAESCBC(["decrypt"], {key_ops: ["decrypt"]}, ["decrypt"]),
85     testWithAESCBC(null, {key_ops: ["decrypt"]}, ["encrypt"]),
86
87     testWithAESCBC(["encrypt", "decrypt"], {key_ops: ["encrypt", "decrypt"]}, ["encrypt", "decrypt"]),
88     testWithAESCBC(["encrypt"], {key_ops: ["encrypt", "decrypt"]}, ["encrypt"]),
89     testWithAESCBC(null, {key_ops: ["encrypt", "decrypt"]}, ["unwrapKey"]),
90
91     testWithAESCBC(["wrapKey"], {key_ops: ["wrapKey"]}, ["wrapKey"]),
92     testWithAESCBC(null, {key_ops: ["wrapKey"]}, ["unwrapKey"]),
93
94     testWithAESCBC(["unwrapKey"], {key_ops: ["unwrapKey"]}, ["unwrapKey"]),
95     testWithAESCBC(["wrapKey", "unwrapKey"], {key_ops: ["wrapKey", "unwrapKey"]}, ["unwrapKey", "wrapKey"]),
96     testWithAESCBC(["encrypt", "decrypt", "wrapKey"], {key_ops: ["encrypt", "decrypt", "wrapKey"]}, ["decrypt", "encrypt", "wrapKey"]),
97
98     testWithAESCBC(["encrypt", "decrypt", "wrapKey", "unwrapKey"], {use: "enc"}, ["decrypt", "encrypt", "unwrapKey", "wrapKey"]),
99     testWithAESCBC(["encrypt", "decrypt", "unwrapKey"], {use: "enc"}, ["decrypt", "encrypt", "unwrapKey"]),
100     testWithAESCBC(["encrypt", "decrypt", "unwrapKey"], {use: "enc"}, ["decrypt", "encrypt", "unwrapKey"]),
101
102     testWithHMAC(["sign"], {key_ops: ["sign"]}, ["sign"]),
103     testWithHMAC(null, {key_ops: ["sign"]}, ["verify"]),
104
105     testWithHMAC(["verify"], {key_ops: ["verify"]}, ["verify"]),
106     testWithHMAC(null, {key_ops: ["verify"]}, ["sign"]),
107
108     testWithHMAC(["sign", "verify"], {use: "sig"}, ["sign", "verify"]),
109     testWithHMAC(["sign"], {use: "sig"}, ["sign"]),
110
111     // Unknown key_ops strings are ignored.
112     testWithAESCBC(["decrypt"], {key_ops: ["'encrypt'", "decrypt"]}, ["decrypt"]),
113     testWithAESCBC(["decrypt"], {key_ops: ["encrypt ", "foo", "decrypt"]}, ["decrypt"]),
114     testWithAESCBC(["decrypt"], {key_ops: ["Encrypt", "decrypt"]}, ["decrypt"]),
115     testWithAESCBC(null, {key_ops: ["'encrypt'", "decrypt"]}, ["encrypt"]),
116     testWithAESCBC(null, {key_ops: ["encrypt "]}, ["encrypt"]),
117     testWithAESCBC(null, {key_ops: ["Encrypt"]}, ["encrypt"]),
118
119 ]).then(finishJSTest, failAndFinishJSTest);
120 </script>
121
122 </body>
123 </html>