Skip to content

Commit fe7d6b1

Browse files
committed
test: make multi-byte randomFill tests flaky-safe
Signed-off-by: kyungrae <kyungrae2002@gmail.com>
1 parent 54310ec commit fe7d6b1

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

test/parallel/test-crypto-random.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,47 +219,54 @@ common.expectWarning('DeprecationWarning',
219219
}
220220

221221
{
222-
const buf = new Uint16Array(4);
222+
const buf = new Uint16Array(10);
223223
const before = Buffer.from(buf.buffer).toString('hex');
224-
crypto.randomFillSync(buf, 1, 1);
224+
crypto.randomFillSync(buf, 1, 8);
225225
const after = Buffer.from(buf.buffer).toString('hex');
226226
assert.notStrictEqual(before, after);
227227
assert.deepStrictEqual(before.slice(0, 4), after.slice(0, 4));
228-
assert.deepStrictEqual(before.slice(8), after.slice(8));
228+
assert.deepStrictEqual(before.slice(-4), after.slice(-4));
229229
}
230230

231231
{
232-
const buf = new Uint32Array(4);
232+
const buf = new Uint32Array(10);
233233
const before = Buffer.from(buf.buffer).toString('hex');
234-
crypto.randomFillSync(buf, 1, 1);
234+
crypto.randomFillSync(buf, 1, 8);
235235
const after = Buffer.from(buf.buffer).toString('hex');
236236
assert.notStrictEqual(before, after);
237237
assert.deepStrictEqual(before.slice(0, 8), after.slice(0, 8));
238-
assert.deepStrictEqual(before.slice(16), after.slice(16));
238+
assert.deepStrictEqual(before.slice(-8), after.slice(-8));
239239
}
240240

241241
{
242-
const buf = new Uint16Array(4);
242+
const buf = new Uint16Array(10);
243243
const before = Buffer.from(buf.buffer).toString('hex');
244-
crypto.randomFill(buf, 1, 1, common.mustSucceed((buf) => {
244+
crypto.randomFill(buf, 1, 8, common.mustSucceed((buf) => {
245245
const after = Buffer.from(buf.buffer).toString('hex');
246246
assert.notStrictEqual(before, after);
247247
assert.deepStrictEqual(before.slice(0, 4), after.slice(0, 4));
248-
assert.deepStrictEqual(before.slice(8), after.slice(8));
248+
assert.deepStrictEqual(before.slice(-4), after.slice(-4));
249249
}));
250250
}
251251

252252
{
253-
const buf = new Uint32Array(4);
253+
const buf = new Uint32Array(10);
254254
const before = Buffer.from(buf.buffer).toString('hex');
255-
crypto.randomFill(buf, 1, 1, common.mustSucceed((buf) => {
255+
crypto.randomFill(buf, 1, 8, common.mustSucceed((buf) => {
256256
const after = Buffer.from(buf.buffer).toString('hex');
257257
assert.notStrictEqual(before, after);
258258
assert.deepStrictEqual(before.slice(0, 8), after.slice(0, 8));
259-
assert.deepStrictEqual(before.slice(16), after.slice(16));
259+
assert.deepStrictEqual(before.slice(-8), after.slice(-8));
260260
}));
261261
}
262262

263+
{
264+
// randomFill() with an offset and no size must not throw for types
265+
// without a .length property, matching randomFillSync().
266+
crypto.randomFill(new ArrayBuffer(10), 2, common.mustSucceed());
267+
crypto.randomFill(new DataView(new ArrayBuffer(10)), 2, common.mustSucceed());
268+
}
269+
263270
{
264271
[
265272
Buffer.alloc(10),

0 commit comments

Comments
 (0)