Skip to content

Commit

Permalink
test: fix Buffer.from(ArrayBufferView) call
Browse files Browse the repository at this point in the history
PR-URL: #43614
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
LiviaMedeiros authored and targos committed Jul 31, 2022
1 parent 33a2902 commit 4d8daae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
21 changes: 20 additions & 1 deletion test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,25 @@ assert.strictEqual(d.length, 0);
assert.strictEqual(b.offset, 0);
}

// Test creating a Buffer from a Uint8Array
{
const ui8 = new Uint8Array(4).fill(42);
const e = Buffer.from(ui8);
for (const [index, value] of e.entries()) {
assert.strictEqual(value, ui8[index]);
}
}
// Test creating a Buffer from a Uint8Array (old constructor)
{
const ui8 = new Uint8Array(4).fill(42);
const e = Buffer(ui8);
for (const [key, value] of e.entries()) {
assert.strictEqual(value, ui8[key]);
}
}

// Test creating a Buffer from a Uint32Array
// Note: it is implicitly interpreted as Array of integers modulo 256
{
const ui32 = new Uint32Array(4).fill(42);
const e = Buffer.from(ui32);
Expand All @@ -50,11 +68,12 @@ assert.strictEqual(d.length, 0);
}
}
// Test creating a Buffer from a Uint32Array (old constructor)
// Note: it is implicitly interpreted as Array of integers modulo 256
{
const ui32 = new Uint32Array(4).fill(42);
const e = Buffer(ui32);
for (const [key, value] of e.entries()) {
assert.deepStrictEqual(value, ui32[key]);
assert.strictEqual(value, ui32[key]);
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-webcrypto-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ for (const ctor of intTypedConstructors) {
}

{
const buf = new Uint16Array(10);
const before = Buffer.from(buf).toString('hex');
const buf = Buffer.alloc(10);
const before = buf.toString('hex');
webcrypto.getRandomValues(buf);
const after = Buffer.from(buf).toString('hex');
const after = buf.toString('hex');
assert.notStrictEqual(before, after);
}

Expand Down

0 comments on commit 4d8daae

Please sign in to comment.