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 cypto.subtle.digest."); | |
12 | |
13 jsTestIsAsync = true; | |
14 | |
15 // Builds a hex string representation of any array-like input (array or | |
16 // ArrayBufferView). The output looks like this: | |
17 // [ab 03 4c 99] | |
18 function byteArrayToHexString(bytes) | |
19 { | |
20 var hexBytes = []; | |
21 | |
22 for (var i = 0; i < bytes.length; ++i) { | |
23 var byteString = bytes[i].toString(16); | |
24 if (byteString.length < 2) | |
25 byteString = "0" + byteString; | |
26 hexBytes.push(byteString); | |
27 } | |
28 | |
29 return "[" + hexBytes.join(" ") + "]"; | |
30 } | |
31 | |
32 // Each sub-test run in this file is asynchronous. Chaining them together | |
33 // manually leads to very unreadable code, due to having closures within | |
34 // closures within closures. Instead of doing that, each subtest calls | |
35 // "startNextTest()" once it has completed. | |
36 | |
37 currentTestIndex = 0; | |
38 | |
39 function startNextTest() | |
40 { | |
41 var currentTest = allTests[currentTestIndex++]; | |
42 | |
43 if (!currentTest) { | |
44 finishJSTest(); | |
45 return; | |
46 } | |
47 | |
48 currentTest(); | |
49 } | |
50 | |
51 function rejectHandler(value) | |
52 { | |
53 debug(" rejected with value of " + value); | |
54 startNextTest(); | |
55 } | |
56 | |
57 function resultHandler(buffer) | |
58 { | |
59 debug(" = " + byteArrayToHexString(new Uint8Array(buffer))); | |
60 startNextTest(); | |
61 } | |
62 | |
63 allTests = [ | |
64 function() | |
65 { | |
66 debug("SHA1 of [] -- with empty process()") | |
67 op = crypto.subtle.digest({name: 'sha-1'}); | |
68 op.process(new Uint8Array([])); | |
69 op.finish().then(resultHandler, rejectHandler); | |
70 }, | |
71 | |
72 function() | |
73 { | |
74 debug("SHA1 of [] -- without calling process()") | |
75 op = crypto.subtle.digest({name: 'sha-1'}); | |
76 op.finish().then(resultHandler, rejectHandler); | |
77 }, | |
78 | |
79 function() | |
80 { | |
81 debug("SHA1 of [0x0]") | |
82 op = crypto.subtle.digest({name: 'sha-1'}); | |
83 op.process(new Uint8Array([0])); | |
84 op.finish().then(resultHandler, rejectHandler); | |
85 }, | |
86 | |
87 function() | |
88 { | |
89 debug("SHA1 of [0x0] -- as an ArrayBuffer") | |
90 op = crypto.subtle.digest({name: 'sha-1'}); | |
91 op.process(new Uint8Array([0]).buffer); | |
92 op.finish().then(resultHandler, rejectHandler); | |
93 }, | |
94 | |
95 function() | |
96 { | |
97 debug("SHA1 of [0, 1, 2, 3, 4, 5] -- multipart") | |
98 op = crypto.subtle.digest({name: 'sha-1'}); | |
99 op.process(new Uint8Array([0])); | |
100 op.process(new Uint8Array([1])); | |
101 op.process(new Uint8Array([2])); | |
102 op.process(new Uint8Array([3])); | |
103 op.process(new Uint8Array([4])); | |
104 op.process(new Uint8Array([5])); | |
105 op.finish().then(resultHandler, rejectHandler); | |
106 }, | |
107 | |
108 function() | |
109 { | |
110 debug("Call process() after finish()"); | |
111 op = crypto.subtle.digest({name: 'sha-1'}); | |
112 op.finish().then(resultHandler, rejectHandler); | |
113 // These should all be no-ops, since has already finished. | |
114 op.process(new Uint8Array([0])); | |
115 op.process(new Uint8Array([1])); | |
116 op.process(new Uint8Array([2])); | |
117 op.process(new Uint8Array([3])); | |
118 }, | |
119 | |
120 function() | |
121 { | |
122 debug("abort()"); | |
123 op = crypto.subtle.digest({name: 'sha-1'}); | |
124 op.abort().then(resultHandler, rejectHandler); | |
125 }, | |
126 | |
127 function() | |
128 { | |
129 debug("abort() and then abort()"); | |
130 op = crypto.subtle.digest({name: 'sha-1'}); | |
131 op.abort().then(resultHandler, rejectHandler); | |
132 op.abort(); | |
133 }, | |
134 | |
135 function() | |
136 { | |
137 debug("process() and then abort()"); | |
138 op = crypto.subtle.digest({name: 'sha-1'}); | |
139 op.process(new Uint8Array([0])); | |
140 op.abort().then(resultHandler, rejectHandler); | |
141 }, | |
142 | |
143 function() | |
144 { | |
145 debug("finish() and then abort()"); | |
146 op = crypto.subtle.digest({name: 'sha-1'}); | |
147 op.finish().then(resultHandler, rejectHandler); | |
148 op.abort(); | |
149 }, | |
150 | |
151 function() | |
152 { | |
153 debug("finish() and then process()"); | |
154 op = crypto.subtle.digest({name: 'sha-1'}); | |
155 op.finish().then(resultHandler, rejectHandler); | |
156 op.process(new Uint8Array([0])); | |
157 }, | |
158 | |
159 function() | |
160 { | |
161 debug("finish() and then abort()"); | |
162 op = crypto.subtle.digest({name: 'sha-1'}); | |
163 op.finish().then(resultHandler, rejectHandler); | |
164 op.abort(); | |
165 }, | |
166 | |
167 function() | |
168 { | |
169 debug("finish() and then finish()"); | |
170 op = crypto.subtle.digest({name: 'sha-1'}); | |
171 op.finish().then(resultHandler, rejectHandler); | |
172 op.finish(); | |
173 }, | |
174 | |
175 function() | |
176 { | |
177 op = crypto.subtle.digest({name: 'sha-1'}); | |
178 shouldThrow("op.process(null)"); | |
179 shouldThrow("op.process()"); | |
180 shouldThrow("op.process(-1)"); | |
181 startNextTest(); | |
182 }, | |
183 ]; | |
184 | |
185 // Begin! | |
186 startNextTest(); | |
187 | |
188 </script> | |
189 | |
190 <script src="../fast/js/resources/js-test-post.js"></script> | |
191 </body> | |
192 </html> | |
OLD | NEW |