Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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("Tests structured cloning of AES keys"); | |
| 13 | |
| 14 jsTestIsAsync = true; | |
| 15 | |
| 16 // Tests the 48 permutations of keys generated by: | |
| 17 // kPossibleAlgorithms x kPossibleExtractable x kPossibleKeyUsages x kPossible KeyData | |
| 18 // | |
| 19 // For practical reasons these tests are not exhaustive. | |
| 20 | |
| 21 var k128BitData = "30112233445566778899aabbccddeeff" | |
| 22 var k192BitData = "800102030405060708090a0b0c0d0e0f1011121314151617"; | |
| 23 var k256BitData = "00112233445546778899aabbccddeeff000102030405060708090a0b0c0d0 e0f"; | |
| 24 | |
| 25 var kPossibleAlgorithms = ['AES-CBC', 'AES-GCM']; | |
| 26 var kPossibleExtractable = [true, false]; | |
| 27 var kPossibleKeyUsages = [[], ['encrypt'], ['decrypt', 'wrapKey'], ['encrypt', ' wrapKey', 'unwrapKey']]; | |
| 28 var kPossibleKeyData = [k128BitData, k192BitData, k256BitData]; | |
| 29 | |
| 30 function runTest(algorithmName, extractable, keyUsages, keyData) | |
| 31 { | |
| 32 var keyLengthBits = keyData.length * 4; | |
| 33 | |
| 34 return crypto.subtle.importKey('raw', hexStringToUint8Array(keyData), { name : algorithmName }, | |
| 35 extractable, keyUsages).then(function(result) { | |
| 36 importedKey = result; | |
| 37 | |
| 38 shouldBe("importedKey.type", evalWrap("secret")); | |
|
jsbell
2014/03/13 20:16:07
This can be shouldBeEqualToString("importedKey.typ
| |
| 39 shouldBe("importedKey.extractable", evalWrap(extractable)); | |
|
jsbell
2014/03/13 20:16:07
Using the shouldBe() helpers is a pain with locals
eroman
2014/03/14 05:24:33
I ended up creating a wrapper function shouldEvalu
| |
| 40 shouldBe("importedKey.algorithm.name", evalWrap(algorithmName)); | |
| 41 shouldBe("importedKey.algorithm.length", evalWrap(keyLengthBits)); | |
| 42 shouldBe("importedKey.usages.join(',')", evalWrap(keyUsages.join(","))); | |
| 43 | |
| 44 clonedKey = cloneKeyAndLog(importedKey); | |
| 45 | |
| 46 shouldBe("clonedKey.type", evalWrap("secret")); | |
| 47 shouldBe("clonedKey.extractable", evalWrap(extractable)); | |
| 48 shouldBe("clonedKey.algorithm.name", evalWrap(algorithmName)); | |
| 49 shouldBe("clonedKey.algorithm.length", evalWrap(keyLengthBits)); | |
| 50 shouldBe("clonedKey.usages.join(',')", evalWrap(keyUsages.join(","))); | |
| 51 | |
| 52 // Check the key bytes. | |
| 53 if (extractable) { | |
| 54 return crypto.subtle.exportKey('raw', clonedKey).then(function(resul t) { | |
| 55 bytesShouldMatchHexString("cloned key data", keyData, result); | |
| 56 }); | |
| 57 } | |
| 58 }); | |
| 59 } | |
| 60 | |
| 61 for (var algorithmIndex = 0; algorithmIndex < kPossibleAlgorithms.length; ++algo rithmIndex) { | |
|
jsbell
2014/03/13 20:16:07
Might be more readable to write these using forEac
eroman
2014/03/14 05:24:33
Done. You were right, much more readable with forE
| |
| 62 for (var extractableIndex = 0; extractableIndex < kPossibleExtractable.lengt h; ++extractableIndex) { | |
| 63 for (var usagesIndex = 0; usagesIndex < kPossibleKeyUsages.length; ++usa gesIndex) { | |
| 64 for (var dataIndex = 0; dataIndex < kPossibleKeyData.length; ++dataI ndex) { | |
| 65 var algorithmName = kPossibleAlgorithms[algorithmIndex]; | |
| 66 var extractable = kPossibleExtractable[extractableIndex]; | |
| 67 var keyUsages = kPossibleKeyUsages[usagesIndex]; | |
| 68 var keyData = kPossibleKeyData[dataIndex]; | |
| 69 | |
| 70 addTask(runTest(algorithmName, extractable, keyUsages, keyData)) ; | |
| 71 } | |
| 72 } | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 completeTestWhenAllTasksDone(); | |
| 77 | |
| 78 </script> | |
| 79 | |
| 80 </body> | |
| 81 </html> | |
| OLD | NEW |