zhangnaisong
2024-03-23 4532b321444257453a86c0f5289a3a5f576db71e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict';
 
var assert = require('assert');
var crypto = require('crypto');
var hash = require('../index');
var validSha1 = /^[0-9a-f]{40}$/i;
 
describe('hash()ing different types', function() {
  it('hashes non-object types', function() {
    var func = function(a){ return a + 1; };
    assert.ok(validSha1.test(hash('Shazbot!')), 'hash string');
    assert.ok(validSha1.test(hash(42)), 'hash number');
    assert.ok(validSha1.test(hash(NaN)), 'hash bool');
    assert.ok(validSha1.test(hash(true)), 'hash bool');
    assert.ok(validSha1.test(hash(func)), 'hash function');
  });
 
  it('hashes special object types', function() {
    var dt = new Date();
    dt.setDate(dt.getDate() + 1);
 
    assert.ok(validSha1.test(hash([1,2,3,4])), 'hash array');
    assert.notEqual(hash([1,2,3,4]), hash([4,3,2,1]), 'different arrays not equal');
    assert.ok(validSha1.test(hash(new Date())), 'hash date');
    assert.notEqual(hash(new Date()), hash(dt), 'different dates not equal');
    assert.ok(validSha1.test(hash(null)), 'hash Null');
    assert.ok(validSha1.test(hash(Number.NaN)), 'hash NaN');
    assert.ok(validSha1.test(hash({ foo: undefined })), 'hash Undefined value');
    assert.ok(validSha1.test(hash(new RegExp())), 'hash regex');
    assert.ok(validSha1.test(hash(new Error())), 'hash error');
  });
 
  it('hashes node.js-internal object types', function() {
    if (typeof process !== 'undefined') {
      assert.ok(validSha1.test(hash(process)), 'hash process');
    }
    
    var timer = setTimeout(function() {}, 0);
    assert.ok(validSha1.test(hash(timer)), 'hash timer');
  });
 
  if (typeof Symbol !== 'undefined') {
    it('hashes Symbols', function() {
      assert.ok(validSha1.test(hash(Symbol('Banana'))), 'hash error');
    });
  }
 
  if (typeof Buffer !== 'undefined') {
    it("Buffers can be hashed", function() {
      assert.ok(validSha1.test(hash(new Buffer('Banana'))), 'hashes Buffers');
    });
  }
 
  if (typeof Uint8Array !== 'undefined') {
    it("Typed arrays can be hashed", function() {
      
      assert.ok(validSha1.test(hash(new Uint8Array([1,2,3,4]))), 'hashes Uint8Array');
      assert.ok(validSha1.test(hash(new  Int8Array([1,2,3,4]))), 'hashes  Int8Array');
      assert.ok(validSha1.test(hash(new Uint16Array([1,2,3,4]))), 'hashes Uint16Array');
      assert.ok(validSha1.test(hash(new  Int16Array([1,2,3,4]))), 'hashes  Int16Array');
      assert.ok(validSha1.test(hash(new Uint32Array([1,2,3,4]))), 'hashes Uint32Array');
      assert.ok(validSha1.test(hash(new  Int32Array([1,2,3,4]))), 'hashes  Int32Array');
      assert.ok(validSha1.test(hash(new Float32Array([1,2,3,4]))), 'hashes Float32Array');
      if (typeof Float64Array !== 'undefined')
      assert.ok(validSha1.test(hash(new Float64Array([1,2,3,4]))), 'hashes Float64Array');
      if (typeof Uint8ClampedArray !== 'undefined')
      assert.ok(validSha1.test(hash(new Uint8ClampedArray([1,2,3,4]))), 'hashes Uint8ClampedArray');
      assert.ok(validSha1.test(hash(new Uint8Array([1,2,3,4]).buffer)), 'hashes ArrayBuffer');
    });
  }
 
  if (typeof Map !== 'undefined') {
    it("Maps can be hashed", function() {
      var map = new Map([['a',1],['b',2]]);
      assert.ok(validSha1.test(hash(map)), 'hashes Maps');
    });
  }
 
  if (typeof WeakMap !== 'undefined') {
    it("WeakMaps can’t be hashed", function() {
      var map = new WeakMap([[{foo: 'bar'},1]]);
      assert.throws(function() {
        validSha1.test(hash(map))
      }, 'does not hash WeakMaps');
    });
  }
 
  if (typeof Set !== 'undefined') {
    it("Sets can be hashed", function() {
      var set = new Set(['you', 'du', 'tu', 'あなた', '您']);
      assert.ok(validSha1.test(hash(set)), 'hashes Sets');
    });
  }
 
  if (typeof WeakSet !== 'undefined') {
    it("WeakSets can’t be hashed", function() {
      var obj = {foo: 'bar'};
      var set = new WeakSet([obj]);
      assert.throws(function() {
        validSha1.test(hash(set))
      }, 'does not hash WeakSets');
    });
  }
 
  it("Builtin types themselves can be hashed", function() {
    var hashcount = {};
    var types = [Object, Date, Number, String, Function, RegExp,
      Error, 0, null, NaN];
    if (typeof WeakSet !== 'undefined') types.push(WeakSet);
    if (typeof Set !== 'undefined') types.push(Set);
    if (typeof WeakMap !== 'undefined') types.push(WeakMap);
    if (typeof Map !== 'undefined') types.push(Map);
    if (typeof Symbol !== 'undefined') types.push(Symbol);
    if (typeof Uint8Array !== 'undefined') types.push(Uint8Array);
 
    // Hash each type
    for (var idx in types) {
      var h = hash(types[idx]);
      assert.ok(validSha1.test(h));
      hashcount[h] = (hashcount[h] || 0) + 1;
    }
 
    // Check for collisions
    var no = 0;
    for (var h in hashcount) {
      assert.equal(hashcount[h], 1);
      no++;
    }
 
    // Self check; did we really hash all the types?
    assert.equal(no, types.length);
  });
  
  it("Builtin types might result in identical hashes with respectFunctionNames = false", function() {
    var hashcount = {};
    var types = [Object, Date, Number, String, Function, RegExp,
      Error, 0, null, NaN];
    if (typeof WeakSet !== 'undefined') types.push(WeakSet);
    if (typeof Set !== 'undefined') types.push(Set);
    if (typeof WeakMap !== 'undefined') types.push(WeakMap);
    if (typeof Map !== 'undefined') types.push(Map);
    if (typeof Symbol !== 'undefined') types.push(Symbol);
    if (typeof Uint8Array !== 'undefined') types.push(Uint8Array);
 
    // Hash each type
    for (var idx in types) {
      var h = hash(types[idx], { respectFunctionNames: false });
      assert.ok(validSha1.test(h));
      hashcount[h] = (hashcount[h] || 0) + 1;
    }
 
    // Check for collisions
    var no = 0;
    for (var h in hashcount) {
      assert.ok(hashcount[h] >= 1);
      no += hashcount[h];
    }
 
    // Self check; did we really hash all the types?
    assert.equal(no, types.length);
  });
  
  it("Functions with identical bodies and different names result in identical hashes with respectFunctionNames = false", function() {
    var fn1 = function a() {};
    var fn2 = function b() {};
    var toStringDummy = function() { return '...'; };
    fn1.toString = toStringDummy;
    fn2.toString = toStringDummy;
    
    var h1 = hash(fn1, { respectFunctionNames: false });
    var h2 = hash(fn2, { respectFunctionNames: false });
    assert.strictEqual(h1, h2);
  });
});