1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24const fixtures = require('../common/fixtures'); 25const assert = require('assert'); 26const fs = require('fs'); 27const filepath = fixtures.path('x.txt'); 28const fd = fs.openSync(filepath, 'r'); 29 30const expected = Buffer.from('xyz\n'); 31 32function test(bufferAsync, bufferSync, expected) { 33 fs.read(fd, 34 bufferAsync, 35 0, 36 expected.length, 37 0, 38 common.mustCall((err, bytesRead) => { 39 assert.ifError(err); 40 assert.strictEqual(bytesRead, expected.length); 41 assert.deepStrictEqual(bufferAsync, expected); 42 })); 43 44 const r = fs.readSync(fd, bufferSync, 0, expected.length, 0); 45 assert.deepStrictEqual(bufferSync, expected); 46 assert.strictEqual(r, expected.length); 47} 48 49test(Buffer.allocUnsafe(expected.length), 50 Buffer.allocUnsafe(expected.length), 51 expected); 52 53test(new Uint8Array(expected.length), 54 new Uint8Array(expected.length), 55 Uint8Array.from(expected)); 56 57{ 58 // Reading beyond file length (3 in this case) should return no data. 59 // This is a test for a bug where reads > uint32 would return data 60 // from the current position in the file. 61 const pos = 0xffffffff + 1; // max-uint32 + 1 62 const nRead = fs.readSync(fd, Buffer.alloc(1), 0, 1, pos); 63 assert.strictEqual(nRead, 0); 64 65 fs.read(fd, Buffer.alloc(1), 0, 1, pos, common.mustCall((err, nRead) => { 66 assert.ifError(err); 67 assert.strictEqual(nRead, 0); 68 })); 69} 70 71assert.throws(() => new fs.Dir(), { 72 code: 'ERR_MISSING_ARGS', 73}); 74 75assert.throws( 76 () => fs.read(fd, Buffer.alloc(1), 0, 1, 0), 77 { 78 message: 'Callback must be a function. Received undefined', 79 code: 'ERR_INVALID_CALLBACK', 80 } 81); 82 83['buffer', 'offset', 'length'].forEach((option) => 84 assert.throws( 85 () => fs.read(fd, { 86 [option]: null 87 }), 88 `not throws when options.${option} is null` 89 )); 90 91assert.throws( 92 () => fs.read(null, Buffer.alloc(1), 0, 1, 0), 93 { 94 message: 'The "fd" argument must be of type number. Received null', 95 code: 'ERR_INVALID_ARG_TYPE', 96 } 97); 98