ebml.js 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,738 @@
1
+ const forEach = require('lodash.foreach');
2
+ const range = require('lodash.range');
3
+ const unexpected = require('unexpected');
4
+ const unexpectedDate = require('unexpected-date');
5
+
6
+ const tools = require('./tools');
7
+
8
+ const expect = unexpected.clone().use(unexpectedDate);
9
+
10
+ describe('EBML', () => {
11
+ describe('tools', () => {
12
+ describe('#readVint()', () => {
13
+ function readVint(buffer, expected) {
14
+ const vint = tools.readVint(buffer, 0);
15
+ expect(expected, 'to be', vint.value);
16
+ expect(buffer.length, 'to be', vint.length);
17
+ }
18
+
19
+ it('should read the correct value for all 1 byte integers', () => {
20
+ forEach(range(0x80), i => readVint(Buffer.from([i | 0x80]), i));
21
+ });
22
+ it('should read the correct value for 1 byte int with non-zero start', () => {
23
+ const b = Buffer.from([0x00, 0x81]);
24
+ const vint = tools.readVint(b, 1);
25
+ expect(vint.value, 'to be', 1);
26
+ expect(vint.length, 'to be', 1);
27
+ });
28
+ it('should read the correct value for all 2 byte integers', () => {
29
+ forEach(range(0x40), i =>
30
+ forEach(range(0xff), j => {
31
+ readVint(Buffer.from([i | 0x40, j]), (i << 8) + j);
32
+ }),
33
+ );
34
+ });
35
+ it('should read the correct value for all 3 byte integers', () => {
36
+ forEach(range(0, 0x20, 1), i =>
37
+ forEach(range(0, 0xff, 2), j =>
38
+ forEach(range(0, 0xff, 3), k => {
39
+ readVint(Buffer.from([i | 0x20, j, k]), (i << 16) + (j << 8) + k);
40
+ }),
41
+ ),
42
+ );
43
+ });
44
+ // not brute forcing any more bytes, takes sooo long
45
+ it('should read the correct value for 4 byte int min/max values', () => {
46
+ readVint(Buffer.from([0x10, 0x20, 0x00, 0x00]), 2 ** 21);
47
+ readVint(Buffer.from([0x1f, 0xff, 0xff, 0xff]), 2 ** 28 - 1);
48
+ });
49
+ it('should read the correct value for 5 byte int min/max values', () => {
50
+ readVint(Buffer.from([0x08, 0x10, 0x00, 0x00, 0x00]), 2 ** 28);
51
+ readVint(Buffer.from([0x0f, 0xff, 0xff, 0xff, 0xff]), 2 ** 35 - 1);
52
+ });
53
+ it('should read the correct value for 6 byte int min/max values', () => {
54
+ readVint(Buffer.from([0x04, 0x08, 0x00, 0x00, 0x00, 0x00]), 2 ** 35);
55
+ readVint(
56
+ Buffer.from([0x07, 0xff, 0xff, 0xff, 0xff, 0xff]),
57
+ 2 ** 42 - 1,
58
+ );
59
+ });
60
+ it('should read the correct value for 7 byte int min/max values', () => {
61
+ readVint(
62
+ Buffer.from([0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00]),
63
+ 2 ** 42,
64
+ );
65
+ readVint(
66
+ Buffer.from([0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
67
+ 2 ** 49 - 1,
68
+ );
69
+ });
70
+ it('should read the correct value for 8 byte int min value', () => {
71
+ readVint(
72
+ Buffer.from([0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
73
+ 2 ** 49,
74
+ );
75
+ });
76
+ it('should read the correct value for the max representable JS number (2^53)', () => {
77
+ readVint(
78
+ Buffer.from([0x01, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
79
+ 2 ** 53,
80
+ );
81
+ });
82
+ // an unknown value is represented by -1
83
+ it('should return value -1 for more than max representable JS number (2^53 + 1)', () => {
84
+ readVint(
85
+ Buffer.from([0x01, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]),
86
+ -1,
87
+ );
88
+ });
89
+ it('should return value -1 for more than max representable JS number (8 byte int max value)', () => {
90
+ readVint(
91
+ Buffer.from([0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
92
+ -1,
93
+ );
94
+ });
95
+ it('should throw for 9+ byte int values', () => {
96
+ expect(
97
+ () => {
98
+ tools.readVint(
99
+ Buffer.from([
100
+ 0x00,
101
+ 0x80,
102
+ 0x00,
103
+ 0x00,
104
+ 0x00,
105
+ 0x00,
106
+ 0x00,
107
+ 0xff,
108
+ 0xff,
109
+ ]),
110
+ );
111
+ },
112
+ 'to throw',
113
+ /Unrepresentable length/,
114
+ );
115
+ });
116
+ });
117
+ describe('#writeVint()', () => {
118
+ function writeVint(value, expected) {
119
+ const actual = tools.writeVint(value);
120
+ expect(expected.toString('hex'), 'to be', actual.toString('hex'));
121
+ }
122
+
123
+ it('should throw when writing -1', () => {
124
+ expect(
125
+ () => {
126
+ tools.writeVint(-1);
127
+ },
128
+ 'to throw',
129
+ /Unrepresentable value/,
130
+ );
131
+ });
132
+ it('should write all 1 byte integers', () => {
133
+ forEach(range(0, 0x80 - 1), i => writeVint(i, Buffer.from([i | 0x80])));
134
+ });
135
+ it('should write 2 byte int min/max values', () => {
136
+ writeVint(2 ** 7 - 1, Buffer.from([0x40, 0x7f]));
137
+ writeVint(2 ** 14 - 2, Buffer.from([0x7f, 0xfe]));
138
+ });
139
+ it('should write 3 byte int min/max values', () => {
140
+ writeVint(2 ** 14 - 1, Buffer.from([0x20, 0x3f, 0xff]));
141
+ writeVint(2 ** 21 - 2, Buffer.from([0x3f, 0xff, 0xfe]));
142
+ });
143
+ it('should write 4 byte int min/max values', () => {
144
+ writeVint(2 ** 21 - 1, Buffer.from([0x10, 0x1f, 0xff, 0xff]));
145
+ writeVint(2 ** 28 - 2, Buffer.from([0x1f, 0xff, 0xff, 0xfe]));
146
+ });
147
+ it('should write 5 byte int min/max value', () => {
148
+ writeVint(2 ** 28 - 1, Buffer.from([0x08, 0x0f, 0xff, 0xff, 0xff]));
149
+ writeVint(2 ** 35 - 2, Buffer.from([0x0f, 0xff, 0xff, 0xff, 0xfe]));
150
+ });
151
+ it('should write 6 byte int min/max value', () => {
152
+ writeVint(
153
+ 2 ** 35 - 1,
154
+ Buffer.from([0x04, 0x07, 0xff, 0xff, 0xff, 0xff]),
155
+ );
156
+ writeVint(
157
+ 2 ** 42 - 2,
158
+ Buffer.from([0x07, 0xff, 0xff, 0xff, 0xff, 0xfe]),
159
+ );
160
+ });
161
+ it('should write 7 byte int min/max value', () => {
162
+ writeVint(
163
+ 2 ** 42 - 1,
164
+ Buffer.from([0x02, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff]),
165
+ );
166
+ writeVint(
167
+ 2 ** 49 - 2,
168
+ Buffer.from([0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe]),
169
+ );
170
+ });
171
+ it('should write the correct value for 8 byte int min value', () => {
172
+ writeVint(
173
+ 2 ** 49 - 1,
174
+ Buffer.from([0x01, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
175
+ );
176
+ });
177
+ it('should write the correct value for the max representable JS number (2^53)', () => {
178
+ writeVint(
179
+ 2 ** 53,
180
+ Buffer.from([0x01, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
181
+ );
182
+ });
183
+
184
+ /*
185
+ * can't prevent this, 2^53 + 1 === 2^53
186
+ * it('should throw for more than max representable JS number (2^53 + 1)', function() {
187
+ * assert.throws(function() {
188
+ * tools.writeVint((2 ** 53) + 1));
189
+ * }, /Unrepresentable value/)
190
+ * })
191
+ */
192
+
193
+ it('should throw for more than max representable JS number (8 byte int max value)', () => {
194
+ expect(
195
+ () => {
196
+ tools.writeVint(2 ** 56 + 1);
197
+ },
198
+ 'to throw',
199
+ /Unrepresentable value/,
200
+ );
201
+ });
202
+ it('should throw for 9+ byte int values', () => {
203
+ expect(
204
+ () => {
205
+ tools.writeVint(2 ** 56 + 1);
206
+ },
207
+ 'to throw',
208
+ /Unrepresentable value/,
209
+ );
210
+ });
211
+ });
212
+ describe('#concatenate', () => {
213
+ it('returns the 2nd buffer if the first is invalid', () => {
214
+ expect(
215
+ tools.concatenate(null, Buffer.from([0x01])),
216
+ 'to equal',
217
+ Buffer.from([0x01]),
218
+ );
219
+ });
220
+ it('returns the 1st buffer if the second is invalid', () => {
221
+ expect(
222
+ tools.concatenate(Buffer.from([0x01]), null),
223
+ 'to equal',
224
+ Buffer.from([0x01]),
225
+ );
226
+ });
227
+ it('returns the two buffers joined if both are valid', () => {
228
+ expect(
229
+ tools.concatenate(Buffer.from([0x01]), Buffer.from([0x01])),
230
+ 'to equal',
231
+ Buffer.from([0x01, 0x01]),
232
+ );
233
+ });
234
+ });
235
+ describe('#readFloat', () => {
236
+ it('can read 32-bit floats', () => {
237
+ expect(
238
+ tools.readFloat(Buffer.from([0x40, 0x20, 0x00, 0x00])),
239
+ 'to equal',
240
+ 2.5,
241
+ );
242
+ });
243
+ it('can read 64-bit floats', () => {
244
+ expect(
245
+ tools.readFloat(
246
+ Buffer.from([0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
247
+ ),
248
+ 'to equal',
249
+ 2.5,
250
+ );
251
+ });
252
+ it('returns NaN with invalid sized arrays', () => {
253
+ expect(tools.readFloat(Buffer.from([0x40, 0x20, 0x00])), 'to be NaN');
254
+ });
255
+ });
256
+ describe('#readUnsigned', () => {
257
+ it('handles 8-bit integers', () => {
258
+ expect(tools.readUnsigned(Buffer.from([0x07])), 'to equal', 7);
259
+ });
260
+ it('handles 16-bit integers', () => {
261
+ expect(tools.readUnsigned(Buffer.from([0x07, 0x07])), 'to equal', 1799);
262
+ });
263
+ it('handles 32-bit integers', () => {
264
+ expect(
265
+ tools.readUnsigned(Buffer.from([0x07, 0x07, 0x07, 0x07])),
266
+ 'to equal',
267
+ 117901063,
268
+ );
269
+ });
270
+ it('handles integers smaller than 49 bits as numbers', () => {
271
+ expect(
272
+ tools.readUnsigned(Buffer.from([0x07, 0x07, 0x07, 0x07, 0x07])),
273
+ 'to equal',
274
+ 30182672135,
275
+ );
276
+ expect(
277
+ tools.readUnsigned(Buffer.from([0x07, 0x07, 0x07, 0x07, 0x07, 0x07])),
278
+ 'to equal',
279
+ 7726764066567,
280
+ );
281
+ });
282
+ it('returns integers 49 bits or larger as strings', () => {
283
+ expect(
284
+ tools.readUnsigned(
285
+ Buffer.from([0x1, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07]),
286
+ ),
287
+ 'to be a string',
288
+ ).and('to equal', '01070707070707');
289
+ });
290
+ });
291
+ describe('#readUtf8', () => {
292
+ it('handles 8-bit strings', () => {
293
+ expect(tools.readUtf8(Buffer.from([0x45])), 'to be a string').and(
294
+ 'to equal',
295
+ 'E',
296
+ );
297
+ });
298
+ it('handles 16-bit strings', () => {
299
+ expect(tools.readUtf8(Buffer.from([0x45, 0x42])), 'to be a string').and(
300
+ 'to equal',
301
+ 'EB',
302
+ );
303
+ });
304
+ it('handles 24-bit strings', () => {
305
+ expect(
306
+ tools.readUtf8(Buffer.from([0x45, 0x42, 0x4d])),
307
+ 'to be a string',
308
+ ).and('to equal', 'EBM');
309
+ });
310
+ it('handles 32-bit strings', () => {
311
+ expect(
312
+ tools.readUtf8(Buffer.from([0x45, 0x42, 0x4d, 0x4c])),
313
+ 'to be a string',
314
+ ).and('to equal', 'EBML');
315
+ });
316
+ it('handles complex strings', () => {
317
+ expect(
318
+ tools.readUtf8(
319
+ Buffer.from([
320
+ 0x41,
321
+ 0x20,
322
+ 0x6e,
323
+ 0x65,
324
+ 0x77,
325
+ 0x20,
326
+ 0x64,
327
+ 0x72,
328
+ 0x61,
329
+ 0x66,
330
+ 0x74,
331
+ 0x20,
332
+ 0x6f,
333
+ 0x66,
334
+ 0x20,
335
+ 0x74,
336
+ 0x68,
337
+ 0x65,
338
+ 0x20,
339
+ 0x73,
340
+ 0x70,
341
+ 0x65,
342
+ 0x63,
343
+ 0x69,
344
+ 0x66,
345
+ 0x69,
346
+ 0x63,
347
+ 0x61,
348
+ 0x74,
349
+ 0x69,
350
+ 0x6f,
351
+ 0x6e,
352
+ 0x20,
353
+ 0x66,
354
+ 0x6f,
355
+ 0x72,
356
+ 0x20,
357
+ 0x45,
358
+ 0x42,
359
+ 0x4d,
360
+ 0x4c,
361
+ 0x2c,
362
+ 0x20,
363
+ 0x45,
364
+ 0x78,
365
+ 0x74,
366
+ 0x65,
367
+ 0x6e,
368
+ 0x73,
369
+ 0x69,
370
+ 0x62,
371
+ 0x6c,
372
+ 0x65,
373
+ 0x20,
374
+ 0x42,
375
+ 0x69,
376
+ 0x6e,
377
+ 0x61,
378
+ 0x72,
379
+ 0x79,
380
+ 0x20,
381
+ 0x4d,
382
+ 0x65,
383
+ 0x74,
384
+ 0x61,
385
+ 0x20,
386
+ 0x4c,
387
+ 0x61,
388
+ 0x6e,
389
+ 0x67,
390
+ 0x75,
391
+ 0x61,
392
+ 0x67,
393
+ 0x65,
394
+ 0x2c,
395
+ 0x20,
396
+ 0x69,
397
+ 0x73,
398
+ 0x20,
399
+ 0x72,
400
+ 0x65,
401
+ 0x61,
402
+ 0x64,
403
+ 0x79,
404
+ 0x20,
405
+ 0x66,
406
+ 0x6f,
407
+ 0x72,
408
+ 0x20,
409
+ 0x72,
410
+ 0x65,
411
+ 0x76,
412
+ 0x69,
413
+ 0x65,
414
+ 0x77,
415
+ 0x2e,
416
+ 0x20,
417
+ 0x45,
418
+ 0x42,
419
+ 0x4d,
420
+ 0x4c,
421
+ 0x20,
422
+ 0x69,
423
+ 0x73,
424
+ 0x20,
425
+ 0x61,
426
+ 0x20,
427
+ 0x62,
428
+ 0x69,
429
+ 0x6e,
430
+ 0x61,
431
+ 0x72,
432
+ 0x79,
433
+ 0x20,
434
+ 0x58,
435
+ 0x4d,
436
+ 0x4c,
437
+ 0x2d,
438
+ 0x6c,
439
+ 0x69,
440
+ 0x6b,
441
+ 0x65,
442
+ 0x20,
443
+ 0x66,
444
+ 0x6f,
445
+ 0x72,
446
+ 0x6d,
447
+ 0x61,
448
+ 0x74,
449
+ 0x20,
450
+ 0x66,
451
+ 0x6f,
452
+ 0x72,
453
+ 0x20,
454
+ 0x65,
455
+ 0x6e,
456
+ 0x63,
457
+ 0x61,
458
+ 0x70,
459
+ 0x73,
460
+ 0x75,
461
+ 0x6c,
462
+ 0x61,
463
+ 0x74,
464
+ 0x69,
465
+ 0x6e,
466
+ 0x67,
467
+ 0x20,
468
+ 0x64,
469
+ 0x61,
470
+ 0x74,
471
+ 0x61,
472
+ 0x20,
473
+ 0x61,
474
+ 0x6e,
475
+ 0x64,
476
+ 0x20,
477
+ 0x69,
478
+ 0x73,
479
+ 0x20,
480
+ 0x74,
481
+ 0x68,
482
+ 0x65,
483
+ 0x20,
484
+ 0x75,
485
+ 0x6e,
486
+ 0x64,
487
+ 0x65,
488
+ 0x72,
489
+ 0x6c,
490
+ 0x79,
491
+ 0x69,
492
+ 0x6e,
493
+ 0x67,
494
+ 0x20,
495
+ 0x66,
496
+ 0x6f,
497
+ 0x72,
498
+ 0x6d,
499
+ 0x61,
500
+ 0x74,
501
+ 0x20,
502
+ 0x6f,
503
+ 0x66,
504
+ 0x20,
505
+ 0x23,
506
+ 0x4d,
507
+ 0x61,
508
+ 0x74,
509
+ 0x72,
510
+ 0x6f,
511
+ 0x73,
512
+ 0x6b,
513
+ 0x61,
514
+ 0x20,
515
+ 0x26,
516
+ 0x20,
517
+ 0x23,
518
+ 0x77,
519
+ 0x65,
520
+ 0x62,
521
+ 0x6d,
522
+ 0x2e,
523
+ 0x20,
524
+ 0x68,
525
+ 0x74,
526
+ 0x74,
527
+ 0x70,
528
+ 0x73,
529
+ 0x3a,
530
+ 0x2f,
531
+ 0x2f,
532
+ 0x74,
533
+ 0x6f,
534
+ 0x6f,
535
+ 0x6c,
536
+ 0x73,
537
+ 0x2e,
538
+ 0x69,
539
+ 0x65,
540
+ 0x74,
541
+ 0x66,
542
+ 0x2e,
543
+ 0x6f,
544
+ 0x72,
545
+ 0x67,
546
+ 0x2f,
547
+ 0x68,
548
+ 0x74,
549
+ 0x6d,
550
+ 0x6c,
551
+ 0x2f,
552
+ 0x64,
553
+ 0x72,
554
+ 0x61,
555
+ 0x66,
556
+ 0x74,
557
+ 0x2d,
558
+ 0x69,
559
+ 0x65,
560
+ 0x74,
561
+ 0x66,
562
+ 0x2d,
563
+ 0x63,
564
+ 0x65,
565
+ 0x6c,
566
+ 0x6c,
567
+ 0x61,
568
+ 0x72,
569
+ 0x2d,
570
+ 0x65,
571
+ 0x62,
572
+ 0x6d,
573
+ 0x6c,
574
+ 0x2d,
575
+ 0x30,
576
+ 0x36,
577
+ 0x20,
578
+ 0x68,
579
+ 0x74,
580
+ 0x74,
581
+ 0x70,
582
+ 0x73,
583
+ 0x3a,
584
+ 0x2f,
585
+ 0x2f,
586
+ 0x70,
587
+ 0x69,
588
+ 0x63,
589
+ 0x2e,
590
+ 0x74,
591
+ 0x77,
592
+ 0x69,
593
+ 0x74,
594
+ 0x74,
595
+ 0x65,
596
+ 0x72,
597
+ 0x2e,
598
+ 0x63,
599
+ 0x6f,
600
+ 0x6d,
601
+ 0x2f,
602
+ 0x35,
603
+ 0x47,
604
+ 0x54,
605
+ 0x4c,
606
+ 0x41,
607
+ 0x57,
608
+ 0x64,
609
+ 0x73,
610
+ 0x65,
611
+ 0x76,
612
+ ]),
613
+ ),
614
+ 'to be a string',
615
+ ).and(
616
+ 'to be',
617
+ 'A new draft of the specification for EBML, Extensible Binary Meta Language, is ready for review. EBML is a binary XML-like format for encapsulating data and is the underlying format of #Matroska & #webm. https://tools.ietf.org/html/draft-ietf-cellar-ebml-06 https://pic.twitter.com/5GTLAWdsev',
618
+ );
619
+ });
620
+ });
621
+ describe('#readSigned', () => {
622
+ it('handles 8-bit integers', () => {
623
+ expect(tools.readSigned(Buffer.from([0x07])), 'to equal', 7);
624
+ });
625
+ it('handles 16-bit integers', () => {
626
+ expect(tools.readSigned(Buffer.from([0x07, 0x07])), 'to equal', 1799);
627
+ });
628
+ it('handles 32-bit integers', () => {
629
+ expect(
630
+ tools.readSigned(Buffer.from([0x07, 0x07, 0x07, 0x07])),
631
+ 'to equal',
632
+ 117901063,
633
+ );
634
+ });
635
+ it('returns NaN with invalid sized arrays', () => {
636
+ expect(tools.readSigned(Buffer.from([0x40, 0x20, 0x00])), 'to be NaN');
637
+ });
638
+ });
639
+ describe('#readDataFromTag', () => {
640
+ it('can read a string from a tag', () => {
641
+ const tagData = { type: 's', name: 'DocType' };
642
+ const buf = Buffer.from([
643
+ 0x6d,
644
+ 0x61,
645
+ 0x74,
646
+ 0x72,
647
+ 0x6f,
648
+ 0x73,
649
+ 0x6b,
650
+ 0x61,
651
+ ]);
652
+ expect(tools.readDataFromTag(tagData, buf), 'to satisfy', {
653
+ type: 's',
654
+ name: 'DocType',
655
+ data: expect
656
+ .it('to be a', Buffer)
657
+ .and(
658
+ 'to equal',
659
+ Buffer.from([0x6d, 0x61, 0x74, 0x72, 0x6f, 0x73, 0x6b, 0x61]),
660
+ ),
661
+ value: 'matroska',
662
+ });
663
+ });
664
+ it('can read an unsigned integer from a tag', () => {
665
+ const tagData = {
666
+ type: 'u',
667
+ name: 'TrackType',
668
+ };
669
+ const buf = Buffer.from([0x77]);
670
+ expect(tools.readDataFromTag(tagData, buf), 'to satisfy', {
671
+ type: 'u',
672
+ name: 'TrackType',
673
+ data: expect
674
+ .it('to be a', Buffer)
675
+ .and('to equal', Buffer.from([0x77])),
676
+ value: 119,
677
+ });
678
+ });
679
+ it('can read a signed integer from a tag', () => {
680
+ const tagData = {
681
+ type: 'i',
682
+ name: 'TrackOffset',
683
+ };
684
+ const buf = Buffer.from([0xa7]);
685
+ expect(tools.readDataFromTag(tagData, buf), 'to satisfy', {
686
+ type: 'i',
687
+ name: 'TrackOffset',
688
+ data: expect
689
+ .it('to be a', Buffer)
690
+ .and('to equal', Buffer.from([0xa7])),
691
+ value: -89,
692
+ });
693
+ });
694
+ it('can read a float from a tag', () => {
695
+ const tagData = {
696
+ type: 'f',
697
+ name: 'Duration',
698
+ };
699
+ const buf = Buffer.from([0x40, 0x00, 0x00, 0x77]);
700
+ expect(tools.readDataFromTag(tagData, buf), 'to satisfy', {
701
+ type: 'f',
702
+ name: 'Duration',
703
+ data: expect
704
+ .it('to be a', Buffer)
705
+ .and('to equal', Buffer.from([0x40, 0x00, 0x00, 0x77])),
706
+ value: expect.it('to be close to', 2.00003, 1e-5),
707
+ });
708
+ });
709
+ });
710
+ describe('#readDate', () => {
711
+ it('handles 8-bit integers', () => {
712
+ expect(tools.readDate(Buffer.from([0x07])), 'to equal', new Date(7));
713
+ });
714
+ it('handles 16-bit integers', () => {
715
+ expect(
716
+ tools.readDate(Buffer.from([0x07, 0x07])),
717
+ 'to equal',
718
+ new Date(1799),
719
+ );
720
+ });
721
+ it('handles 32-bit integers', () => {
722
+ expect(
723
+ tools.readDate(Buffer.from([0x07, 0x07, 0x07, 0x07])),
724
+ 'to equal',
725
+ new Date(117901063),
726
+ );
727
+ });
728
+ it('returns now with invalid sized arrays', () => {
729
+ expect(
730
+ tools.readDate(Buffer.from([0x40, 0x20, 0x00])),
731
+ 'to be close to',
732
+ new Date(0),
733
+ 2000,
734
+ );
735
+ });
736
+ });
737
+ });
738
+ });