-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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: consolidating buffer.read() #11297
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
// testing basic buffer read functions | ||
const buf = Buffer.from([0xa4, 0xfd, 0x48, 0xea, 0xcf, 0xff, 0xd9, 0x01, 0xde]); | ||
|
||
function read(buff, funx, args, expected) { | ||
|
||
assert.strictEqual(buff[funx](...args), expected); | ||
assert.throws( | ||
() => buff[funx](-1), | ||
/^RangeError: Index out of range$/ | ||
); | ||
|
||
assert.doesNotThrow( | ||
() => assert.strictEqual(buff[funx](...args, true), expected), | ||
'noAssert does not change return value for valid ranges' | ||
); | ||
|
||
} | ||
|
||
// testing basic functionality of readDoubleBE() and readDOubleLE() | ||
read(buf, 'readDoubleBE', [1], -3.1827727774563287e+295); | ||
read(buf, 'readDoubleLE', [1], -6.966010051009108e+144); | ||
|
||
// testing basic functionality of readFLoatBE() and readFloatLE() | ||
read(buf, 'readFloatBE', [1], -1.6691549692541768e+37); | ||
read(buf, 'readFloatLE', [1], -7861303808); | ||
|
||
// testing basic functionality of readInt8() | ||
read(buf, 'readInt8', [1], -3); | ||
|
||
// testing basic functionality of readInt16BE() and readInt16LE() | ||
read(buf, 'readInt16BE', [1], -696); | ||
read(buf, 'readInt16LE', [1], 0x48fd); | ||
|
||
// testing basic functionality of readInt32BE() and readInt32LE() | ||
read(buf, 'readInt32BE', [1], -45552945); | ||
read(buf, 'readInt32LE', [1], -806729475); | ||
|
||
// testing basic functionality of readIntBE() and readIntLE() | ||
read(buf, 'readIntBE', [1, 1], -3); | ||
read(buf, 'readIntLE', [2, 1], 0x48); | ||
|
||
// testing basic functionality of readUInt8() | ||
read(buf, 'readUInt8', [1], 0xfd); | ||
|
||
// testing basic functionality of readUInt16BE() and readUInt16LE() | ||
read(buf, 'readUInt16BE', [2], 0x48ea); | ||
read(buf, 'readUInt16LE', [2], 0xea48); | ||
|
||
// testing basic functionality of readUInt32BE() and readUInt32LE() | ||
read(buf, 'readUInt32BE', [1], 0xfd48eacf); | ||
read(buf, 'readUInt32LE', [1], 0xcfea48fd); | ||
|
||
// testing basic functionality of readUIntBE() and readUIntLE() | ||
read(buf, 'readUIntBE', [2, 0], 0xfd); | ||
read(buf, 'readUIntLE', [2, 0], 0x48); | ||
|
||
// attempt to overflow buffers, similar to previous bug in array buffers | ||
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(0xffffffff), | ||
RangeError); | ||
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(0xffffffff), | ||
RangeError); | ||
|
||
// ensure negative values can't get past offset | ||
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError); | ||
assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError); | ||
|
||
// offset checks | ||
{ | ||
const buf = Buffer.allocUnsafe(0); | ||
|
||
assert.throws(() => buf.readUInt8(0), RangeError); | ||
assert.throws(() => buf.readInt8(0), RangeError); | ||
} | ||
|
||
{ | ||
const buf = Buffer.from([0xFF]); | ||
|
||
assert.strictEqual(buf.readUInt8(0), 255); | ||
assert.strictEqual(buf.readInt8(0), -1); | ||
} | ||
|
||
[16, 32].forEach((bits) => { | ||
const buf = Buffer.allocUnsafe(bits / 8 - 1); | ||
|
||
assert.throws(() => buf[`readUInt${bits}BE`](0), | ||
RangeError, | ||
`readUInt${bits}BE()`); | ||
|
||
assert.throws(() => buf[`readUInt${bits}LE`](0), | ||
RangeError, | ||
`readUInt${bits}LE()`); | ||
|
||
assert.throws(() => buf[`readInt${bits}BE`](0), | ||
RangeError, | ||
`readInt${bits}BE()`); | ||
|
||
assert.throws(() => buf[`readInt${bits}LE`](0), | ||
RangeError, | ||
`readInt${bits}LE()`); | ||
}); | ||
|
||
[16, 32].forEach((bits) => { | ||
const buf = Buffer.from([0xFF, 0xFF, 0xFF, 0xFF]); | ||
|
||
assert.strictEqual(buf[`readUInt${bits}BE`](0), | ||
(0xFFFFFFFF >>> (32 - bits))); | ||
|
||
assert.strictEqual(buf[`readUInt${bits}LE`](0), | ||
(0xFFFFFFFF >>> (32 - bits))); | ||
|
||
assert.strictEqual(buf[`readInt${bits}BE`](0), | ||
(0xFFFFFFFF >> (32 - bits))); | ||
|
||
assert.strictEqual(buf[`readInt${bits}LE`](0), | ||
(0xFFFFFFFF >> (32 - bits))); | ||
}); | ||
|
||
// test for common read(U)IntLE/BE | ||
{ | ||
const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]); | ||
|
||
assert.strictEqual(buf.readUIntLE(0, 1), 0x01); | ||
assert.strictEqual(buf.readUIntBE(0, 1), 0x01); | ||
assert.strictEqual(buf.readUIntLE(0, 3), 0x030201); | ||
assert.strictEqual(buf.readUIntBE(0, 3), 0x010203); | ||
assert.strictEqual(buf.readUIntLE(0, 5), 0x0504030201); | ||
assert.strictEqual(buf.readUIntBE(0, 5), 0x0102030405); | ||
assert.strictEqual(buf.readUIntLE(0, 6), 0x060504030201); | ||
assert.strictEqual(buf.readUIntBE(0, 6), 0x010203040506); | ||
assert.strictEqual(buf.readIntLE(0, 1), 0x01); | ||
assert.strictEqual(buf.readIntBE(0, 1), 0x01); | ||
assert.strictEqual(buf.readIntLE(0, 3), 0x030201); | ||
assert.strictEqual(buf.readIntBE(0, 3), 0x010203); | ||
assert.strictEqual(buf.readIntLE(0, 5), 0x0504030201); | ||
assert.strictEqual(buf.readIntBE(0, 5), 0x0102030405); | ||
assert.strictEqual(buf.readIntLE(0, 6), 0x060504030201); | ||
assert.strictEqual(buf.readIntBE(0, 6), 0x010203040506); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we check the actual errors itself, so that we can avoid problems like #11162?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm in favor of this, but since this PR is a straightforward re-organization of the tests and not actual changes/enhancements, I'd be OK with that happening in a subsequent PR.