forked from daegalus/dart-uuid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid_test.dart
576 lines (483 loc) · 17.2 KB
/
uuid_test.dart
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// ignore_for_file: deprecated_member_use_from_same_package
// TODO: Remove this ignore when we remove the deprecated options.
import 'dart:typed_data';
import 'package:test/test.dart';
import 'package:uuid/data.dart';
import 'package:uuid/uuid.dart';
import 'package:uuid/rng.dart';
void main() {
var uuid = const Uuid();
const testTime = 1321644961388;
group('[Version 1 Tests]', () {
test('IDs created at same mSec are different', () {
expect(uuid.v1(options: {'mSecs': testTime}),
isNot(equals(uuid.v1(options: {'mSecs': testTime}))));
});
test('Exception thrown when > 10K ids created in 1 ms', () {
var thrown = false;
try {
uuid.v1(options: {'mSecs': testTime, 'nSecs': 10000});
} catch (e) {
thrown = true;
}
expect(thrown, equals(true));
});
test('Clock regression by msec increments the clockseq - mSec', () {
var uidt = uuid.v1(options: {'mSecs': testTime});
var uidtb = uuid.v1(options: {'mSecs': testTime - 1});
expect(
(int.parse("0x${uidtb.split('-')[3]}") -
int.parse("0x${uidt.split('-')[3]}")),
anyOf(equals(1), equals(-16383)));
});
test('Clock regression by msec increments the clockseq - nSec', () {
var uidt = uuid.v1(options: {'mSecs': testTime, 'nSecs': 10});
var uidtb = uuid.v1(options: {'mSecs': testTime, 'nSecs': 9});
expect(
(int.parse("0x${uidtb.split('-')[3]}") -
int.parse("0x${uidt.split('-')[3]}")),
equals(1));
});
test('Explicit options produce expected id', () {
var id = uuid.v1(options: {
'mSecs': 1321651533573,
'nSecs': 5432,
'clockSeq': 0x385c,
'node': [0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10]
});
expect(id, equals('d9428888-122b-11e1-b85c-61cd3cbb3210'));
});
test('Ids spanning 1ms boundary are 100ns apart', () {
var u0 = uuid.v1(options: {'mSecs': testTime, 'nSecs': 9999});
var u1 = uuid.v1(options: {'mSecs': testTime + 1, 'nSecs': 0});
var before = u0.split('-')[0], after = u1.split('-')[0];
var dt = int.parse('0x$after') - int.parse('0x$before');
expect(dt, equals(1));
});
test('Generate lots of codes to see if we get v1 collisions.', () {
var uuids = <dynamic>{};
var collisions = 0;
for (var i = 0; i < 10000000; i++) {
var code = uuid.v1();
if (uuids.contains(code)) {
collisions++;
print('Collision of code: $code');
} else {
uuids.add(code);
}
}
expect(collisions, equals(0));
expect(uuids.length, equals(10000000));
});
test(
'Generate lots of codes to check we don\'t generate variant 2 V1 codes.',
() {
for (var i = 0; i < 10000; i++) {
var code = Uuid().v1();
expect(code[19], isNot(equals('d')));
expect(code[19], isNot(equals('c')));
}
});
test('Using buffers', () {
var buffer = Uint8List(16);
var options = {'mSecs': testTime, 'nSecs': 0};
var wihoutBuffer = uuid.v1(options: options);
uuid.v1buffer(buffer, options: options);
expect(Uuid.unparse(buffer), equals(wihoutBuffer));
});
test('Using Objects', () {
var options = {'mSecs': testTime, 'nSecs': 0};
var regular = uuid.v1(options: options);
var obj = uuid.v1obj(options: options);
expect(obj.uuid, equals(regular));
});
});
group('[Version 4 Tests]', () {
test('Check if V4 is consistent using a static seed', () {
var u0 = uuid.v4(options: {
'rng': MathRNG(seed: 1),
});
var u1 = 'a473ff7b-b3cd-4899-a04d-ea0fbd30a72e';
expect(u0, equals(u1));
});
test('Consistency check with buffer', () {
var buffer = Uint8List(16);
uuid.v4buffer(buffer, options: {
'rng': MathRNG(seed: 1),
});
var u1 = 'a473ff7b-b3cd-4899-a04d-ea0fbd30a72e';
expect(Uuid.unparse(buffer), equals(u1));
});
test('Using Objects', () {
var regular = uuid.v4(options: {
'rng': MathRNG(seed: 1),
});
var obj = uuid.v4obj(options: {
'rng': MathRNG(seed: 1),
});
expect(obj.uuid, equals(regular));
});
test('Return same output as entered for "random" option', () {
var u0 = uuid.v4(options: {
'random': [
0x10,
0x91,
0x56,
0xbe,
0xc4,
0xfb,
0xc1,
0xea,
0x71,
0xb4,
0xef,
0xe1,
0x67,
0x1c,
0x58,
0x36
]
});
var u1 = '109156be-c4fb-41ea-b1b4-efe1671c5836';
expect(u0, equals(u1));
});
test('Make sure that really fast uuid.v4 doesn\'t produce duplicates', () {
var list = List.filled(1000, null).map((something) => uuid.v4()).toList();
var setList = list.toSet();
expect(list.length, equals(setList.length));
});
test(
'Another round of testing uuid.v4 to make sure it doesn\'t produce duplicates on high amounts of entries.',
() {
final numToGenerate = 3 * 1000 * 1000;
final values = <String>{}; // set of strings
var generator = Uuid();
var numDuplicates = 0;
for (var i = 0; i < numToGenerate; i++) {
final uuid = generator.v4();
if (!values.contains(uuid)) {
values.add(uuid);
} else {
numDuplicates++;
}
}
expect(numDuplicates, equals(0), reason: 'duplicate UUIDs generated');
});
test('Check if V4 supports Microsoft Guid', () {
var guidString = '2400ee73-282c-4334-e153-08d8f922d1f9';
var isValidDefault = Uuid.isValidUUID(fromString: guidString);
expect(isValidDefault, false);
var isValidRFC = Uuid.isValidUUID(
fromString: guidString, validationMode: ValidationMode.strictRFC4122);
expect(isValidRFC, false);
var isValidNonStrict = Uuid.isValidUUID(
fromString: guidString, validationMode: ValidationMode.nonStrict);
expect(isValidNonStrict, true);
});
});
group('[Version 5 Tests]', () {
test('Using URL namespace and custom name', () {
var u0 = uuid.v5(Uuid.NAMESPACE_URL, 'www.google.com');
var u1 = uuid.v5(Uuid.NAMESPACE_URL, 'www.google.com');
expect(u0, equals(u1));
});
test('Using Random namespace and custom name', () {
var u0 = uuid.v5(null, 'www.google.com');
var u1 = uuid.v5(null, 'www.google.com');
expect(u0, isNot(equals(u1)));
});
test('Using buffers', () {
var buffer = Uint8List(16);
var wihoutBuffer =
uuid.v5(null, 'www.google.com', options: {'randomNamespace': false});
uuid.v5buffer(null, 'www.google.com', buffer,
options: {'randomNamespace': false});
expect(Uuid.unparse(buffer), equals(wihoutBuffer));
});
test('Using Objects', () {
var regular =
uuid.v5(null, 'www.google.com', options: {'randomNamespace': false});
var obj = uuid
.v5obj(null, 'www.google.com', options: {'randomNamespace': false});
expect(obj.uuid, equals(regular));
});
});
group('[Version 6 Tests]', () {
test('IDs created at same mSec are different', () {
expect(
uuid.v6(config: V6Options(null, testTime, null, null, null)),
isNot(equals(
uuid.v6(config: V6Options(null, testTime, null, null, null)))));
});
test('Exception thrown when > 10K ids created in 1 ms', () {
var thrown = false;
try {
uuid.v6(config: V6Options(null, testTime, 10000, null, null));
} catch (e) {
thrown = true;
}
expect(thrown, equals(true));
});
test('Clock regression by msec increments the clockseq - mSec', () {
var uidt = uuid.v6(config: V6Options(null, testTime, null, null, null));
var uidtb =
uuid.v6(config: V6Options(null, testTime - 1, null, null, null));
expect(
(int.parse("0x${uidtb.split('-')[3]}") -
int.parse("0x${uidt.split('-')[3]}")),
anyOf(equals(1), equals(-16383)));
});
test('Clock regression by msec increments the clockseq - nSec', () {
var uidt = uuid.v6(config: V6Options(null, testTime, 10, null, null));
var uidtb = uuid.v6(config: V6Options(null, testTime, 9, null, null));
expect(
(int.parse("0x${uidtb.split('-')[3]}") -
int.parse("0x${uidt.split('-')[3]}")),
equals(1));
});
test('Explicit options produce expected id', () {
var id = uuid.v6(
config: V6Options(0x385c, 1321651533573, 5432,
[0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10], null));
expect(id, equals('1e1122bd-9428-6888-b85c-61cd3cbb3210'));
});
test('Ids spanning 1ms boundary are 100ns apart', () {
var u0 = uuid.v6(config: V6Options(null, testTime, 9999, null, null));
var u1 = uuid.v6(config: V6Options(null, testTime + 1, 0, null, null));
var before = u0.split('-')[2], after = u1.split('-')[2];
var dt = int.parse('0x$after') - int.parse('0x$before');
expect(dt, equals(1));
});
test('Generate lots of codes to see if we get v6 collisions.', () {
var uuids = <dynamic>{};
var collisions = 0;
for (var i = 0; i < 10000000; i++) {
var code = uuid.v6();
if (uuids.contains(code)) {
collisions++;
print('Collision of code: $code');
} else {
uuids.add(code);
}
}
expect(collisions, equals(0));
expect(uuids.length, equals(10000000));
});
test(
'Generate lots of codes to check we don\'t generate variant 2 V6 codes.',
() {
for (var i = 0; i < 10000; i++) {
var code = Uuid().v6();
expect(code[19], isNot(equals('d')));
expect(code[19], isNot(equals('c')));
}
});
test('Using buffers', () {
var buffer = Uint8List(16);
var options = V6Options(null, testTime, 0, null, null);
var wihoutBuffer = uuid.v6(config: options);
uuid.v6buffer(buffer, config: options);
expect(Uuid.unparse(buffer), equals(wihoutBuffer));
});
test('Using Objects', () {
var options = V6Options(null, testTime, 0, null, null);
var regular = uuid.v6(config: options);
var obj = uuid.v6obj(config: options);
expect(obj.uuid, equals(regular));
});
});
group('[Version 7 Tests]', () {
test('IDs created at same mSec are different', () {
expect(uuid.v7(config: V7Options(testTime, null)),
isNot(equals(uuid.v7(config: V7Options(testTime, null)))));
});
test('Explicit options produce expected id', () {
final rand = MathRNG(seed: 1).generate();
var options = V7Options(1321651533573, rand);
var id = uuid.v7(config: options);
expect(id, equals('0133b891-f705-7473-bf7b-b3cdc899a04d'));
});
test('Generate lots of codes to see if we get v7 collisions.', () {
var uuids = <dynamic>{};
var collisions = 0;
for (var i = 0; i < 10000000; i++) {
var code = uuid.v7();
if (uuids.contains(code)) {
collisions++;
//print('Collision of code: $code');
} else {
uuids.add(code);
}
}
expect(collisions, equals(0));
expect(uuids.length, equals(10000000));
});
test(
'Generate lots of codes to check we don\'t generate variant 2 V7 codes.',
() {
for (var i = 0; i < 10000; i++) {
var code = Uuid().v7();
expect(code[19], isNot(equals('d')));
expect(code[19], isNot(equals('c')));
}
});
test('Using buffers', () {
var buffer = Uint8List(16);
final rand = MathRNG(seed: 1).generate();
var options = V7Options(testTime, rand);
var wihoutBuffer = uuid.v7(config: options);
uuid.v7buffer(buffer, config: options);
expect(Uuid.unparse(buffer), equals(wihoutBuffer));
});
test('Using Objects', () {
final rand = MathRNG(seed: 1).generate();
var options = V7Options(testTime, rand);
var regular = uuid.v7(config: options);
var obj = uuid.v7obj(config: options);
expect(obj.uuid, equals(regular));
});
});
group('[Parse/Unparse Tests]', () {
test('Parsing a short/cut-off UUID', () {
var id = '00112233445566778899aabbccddeeff';
expect(() => Uuid.parse(id.substring(0, 10)),
throwsA(isA<FormatException>()));
});
test('Parsing a dirty string with a UUID in it', () {
var id = '00112233445566778899aabbccddeeff';
expect(() => Uuid.unparse(Uuid.parse('(this is the uuid -> $id$id')),
throwsA(isA<FormatException>()));
});
const size = 64;
final buffer = Uint8List(size);
group('Buffer offset good:', () {
for (final testCase in {
'offset=0': 0,
'offset=1': 1,
'offset in the middle': 32,
'offset 16 bytes before the end': size - 16,
}.entries) {
test(testCase.key, () {
final v = Uuid.parse(Uuid.NAMESPACE_OID,
buffer: buffer, offset: testCase.value);
expect(Uuid.unparse(v, offset: testCase.value),
equals(Uuid.NAMESPACE_OID));
});
}
});
group('Buffer offset bad:', () {
for (final testCase in {
'offset 15 bytes before end': size - 15,
'offset at end of buffer': size,
'offset after end of buffer': size + 1,
'offset is negative': -1
}.entries) {
test(testCase.key, () {
expect(
() => Uuid.parse(Uuid.NAMESPACE_OID,
buffer: buffer, offset: testCase.value),
throwsA(isA<RangeError>()));
});
}
});
});
group('[UuidValue]', () {
test('Construct UuidValue instance', () {
const validUUID = '87cd4eb3-cb88-449b-a1da-e468fd829310';
expect(Uuid.isValidUUID(fromString: validUUID), true);
final uuidval = UuidValue.withValidation(validUUID);
expect(uuidval.uuid, validUUID);
});
test('Pass invalid Uuid to constructor', () {
const invalidUUID = 'for sure not a valid uuid';
expect(Uuid.isValidUUID(fromString: invalidUUID), false);
expect(() => UuidValue.withValidation(invalidUUID),
throwsA(isA<FormatException>()));
final uuidval = UuidValue.fromString(invalidUUID);
expect(uuidval.uuid, invalidUUID);
});
test('Pass valid Guid to constructor without validation mode', () {
const validGUID = '2400ee73-282c-4334-e153-08d8f922d1f9';
expect(Uuid.isValidUUID(fromString: validGUID), false);
expect(
() => UuidValue.withValidation(validGUID),
throwsA(isA<FormatException>().having(
(error) => error.message,
'message',
'The provided UUID is not RFC4122 compliant. It seems you might be using a Microsoft GUID. Try setting `validationMode = ValidationMode.nonStrict`',
)));
final uuidval = UuidValue.fromString(validGUID);
expect(uuidval.uuid, validGUID.toLowerCase());
});
test('Pass valid Guid to constructor with validation mode nonStrict', () {
const validGUID = '2400ee73-282c-4334-e153-08d8f922d1f9';
expect(
Uuid.isValidUUID(
fromString: validGUID, validationMode: ValidationMode.nonStrict),
true);
final uuidval =
UuidValue.withValidation(validGUID, ValidationMode.nonStrict);
expect(uuidval.uuid, validGUID.toLowerCase());
});
});
group('[Test Vectors]', () {
group('[UUID6]', () {
for (final testCase in {
'Tuesday, February 22, 2022 2:22:22.000000 PM GMT-05:00': [
1645557742000,
'1EC9414C-232A-6B00-B3C8-9E6BDECED846'
],
}.entries) {
test(testCase.key, () {
var nodeId = <int>[0x9E, 0x6B, 0xDE, 0xCE, 0xD8, 0x46];
var clockSeq = (0xB3 << 8 | 0xC8) & 0x3ffff;
final uuid = Uuid().v6(
config: V6Options(
clockSeq, testCase.value[0] as int, 0, nodeId, null));
expect(uuid.toUpperCase(), equals(testCase.value[1]));
});
}
});
group('[UUID7]', () {
for (final testCase in {
'Tuesday, February 22, 2022 2:22:22.000000 PM GMT-05:00': [
1645557742000,
'017F22E2-79B0-7CC3-98C4-DC0C0C07398F'
],
}.entries) {
test(testCase.key, () {
final rand = [
0x0C,
0xC3,
0x18,
0xC4,
0xDC,
0x0C,
0x0C,
0x07,
0x39,
0x8F
];
final uuid =
Uuid().v7(config: V7Options(testCase.value[0] as int, rand));
expect(uuid.toUpperCase(), equals(testCase.value[1]));
});
}
});
group('[UUID8]', () {
for (final testCase in {
'Tuesday, February 22, 2022 2:22:22.222000 PM GMT-05:00': [
DateTime.fromMillisecondsSinceEpoch(1645557742222).toUtc(),
'20220222-1922-8422-B222-B3CDC899A04D'
],
}.entries) {
test(testCase.key, () {
final rand = MathRNG(seed: 1).generate();
final uuid =
Uuid().v8(config: V8Options(testCase.value[0] as DateTime, rand));
expect(uuid.toUpperCase(), equals(testCase.value[1]));
});
}
});
});
}