Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/browser/hash.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { hash, hashAsync, StringSHA1 } from '../../common/hash.js';
8
import { ensureNoDisposablesAreLeakedInTestSuite } from '../common/utils.js';
9
10
suite('Hash', () => {
11
test('string', () => {
12
assert.strictEqual(hash('hello'), hash('hello'));
13
assert.notStrictEqual(hash('hello'), hash('world'));
14
assert.notStrictEqual(hash('hello'), hash('olleh'));
15
assert.notStrictEqual(hash('hello'), hash('Hello'));
16
assert.notStrictEqual(hash('hello'), hash('Hello '));
17
assert.notStrictEqual(hash('h'), hash('H'));
18
assert.notStrictEqual(hash('-'), hash('_'));
19
});
20
21
test('number', () => {
22
assert.strictEqual(hash(1), hash(1));
23
assert.notStrictEqual(hash(0), hash(1));
24
assert.notStrictEqual(hash(1), hash(-1));
25
assert.notStrictEqual(hash(0x12345678), hash(0x123456789));
26
});
27
28
test('boolean', () => {
29
assert.strictEqual(hash(true), hash(true));
30
assert.notStrictEqual(hash(true), hash(false));
31
});
32
33
test('array', () => {
34
assert.strictEqual(hash([1, 2, 3]), hash([1, 2, 3]));
35
assert.strictEqual(hash(['foo', 'bar']), hash(['foo', 'bar']));
36
assert.strictEqual(hash([]), hash([]));
37
assert.strictEqual(hash([]), hash(new Array()));
38
assert.notStrictEqual(hash(['foo', 'bar']), hash(['bar', 'foo']));
39
assert.notStrictEqual(hash(['foo', 'bar']), hash(['bar', 'foo', null]));
40
assert.notStrictEqual(hash(['foo', 'bar', null]), hash(['bar', 'foo', null]));
41
assert.notStrictEqual(hash(['foo', 'bar']), hash(['bar', 'foo', undefined]));
42
assert.notStrictEqual(hash(['foo', 'bar', undefined]), hash(['bar', 'foo', undefined]));
43
assert.notStrictEqual(hash(['foo', 'bar', null]), hash(['foo', 'bar', undefined]));
44
});
45
46
test('object', () => {
47
assert.strictEqual(hash({}), hash({}));
48
assert.strictEqual(hash({}), hash(Object.create(null)));
49
assert.strictEqual(hash({ 'foo': 'bar' }), hash({ 'foo': 'bar' }));
50
assert.strictEqual(hash({ 'foo': 'bar', 'foo2': undefined }), hash({ 'foo2': undefined, 'foo': 'bar' }));
51
assert.notStrictEqual(hash({ 'foo': 'bar' }), hash({ 'foo': 'bar2' }));
52
assert.notStrictEqual(hash({}), hash([]));
53
});
54
55
test('array - unexpected collision', function () {
56
const a = hash([undefined, undefined, undefined, undefined, undefined]);
57
const b = hash([undefined, undefined, 'HHHHHH', [{ line: 0, character: 0 }, { line: 0, character: 0 }], undefined]);
58
assert.notStrictEqual(a, b);
59
});
60
61
test('all different', () => {
62
const candidates: any[] = [
63
null, undefined, {}, [], 0, false, true, '', ' ', [null], [undefined], [undefined, undefined], { '': undefined }, { [' ']: undefined },
64
'ab', 'ba', ['ab']
65
];
66
const hashes: number[] = candidates.map(hash);
67
for (let i = 0; i < hashes.length; i++) {
68
assert.strictEqual(hashes[i], hash(candidates[i])); // verify that repeated invocation returns the same hash
69
for (let k = i + 1; k < hashes.length; k++) {
70
assert.notStrictEqual(hashes[i], hashes[k], `Same hash ${hashes[i]} for ${JSON.stringify(candidates[i])} and ${JSON.stringify(candidates[k])}`);
71
}
72
}
73
});
74
75
76
async function checkSHA1(str: string, expected: string) {
77
78
// Test with StringSHA1
79
const hash = new StringSHA1();
80
hash.update(str);
81
let actual = hash.digest();
82
assert.strictEqual(actual, expected);
83
84
// Test with crypto.subtle
85
actual = await hashAsync(str);
86
assert.strictEqual(actual, expected);
87
}
88
89
test('sha1-1', () => {
90
return checkSHA1('\udd56', '9bdb77276c1852e1fb067820472812fcf6084024');
91
});
92
93
test('sha1-2', () => {
94
return checkSHA1('\udb52', '9bdb77276c1852e1fb067820472812fcf6084024');
95
});
96
97
test('sha1-3', () => {
98
return checkSHA1('\uda02ꑍ', '9b483a471f22fe7e09d83f221871a987244bbd3f');
99
});
100
101
test('sha1-4', () => {
102
return checkSHA1('hello', 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d');
103
});
104
105
ensureNoDisposablesAreLeakedInTestSuite();
106
});
107
108