|
| 1 | +// Flags: --expose-internals |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | + |
| 6 | +const assert = require('assert'); |
| 7 | +const { |
| 8 | + validateOffsetLengthRead, |
| 9 | + validateOffsetLengthWrite, |
| 10 | +} = require('internal/fs/utils'); |
| 11 | + |
| 12 | +{ |
| 13 | + const offset = -1; |
| 14 | + assert.throws( |
| 15 | + () => validateOffsetLengthRead(offset, 0, 0), |
| 16 | + common.expectsError({ |
| 17 | + code: 'ERR_OUT_OF_RANGE', |
| 18 | + name: 'RangeError', |
| 19 | + message: 'The value of "offset" is out of range. ' + |
| 20 | + `It must be >= 0. Received ${offset}` |
| 21 | + }) |
| 22 | + ); |
| 23 | +} |
| 24 | + |
| 25 | +{ |
| 26 | + const length = -1; |
| 27 | + assert.throws( |
| 28 | + () => validateOffsetLengthRead(0, length, 0), |
| 29 | + common.expectsError({ |
| 30 | + code: 'ERR_OUT_OF_RANGE', |
| 31 | + name: 'RangeError', |
| 32 | + message: 'The value of "length" is out of range. ' + |
| 33 | + `It must be >= 0. Received ${length}` |
| 34 | + }) |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +{ |
| 39 | + const offset = 1; |
| 40 | + const length = 1; |
| 41 | + const byteLength = offset + length - 1; |
| 42 | + assert.throws( |
| 43 | + () => validateOffsetLengthRead(offset, length, byteLength), |
| 44 | + common.expectsError({ |
| 45 | + code: 'ERR_OUT_OF_RANGE', |
| 46 | + name: 'RangeError', |
| 47 | + message: 'The value of "length" is out of range. ' + |
| 48 | + `It must be <= ${byteLength - offset}. Received ${length}` |
| 49 | + }) |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +// Most platforms don't allow reads or writes >= 2 GB. |
| 54 | +// See https://github.com/libuv/libuv/pull/1501. |
| 55 | +const kIoMaxLength = 2 ** 31 - 1; |
| 56 | + |
| 57 | +// RangeError when offset > byteLength |
| 58 | +{ |
| 59 | + const offset = 100; |
| 60 | + const length = 100; |
| 61 | + const byteLength = 50; |
| 62 | + assert.throws( |
| 63 | + () => validateOffsetLengthWrite(offset, length, byteLength), |
| 64 | + common.expectsError({ |
| 65 | + code: 'ERR_OUT_OF_RANGE', |
| 66 | + name: 'RangeError', |
| 67 | + message: 'The value of "offset" is out of range. ' + |
| 68 | + `It must be <= ${byteLength}. Received ${offset}` |
| 69 | + }) |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +// RangeError when byteLength < kIoMaxLength, and length > byteLength - offset. |
| 74 | +{ |
| 75 | + const offset = kIoMaxLength - 150; |
| 76 | + const length = 200; |
| 77 | + const byteLength = kIoMaxLength - 100; |
| 78 | + assert.throws( |
| 79 | + () => validateOffsetLengthWrite(offset, length, byteLength), |
| 80 | + common.expectsError({ |
| 81 | + code: 'ERR_OUT_OF_RANGE', |
| 82 | + name: 'RangeError', |
| 83 | + message: 'The value of "length" is out of range. ' + |
| 84 | + `It must be <= ${byteLength - offset}. Received ${length}` |
| 85 | + }) |
| 86 | + ); |
| 87 | +} |
0 commit comments