Skip to content

Commit d82a478

Browse files
kyungrae2002aduh95
authored andcommitted
test,doc: cover and document multi-byte offset/size in randomFill
Signed-off-by: kyungrae <kyungrae2002@gmail.com> PR-URL: #64834 Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
1 parent 85ce2b6 commit d82a478

3 files changed

Lines changed: 62 additions & 7 deletions

File tree

doc/api/crypto.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5608,9 +5608,12 @@ changes:
56085608

56095609
* `buffer` {ArrayBuffer|Buffer|TypedArray|DataView} Must be supplied. The
56105610
size of the provided `buffer` must not be larger than `2**31 - 1`.
5611-
* `offset` {number} **Default:** `0`
5612-
* `size` {number} **Default:** `buffer.length - offset`. The `size` must
5613-
not be larger than `2**31 - 1`.
5611+
* `offset` {number} The start position, in elements for a `TypedArray` and in
5612+
bytes for an `ArrayBuffer` or `DataView`. **Default:** `0`
5613+
* `size` {number} The amount to fill, in the same units as `offset`.
5614+
**Default:** `buffer.length - offset` for a `TypedArray`, or
5615+
`buffer.byteLength - offset` for an `ArrayBuffer` or `DataView`. The `size`
5616+
must not be larger than `2**31 - 1`.
56145617
* `callback` {Function} `function(err, buf) {}`.
56155618

56165619
This function is similar to [`crypto.randomBytes()`][] but requires the first
@@ -5745,9 +5748,12 @@ changes:
57455748

57465749
* `buffer` {ArrayBuffer|Buffer|TypedArray|DataView} Must be supplied. The
57475750
size of the provided `buffer` must not be larger than `2**31 - 1`.
5748-
* `offset` {number} **Default:** `0`
5749-
* `size` {number} **Default:** `buffer.length - offset`. The `size` must
5750-
not be larger than `2**31 - 1`.
5751+
* `offset` {number} The start position, in elements for a `TypedArray` and in
5752+
bytes for an `ArrayBuffer` or `DataView`. **Default:** `0`
5753+
* `size` {number} The amount to fill, in the same units as `offset`.
5754+
**Default:** `buffer.length - offset` for a `TypedArray`, or
5755+
`buffer.byteLength - offset` for an `ArrayBuffer` or `DataView`. The `size`
5756+
must not be larger than `2**31 - 1`.
57515757
* Returns: {ArrayBuffer|Buffer|TypedArray|DataView} The object passed as
57525758
`buffer` argument.
57535759

lib/internal/crypto/random.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ function randomFill(buf, offset, size, callback) {
168168
size = buf.length;
169169
} else if (typeof size === 'function') {
170170
callback = size;
171-
size = buf.length - offset;
171+
size = (buf.length ?? buf.byteLength) - offset;
172172
} else {
173173
validateFunction(callback, 'callback');
174174
}

test/parallel/test-crypto-random.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,55 @@ common.expectWarning('DeprecationWarning',
218218
}));
219219
}
220220

221+
{
222+
const buf = new Uint16Array(10);
223+
const before = Buffer.from(buf.buffer).toString('hex');
224+
crypto.randomFillSync(buf, 1, 8);
225+
const after = Buffer.from(buf.buffer).toString('hex');
226+
assert.notStrictEqual(before, after);
227+
assert.deepStrictEqual(before.slice(0, 4), after.slice(0, 4));
228+
assert.deepStrictEqual(before.slice(-4), after.slice(-4));
229+
}
230+
231+
{
232+
const buf = new Uint32Array(10);
233+
const before = Buffer.from(buf.buffer).toString('hex');
234+
crypto.randomFillSync(buf, 1, 8);
235+
const after = Buffer.from(buf.buffer).toString('hex');
236+
assert.notStrictEqual(before, after);
237+
assert.deepStrictEqual(before.slice(0, 8), after.slice(0, 8));
238+
assert.deepStrictEqual(before.slice(-8), after.slice(-8));
239+
}
240+
241+
{
242+
const buf = new Uint16Array(10);
243+
const before = Buffer.from(buf.buffer).toString('hex');
244+
crypto.randomFill(buf, 1, 8, common.mustSucceed((buf) => {
245+
const after = Buffer.from(buf.buffer).toString('hex');
246+
assert.notStrictEqual(before, after);
247+
assert.deepStrictEqual(before.slice(0, 4), after.slice(0, 4));
248+
assert.deepStrictEqual(before.slice(-4), after.slice(-4));
249+
}));
250+
}
251+
252+
{
253+
const buf = new Uint32Array(10);
254+
const before = Buffer.from(buf.buffer).toString('hex');
255+
crypto.randomFill(buf, 1, 8, common.mustSucceed((buf) => {
256+
const after = Buffer.from(buf.buffer).toString('hex');
257+
assert.notStrictEqual(before, after);
258+
assert.deepStrictEqual(before.slice(0, 8), after.slice(0, 8));
259+
assert.deepStrictEqual(before.slice(-8), after.slice(-8));
260+
}));
261+
}
262+
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+
221270
{
222271
[
223272
Buffer.alloc(10),

0 commit comments

Comments
 (0)