Debian Codegraph @

closure-library/closure/goog/crypt/blobhasher_test.js

raw
1 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.crypt.BlobHasherTest');
16 goog.setTestOnly('goog.crypt.BlobHasherTest');
17
18 goog.require('goog.crypt');
19 goog.require('goog.crypt.BlobHasher');
20 goog.require('goog.crypt.Md5');
21 goog.require('goog.events');
22 goog.require('goog.testing.PropertyReplacer');
23 goog.require('goog.testing.jsunit');
24
25 // A browser-independent mock of goog.fs.sliceBlob. The actual implementation
26 // calls the underlying slice method differently based on browser version.
27 // This mock does not support negative opt_end.
28 var fsSliceBlobMock = function(blob, start, opt_end) {
29 if (!goog.isNumber(opt_end)) {
30 opt_end = blob.size;
31 }
32 return blob.slice(start, opt_end);
33 };
34
35 // Mock out the Blob using a string.
36 BlobMock = function(string) {
37 this.data = string;
38 this.size = this.data.length;
39 };
40
41 BlobMock.prototype.slice = function(start, end) {
42 return new BlobMock(this.data.substr(start, end - start));
43 };
44
45
46 // Mock out the FileReader to have control over the flow.
47 FileReaderMock = function() {
48 this.array_ = [];
49 this.result = null;
50 this.readyState = this.EMPTY;
51
52 this.onload = null;
53 this.onabort = null;
54 this.onerror = null;
55 };
56
57 FileReaderMock.prototype.EMPTY = 0;
58 FileReaderMock.prototype.LOADING = 1;
59 FileReaderMock.prototype.DONE = 2;
60
61 FileReaderMock.prototype.mockLoad = function() {
62 this.readyState = this.DONE;
63 this.result = this.array_;
64 if (this.onload) {
65 this.onload.call();
66 }
67 };
68
69 FileReaderMock.prototype.abort = function() {
70 this.readyState = this.DONE;
71 if (this.onabort) {
72 this.onabort.call();
73 }
74 };
75
76 FileReaderMock.prototype.mockError = function() {
77 this.readyState = this.DONE;
78 if (this.onerror) {
79 this.onerror.call();
80 }
81 };
82
83 FileReaderMock.prototype.readAsArrayBuffer = function(blobMock) {
84 this.readyState = this.LOADING;
85 this.array_ = [];
86 for (var i = 0; i < blobMock.size; ++i) {
87 this.array_[i] = blobMock.data.charCodeAt(i);
88 }
89 };
90
91 FileReaderMock.prototype.isLoading = function() {
92 return this.readyState == this.LOADING;
93 };
94
95 var stubs = new goog.testing.PropertyReplacer();
96 function setUp() {
97 stubs.set(goog.global, 'FileReader', FileReaderMock);
98 stubs.set(goog.fs, 'sliceBlob', fsSliceBlobMock);
99 }
100
101 function tearDown() {
102 stubs.reset();
103 }
104
105
106 /**
107 * Makes the blobHasher read chunks from the blob and hash it. The number of
108 * reads shall not exceed a pre-determined number (typically blob size / chunk
109 * size) for computing hash. This function fails fast (after maxReads is
110 * reached), assuming that the hasher failed to generate hashes. This prevents
111 * the test suite from going into infinite loop.
112 * @param {!goog.crypt.BlobHasher} blobHasher Hasher in action.
113 * @param {number} maxReads Max number of read attempts.
114 */
115 function readFromBlob(blobHasher, maxReads) {
116 var counter = 0;
117 while (blobHasher.fileReader_ && blobHasher.fileReader_.isLoading() &&
118 counter <= maxReads) {
119 blobHasher.fileReader_.mockLoad();
120 counter++;
121 }
122 assertTrue(counter <= maxReads);
123 return counter;
124 }
125
126 function testBasicOperations() {
127 if (!window.Blob) {
128 return;
129 }
130
131 // Test hashing with one chunk.
132 var hashFn = new goog.crypt.Md5();
133 var blobHasher = new goog.crypt.BlobHasher(hashFn);
134 var blob = new BlobMock('The quick brown fox jumps over the lazy dog');
135 blobHasher.hash(blob);
136 readFromBlob(blobHasher, 1);
137 assertEquals(
138 '9e107d9d372bb6826bd81d3542a419d6',
139 goog.crypt.byteArrayToHex(blobHasher.getHash()));
140
141 // Test hashing with multiple chunks.
142 blobHasher = new goog.crypt.BlobHasher(hashFn, 7);
143 blobHasher.hash(blob);
144 readFromBlob(blobHasher, Math.ceil(blob.size / 7));
145 assertEquals(
146 '9e107d9d372bb6826bd81d3542a419d6',
147 goog.crypt.byteArrayToHex(blobHasher.getHash()));
148
149 // Test hashing with no chunks.
150 blob = new BlobMock('');
151 blobHasher.hash(blob);
152 readFromBlob(blobHasher, 1);
153 assertEquals(
154 'd41d8cd98f00b204e9800998ecf8427e',
155 goog.crypt.byteArrayToHex(blobHasher.getHash()));
156 }
157
158 function testNormalFlow() {
159 if (!window.Blob) {
160 return;
161 }
162
163 // Test the flow with one chunk.
164 var hashFn = new goog.crypt.Md5();
165 var blobHasher = new goog.crypt.BlobHasher(hashFn, 13);
166 var blob = new BlobMock('short');
167 var startedEvents = 0;
168 var progressEvents = 0;
169 var completeEvents = 0;
170 goog.events.listen(
171 blobHasher, goog.crypt.BlobHasher.EventType.STARTED,
172 function() { ++startedEvents; });
173 goog.events.listen(
174 blobHasher, goog.crypt.BlobHasher.EventType.PROGRESS,
175 function() { ++progressEvents; });
176 goog.events.listen(
177 blobHasher, goog.crypt.BlobHasher.EventType.COMPLETE,
178 function() { ++completeEvents; });
179 blobHasher.hash(blob);
180 assertEquals(1, startedEvents);
181 assertEquals(0, progressEvents);
182 assertEquals(0, completeEvents);
183 readFromBlob(blobHasher, 1);
184 assertEquals(1, startedEvents);
185 assertEquals(1, progressEvents);
186 assertEquals(1, completeEvents);
187
188 // Test the flow with multiple chunks.
189 blob = new BlobMock('The quick brown fox jumps over the lazy dog');
190 startedEvents = 0;
191 progressEvents = 0;
192 completeEvents = 0;
193 var progressLoops = 0;
194 blobHasher.hash(blob);
195 assertEquals(1, startedEvents);
196 assertEquals(0, progressEvents);
197 assertEquals(0, completeEvents);
198 progressLoops = readFromBlob(blobHasher, Math.ceil(blob.size / 13));
199 assertEquals(1, startedEvents);
200 assertEquals(progressLoops, progressEvents);
201 assertEquals(1, completeEvents);
202 }
203
204 function testAbortsAndErrors() {
205 if (!window.Blob) {
206 return;
207 }
208
209 var hashFn = new goog.crypt.Md5();
210 var blobHasher = new goog.crypt.BlobHasher(hashFn, 13);
211 var blob = new BlobMock('The quick brown fox jumps over the lazy dog');
212 var abortEvents = 0;
213 var errorEvents = 0;
214 var completeEvents = 0;
215 goog.events.listen(
216 blobHasher, goog.crypt.BlobHasher.EventType.ABORT,
217 function() { ++abortEvents; });
218 goog.events.listen(
219 blobHasher, goog.crypt.BlobHasher.EventType.ERROR,
220 function() { ++errorEvents; });
221 goog.events.listen(
222 blobHasher, goog.crypt.BlobHasher.EventType.COMPLETE,
223 function() { ++completeEvents; });
224
225 // Immediate abort.
226 blobHasher.hash(blob);
227 assertEquals(0, abortEvents);
228 assertEquals(0, errorEvents);
229 assertEquals(0, completeEvents);
230 blobHasher.abort();
231 blobHasher.abort();
232 assertEquals(1, abortEvents);
233 assertEquals(0, errorEvents);
234 assertEquals(0, completeEvents);
235 abortEvents = 0;
236
237 // Delayed abort.
238 blobHasher.hash(blob);
239 blobHasher.fileReader_.mockLoad();
240 assertEquals(0, abortEvents);
241 assertEquals(0, errorEvents);
242 assertEquals(0, completeEvents);
243 blobHasher.abort();
244 blobHasher.abort();
245 assertEquals(1, abortEvents);
246 assertEquals(0, errorEvents);
247 assertEquals(0, completeEvents);
248 abortEvents = 0;
249
250 // Immediate error.
251 blobHasher.hash(blob);
252 blobHasher.fileReader_.mockError();
253 assertEquals(0, abortEvents);
254 assertEquals(1, errorEvents);
255 assertEquals(0, completeEvents);
256 errorEvents = 0;
257
258 // Delayed error.
259 blobHasher.hash(blob);
260 blobHasher.fileReader_.mockLoad();
261 blobHasher.fileReader_.mockError();
262 assertEquals(0, abortEvents);
263 assertEquals(1, errorEvents);
264 assertEquals(0, completeEvents);
265 abortEvents = 0;
266 }
267
268 function testBasicThrottling() {
269 if (!window.Blob) {
270 return;
271 }
272
273 var hashFn = new goog.crypt.Md5();
274 var blobHasher = new goog.crypt.BlobHasher(hashFn, 5);
275 var blob = new BlobMock('The quick brown fox jumps over the lazy dog');
276 var throttledEvents = 0;
277 var completeEvents = 0;
278 goog.events.listen(
279 blobHasher, goog.crypt.BlobHasher.EventType.THROTTLED,
280 function() { ++throttledEvents; });
281 goog.events.listen(
282 blobHasher, goog.crypt.BlobHasher.EventType.COMPLETE,
283 function() { ++completeEvents; });
284
285 // Start a throttled hash. No chunks should be processed yet.
286 blobHasher.setHashingLimit(0);
287 assertEquals(0, throttledEvents);
288 blobHasher.hash(blob);
289 assertEquals(1, throttledEvents);
290 assertEquals(0, blobHasher.getBytesProcessed());
291 assertNull(blobHasher.fileReader_);
292
293 // One chunk should be processed.
294 blobHasher.setHashingLimit(4);
295 assertEquals(1, throttledEvents);
296 assertEquals(1, readFromBlob(blobHasher, 1));
297 assertEquals(2, throttledEvents);
298 assertEquals(4, blobHasher.getBytesProcessed());
299
300 // One more chunk should be processed.
301 blobHasher.setHashingLimit(5);
302 assertEquals(2, throttledEvents);
303 assertEquals(1, readFromBlob(blobHasher, 1));
304 assertEquals(3, throttledEvents);
305 assertEquals(5, blobHasher.getBytesProcessed());
306
307 // Two more chunks should be processed.
308 blobHasher.setHashingLimit(15);
309 assertEquals(3, throttledEvents);
310 assertEquals(2, readFromBlob(blobHasher, 2));
311 assertEquals(4, throttledEvents);
312 assertEquals(15, blobHasher.getBytesProcessed());
313
314 // The entire blob should be processed.
315 blobHasher.setHashingLimit(Infinity);
316 var expectedChunks = Math.ceil(blob.size / 5) - 3;
317 assertEquals(expectedChunks, readFromBlob(blobHasher, expectedChunks));
318 assertEquals(4, throttledEvents);
319 assertEquals(1, completeEvents);
320 assertEquals(
321 '9e107d9d372bb6826bd81d3542a419d6',
322 goog.crypt.byteArrayToHex(blobHasher.getHash()));
323 }
324
325 function testLengthZeroThrottling() {
326 if (!window.Blob) {
327 return;
328 }
329
330 var hashFn = new goog.crypt.Md5();
331 var blobHasher = new goog.crypt.BlobHasher(hashFn);
332 var throttledEvents = 0;
333 var completeEvents = 0;
334 goog.events.listen(
335 blobHasher, goog.crypt.BlobHasher.EventType.THROTTLED,
336 function() { ++throttledEvents; });
337 goog.events.listen(
338 blobHasher, goog.crypt.BlobHasher.EventType.COMPLETE,
339 function() { ++completeEvents; });
340
341 // Test throttling with length 0 blob.
342 var blob = new BlobMock('');
343 blobHasher.setHashingLimit(0);
344 blobHasher.hash(blob);
345 assertEquals(0, throttledEvents);
346 assertEquals(1, completeEvents);
347 assertEquals(
348 'd41d8cd98f00b204e9800998ecf8427e',
349 goog.crypt.byteArrayToHex(blobHasher.getHash()));
350 }
351
352 function testAbortsAndErrorsWhileThrottling() {
353 if (!window.Blob) {
354 return;
355 }
356
357 var hashFn = new goog.crypt.Md5();
358 var blobHasher = new goog.crypt.BlobHasher(hashFn, 5);
359 var blob = new BlobMock('The quick brown fox jumps over the lazy dog');
360 var abortEvents = 0;
361 var errorEvents = 0;
362 var throttledEvents = 0;
363 var completeEvents = 0;
364 goog.events.listen(
365 blobHasher, goog.crypt.BlobHasher.EventType.ABORT,
366 function() { ++abortEvents; });
367 goog.events.listen(
368 blobHasher, goog.crypt.BlobHasher.EventType.ERROR,
369 function() { ++errorEvents; });
370 goog.events.listen(
371 blobHasher, goog.crypt.BlobHasher.EventType.THROTTLED,
372 function() { ++throttledEvents; });
373 goog.events.listen(
374 blobHasher, goog.crypt.BlobHasher.EventType.COMPLETE,
375 function() { ++completeEvents; });
376
377 // Test that processing cannot be continued after abort.
378 blobHasher.setHashingLimit(0);
379 blobHasher.hash(blob);
380 assertEquals(1, throttledEvents);
381 blobHasher.abort();
382 assertEquals(1, abortEvents);
383 blobHasher.setHashingLimit(10);
384 assertNull(blobHasher.fileReader_);
385 assertEquals(1, throttledEvents);
386 assertEquals(0, completeEvents);
387 assertNull(blobHasher.getHash());
388
389 // Test that processing cannot be continued after error.
390 blobHasher.hash(blob);
391 assertEquals(1, throttledEvents);
392 blobHasher.fileReader_.mockError();
393 assertEquals(1, errorEvents);
394 blobHasher.setHashingLimit(100);
395 assertNull(blobHasher.fileReader_);
396 assertEquals(1, throttledEvents);
397 assertEquals(0, completeEvents);
398 assertNull(blobHasher.getHash());
399 }