Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix Buffer.from(ArrayBufferView) call #43614

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, that's not the behavior I'd have expected...

Copy link
Contributor Author

@LiviaMedeiros LiviaMedeiros Jun 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer.from() is not defined by documentation for any ArrayBufferView except for Uint8Array and Buffer.

I agree that it's not what anyone would expect. There were related bugs in node and undici, and who knows how many similar bugs in ecosystem.

Not sure if this test was intentional or not, so probably it shouldn't be removed for now, just marked with a comment.
I'll add uint8-based test in case this is what was intended.

{
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