| OLD | NEW |
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <html> | 2 <html> |
| 3 <head> | 3 <head> |
| 4 <script src="../fast/js/resources/js-test-pre.js"></script> | 4 <script src="../fast/js/resources/js-test-pre.js"></script> |
| 5 <script src="resources/common.js"></script> | 5 <script src="resources/common.js"></script> |
| 6 </head> | 6 </head> |
| 7 <body> | 7 <body> |
| 8 <p id="description"></p> | 8 <p id="description"></p> |
| 9 <div id="console"></div> | 9 <div id="console"></div> |
| 10 | 10 |
| 11 <script> | 11 <script> |
| 12 description("Tests cypto.subtle.importKey."); | 12 description("Tests cypto.subtle.importKey."); |
| 13 | 13 |
| 14 jsTestIsAsync = true; | 14 jsTestIsAsync = true; |
| 15 | 15 |
| 16 // Each sub-test run in this file is asynchronous. Chaining them together | 16 aesCbc = {name: 'aes-cbc'}; |
| 17 // manually leads to very unreadable code, due to having closures within | |
| 18 // closures within closures. Instead of doing that, each subtest calls | |
| 19 // "startNextTest()" once it has completed. | |
| 20 | 17 |
| 21 currentTestIndex = 0; | 18 Promise.fulfill(null).then(function() { |
| 19 keyFormat = "raw"; |
| 20 data = asciiToArrayBuffer("private"); |
| 21 algorithm = { name: 'hmac', hash: { name: 'sha-256' } }; |
| 22 extractable = true; |
| 23 // Note there are duplicates |
| 24 keyUsages = ['encrypt', 'encrypt', 'encrypt', 'sign']; |
| 22 | 25 |
| 23 function startNextTest() | 26 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); |
| 24 { | 27 }).then(function(result) { |
| 25 var currentTest = allTests[currentTestIndex++]; | 28 key = result; |
| 26 | 29 shouldBe("key.type", "'private'") |
| 27 if (!currentTest) { | 30 shouldBe("key.extractable", "true") |
| 28 finishJSTest(); | 31 shouldBe("key.algorithm.name", "'HMAC'") |
| 29 return; | 32 shouldBe("key.algorithm.hash.name", "'SHA-256'") |
| 30 } | 33 shouldBe("key.usages.join(',')", "'encrypt,sign'") |
| 31 | |
| 32 currentTest(); | |
| 33 } | |
| 34 | |
| 35 function rejectHandler(value) | |
| 36 { | |
| 37 debug(" rejected with value of " + value); | |
| 38 startNextTest(); | |
| 39 } | |
| 40 | |
| 41 function failHandler(value) | |
| 42 { | |
| 43 testFailed(value); | |
| 44 startNextTest(); | |
| 45 } | |
| 46 | |
| 47 aesCbc = { name: 'aes-cbc' }; | |
| 48 | |
| 49 allTests = [ | |
| 50 function() | |
| 51 { | |
| 52 keyFormat = "raw"; | |
| 53 data = asciiToArrayBuffer("private"); | |
| 54 algorithm = { name: 'hmac', hash: { name: 'sha-256' } }; | |
| 55 extractable = true; | |
| 56 // Note there are duplicates | |
| 57 keyUsages = ['encrypt', 'encrypt', 'encrypt', 'sign']; | |
| 58 | |
| 59 crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsag
es).then( | |
| 60 function(value) { | |
| 61 key = value; | |
| 62 shouldBe("key.type", "'private'") | |
| 63 shouldBe("key.extractable", "true") | |
| 64 shouldBe("key.algorithm.name", "'HMAC'") | |
| 65 shouldBe("key.algorithm.hash.name", "'SHA-256'") | |
| 66 shouldBe("key.usages.join(',')", "'encrypt,sign'") | |
| 67 | |
| 68 startNextTest(); | |
| 69 }, failHandler); | |
| 70 }, | |
| 71 | 34 |
| 72 // Same test as above, but with an keyUsages, and AES-CBC. | 35 // Same test as above, but with an keyUsages, and AES-CBC. |
| 73 function() | 36 keyFormat = "raw"; |
| 74 { | 37 data = asciiToArrayBuffer("private"); |
| 75 keyFormat = "raw"; | 38 algorithm = aesCbc; |
| 76 data = asciiToArrayBuffer("private"); | 39 extractable = true; |
| 77 algorithm = aesCbc; | 40 keyUsages = []; |
| 78 extractable = true; | |
| 79 keyUsages = []; | |
| 80 | 41 |
| 81 crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsag
es).then( | 42 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); |
| 82 function(value) { | 43 }).then(function(result) { |
| 83 key = value; | 44 key = result; |
| 84 shouldBe("key.type", "'private'") | 45 shouldBe("key.type", "'private'") |
| 85 shouldBe("key.extractable", "true") | 46 shouldBe("key.extractable", "true") |
| 86 shouldBe("key.algorithm.name", "'AES-CBC'") | 47 shouldBe("key.algorithm.name", "'AES-CBC'") |
| 87 shouldBe("key.usages.join(',')", "''") | 48 shouldBe("key.usages.join(',')", "''") |
| 88 | |
| 89 startNextTest(); | |
| 90 }, failHandler); | |
| 91 }, | |
| 92 | 49 |
| 93 // Same test as above, but with extractable = false. | 50 // Same test as above, but with extractable = false. |
| 94 function() | 51 keyFormat = "raw"; |
| 95 { | 52 data = asciiToArrayBuffer("private"); |
| 96 keyFormat = "raw"; | 53 algorithm = aesCbc; |
| 97 data = asciiToArrayBuffer("private"); | 54 extractable = false; |
| 98 algorithm = aesCbc; | 55 keyUsages = []; |
| 99 extractable = false; | |
| 100 keyUsages = []; | |
| 101 | 56 |
| 102 crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsag
es).then( | 57 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); |
| 103 function(value) { | 58 }).then(function(result) { |
| 104 key = value; | 59 key = result; |
| 105 shouldBe("key.type", "'private'") | 60 shouldBe("key.type", "'private'") |
| 106 shouldBe("key.extractable", "false") | 61 shouldBe("key.extractable", "false") |
| 107 shouldBe("key.algorithm.name", "'AES-CBC'") | 62 shouldBe("key.algorithm.name", "'AES-CBC'") |
| 108 shouldBe("key.usages.join(',')", "''") | 63 shouldBe("key.usages.join(',')", "''") |
| 109 | |
| 110 startNextTest(); | |
| 111 }, failHandler); | |
| 112 }, | |
| 113 | 64 |
| 114 // Same test as above, but with key.type of public. | 65 // Same test as above, but with key.type of public. |
| 115 function() | 66 keyFormat = "raw"; |
| 116 { | 67 data = asciiToArrayBuffer("public"); |
| 117 keyFormat = "raw"; | 68 algorithm = aesCbc; |
| 118 data = asciiToArrayBuffer("public"); | 69 extractable = false; |
| 119 algorithm = aesCbc; | 70 keyUsages = []; |
| 120 extractable = false; | |
| 121 keyUsages = []; | |
| 122 | 71 |
| 123 crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsag
es).then( | 72 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); |
| 124 function(value) { | 73 }).then(function(result) { |
| 125 key = value; | 74 key = result; |
| 126 shouldBe("key.type", "'public'") | 75 shouldBe("key.type", "'public'") |
| 127 shouldBe("key.extractable", "false") | 76 shouldBe("key.extractable", "false") |
| 128 shouldBe("key.algorithm.name", "'AES-CBC'") | 77 shouldBe("key.algorithm.name", "'AES-CBC'") |
| 129 shouldBe("key.usages.join(',')", "''") | 78 shouldBe("key.usages.join(',')", "''") |
| 130 | |
| 131 startNextTest(); | |
| 132 }, failHandler); | |
| 133 }, | |
| 134 | 79 |
| 135 // Same test as above, but with keyFormat = spki | 80 // Same test as above, but with keyFormat = spki |
| 136 function() | 81 keyFormat = "spki"; |
| 137 { | 82 data = asciiToArrayBuffer("public"); |
| 138 keyFormat = "spki"; | 83 algorithm = aesCbc; |
| 139 data = asciiToArrayBuffer("public"); | 84 extractable = false; |
| 140 algorithm = aesCbc; | 85 keyUsages = []; |
| 141 extractable = false; | |
| 142 keyUsages = []; | |
| 143 | 86 |
| 144 crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsag
es).then( | 87 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); |
| 145 function(value) { | 88 }).then(function(result) { |
| 146 key = value; | 89 key = result; |
| 147 shouldBe("key.type", "'public'") | 90 shouldBe("key.type", "'public'") |
| 148 shouldBe("key.extractable", "false") | 91 shouldBe("key.extractable", "false") |
| 149 shouldBe("key.algorithm.name", "'AES-CBC'") | 92 shouldBe("key.algorithm.name", "'AES-CBC'") |
| 150 shouldBe("key.usages.join(',')", "''") | 93 shouldBe("key.usages.join(',')", "''") |
| 151 | 94 |
| 152 startNextTest(); | 95 keyFormat = "spki"; |
| 153 }, failHandler); | 96 data = asciiToArrayBuffer("error"); |
| 154 }, | 97 algorithm = aesCbc; |
| 98 extractable = false; |
| 99 keyUsages = []; |
| 155 | 100 |
| 156 function() | 101 return crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyU
sages); |
| 157 { | 102 }).then(undefined, function(result) { |
| 158 keyFormat = "spki"; | 103 debug("rejected with " + result); |
| 159 data = asciiToArrayBuffer("error"); | |
| 160 algorithm = aesCbc; | |
| 161 extractable = false; | |
| 162 keyUsages = []; | |
| 163 | 104 |
| 164 crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsag
es).then( | 105 keyFormat = "raw"; |
| 165 failHandler, | 106 data = asciiToArrayBuffer(""); |
| 166 function(value) { | 107 algorithm = aesCbc; |
| 167 debug("rejected with " + value); | 108 extractable = true; |
| 168 startNextTest(); | 109 keyUsages = []; |
| 169 }); | |
| 170 }, | |
| 171 | 110 |
| 172 function() | 111 // Invalid format. |
| 173 { | 112 shouldThrow("crypto.subtle.importKey('invalid format', data, algorithm, extr
actable, keyUsages)"); |
| 174 keyFormat = "raw"; | |
| 175 data = asciiToArrayBuffer(""); | |
| 176 algorithm = aesCbc; | |
| 177 extractable = true; | |
| 178 | 113 |
| 179 // Note contains duplicates and invalid entries. | 114 // Invalid key usage. |
| 180 keyUsages = []; | 115 shouldThrow("crypto.subtle.importKey(keyFormat, data, algorithm, extractable
, ['SIGN'])"); |
| 181 | 116 |
| 182 // Invalid format. | 117 // Undefined key usage. |
| 183 shouldThrow("crypto.subtle.importKey('invalid format', data, algorithm,
extractable, keyUsages)"); | 118 // FIXME: http://crbug.com/262383 |
| 119 //shouldThrow("crypto.subtle.importKey(keyFormat, data, algorithm, extractab
le, undefined)"); |
| 184 | 120 |
| 185 // Invalid key usage. | 121 // Invalid data |
| 186 shouldThrow("crypto.subtle.importKey(keyFormat, data, algorithm, extract
able, ['SIGN'])"); | 122 shouldThrow("crypto.subtle.importKey(keyFormat, [], algorithm, extractable,
keyUsages)"); |
| 123 shouldThrow("crypto.subtle.importKey(keyFormat, null, algorithm, extractable
, keyUsages)"); |
| 187 | 124 |
| 188 // Undefined key usage. | 125 // Missing hash parameter for HMAC. |
| 189 // FIXME: http://crbug.com/262383 | 126 shouldThrow("crypto.subtle.importKey(keyFormat, data, {name: 'hmac'}, extrac
table, keyUsages)"); |
| 190 //shouldThrow("crypto.subtle.importKey(keyFormat, data, algorithm, extra
ctable, undefined)"); | |
| 191 | 127 |
| 192 // Invalid data | 128 // SHA-1 doesn't support the importKey operation. |
| 193 shouldThrow("crypto.subtle.importKey(keyFormat, [], algorithm, extractab
le, keyUsages)"); | 129 shouldThrow("crypto.subtle.importKey(keyFormat, data, {name: 'sha-1'}, extra
ctable, keyUsages)"); |
| 194 shouldThrow("crypto.subtle.importKey(keyFormat, null, algorithm, extract
able, keyUsages)"); | 130 }).then(finishJSTest, failAndFinishJSTest); |
| 195 | 131 |
| 196 // Missing hash parameter for HMAC. | |
| 197 invalidHmac = { name: 'hmac' }; | |
| 198 shouldThrow("crypto.subtle.importKey(keyFormat, data, invalidHmac, extra
ctable, keyUsages)"); | |
| 199 | |
| 200 // SHA-1 doesn't support the importKey operation. | |
| 201 sha1 = { name: 'sha-1' }; | |
| 202 shouldThrow("crypto.subtle.importKey(keyFormat, data, sha1, extractable,
keyUsages)"); | |
| 203 | |
| 204 startNextTest(); | |
| 205 }, | |
| 206 | |
| 207 ]; | |
| 208 | |
| 209 // Begin! | |
| 210 startNextTest(); | |
| 211 </script> | 132 </script> |
| 212 | 133 |
| 213 <script src="../fast/js/resources/js-test-post.js"></script> | 134 <script src="../fast/js/resources/js-test-post.js"></script> |
| 214 </body> | 135 </body> |
| 215 </html> | 136 </html> |
| OLD | NEW |