-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathrandom_tests.ts
More file actions
790 lines (712 loc) · 23.3 KB
/
random_tests.ts
File metadata and controls
790 lines (712 loc) · 23.3 KB
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
/* eslint-disable @typescript-eslint/no-unused-expressions */
import { test, assertThrowsAsync } from '../util';
import { expect } from 'chai';
import crypto, {
ab2str,
abvToArrayBuffer,
Buffer,
} from 'react-native-quick-crypto';
// copied from https://github.com/nodejs/node/blob/master/test/parallel/test-crypto-random.js
const SUITE = 'random';
// --- Phase 4.5: fire-and-forget async assertion regression ---
//
// Pre-fix, every `crypto.randomFill(buf, (err, res) => { expect(...) })`
// call had its assertions silently swallowed: the test function returned
// before the callback fired, so a thrown AssertionError became an
// unhandled rejection rather than a test failure. The fix wraps each
// callback in a Promise the test function returns. The two tests below
// prove the pattern actually surfaces failures now.
test(SUITE, 'fire-and-forget regression: failing assert rejects the test', () =>
assertThrowsAsync(
() =>
new Promise<void>((resolve, reject) => {
const buf = Buffer.alloc(8);
crypto.randomFill(buf, (err, _res) => {
try {
expect(err).to.be.null;
expect(_res.length).to.equal(999); // intentionally wrong
resolve();
} catch (e) {
reject(e);
}
});
}),
'expected', // chai assertion failure message contains "expected ..."
),
);
test(SUITE, 'simple test 1', () => {
const buf = Buffer.alloc(10);
const before = buf.toString('hex');
const after = crypto.randomFillSync(buf).toString('hex');
expect(before).not.to.equal(after);
});
test(SUITE, 'simple test 2', () => {
const buf = new Uint8Array(new Array(10).fill(0));
const before = Buffer.from(buf).toString('hex');
crypto.randomFillSync(buf);
const after = Buffer.from(buf).toString('hex');
expect(before).not.to.equal(after);
});
test(SUITE, 'simple test 3', () => {
[
new Uint16Array(10),
new Uint32Array(10),
new Float32Array(10),
new Float64Array(10),
new DataView(new ArrayBuffer(10)),
].forEach(buf => {
const before = Buffer.from(buf.buffer).toString('hex');
crypto.randomFillSync(buf);
const after = Buffer.from(buf.buffer).toString('hex');
expect(before).not.to.equal(after);
});
});
test(SUITE, 'simple test 4 - randomFillSync ArrayBuffer', () => {
[new ArrayBuffer(10), new ArrayBuffer(10)].forEach(buf => {
const before = Buffer.from(buf).toString('hex');
crypto.randomFillSync(buf);
const after = Buffer.from(buf).toString('hex');
expect(before).not.to.equal(after);
});
});
test(SUITE, 'simple test 5 - randomFill Buffer ', () => {
const buf = Buffer.alloc(10);
const before = buf.toString('hex');
return new Promise<void>((resolve, reject) => {
crypto.randomFill(buf, (err: Error | null, res: Buffer) => {
try {
expect(err).to.be.null;
const after = res?.toString('hex');
expect(before).not.to.equal(after);
resolve();
} catch (e) {
reject(e);
}
});
});
});
test(SUITE, 'simple test 6', () => {
const buf = new Uint8Array(new Array(10).fill(0));
const before = Buffer.from(buf).toString('hex');
return new Promise<void>((resolve, reject) => {
crypto.randomFill(buf, (err: Error | null, res: Uint8Array) => {
try {
expect(err).to.be.null;
const after = Buffer.from(res).toString('hex');
expect(before).not.to.equal(after);
resolve();
} catch (e) {
reject(e);
}
});
});
});
type BufTypes =
| Uint16Array
| Uint32Array
| Float32Array
| Float64Array
| DataView;
const bufs: [BufTypes, string][] = [
[new Uint16Array(10), 'Uint16Array'],
[new Uint32Array(10), 'Uint32Array'],
[new Float32Array(10), 'Float32Array'],
[new Float64Array(10), 'Float64Array'],
[new DataView(new ArrayBuffer(10)), 'DataView'],
];
bufs.forEach(([buf, name]) => {
test(SUITE, `simple test 7, ${name}`, () => {
const ab = abvToArrayBuffer(buf);
const before = ab2str(ab);
return new Promise<void>((resolve, reject) => {
crypto.randomFill(ab, (err: Error | null, buf2: ArrayBuffer) => {
try {
expect(err).to.be.null;
const after = Buffer.from(buf2).toString('hex');
expect(before).not.to.equal(after);
resolve();
} catch (e) {
reject(e);
}
});
});
});
});
test(SUITE, 'simple test 8', () => {
// Two ArrayBuffers, two callbacks — resolve only when both have asserted.
return Promise.all(
[new ArrayBuffer(10), new ArrayBuffer(10)].map(
buf =>
new Promise<void>((resolve, reject) => {
const before = Buffer.from(buf).toString('hex');
crypto.randomFill(buf, (err: Error | null, res: ArrayBuffer) => {
try {
expect(err).to.be.null;
const after = Buffer.from(res).toString('hex');
expect(before).not.to.equal(after);
resolve();
} catch (e) {
reject(e);
}
});
}),
),
).then(() => {});
});
test(SUITE, 'randomFillSync - deepStringEqual - Buffer', () => {
const buf = Buffer.alloc(10);
const before = buf.toString('hex');
crypto.randomFillSync(buf, 5, 5);
const after = buf.toString('hex');
expect(before).not.to.equal(after);
expect(before.slice(0, 5)).to.equal(after.slice(0, 5));
});
test(SUITE, 'randomFillSync - deepStringEqual - Uint8Array', () => {
const buf = new Uint8Array(new Array(10).fill(0));
const before = Buffer.from(buf).toString('hex');
crypto.randomFillSync(buf, 5, 5);
const after = Buffer.from(buf).toString('hex');
expect(before).not.to.equal(after);
expect(before.slice(0, 5)).to.equal(after.slice(0, 5));
});
test(SUITE, 'randomFillSync - deepStringEqual - Buffer no size', () => {
const buf = Buffer.alloc(10);
const before = buf.toString('hex');
crypto.randomFillSync(buf, 5);
const after = buf.toString('hex');
expect(before).not.to.equal(after);
expect(before.slice(0, 5)).to.equal(after.slice(0, 5));
});
test(SUITE, 'randomFill - deepStringEqual - Buffer', () => {
const buf = Buffer.alloc(10);
const before = buf.toString('hex');
return new Promise<void>((resolve, reject) => {
crypto.randomFill(buf, 5, 5, (err: Error | null, res: Buffer) => {
try {
expect(err).to.be.null;
const after = Buffer.from(res).toString('hex');
expect(before).not.to.equal(after);
expect(before.slice(0, 5)).to.equal(after.slice(0, 5));
resolve();
} catch (e) {
reject(e);
}
});
});
});
test(SUITE, 'randomFill - deepStringEqual - Uint8Array', () => {
const buf = new Uint8Array(new Array(10).fill(0));
const before = Buffer.from(buf).toString('hex');
return new Promise<void>((resolve, reject) => {
crypto.randomFill(buf, 5, 5, (err: Error | null, res: Uint8Array) => {
try {
expect(err).to.be.null;
const after = Buffer.from(res).toString('hex');
expect(before).not.to.equal(after);
expect(before.slice(0, 5)).to.equal(after.slice(0, 5));
resolve();
} catch (e) {
reject(e);
}
});
});
});
// finish
// describe('errors checks', () => {
// [Buffer.alloc(10), new Uint8Array(new Array(10).fill(0))].forEach((buf) => {
// const buffer = buf;
// test(SUITE, 'Expected byteLength of 10', () => {
// const len = Buffer.byteLength(buffer);
// assert.strictEqual(len, 10, `Expected byteLength of 10, got ${len}`);
// });
// const typeErrObj = {
// code: 'ERR_INVALID_ARG_TYPE',
// name: 'TypeError',
// message:
// 'The "offset" argument must be of type number. ' +
// "Received type string ('test')",
// };
// test(SUITE, 'offset must be a number', () => {
// assert.throws(
// () => crypto.randomFillSync(buffer, 'test'),
// /ERR_INVALID_ARG_TYPE/,
// typeErrObj.message
// );
// });
// test(SUITE, 'offsetMustBe a number ', () => {
// assert.throws(
// () => crypto.randomFill(buffer, 'test', () => {}),
// typeErrObj
// );
// });
// typeErrObj.message = typeErrObj.message.replace('offset', 'size');
// assert.throws(() => crypto.randomFillSync(buffer, 0, 'test'), typeErrObj);
// assert.throws(
// () => crypto.randomFill(buffer, 0, 'test', () => {})),
// typeErrObj
// );
// [NaN, kMaxPossibleLength + 1, -10, (-1 >>> 0) + 1].forEach(
// (offsetSize) => {
// const errObj = {
// code: 'ERR_OUT_OF_RANGE',
// name: 'RangeError',
// message:
// 'The value of "offset" is out of range. ' +
// `It must be >= 0 && <= 10. Received ${offsetSize}`,
// };
// assert.throws(() => crypto.randomFillSync(buf, offsetSize), errObj);
// assert.throws(
// () => crypto.randomFill(buffer, offsetSize, () => {}),
// errObj
// );
// errObj.message =
// 'The value of "size" is out of range. It must be >= ' +
// `0 && <= ${kMaxPossibleLength}. Received ${offsetSize}`;
// assert.throws(
// () => crypto.randomFillSync(buffer, 1, offsetSize),
// errObj
// );
// assert.throws(
// () => crypto.randomFill(buffer, 1, offsetSize, () => {}),
// errObj
// );
// }
// );
// const rangeErrObj = {
// code: 'ERR_OUT_OF_RANGE',
// name: 'RangeError',
// message:
// 'The value of "size + offset" is out of range. ' +
// 'It must be <= 10. Received 11',
// };
// assert.throws(() => crypto.randomFillSync(buf, 1, 10), rangeErrObj);
// assert.throws(() => crypto.randomFill(buf, 1, 10, () => {}), rangeErrObj);
// });
// });
// https://github.com/nodejs/node-v0.x-archive/issues/5126,
// "FATAL ERROR: v8::Object::SetIndexedPropertiesToExternalArrayData() length
// exceeds max acceptable value"
// handle errors properly
// assert.throws(() => crypto.randomBytes((-1 >>> 0) + 1), {
// code: 'ERR_OUT_OF_RANGE',
// name: 'RangeError',
// message:
// 'The value of "size" is out of range. ' +
// `It must be >= 0 && <= ${kMaxPossibleLength}. Received 4294967296`,
// });
// [1, true, NaN, null, undefined, {}, []].forEach((i) => {
// const buf = Buffer.alloc(10);
// assert.throws(() => crypto.randomFillSync(i), {
// code: 'ERR_INVALID_ARG_TYPE',
// name: 'TypeError',
// });
// assert.throws(() => crypto.randomFill(i, common.mustNotCall()), {
// code: 'ERR_INVALID_ARG_TYPE',
// name: 'TypeError',
// });
// assert.throws(() => crypto.randomFill(buf, 0, 10, i), {
// code: 'ERR_INVALID_ARG_TYPE',
// name: 'TypeError',
// });
// });
// [1, true, NaN, null, {}, []].forEach((i) => {
// assert.throws(() => crypto.randomBytes(1, i), {
// code: 'ERR_INVALID_ARG_TYPE',
// name: 'TypeError',
// });
// });
// Note: randomBytes & pseudoRandomBytes are equivalent (as of now), so this
// will only run "lengths" number of tests, not 2 x "lengths"
[crypto.randomBytes, crypto.pseudoRandomBytes].map(fn => {
[0, 1, 2, 4, 16, 256, 1024, 101.2].map(len => {
test(SUITE, `${fn.name} @ ${len}`, () => {
return new Promise<void>((resolve, reject) => {
fn(len, (ex: Error | null, buf?: Buffer) => {
try {
expect(ex).to.be.null;
expect(buf?.length).to.equal(Math.floor(len));
expect(Buffer.isBuffer(buf)).to.be.true;
resolve();
} catch (e) {
reject(e);
}
});
});
});
});
});
['pseudoRandomBytes', 'prng', 'rng'].forEach(name => {
test(SUITE, name, () => {
const desc = Object.getOwnPropertyDescriptor(crypto, name);
expect(desc).to.not.be.undefined;
expect(desc?.configurable).to.be.true;
// expect(desc?.enumerable).to.be.false;
});
});
test(SUITE, 'randomInt - Asynchronous API', () => {
return new Promise<void>((resolve, reject) => {
const randomInts: number[] = [];
let settled = false;
const reportFail = (e: unknown) => {
if (!settled) {
settled = true;
reject(e);
}
};
for (let i = 0; i < 100; i++) {
crypto.randomInt(3, (err: Error | null, n: number) => {
if (settled) return;
try {
expect(err).to.be.null;
expect(n).to.be.greaterThanOrEqual(0);
expect(n).to.be.lessThan(3);
randomInts.push(n);
if (randomInts.length === 100) {
expect(randomInts).not.to.contain(-1);
expect(randomInts).to.contain(0);
expect(randomInts).to.contain(1);
expect(randomInts).to.contain(2);
expect(randomInts).not.to.contain(3);
settled = true;
resolve();
}
} catch (e) {
reportFail(e);
}
});
}
});
});
test(SUITE, 'randomInt - Synchronous API', () => {
const randomInts = [];
for (let i = 0; i < 100; i++) {
const n = crypto.randomInt(3);
expect(n).to.be.greaterThanOrEqual(0);
expect(n).to.be.lessThan(3);
randomInts.push(n);
}
expect(randomInts).not.to.contain(-1);
expect(randomInts).to.contain(0);
expect(randomInts).to.contain(1);
expect(randomInts).to.contain(2);
expect(randomInts).not.to.contain(3);
});
test(SUITE, 'randomInt positive range', () => {
return new Promise<void>((resolve, reject) => {
const randomInts: number[] = [];
let settled = false;
for (let i = 0; i < 100; i++) {
crypto.randomInt(1, 3, (err: Error | null, n: number) => {
if (settled) return;
try {
expect(err).to.be.null;
expect(n).to.be.greaterThanOrEqual(1);
expect(n).to.be.lessThan(3);
randomInts.push(n);
if (randomInts.length === 100) {
expect(randomInts).to.contain(1);
expect(randomInts).to.contain(2);
settled = true;
resolve();
}
} catch (e) {
settled = true;
reject(e);
}
});
}
});
});
test(SUITE, 'randomInt negative range', () => {
return new Promise<void>((resolve, reject) => {
const randomInts: number[] = [];
let settled = false;
for (let i = 0; i < 100; i++) {
crypto.randomInt(-10, -8, (err: Error | null, n: number) => {
if (settled) return;
try {
expect(err).to.be.null;
expect(n).to.be.greaterThanOrEqual(-10);
expect(n).to.be.lessThan(-8);
randomInts.push(n);
if (randomInts.length === 100) {
expect(randomInts).not.to.contain(-11);
expect(randomInts).to.contain(-10);
expect(randomInts).to.contain(-9);
expect(randomInts).not.to.contain(-8);
settled = true;
resolve();
}
} catch (e) {
settled = true;
reject(e);
}
});
}
});
});
// ['10', true, NaN, null, {}, []].forEach((i) => {
// const invalidMinError = {
// code: 'ERR_INVALID_ARG_TYPE',
// name: 'TypeError',
// message:
// 'The "min" argument must be a safe integer.' +
// `${common.invalidArgTypeHelper(i)}`,
// };
// const invalidMaxError = {
// code: 'ERR_INVALID_ARG_TYPE',
// name: 'TypeError',
// message:
// 'The "max" argument must be a safe integer.' +
// `${common.invalidArgTypeHelper(i)}`,
// };
// assert.throws(() => crypto.randomInt(i, 100), invalidMinError);
// assert.throws(
// () => crypto.randomInt(i, 100, common.mustNotCall()),
// invalidMinError
// );
// assert.throws(() => crypto.randomInt(i), invalidMaxError);
// assert.throws(
// () => crypto.randomInt(i, common.mustNotCall()),
// invalidMaxError
// );
// assert.throws(
// () => crypto.randomInt(0, i, common.mustNotCall()),
// invalidMaxError
// );
// assert.throws(() => crypto.randomInt(0, i), invalidMaxError);
// });
// assert.throws(
// () => crypto.randomInt(minInt - 1, minInt + 5, common.mustNotCall()),
// {
// code: 'ERR_INVALID_ARG_TYPE',
// name: 'TypeError',
// message:
// 'The "min" argument must be a safe integer.' +
// `${common.invalidArgTypeHelper(minInt - 1)}`,
// }
// );
// assert.throws(() => crypto.randomInt(maxInt + 1, common.mustNotCall()), {
// code: 'ERR_INVALID_ARG_TYPE',
// name: 'TypeError',
// message:
// 'The "max" argument must be a safe integer.' +
// `${common.invalidArgTypeHelper(maxInt + 1)}`,
// });
for (const interval of [[0], [1, 1], [3, 2], [-5, -5], [11, -10]]) {
test(SUITE, 'range ' + interval.toString(), () => {
expect(() => {
crypto.randomInt(1, MAX_RANGE + 2, () => {});
}).to.throw(
/ERR_OUT_OF_RANGE/,
'The value of "max" is out of range. It must be greater than ' +
`the value of "min" (${interval[interval.length - 2] || 0}). ` +
`Received ${interval[interval.length - 1]}`,
);
});
}
const MAX_RANGE = 0xffffffffffff;
const maxInt = Number.MAX_SAFE_INTEGER;
const minInt = Number.MIN_SAFE_INTEGER;
test(SUITE, 'minInt, minInt + 5 ', () => {
crypto.randomInt(minInt, minInt + 5, () => {
// done();
});
});
test(SUITE, 'maxint - 5, maxint', () => {
crypto.randomInt(maxInt - 5, maxInt, () => {
// done();
});
});
test(SUITE, 'randomInt 1', () => {
crypto.randomInt(1, () => {
// done();
});
});
test(SUITE, 'randomInt 0 - 1', () => {
crypto.randomInt(0, 1, () => {
// done();
});
});
test(SUITE, 'maxRange', () => {
crypto.randomInt(MAX_RANGE, () => {
// done();
});
});
test(SUITE, 'maxRange move + 1', () => {
crypto.randomInt(1, MAX_RANGE + 1, () => {
// done();
});
});
test(SUITE, 'ERR_OUT_OF_RANGE 1', () => {
expect(() => {
crypto.randomInt(1, MAX_RANGE + 2, () => {});
}).to.throw(
/ERR_OUT_OF_RANGE/,
'The value of "max" is out of range. ' +
`It must be <= ${MAX_RANGE}. ` +
'Received 281_474_976_710_657',
);
});
test(SUITE, 'ERR_OUT_OF_RANGE 2', () => {
expect(() => {
crypto.randomInt(MAX_RANGE + 1, () => {});
}).to.throw(
/ERR_OUT_OF_RANGE/,
'The value of "max" is out of range. ' +
`It must be <= ${MAX_RANGE}. ` +
'Received 281_474_976_710_656',
);
});
[true, NaN, [], 10].forEach(val => {
test(SUITE, `expect type error: ${val}`, () => {
expect(() => {
// @ts-expect-error - testing bad args
crypto.randomInt(0, 1, val);
}).to.throw(/callback must be a function or undefined/);
});
});
test(SUITE, 'randomFill int16', () => {
crypto.randomFill(new Uint16Array(10), 0, () => {
// done();
});
});
test(SUITE, 'randomFill int32', () => {
crypto.randomFill(new Uint32Array(10), 0, () => {
// done();
});
});
test(SUITE, 'randomFill int32, 1', () => {
crypto.randomFill(new Uint32Array(10), 0, 1, () => {
// done();
});
});
test(SUITE, 'crypto.getRandomValues', () => {
const r = crypto.getRandomValues(new Uint8Array(10));
expect(r.length).to.equal(10);
});
// WebCrypto §getRandomValues: byteLength > 65536 must throw a
// QuotaExceededError DOMException carrying `quota` and `requested`.
test(SUITE, 'getRandomValues - QuotaExceededError on > 65536 bytes', () => {
let caught: unknown;
try {
crypto.getRandomValues(new Uint8Array(65537));
} catch (e) {
caught = e;
}
const err = caught as Error & { quota?: number; requested?: number };
expect(err).to.be.instanceOf(Error);
expect(err.name).to.equal('QuotaExceededError');
expect(err.quota).to.equal(65536);
expect(err.requested).to.equal(65537);
});
// WebCrypto §getRandomValues: non-integer-typed views must throw
// TypeMismatchError. Float and DataView are explicitly excluded.
[
['Float32Array', () => new Float32Array(4)],
['Float64Array', () => new Float64Array(4)],
['DataView', () => new DataView(new ArrayBuffer(8))],
].forEach(([name, make]) => {
test(SUITE, `getRandomValues - TypeMismatchError on ${name}`, () => {
let caught: unknown;
try {
// @ts-expect-error - intentionally passing disallowed view type
crypto.getRandomValues((make as () => ArrayBufferView)());
} catch (e) {
caught = e;
}
const err = caught as Error;
expect(err).to.be.instanceOf(Error);
expect(err.name).to.equal('TypeMismatchError');
});
});
// Issue #953: TypedArray views over larger ArrayBuffers
// getRandomValues / randomFillSync should only fill the view, not the entire
// underlying ArrayBuffer.
test(
SUITE,
'getRandomValues - view over larger buffer preserves surrounding data',
() => {
const heap = new ArrayBuffer(1024);
const full = new Uint8Array(heap);
full.fill(42);
const view = new Uint8Array(heap, 100, 32);
crypto.getRandomValues(view);
// Bytes before the view must be untouched
expect(full[0]).to.equal(42);
expect(full[99]).to.equal(42);
// Bytes after the view must be untouched
expect(full[132]).to.equal(42);
expect(full[1023]).to.equal(42);
// The view itself must have been randomized (not still all 42)
const viewStillAll42 = view.every(b => b === 42);
expect(viewStillAll42).to.be.false;
},
);
test(
SUITE,
'randomFillSync - view over larger buffer preserves surrounding data',
() => {
const heap = new ArrayBuffer(1024);
const full = new Uint8Array(heap);
full.fill(42);
const view = new Uint8Array(heap, 200, 64);
crypto.randomFillSync(view);
expect(full[0]).to.equal(42);
expect(full[199]).to.equal(42);
expect(full[264]).to.equal(42);
expect(full[1023]).to.equal(42);
// The view itself must have been randomized
const viewStillAll42 = view.every(b => b === 42);
expect(viewStillAll42).to.be.false;
},
);
test(SUITE, 'randomFillSync - view with offset and size params', () => {
const heap = new ArrayBuffer(512);
const full = new Uint8Array(heap);
full.fill(42);
// View starts at byte 100, length 64
// randomFillSync offset=10, size=20 → should fill view bytes 10-29,
// i.e. heap bytes 110-129 only
const view = new Uint8Array(heap, 100, 64);
crypto.randomFillSync(view, 10, 20);
// Within the view but before the offset — these must stay 42
// With the bug, offset is applied to the underlying buffer (byte 10)
// instead of relative to the view (byte 110), so byte 10 gets randomized
// while bytes 100-109 stay 42 by accident. Check byte 10 to catch this.
expect(full[10]).to.equal(42);
expect(full[29]).to.equal(42);
// View bytes before the offset (heap 100-109) must be untouched
expect(full[100]).to.equal(42);
expect(full[109]).to.equal(42);
// The filled region (heap 110-129) must have been randomized
const filled = full.slice(110, 130);
const filledStillAll42 = filled.every(b => b === 42);
expect(filledStillAll42).to.be.false;
});
test(
SUITE,
'randomFill (async) - view over larger buffer preserves surrounding data',
() => {
const heap = new ArrayBuffer(1024);
const full = new Uint8Array(heap);
full.fill(42);
const view = new Uint8Array(heap, 100, 32);
return new Promise<void>((resolve, reject) => {
crypto.randomFill(view, (err: Error | null) => {
try {
expect(err).to.be.null;
expect(full[0]).to.equal(42);
expect(full[99]).to.equal(42);
expect(full[132]).to.equal(42);
expect(full[1023]).to.equal(42);
const viewStillAll42 = view.every(b => b === 42);
expect(viewStillAll42).to.be.false;
resolve();
} catch (e) {
reject(e);
}
});
});
},
);