11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst {
81cb0ef41Sopenharmony_ci  validateOffsetLengthRead,
91cb0ef41Sopenharmony_ci  validateOffsetLengthWrite,
101cb0ef41Sopenharmony_ci} = require('internal/fs/utils');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci{
131cb0ef41Sopenharmony_ci  const offset = -1;
141cb0ef41Sopenharmony_ci  assert.throws(
151cb0ef41Sopenharmony_ci    () => validateOffsetLengthRead(offset, 0, 0),
161cb0ef41Sopenharmony_ci    common.expectsError({
171cb0ef41Sopenharmony_ci      code: 'ERR_OUT_OF_RANGE',
181cb0ef41Sopenharmony_ci      name: 'RangeError',
191cb0ef41Sopenharmony_ci      message: 'The value of "offset" is out of range. ' +
201cb0ef41Sopenharmony_ci                 `It must be >= 0. Received ${offset}`
211cb0ef41Sopenharmony_ci    })
221cb0ef41Sopenharmony_ci  );
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci{
261cb0ef41Sopenharmony_ci  const length = -1;
271cb0ef41Sopenharmony_ci  assert.throws(
281cb0ef41Sopenharmony_ci    () => validateOffsetLengthRead(0, length, 0),
291cb0ef41Sopenharmony_ci    common.expectsError({
301cb0ef41Sopenharmony_ci      code: 'ERR_OUT_OF_RANGE',
311cb0ef41Sopenharmony_ci      name: 'RangeError',
321cb0ef41Sopenharmony_ci      message: 'The value of "length" is out of range. ' +
331cb0ef41Sopenharmony_ci                 `It must be >= 0. Received ${length}`
341cb0ef41Sopenharmony_ci    })
351cb0ef41Sopenharmony_ci  );
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci{
391cb0ef41Sopenharmony_ci  const offset = 1;
401cb0ef41Sopenharmony_ci  const length = 1;
411cb0ef41Sopenharmony_ci  const byteLength = offset + length - 1;
421cb0ef41Sopenharmony_ci  assert.throws(
431cb0ef41Sopenharmony_ci    () => validateOffsetLengthRead(offset, length, byteLength),
441cb0ef41Sopenharmony_ci    common.expectsError({
451cb0ef41Sopenharmony_ci      code: 'ERR_OUT_OF_RANGE',
461cb0ef41Sopenharmony_ci      name: 'RangeError',
471cb0ef41Sopenharmony_ci      message: 'The value of "length" is out of range. ' +
481cb0ef41Sopenharmony_ci                 `It must be <= ${byteLength - offset}. Received ${length}`
491cb0ef41Sopenharmony_ci    })
501cb0ef41Sopenharmony_ci  );
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci// Most platforms don't allow reads or writes >= 2 GiB.
541cb0ef41Sopenharmony_ci// See https://github.com/libuv/libuv/pull/1501.
551cb0ef41Sopenharmony_ciconst kIoMaxLength = 2 ** 31 - 1;
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci// RangeError when offset > byteLength
581cb0ef41Sopenharmony_ci{
591cb0ef41Sopenharmony_ci  const offset = 100;
601cb0ef41Sopenharmony_ci  const length = 100;
611cb0ef41Sopenharmony_ci  const byteLength = 50;
621cb0ef41Sopenharmony_ci  assert.throws(
631cb0ef41Sopenharmony_ci    () => validateOffsetLengthWrite(offset, length, byteLength),
641cb0ef41Sopenharmony_ci    common.expectsError({
651cb0ef41Sopenharmony_ci      code: 'ERR_OUT_OF_RANGE',
661cb0ef41Sopenharmony_ci      name: 'RangeError',
671cb0ef41Sopenharmony_ci      message: 'The value of "offset" is out of range. ' +
681cb0ef41Sopenharmony_ci               `It must be <= ${byteLength}. Received ${offset}`
691cb0ef41Sopenharmony_ci    })
701cb0ef41Sopenharmony_ci  );
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci// RangeError when byteLength < kIoMaxLength, and length > byteLength - offset.
741cb0ef41Sopenharmony_ci{
751cb0ef41Sopenharmony_ci  const offset = kIoMaxLength - 150;
761cb0ef41Sopenharmony_ci  const length = 200;
771cb0ef41Sopenharmony_ci  const byteLength = kIoMaxLength - 100;
781cb0ef41Sopenharmony_ci  assert.throws(
791cb0ef41Sopenharmony_ci    () => validateOffsetLengthWrite(offset, length, byteLength),
801cb0ef41Sopenharmony_ci    common.expectsError({
811cb0ef41Sopenharmony_ci      code: 'ERR_OUT_OF_RANGE',
821cb0ef41Sopenharmony_ci      name: 'RangeError',
831cb0ef41Sopenharmony_ci      message: 'The value of "length" is out of range. ' +
841cb0ef41Sopenharmony_ci               `It must be <= ${byteLength - offset}. Received ${length}`
851cb0ef41Sopenharmony_ci    })
861cb0ef41Sopenharmony_ci  );
871cb0ef41Sopenharmony_ci}
88