Skip to content

Commit e718a6e

Browse files
LiviaMedeirostargos
authored andcommitted
test: fix Buffer.from(ArrayBufferView) call
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>
1 parent 953fefe commit e718a6e

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

test/parallel/test-buffer-alloc.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,25 @@ assert.strictEqual(d.length, 0);
4141
assert.strictEqual(b.offset, 0);
4242
}
4343

44+
// Test creating a Buffer from a Uint8Array
45+
{
46+
const ui8 = new Uint8Array(4).fill(42);
47+
const e = Buffer.from(ui8);
48+
for (const [index, value] of e.entries()) {
49+
assert.strictEqual(value, ui8[index]);
50+
}
51+
}
52+
// Test creating a Buffer from a Uint8Array (old constructor)
53+
{
54+
const ui8 = new Uint8Array(4).fill(42);
55+
const e = Buffer(ui8);
56+
for (const [key, value] of e.entries()) {
57+
assert.strictEqual(value, ui8[key]);
58+
}
59+
}
60+
4461
// Test creating a Buffer from a Uint32Array
62+
// Note: it is implicitly interpreted as Array of integers modulo 256
4563
{
4664
const ui32 = new Uint32Array(4).fill(42);
4765
const e = Buffer.from(ui32);
@@ -50,11 +68,12 @@ assert.strictEqual(d.length, 0);
5068
}
5169
}
5270
// Test creating a Buffer from a Uint32Array (old constructor)
71+
// Note: it is implicitly interpreted as Array of integers modulo 256
5372
{
5473
const ui32 = new Uint32Array(4).fill(42);
5574
const e = Buffer(ui32);
5675
for (const [key, value] of e.entries()) {
57-
assert.deepStrictEqual(value, ui32[key]);
76+
assert.strictEqual(value, ui32[key]);
5877
}
5978
}
6079

test/parallel/test-webcrypto-random.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ for (const ctor of intTypedConstructors) {
4747
}
4848

4949
{
50-
const buf = new Uint16Array(10);
51-
const before = Buffer.from(buf).toString('hex');
50+
const buf = Buffer.alloc(10);
51+
const before = buf.toString('hex');
5252
webcrypto.getRandomValues(buf);
53-
const after = Buffer.from(buf).toString('hex');
53+
const after = buf.toString('hex');
5454
assert.notStrictEqual(before, after);
5555
}
5656

0 commit comments

Comments
 (0)