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 </head> | 5 </head> |
6 <body> | 6 <body> |
7 <p id="description"></p> | 7 <p id="description"></p> |
8 <div id="console"></div> | 8 <div id="console"></div> |
9 | 9 |
10 <script> | 10 <script> |
11 description("Tests algorithm normalization."); | 11 description("Tests algorithm normalization."); |
12 | 12 |
13 jsTestIsAsync = true; | |
14 | |
15 // ------------------------------- | 13 // ------------------------------- |
16 // Helpers to return a normalized algorithm identifier. | 14 // Helpers to return a normalized algorithm identifier. |
17 // ------------------------------- | 15 // ------------------------------- |
18 | 16 |
19 key = null; | 17 function normalizeDigest(algorithmIdentifier) { |
20 | |
21 function normalizeDigest(algorithmIdentifier) | |
22 { | |
23 return crypto.subtle.digest(algorithmIdentifier).algorithm; | 18 return crypto.subtle.digest(algorithmIdentifier).algorithm; |
24 } | 19 } |
25 | 20 |
26 function normalizeEncrypt(algorithmIdentifier) | 21 function normalizeEncrypt(algorithmIdentifier) { |
27 { | 22 // TODO(eroman): Use a valid key. |
| 23 var key; |
28 return crypto.subtle.encrypt(algorithmIdentifier, key).algorithm; | 24 return crypto.subtle.encrypt(algorithmIdentifier, key).algorithm; |
29 } | 25 } |
30 | 26 |
31 function normalizeSign(algorithmIdentifier) | 27 function normalizeSign(algorithmIdentifier) { |
32 { | 28 // TODO(eroman): Use a valid key. |
| 29 var key; |
33 return crypto.subtle.sign(algorithmIdentifier, key).algorithm; | 30 return crypto.subtle.sign(algorithmIdentifier, key).algorithm; |
34 } | 31 } |
35 | 32 |
36 function runTests() | 33 // ------------------------------- |
37 { | 34 // Case insensitivity of "name" |
38 // ------------------------------- | 35 // ------------------------------- |
39 // Case insensitivity of "name" | 36 algorithm = normalizeDigest({name: "SHA-1"}); |
40 // ------------------------------- | 37 shouldBe("algorithm.name", "'SHA-1'"); |
41 algorithm = normalizeDigest({name: "SHA-1"}); | 38 algorithm = normalizeDigest({name: "sHa-256"}); |
42 shouldBe("algorithm.name", "'SHA-1'"); | 39 shouldBe("algorithm.name", "'SHA-256'"); |
43 algorithm = normalizeDigest({name: "sHa-256"}); | |
44 shouldBe("algorithm.name", "'SHA-256'"); | |
45 | 40 |
46 // ------------------------------- | 41 // ------------------------------- |
47 // Failures if "name" is invalid | 42 // Failures if "name" is invalid |
48 // ------------------------------- | 43 // ------------------------------- |
49 shouldThrow("normalizeDigest({})"); | 44 shouldThrow("normalizeDigest({})"); |
50 shouldThrow("normalizeDigest({name: null})"); | 45 shouldThrow("normalizeDigest({name: null})"); |
51 shouldThrow("normalizeDigest({name: -1})"); | 46 shouldThrow("normalizeDigest({name: -1})"); |
52 shouldThrow("normalizeDigest({name: ''})"); | 47 shouldThrow("normalizeDigest({name: ''})"); |
53 shouldThrow("normalizeDigest({name: 'nosuchalgorithm'})"); | 48 shouldThrow("normalizeDigest({name: 'nosuchalgorithm'})"); |
54 shouldThrow("normalizeDigest({name: '\\u0189'})"); | 49 shouldThrow("normalizeDigest({name: '\\u0189'})"); |
55 | 50 |
56 // ------------------------------- | 51 // ------------------------------- |
57 // Failures if the algorithm identifier is not an object | 52 // Failures if the algorithm identifier is not an object |
58 // ------------------------------- | 53 // ------------------------------- |
59 shouldThrow("normalizeDigest(null)"); | 54 shouldThrow("normalizeDigest(null)"); |
60 shouldThrow("normalizeDigest(0)"); | 55 shouldThrow("normalizeDigest(0)"); |
61 shouldThrow("normalizeDigest(undefined)"); | 56 shouldThrow("normalizeDigest(undefined)"); |
62 shouldThrow("normalizeDigest('')"); | 57 shouldThrow("normalizeDigest('')"); |
63 | 58 |
64 // ------------------------------- | 59 // ------------------------------- |
65 // Skip unrecognized parameters. | 60 // Skip unrecognized parameters. |
66 // ------------------------------- | 61 // ------------------------------- |
67 algorithm = normalizeDigest({name: "sHa-1", noSuchParam: 3}); | 62 algorithm = normalizeDigest({name: "sHa-1", noSuchParam: 3}); |
68 shouldBeUndefined("algorithm.noSuchParam"); | 63 shouldBeUndefined("algorithm.noSuchParam"); |
69 | 64 |
70 // ------------------------------- | 65 // ------------------------------- |
71 // Normalized algorithm COPIES all data | 66 // Normalized algorithm COPIES all data |
72 // ------------------------------- | 67 // ------------------------------- |
73 originalIv = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1
4, 15]); | 68 originalIv = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1
5]); |
74 algorithm = normalizeEncrypt({ name: "aes-cbc", iv: originalIv }); | 69 algorithm = normalizeEncrypt({ name: "aes-cbc", iv: originalIv }); |
75 | 70 |
76 // Make sure it constructed the normalized result. | 71 // Make sure it constructed the normalized result. |
77 shouldBe("algorithm.name", "'AES-CBC'"); | 72 shouldBe("algorithm.name", "'AES-CBC'"); |
78 shouldBe("algorithm.iv.length", "16"); | 73 shouldBe("algorithm.iv.length", "16"); |
79 shouldBe("algorithm.iv[3]", "3"); | 74 shouldBe("algorithm.iv[3]", "3"); |
80 | 75 |
81 // Mutate the original (un-normalized) algorithm. Verify that this doesn't a
ffect the normalized output. | 76 // Mutate the original (un-normalized) algorithm. Verify that this doesn't affec
t the normalized output. |
82 originalIv[3] = 0; | 77 originalIv[3] = 0; |
83 shouldBe("algorithm.iv[3]", "3"); | 78 shouldBe("algorithm.iv[3]", "3"); |
84 | 79 |
85 // ------------------------------- | 80 // ------------------------------- |
86 // AES-CBC normalization failures | 81 // AES-CBC normalization failures |
87 // ------------------------------- | 82 // ------------------------------- |
88 | 83 |
89 // The "iv" MUST be 16 bytes long. | 84 // The "iv" MUST be 16 bytes long. |
90 rawAlgorithm = { | 85 rawAlgorithm = { |
91 name: "AES-CBC", | 86 name: "AES-CBC", |
92 iv: new Uint8Array([1, 2, 3]) | 87 iv: new Uint8Array([1, 2, 3]) |
93 }; | 88 }; |
94 shouldThrow("normalizeEncrypt(rawAlgorithm)"); | 89 shouldThrow("normalizeEncrypt(rawAlgorithm)"); |
95 | 90 |
96 // ------------------------------- | 91 // ------------------------------- |
97 // Normalize a normalized algorithm (SHA-384) | 92 // Normalize a normalized algorithm (SHA-384) |
98 // ------------------------------- | 93 // ------------------------------- |
99 algorithm = normalizeDigest({name: "sHa-384"}); | 94 algorithm = normalizeDigest({name: "sHa-384"}); |
100 shouldBe("algorithm.name", "'SHA-384'"); | 95 shouldBe("algorithm.name", "'SHA-384'"); |
101 algorithm = normalizeDigest(algorithm); | 96 algorithm = normalizeDigest(algorithm); |
102 shouldBe("algorithm.name", "'SHA-384'"); | 97 shouldBe("algorithm.name", "'SHA-384'"); |
103 | 98 |
104 // ------------------------------- | 99 // ------------------------------- |
105 // Normalize a normalized algorithm (AES-CBC, encrypt) | 100 // Normalize a normalized algorithm (AES-CBC, encrypt) |
106 // ------------------------------- | 101 // ------------------------------- |
107 algorithm = normalizeEncrypt({ name: "aEs-cbc", iv: originalIv }); | 102 algorithm = normalizeEncrypt({ name: "aEs-cbc", iv: originalIv }); |
108 // Make sure it constructed the normalized result. | 103 // Make sure it constructed the normalized result. |
109 shouldBe("algorithm.name", "'AES-CBC'"); | 104 shouldBe("algorithm.name", "'AES-CBC'"); |
110 shouldBe("algorithm.iv.length", "16"); | 105 shouldBe("algorithm.iv.length", "16"); |
111 shouldBe("algorithm.iv[1]", "1"); | 106 shouldBe("algorithm.iv[1]", "1"); |
112 algorithm = normalizeEncrypt(algorithm); | 107 algorithm = normalizeEncrypt(algorithm); |
113 shouldBe("algorithm.name", "'AES-CBC'"); | 108 shouldBe("algorithm.name", "'AES-CBC'"); |
114 shouldBe("algorithm.iv.length", "16"); | 109 shouldBe("algorithm.iv.length", "16"); |
115 shouldBe("algorithm.iv[1]", "1"); | 110 shouldBe("algorithm.iv[1]", "1"); |
116 | 111 |
117 // ------------------------------- | 112 // ------------------------------- |
118 // Unsupported operation on algorithm | 113 // Unsupported operation on algorithm |
119 // ------------------------------- | 114 // ------------------------------- |
120 shouldThrow("normalizeEncrypt({ name: 'SHA-1' })"); | 115 shouldThrow("normalizeEncrypt({ name: 'SHA-1' })"); |
121 shouldThrow("normalizeDigest({ name: 'AES-CBC', iv: originalIv })"); | 116 shouldThrow("normalizeDigest({ name: 'AES-CBC', iv: originalIv })"); |
122 | 117 |
123 // ------------------------------- | 118 // ------------------------------- |
124 // Normalize HMAC | 119 // Normalize HMAC |
125 // ------------------------------- | 120 // ------------------------------- |
126 shouldThrow("normalizeSign({name: 'hmac'})"); // Missing "hash" | 121 shouldThrow("normalizeSign({name: 'hmac'})"); // Missing "hash" |
127 shouldThrow("normalizeSign({name: 'hmac', hash: 'foo'})"); // Not a valid "h
ash" | 122 shouldThrow("normalizeSign({name: 'hmac', hash: 'foo'})"); // Not a valid "hash" |
128 shouldThrow("normalizeSign({name: 'hmac', hash: { name: 'AES-CBC', iv: origi
nalIv }})"); // Not a valid "hash" | 123 shouldThrow("normalizeSign({name: 'hmac', hash: { name: 'AES-CBC', iv: originalI
v }})"); // Not a valid "hash" |
129 | 124 |
130 validHmacSha1 = {name: 'hmac', hash: {name: 'Sha-1'}}; | 125 validHmacSha1 = {name: 'hmac', hash: {name: 'Sha-1'}}; |
131 algorithm = normalizeSign(validHmacSha1); | 126 algorithm = normalizeSign(validHmacSha1); |
132 shouldBe("algorithm.name", "'HMAC'"); | 127 shouldBe("algorithm.name", "'HMAC'"); |
133 shouldBe("algorithm.hash.name", "'SHA-1'"); | 128 shouldBe("algorithm.hash.name", "'SHA-1'"); |
134 | 129 |
135 shouldThrow("normalizeEncrypt(validHmacSha1)"); // Not defined for encrypt() | 130 shouldThrow("normalizeEncrypt(validHmacSha1)"); // Not defined for encrypt() |
136 } | |
137 | |
138 function keyImported(newKey) | |
139 { | |
140 key = newKey; | |
141 runTests(); | |
142 finishJSTest(); | |
143 } | |
144 | |
145 function failedKeyImport(value) | |
146 { | |
147 debug("Failed importing key: " + value); | |
148 finishJSTest(); | |
149 } | |
150 | |
151 // This is a bogus key import, however the mock constructs something usable. | |
152 keyFormat = "spki"; | |
153 data = new Uint8Array([]); | |
154 algorithm = {name: "Sha-256"}; | |
155 extractable = false; | |
156 keyUsages = []; | |
157 | |
158 crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages).then
(keyImported, failedKeyImport); | |
159 | 131 |
160 </script> | 132 </script> |
161 | 133 |
162 <script src="../fast/js/resources/js-test-post.js"></script> | 134 <script src="../fast/js/resources/js-test-post.js"></script> |
163 </body> | 135 </body> |
164 </html> | 136 </html> |
OLD | NEW |