From 9f2bf3c1b38afe6d78d870793e83cabc8bbc5277 Mon Sep 17 00:00:00 2001 From: Keita Akutsu Date: Fri, 8 Jun 2018 01:30:58 +0900 Subject: [PATCH] test: add some test cases for validateOffsetLengthWrite PR-URL: https://github.com/nodejs/node/pull/21195 Reviewed-By: Ujjwal Sharma Reviewed-By: Trivikram Kamat Reviewed-By: Weijia Wang Reviewed-By: James M Snell Reviewed-By: Anatoli Papirovski --- .../test-fs-util-validateoffsetlengthwrite.js | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 test/parallel/test-fs-util-validateoffsetlengthwrite.js diff --git a/test/parallel/test-fs-util-validateoffsetlengthwrite.js b/test/parallel/test-fs-util-validateoffsetlengthwrite.js new file mode 100644 index 00000000000000..9aca956bd505bf --- /dev/null +++ b/test/parallel/test-fs-util-validateoffsetlengthwrite.js @@ -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}` + } + ); +}