11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures'); 251cb0ef41Sopenharmony_ciconst assert = require('assert'); 261cb0ef41Sopenharmony_ciconst fs = require('fs'); 271cb0ef41Sopenharmony_ciconst filepath = fixtures.path('x.txt'); 281cb0ef41Sopenharmony_ciconst fd = fs.openSync(filepath, 'r'); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciconst expected = Buffer.from('xyz\n'); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_cifunction test(bufferAsync, bufferSync, expected) { 331cb0ef41Sopenharmony_ci fs.read(fd, 341cb0ef41Sopenharmony_ci bufferAsync, 351cb0ef41Sopenharmony_ci 0, 361cb0ef41Sopenharmony_ci expected.length, 371cb0ef41Sopenharmony_ci 0, 381cb0ef41Sopenharmony_ci common.mustSucceed((bytesRead) => { 391cb0ef41Sopenharmony_ci assert.strictEqual(bytesRead, expected.length); 401cb0ef41Sopenharmony_ci assert.deepStrictEqual(bufferAsync, expected); 411cb0ef41Sopenharmony_ci })); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci const r = fs.readSync(fd, bufferSync, 0, expected.length, 0); 441cb0ef41Sopenharmony_ci assert.deepStrictEqual(bufferSync, expected); 451cb0ef41Sopenharmony_ci assert.strictEqual(r, expected.length); 461cb0ef41Sopenharmony_ci} 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_citest(Buffer.allocUnsafe(expected.length), 491cb0ef41Sopenharmony_ci Buffer.allocUnsafe(expected.length), 501cb0ef41Sopenharmony_ci expected); 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_citest(new Uint8Array(expected.length), 531cb0ef41Sopenharmony_ci new Uint8Array(expected.length), 541cb0ef41Sopenharmony_ci Uint8Array.from(expected)); 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci{ 571cb0ef41Sopenharmony_ci // Reading beyond file length (3 in this case) should return no data. 581cb0ef41Sopenharmony_ci // This is a test for a bug where reads > uint32 would return data 591cb0ef41Sopenharmony_ci // from the current position in the file. 601cb0ef41Sopenharmony_ci const pos = 0xffffffff + 1; // max-uint32 + 1 611cb0ef41Sopenharmony_ci const nRead = fs.readSync(fd, Buffer.alloc(1), 0, 1, pos); 621cb0ef41Sopenharmony_ci assert.strictEqual(nRead, 0); 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci fs.read(fd, Buffer.alloc(1), 0, 1, pos, common.mustSucceed((nRead) => { 651cb0ef41Sopenharmony_ci assert.strictEqual(nRead, 0); 661cb0ef41Sopenharmony_ci })); 671cb0ef41Sopenharmony_ci} 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ciassert.throws(() => new fs.Dir(), { 701cb0ef41Sopenharmony_ci code: 'ERR_MISSING_ARGS', 711cb0ef41Sopenharmony_ci}); 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ciassert.throws( 741cb0ef41Sopenharmony_ci () => fs.read(fd, Buffer.alloc(1), 0, 1, 0), 751cb0ef41Sopenharmony_ci { 761cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 771cb0ef41Sopenharmony_ci } 781cb0ef41Sopenharmony_ci); 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ciassert.throws( 811cb0ef41Sopenharmony_ci () => fs.read(fd, { buffer: null }, common.mustNotCall()), 821cb0ef41Sopenharmony_ci /TypeError: Cannot read properties of null \(reading 'byteLength'\)/, 831cb0ef41Sopenharmony_ci 'throws when options.buffer is null' 841cb0ef41Sopenharmony_ci); 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ciassert.throws( 871cb0ef41Sopenharmony_ci () => fs.readSync(fd, { buffer: null }), 881cb0ef41Sopenharmony_ci { 891cb0ef41Sopenharmony_ci name: 'TypeError', 901cb0ef41Sopenharmony_ci message: 'The "buffer" argument must be an instance of Buffer, ' + 911cb0ef41Sopenharmony_ci 'TypedArray, or DataView. Received an instance of Object', 921cb0ef41Sopenharmony_ci }, 931cb0ef41Sopenharmony_ci 'throws when options.buffer is null' 941cb0ef41Sopenharmony_ci); 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ciassert.throws( 971cb0ef41Sopenharmony_ci () => fs.read(null, Buffer.alloc(1), 0, 1, 0), 981cb0ef41Sopenharmony_ci { 991cb0ef41Sopenharmony_ci message: 'The "fd" argument must be of type number. Received null', 1001cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 1011cb0ef41Sopenharmony_ci } 1021cb0ef41Sopenharmony_ci); 103