• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.mustSucceed((bytesRead) => {
39            assert.strictEqual(bytesRead, expected.length);
40            assert.deepStrictEqual(bufferAsync, expected);
41          }));
42
43  const r = fs.readSync(fd, bufferSync, 0, expected.length, 0);
44  assert.deepStrictEqual(bufferSync, expected);
45  assert.strictEqual(r, expected.length);
46}
47
48test(Buffer.allocUnsafe(expected.length),
49     Buffer.allocUnsafe(expected.length),
50     expected);
51
52test(new Uint8Array(expected.length),
53     new Uint8Array(expected.length),
54     Uint8Array.from(expected));
55
56{
57  // Reading beyond file length (3 in this case) should return no data.
58  // This is a test for a bug where reads > uint32 would return data
59  // from the current position in the file.
60  const pos = 0xffffffff + 1; // max-uint32 + 1
61  const nRead = fs.readSync(fd, Buffer.alloc(1), 0, 1, pos);
62  assert.strictEqual(nRead, 0);
63
64  fs.read(fd, Buffer.alloc(1), 0, 1, pos, common.mustSucceed((nRead) => {
65    assert.strictEqual(nRead, 0);
66  }));
67}
68
69assert.throws(() => new fs.Dir(), {
70  code: 'ERR_MISSING_ARGS',
71});
72
73assert.throws(
74  () => fs.read(fd, Buffer.alloc(1), 0, 1, 0),
75  {
76    code: 'ERR_INVALID_ARG_TYPE',
77  }
78);
79
80assert.throws(
81  () => fs.read(fd, { buffer: null }, common.mustNotCall()),
82  /TypeError: Cannot read properties of null \(reading 'byteLength'\)/,
83  'throws when options.buffer is null'
84);
85
86assert.throws(
87  () => fs.readSync(fd, { buffer: null }),
88  {
89    name: 'TypeError',
90    message: 'The "buffer" argument must be an instance of Buffer, ' +
91    'TypedArray, or DataView. Received an instance of Object',
92  },
93  'throws when options.buffer is null'
94);
95
96assert.throws(
97  () => fs.read(null, Buffer.alloc(1), 0, 1, 0),
98  {
99    message: 'The "fd" argument must be of type number. Received null',
100    code: 'ERR_INVALID_ARG_TYPE',
101  }
102);
103