|
| 1 | +// Flags: --expose-internals |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | + |
| 6 | +const { validateOffsetLengthWrite } = require('internal/fs/utils'); |
| 7 | +const { kMaxLength } = require('buffer'); |
| 8 | + |
| 9 | +// RangeError when offset > byteLength |
| 10 | +{ |
| 11 | + const offset = 100; |
| 12 | + const length = 100; |
| 13 | + const byteLength = 50; |
| 14 | + common.expectsError( |
| 15 | + () => validateOffsetLengthWrite(offset, length, byteLength), |
| 16 | + { |
| 17 | + code: 'ERR_OUT_OF_RANGE', |
| 18 | + type: RangeError, |
| 19 | + message: 'The value of "offset" is out of range. ' + |
| 20 | + `It must be <= ${byteLength}. Received ${offset}` |
| 21 | + } |
| 22 | + ); |
| 23 | +} |
| 24 | + |
| 25 | +// RangeError when byteLength > kMaxLength, and length > kMaxLength - offset . |
| 26 | +{ |
| 27 | + const offset = kMaxLength; |
| 28 | + const length = 100; |
| 29 | + const byteLength = kMaxLength + 1; |
| 30 | + common.expectsError( |
| 31 | + () => validateOffsetLengthWrite(offset, length, byteLength), |
| 32 | + { |
| 33 | + code: 'ERR_OUT_OF_RANGE', |
| 34 | + type: RangeError, |
| 35 | + message: 'The value of "length" is out of range. ' + |
| 36 | + `It must be <= ${kMaxLength - offset}. Received ${length}` |
| 37 | + } |
| 38 | + ); |
| 39 | +} |
| 40 | + |
| 41 | +// RangeError when byteLength < kMaxLength, and length > byteLength - offset . |
| 42 | +{ |
| 43 | + const offset = kMaxLength - 150; |
| 44 | + const length = 200; |
| 45 | + const byteLength = kMaxLength - 100; |
| 46 | + common.expectsError( |
| 47 | + () => validateOffsetLengthWrite(offset, length, byteLength), |
| 48 | + { |
| 49 | + code: 'ERR_OUT_OF_RANGE', |
| 50 | + type: RangeError, |
| 51 | + message: 'The value of "length" is out of range. ' + |
| 52 | + `It must be <= ${byteLength - offset}. Received ${length}` |
| 53 | + } |
| 54 | + ); |
| 55 | +} |
0 commit comments