-
-
Notifications
You must be signed in to change notification settings - Fork 879
/
test.js
274 lines (229 loc) · 8.96 KB
/
test.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
var assert = require('assert');
var uuidv1 = require('../v1');
var uuidv3 = require('../v3');
var uuidv4 = require('../v4');
var uuidv5 = require('../v5');
var crypto = require('crypto');
// Verify ordering of v1 ids created with explicit times
var TIME = 1321644961388; // 2011-11-18 11:36:01.388-08:00
var HASH_SAMPLES = [
{
input: '',
sha1: 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
md5: 'd41d8cd98f00b204e9800998ecf8427e',
},
// Extended ascii chars
{
input: '\t\b\f !\"#$%&\'()*+,-.\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u00A1\u00A2\u00A3\u00A4\u00A5\u00A6\u00A7\u00A8\u00A9\u00AA\u00AB\u00AC\u00AE\u00AF\u00B0\u00B1\u00B2\u00B3\u00B4\u00B5\u00B6\u00B7\u00B8\u00B9\u00BA\u00BB\u00BC\u00BD\u00BE\u00BF\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5\u00C6\u00C7\u00C8\u00C9\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF\u00D0\u00D1\u00D2\u00D3\u00D4\u00D5\u00D6\u00D7\u00D8\u00D9\u00DA\u00DB\u00DC\u00DD\u00DE\u00DF\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5\u00E6\u00E7\u00E8\u00E9\u00EA\u00EB\u00EC\u00ED\u00EE\u00EF\u00F0\u00F1\u00F2\u00F3\u00F4\u00F5\u00F6\u00F7\u00F8\u00F9\u00FA\u00FB\u00FC\u00FD\u00FE\u00FF',
sha1: 'ca4a426a3d536f14cfd79011e79e10d64de950a0',
md5: 'e8098ec21950f841731d28749129d3ee',
},
// A sampling from the Unicode BMP
{
input: '\u00A5\u0104\u018F\u0256\u02B1o\u0315\u038E\u0409\u0500\u0531\u05E1\u05B6\u0920\u0903\u09A4\u0983\u0A20\u0A02\u0AA0\u0A83\u0B06\u0C05\u0C03\u1401\u16A0',
sha1: 'f2753ebc390e5f637e333c2a4179644a93ae9f65',
md5: '231b309e277b6be8bb3d6c688b7f098b',
}
];
function hashToHex(hash) {
return hash.map(function(b) {
return (0x100 + b).toString(16).slice(-2);
}).join('');
}
function compare(name, ids) {
test(name, function() {
// avoid .map for older browsers
for (var i=0 ; i<ids.length ; ++i) {
ids[i] = ids[i].split('-').reverse().join('-');
}
ids = ids.sort();
var sorted = ([].concat(ids)).sort();
assert(sorted.toString() == ids.toString(), name + ' have expected order');
});
}
test('nodeRNG', function() {
var rng = require('../lib/rng');
assert.equal(rng.name, 'nodeRNG');
assert(rng.allowDubiousRNG, 'Supports dubious RNG API');
var bytes = rng();
assert.equal(bytes.length, 16);
for (var i = 0; i < bytes.length; i++) {
assert.equal(typeof(bytes[i]), 'number');
}
});
test('mathRNG', function() {
var rng = require('../lib/rng-browser');
assert.equal(rng.name, 'mathRNG');
assert(rng.allowDubiousRNG, 'Supports dubious RNG API');
assert.throws(rng);
rng.allowDubiousRNG(true);
assert.doesNotThrow(rng);;
var bytes = rng();
assert.equal(bytes.length, 16);
for (var i = 0; i < bytes.length; i++) {
assert.equal(typeof(bytes[i]), 'number');
}
});
test('cryptoRNG', function() {
var randomFillSync = crypto.randomFillSync;
// Purge require cache
Object.keys(require.cache).forEach(function(path) {
if (/rng-browser/.test(path)) delete require.cache[path];
});
// We shim the web crypto API to trigger cryptoRNG code path in rng module,
// then unshim once we've required it
global.crypto = {
getRandomValues: function(arr) {
return randomFillSync(arr);
}
};
var rng = require('../lib/rng-browser');
delete global.crypto;
assert(rng.allowDubiousRNG, 'Supports dubious RNG API');
assert.equal(rng.name, 'whatwgRNG');
var bytes = rng();
assert.equal(bytes.length, 16);
for (var i = 0; i < bytes.length; i++) {
assert.equal(typeof(bytes[i]), 'number');
}
});
test('sha1 node', function() {
var sha1 = require('../lib/sha1');
HASH_SAMPLES.forEach(function(sample) {
// Convert the sha1 Buffer to an Array here so we can call map() on it in hashToHex
assert.equal(hashToHex(Array.prototype.slice.apply(sha1(sample.input))), sample.sha1);
});
});
test('sha1 browser', function() {
var sha1 = require('../lib/sha1-browser');
HASH_SAMPLES.forEach(function(sample) {
assert.equal(hashToHex(sha1(sample.input)), sample.sha1);
});
});
test('md5 node', function() {
var md5 = require('../lib/md5');
HASH_SAMPLES.forEach(function(sample) {
// Convert the sha1 Buffer to an Array here so we can call map() on it in hashToHex
assert.equal(hashToHex(Array.prototype.slice.apply(md5(sample.input))), sample.md5);
});
});
test('md5 browser', function() {
var md5 = require('../lib/md5-browser');
HASH_SAMPLES.forEach(function(sample) {
assert.equal(hashToHex(md5(sample.input)), sample.md5);
});
});
test('v3', function() {
// Expect to get the same results as http://tools.adjet.org/uuid-v3
assert.equal(uuidv3('hello.example.com', uuidv3.DNS), '9125a8dc-52ee-365b-a5aa-81b0b3681cf6');
assert.equal(uuidv3('http://example.com/hello', uuidv3.URL), 'c6235813-3ba4-3801-ae84-e0a6ebb7d138');
assert.equal(uuidv3('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'), 'a981a0c2-68b1-35dc-bcfc-296e52ab01ec');
// test the buffer functionality
var buf = new Array(16);
var testBuf = [0x91, 0x25, 0xa8, 0xdc, 0x52, 0xee, 0x36, 0x5b, 0xa5, 0xaa, 0x81, 0xb0, 0xb3, 0x68, 0x1c, 0xf6];
uuidv3('hello.example.com', uuidv3.DNS, buf);
assert.ok(buf.length === testBuf.length && buf.every(function(elem, idx) {
return elem === testBuf[idx];
}));
// test offsets as well
buf = new Array(19);
for (var i=0; i<3; ++i) buf[i] = 'landmaster';
uuidv3('hello.example.com', uuidv3.DNS, buf, 3);
assert.ok(buf.length === testBuf.length+3 && buf.every(function (elem, idx) {
return (idx >= 3) ? (elem === testBuf[idx-3]) : (elem === 'landmaster');
}), "hello");
});
test('v5', function() {
// Expect to get the same results as http://tools.adjet.org/uuid-v5
assert.equal(uuidv5('hello.example.com', uuidv5.DNS), 'fdda765f-fc57-5604-a269-52a7df8164ec');
assert.equal(uuidv5('http://example.com/hello', uuidv5.URL), '3bbcee75-cecc-5b56-8031-b6641c1ed1f1');
assert.equal(uuidv5('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'), '90123e1c-7512-523e-bb28-76fab9f2f73d');
// test the buffer functionality
var buf = new Array(16);
var testBuf = [0xfd, 0xda, 0x76, 0x5f, 0xfc, 0x57, 0x56, 0x04, 0xa2, 0x69, 0x52, 0xa7, 0xdf, 0x81, 0x64, 0xec];
uuidv5('hello.example.com', uuidv5.DNS, buf);
assert.ok(buf.length === testBuf.length && buf.every(function(elem, idx) {
return elem === testBuf[idx];
}));
// test offsets as well
buf = new Array(19);
for (var i=0; i<3; ++i) buf[i] = 'landmaster';
uuidv5('hello.example.com', uuidv5.DNS, buf, 3);
assert.ok(buf.length === testBuf.length+3 && buf.every(function(elem, idx) {
return (idx >= 3) ? (elem === testBuf[idx-3]) : (elem === 'landmaster');
}));
});
// Verify ordering of v1 ids created using default behavior
compare('uuids with current time', [
uuidv1(),
uuidv1(),
uuidv1(),
uuidv1(),
uuidv1()
]);
// Verify ordering of v1 ids created with explicit times
compare('uuids with time option', [
uuidv1({msecs: TIME - 10*3600*1000}),
uuidv1({msecs: TIME - 1}),
uuidv1({msecs: TIME}),
uuidv1({msecs: TIME + 1}),
uuidv1({msecs: TIME + 28*24*3600*1000})
]);
test('msec', function() {
assert(
uuidv1({msecs: TIME}) !== uuidv1({msecs: TIME}),
'IDs created at same msec are different'
);
});
test('exception thrown when > 10k ids created in 1ms', function() {
// Verify throw if too many ids created
var thrown = false;
try {
uuidv1({msecs: TIME, nsecs: 10000});
} catch (e) {
thrown = true;
}
assert(thrown, 'Exception thrown when > 10K ids created in 1 ms');
});
test('clock regression by msec', function() {
// Verify clock regression bumps clockseq
var uidt = uuidv1({msecs: TIME});
var uidtb = uuidv1({msecs: TIME - 1});
assert(
parseInt(uidtb.split('-')[3], 16) - parseInt(uidt.split('-')[3], 16) === 1,
'Clock regression by msec increments the clockseq'
);
});
test('clock regression by nsec', function() {
// Verify clock regression bumps clockseq
var uidtn = uuidv1({msecs: TIME, nsecs: 10});
var uidtnb = uuidv1({msecs: TIME, nsecs: 9});
assert(
parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1,
'Clock regression by nsec increments the clockseq'
);
});
test('explicit options product expected id', function() {
// Verify explicit options produce expected id
var id = uuidv1({
msecs: 1321651533573,
nsecs: 5432,
clockseq: 0x385c,
node: [0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10]
});
assert(id == 'd9428888-122b-11e1-b85c-61cd3cbb3210', 'Explicit options produce expected id');
});
test('ids spanning 1ms boundary are 100ns apart', function() {
// Verify adjacent ids across a msec boundary are 1 time unit apart
var u0 = uuidv1({msecs: TIME, nsecs: 9999});
var u1 = uuidv1({msecs: TIME + 1, nsecs: 0});
var before = u0.split('-')[0], after = u1.split('-')[0];
var dt = parseInt(after, 16) - parseInt(before, 16);
assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart');
});
test('legacy API', function() {
var uuid = require('..');
assert.equal(uuid.v1, uuidv1, 'uuid.v1 = uuid/v1');
assert.equal(uuid.v4, uuidv4, 'uuid.v4 = uuid/v4');
assert.equal(uuid, uuidv4, 'uuid = uuid/v4');
});