OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <head> |
| 4 <script src="../fast/js/resources/js-test-pre.js"></script> |
| 5 </head> |
| 6 <body> |
| 7 <p id="description"></p> |
| 8 <div id="console"></div> |
| 9 |
| 10 <script> |
| 11 description("Tests algorithm normalization."); |
| 12 |
| 13 // ------------------------------- |
| 14 // Helpers to return a normalized algorithm identifier. |
| 15 // ------------------------------- |
| 16 |
| 17 function normalizeDigest(algorithmIdentifier) { |
| 18 return crypto.subtle.digest(algorithmIdentifier).algorithm; |
| 19 } |
| 20 |
| 21 function normalizeEncrypt(algorithmIdentifier) { |
| 22 // TODO(eroman): Use a valid key. |
| 23 var key; |
| 24 return crypto.subtle.encrypt(algorithmIdentifier, key).algorithm; |
| 25 } |
| 26 |
| 27 // ------------------------------- |
| 28 // Case insensitivity of "name" |
| 29 // ------------------------------- |
| 30 algorithm = normalizeDigest({name: "SHA-1"}); |
| 31 shouldBe("algorithm.name", "'SHA-1'"); |
| 32 algorithm = normalizeDigest({name: "sHa-256"}); |
| 33 shouldBe("algorithm.name", "'SHA-256'"); |
| 34 |
| 35 // ------------------------------- |
| 36 // Failures if "name" is invalid |
| 37 // ------------------------------- |
| 38 shouldThrow("normalizeDigest({})"); |
| 39 shouldThrow("normalizeDigest({name: null})"); |
| 40 shouldThrow("normalizeDigest({name: -1})"); |
| 41 shouldThrow("normalizeDigest({name: ''})"); |
| 42 shouldThrow("normalizeDigest({name: 'nosuchalgorithm'})"); |
| 43 shouldThrow("normalizeDigest({name: '\\u0189'})"); |
| 44 |
| 45 // ------------------------------- |
| 46 // Failures if the algorithm identifier is not an object |
| 47 // ------------------------------- |
| 48 shouldThrow("normalizeDigest(null)"); |
| 49 shouldThrow("normalizeDigest(0)"); |
| 50 shouldThrow("normalizeDigest(undefined)"); |
| 51 shouldThrow("normalizeDigest('')"); |
| 52 |
| 53 // ------------------------------- |
| 54 // Skip unrecognized parameters. |
| 55 // ------------------------------- |
| 56 algorithm = normalizeDigest({name: "sHa-1", noSuchParam: 3}); |
| 57 shouldBeUndefined("algorithm.noSuchParam"); |
| 58 |
| 59 // ------------------------------- |
| 60 // Normalized algorithm COPIES all data |
| 61 // ------------------------------- |
| 62 originalIv = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1
5]); |
| 63 algorithm = normalizeEncrypt({ name: "aes-cbc", iv: originalIv }); |
| 64 |
| 65 // Make sure it constructed the normalized result. |
| 66 shouldBe("algorithm.name", "'AES-CBC'"); |
| 67 shouldBe("algorithm.iv.length", "16"); |
| 68 shouldBe("algorithm.iv[3]", "3"); |
| 69 |
| 70 // Mutate the original (un-normalized) algorithm. Verify that this doesn't affec
t the normalized output. |
| 71 originalIv[3] = 0; |
| 72 shouldBe("algorithm.iv[3]", "3"); |
| 73 |
| 74 // ------------------------------- |
| 75 // AES-CBC normalization failures |
| 76 // ------------------------------- |
| 77 |
| 78 // The "iv" MUST be 16 bytes long. |
| 79 rawAlgorithm = { |
| 80 name: "AES-CBC", |
| 81 iv: new Uint8Array([1, 2, 3]) |
| 82 }; |
| 83 shouldThrow("normalizeEncrypt(rawAlgorithm)"); |
| 84 |
| 85 // ------------------------------- |
| 86 // Normalize a normalized algorithm (SHA-384) |
| 87 // ------------------------------- |
| 88 algorithm = normalizeDigest({name: "sHa-384"}); |
| 89 shouldBe("algorithm.name", "'SHA-384'"); |
| 90 algorithm = normalizeDigest(algorithm); |
| 91 shouldBe("algorithm.name", "'SHA-384'"); |
| 92 |
| 93 // ------------------------------- |
| 94 // Normalize a normalized algorithm (AES-CBC, encrypt) |
| 95 // ------------------------------- |
| 96 algorithm = normalizeEncrypt({ name: "aEs-cbc", iv: originalIv }); |
| 97 // Make sure it constructed the normalized result. |
| 98 shouldBe("algorithm.name", "'AES-CBC'"); |
| 99 shouldBe("algorithm.iv.length", "16"); |
| 100 shouldBe("algorithm.iv[1]", "1"); |
| 101 algorithm = normalizeEncrypt(algorithm); |
| 102 shouldBe("algorithm.name", "'AES-CBC'"); |
| 103 shouldBe("algorithm.iv.length", "16"); |
| 104 shouldBe("algorithm.iv[1]", "1"); |
| 105 |
| 106 // ------------------------------- |
| 107 // Unsupported operation on algorithm |
| 108 // ------------------------------- |
| 109 shouldThrow("normalizeEncrypt({ name: 'SHA-1' })"); |
| 110 shouldThrow("normalizeDigest({ name: 'AES-CBC', iv: originalIv })"); |
| 111 |
| 112 </script> |
| 113 |
| 114 <script src="../fast/js/resources/js-test-post.js"></script> |
| 115 </body> |
| 116 </html> |
OLD | NEW |