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

Conversation

LiviaMedeiros
Copy link
Contributor

No description provided.

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. test Issues and PRs related to the tests. labels Jun 29, 2022
@LiviaMedeiros LiviaMedeiros added the request-ci Add this label to start a Jenkins CI on a PR. label Jun 29, 2022
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 29, 2022
@nodejs-github-bot

This comment was marked as outdated.

@@ -42,6 +42,7 @@ assert.strictEqual(d.length, 0);
}

// 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.

test/parallel/test-buffer-alloc.js Outdated Show resolved Hide resolved
@nodejs-github-bot

This comment was marked as outdated.

@@ -47,7 +47,7 @@ for (const ctor of intTypedConstructors) {
}

{
const buf = new Uint16Array(10);
const buf = new Uint8Array(10);
Copy link
Member

Choose a reason for hiding this comment

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

This test looks like it might be using Uint16Array intentionally. There are a couple of ways to work around this issue (for example, use buf.toString() instead of Buffer.from(buf).toString('hex') or use Buffer.from(buf.buffer)), but it seems to me that this test is performing the exact same test as the loop above for ctor === Uint16Array. So I'd suggest removing this block (line 50 to 56) entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if I understand a potential intention here. Using Uint16Array in context of this method makes sense in terms of generating random utf16 strings (i.e. String.fromCharCode(...crypto.getRandomValues(new Uint16Array(charLength)))) but it's probably not worth testing explicitly.

Technically, this test was worse than ctor === Uint16Array above, as it tested only 10 bytes out of 20 generated. :)

I agree that at this point it doesn't serve any purpose. Documentation allows to use instances of Buffer and the test for it was lacking, so I guess Buffer.alloc(10) will make it useful.

@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot
Copy link
Collaborator

PR-URL: nodejs#43614
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
@LiviaMedeiros
Copy link
Contributor Author

Landed in 5c6eee4

targos pushed a commit that referenced this pull request Jul 12, 2022
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>
targos pushed a commit that referenced this pull request Jul 20, 2022
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>
targos pushed a commit that referenced this pull request Jul 31, 2022
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>
guangwong pushed a commit to noslate-project/node that referenced this pull request Oct 10, 2022
PR-URL: nodejs/node#43614
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run. test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants