Skip to content

Commit

Permalink
test: add some test cases for validateOffsetLengthWrite
Browse files Browse the repository at this point in the history
PR-URL: #21195
Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Weijia Wang <starkwang@126.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
  • Loading branch information
kakts authored and targos committed Jun 28, 2018
1 parent 511d610 commit 9f2bf3c
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions test/parallel/test-fs-util-validateoffsetlengthwrite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Flags: --expose-internals
'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}`
}
);
}

0 comments on commit 9f2bf3c

Please sign in to comment.