Skip to content
Closed
Changes from 1 commit
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
Next Next commit
test: add some test cases for validateOffsetLengthWrite.
  • Loading branch information
kakts committed Jun 7, 2018
commit 36588a2c00cbd8ba13645554d6ee2b55eff58866
56 changes: 56 additions & 0 deletions test/parallel/test-fs-util-validateoffsetlengthwrite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Flags: --expose-internals

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: unnecessary blank line here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I'll delete it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@jasnell I fixed it.

'use strict';

const common = require('../common');

const { validateOffsetLengthWrite } = require('internal/fs/utils');
const { kMaxLength } = require('buffer');

// RangeError when offset > byteLength
{
const offset = 100;
const length = 100;
const byteLength = 50;
common.expectsError(
() => validateOffsetLengthWrite(offset, length, byteLength),
{
code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: 'The value of "offset" is out of range. ' +
`It must be <= ${byteLength}. Received ${offset}`
}
);
}

// RangeError when byteLength > kMaxLength, and length > kMaxLength - offset .
{
const offset = kMaxLength;
const length = 100;
const byteLength = kMaxLength + 1;
common.expectsError(
() => validateOffsetLengthWrite(offset, length, byteLength),
{
code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: 'The value of "length" is out of range. ' +
`It must be <= ${kMaxLength - offset}. Received ${length}`
}
);
}

// RangeError when byteLength < kMaxLength, and length > byteLength - offset .
{
const offset = kMaxLength - 150;
const length = 200;
const byteLength = kMaxLength - 100;
common.expectsError(
() => validateOffsetLengthWrite(offset, length, byteLength),
{
code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: 'The value of "length" is out of range. ' +
`It must be <= ${byteLength - offset}. Received ${length}`
}
);
}