Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/crypt/hashtester.js
2868 views
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
/**
16
* @fileoverview Unit tests for the abstract cryptographic hash interface.
17
*
18
*/
19
20
goog.provide('goog.crypt.hashTester');
21
22
goog.require('goog.array');
23
goog.require('goog.crypt');
24
goog.require('goog.dom');
25
goog.require('goog.dom.TagName');
26
goog.require('goog.testing.PerformanceTable');
27
goog.require('goog.testing.PseudoRandom');
28
goog.require('goog.testing.asserts');
29
goog.setTestOnly('hashTester');
30
31
32
/**
33
* Runs basic tests.
34
*
35
* @param {!goog.crypt.Hash} hash A hash instance.
36
*/
37
goog.crypt.hashTester.runBasicTests = function(hash) {
38
// Compute first hash.
39
hash.update([97, 158]);
40
var golden1 = hash.digest();
41
42
// Compute second hash.
43
hash.reset();
44
hash.update('aB');
45
var golden2 = hash.digest();
46
assertTrue(
47
'Two different inputs resulted in a hash collision',
48
!!goog.testing.asserts.findDifferences(golden1, golden2));
49
50
// Empty hash.
51
hash.reset();
52
var empty = hash.digest();
53
assertTrue(
54
'Empty hash collided with a non-trivial one',
55
!!goog.testing.asserts.findDifferences(golden1, empty) &&
56
!!goog.testing.asserts.findDifferences(golden2, empty));
57
58
// Zero-length array update.
59
hash.reset();
60
hash.update([]);
61
assertArrayEquals(
62
'Updating with an empty array did not give an empty hash', empty,
63
hash.digest());
64
65
// Zero-length string update.
66
hash.reset();
67
hash.update('');
68
assertArrayEquals(
69
'Updating with an empty string did not give an empty hash', empty,
70
hash.digest());
71
72
// Recompute the first hash.
73
hash.reset();
74
hash.update([97, 158]);
75
assertArrayEquals(
76
'The reset did not produce the initial state', golden1, hash.digest());
77
78
// Check for a trivial collision.
79
hash.reset();
80
hash.update([158, 97]);
81
assertTrue(
82
'Swapping bytes resulted in a hash collision',
83
!!goog.testing.asserts.findDifferences(golden1, hash.digest()));
84
85
// Compare array and string input.
86
hash.reset();
87
hash.update([97, 66]);
88
assertArrayEquals(
89
'String and array inputs should give the same result', golden2,
90
hash.digest());
91
92
// Compute in parts.
93
hash.reset();
94
hash.update('a');
95
hash.update([158]);
96
assertArrayEquals(
97
'Partial updates resulted in a different hash', golden1, hash.digest());
98
99
// Test update with specified length.
100
hash.reset();
101
hash.update('aB', 0);
102
hash.update([97, 158, 32], 2);
103
assertArrayEquals(
104
'Updating with an explicit buffer length did not work', golden1,
105
hash.digest());
106
};
107
108
109
/**
110
* Runs block tests.
111
*
112
* @param {!goog.crypt.Hash} hash A hash instance.
113
* @param {number} blockBytes Size of the hash block.
114
*/
115
goog.crypt.hashTester.runBlockTests = function(hash, blockBytes) {
116
// Compute a message which is 1 byte shorter than hash block size.
117
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
118
var message = '';
119
for (var i = 0; i < blockBytes - 1; i++) {
120
message += chars.charAt(i % chars.length);
121
}
122
123
// Compute golden hash for 1 block + 2 bytes.
124
hash.update(message + '123');
125
var golden1 = hash.digest();
126
127
// Compute golden hash for 2 blocks + 1 byte.
128
hash.reset();
129
hash.update(message + message + '123');
130
var golden2 = hash.digest();
131
132
// Almost fill a block, then overflow.
133
hash.reset();
134
hash.update(message);
135
hash.update('123');
136
assertArrayEquals(golden1, hash.digest());
137
138
// Fill a block.
139
hash.reset();
140
hash.update(message + '1');
141
hash.update('23');
142
assertArrayEquals(golden1, hash.digest());
143
144
// Overflow a block.
145
hash.reset();
146
hash.update(message + '12');
147
hash.update('3');
148
assertArrayEquals(golden1, hash.digest());
149
150
// Test single overflow with an array.
151
hash.reset();
152
hash.update(goog.crypt.stringToByteArray(message + '123'));
153
assertArrayEquals(golden1, hash.digest());
154
155
// Almost fill a block, then overflow this and the next block.
156
hash.reset();
157
hash.update(message);
158
hash.update(message + '123');
159
assertArrayEquals(golden2, hash.digest());
160
161
// Fill two blocks.
162
hash.reset();
163
hash.update(message + message + '12');
164
hash.update('3');
165
assertArrayEquals(golden2, hash.digest());
166
167
// Test double overflow with an array.
168
hash.reset();
169
hash.update(goog.crypt.stringToByteArray(message));
170
hash.update(goog.crypt.stringToByteArray(message + '123'));
171
assertArrayEquals(golden2, hash.digest());
172
};
173
174
175
/**
176
* Runs performance tests.
177
*
178
* @param {function():!goog.crypt.Hash} hashFactory A hash factory.
179
* @param {string} hashName Name of the hashing function.
180
*/
181
goog.crypt.hashTester.runPerfTests = function(hashFactory, hashName) {
182
var body = goog.dom.getDocument().body;
183
var perfTable = goog.dom.createElement(goog.dom.TagName.DIV);
184
goog.dom.appendChild(body, perfTable);
185
186
var table = new goog.testing.PerformanceTable(perfTable);
187
188
function runPerfTest(byteLength, updateCount) {
189
var label =
190
(hashName + ': ' + updateCount + ' update(s) of ' + byteLength +
191
' bytes');
192
193
function run(data, dataType) {
194
table.run(function() {
195
var hash = hashFactory();
196
for (var i = 0; i < updateCount; i++) {
197
hash.update(data, byteLength);
198
}
199
var digest = hash.digest();
200
}, label + ' (' + dataType + ')');
201
}
202
203
var byteArray = goog.crypt.hashTester.createRandomByteArray_(byteLength);
204
var byteString = goog.crypt.hashTester.createByteString_(byteArray);
205
206
run(byteArray, 'byte array');
207
run(byteString, 'byte string');
208
}
209
210
var MESSAGE_LENGTH_LONG = 10000000; // 10 Mbytes
211
var MESSAGE_LENGTH_SHORT = 10; // 10 bytes
212
var MESSAGE_COUNT_SHORT = MESSAGE_LENGTH_LONG / MESSAGE_LENGTH_SHORT;
213
214
runPerfTest(MESSAGE_LENGTH_LONG, 1);
215
runPerfTest(MESSAGE_LENGTH_SHORT, MESSAGE_COUNT_SHORT);
216
};
217
218
219
/**
220
* Creates and returns a random byte array.
221
*
222
* @param {number} length Length of the byte array.
223
* @return {!Array<number>} An array of bytes.
224
* @private
225
*/
226
goog.crypt.hashTester.createRandomByteArray_ = function(length) {
227
var random = new goog.testing.PseudoRandom(0);
228
var bytes = [];
229
230
for (var i = 0; i < length; ++i) {
231
// Generates an integer from 0 to 255.
232
var b = Math.floor(random.random() * 0x100);
233
bytes.push(b);
234
}
235
236
return bytes;
237
};
238
239
240
/**
241
* Creates a string from an array of bytes.
242
*
243
* @param {!Array<number>} bytes An array of bytes.
244
* @return {string} The string encoded by the bytes.
245
* @private
246
*/
247
goog.crypt.hashTester.createByteString_ = function(bytes) {
248
var str = '';
249
goog.array.forEach(bytes, function(b) { str += String.fromCharCode(b); });
250
return str;
251
};
252
253