• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const fs = require('fs');
4const assert = require('assert');
5const fixtures = require('../common/fixtures');
6
7const filepath = fixtures.path('x.txt');
8const fd = fs.openSync(filepath, 'r');
9const expected = 'xyz\n';
10
11
12// Error must be thrown with string
13assert.throws(
14  () => fs.read(fd, expected.length, 0, 'utf-8', common.mustNotCall()),
15  {
16    code: 'ERR_INVALID_ARG_TYPE',
17    name: 'TypeError',
18    message: 'The "buffer" argument must be an instance of Buffer, ' +
19             'TypedArray, or DataView. Received type number (4)'
20  }
21);
22
23[true, null, undefined, () => {}, {}].forEach((value) => {
24  assert.throws(() => {
25    fs.read(value,
26            Buffer.allocUnsafe(expected.length),
27            0,
28            expected.length,
29            0,
30            common.mustNotCall());
31  }, {
32    code: 'ERR_INVALID_ARG_TYPE',
33    name: 'TypeError'
34  });
35});
36
37assert.throws(() => {
38  fs.read(fd,
39          Buffer.allocUnsafe(expected.length),
40          -1,
41          expected.length,
42          0,
43          common.mustNotCall());
44}, {
45  code: 'ERR_OUT_OF_RANGE',
46  name: 'RangeError',
47});
48
49assert.throws(() => {
50  fs.read(fd,
51          Buffer.allocUnsafe(expected.length),
52          NaN,
53          expected.length,
54          0,
55          common.mustNotCall());
56}, {
57  code: 'ERR_OUT_OF_RANGE',
58  name: 'RangeError',
59  message: 'The value of "offset" is out of range. It must be an integer. ' +
60           'Received NaN'
61});
62
63assert.throws(() => {
64  fs.read(fd,
65          Buffer.allocUnsafe(expected.length),
66          0,
67          -1,
68          0,
69          common.mustNotCall());
70}, {
71  code: 'ERR_OUT_OF_RANGE',
72  name: 'RangeError',
73  message: 'The value of "length" is out of range. ' +
74           'It must be >= 0. Received -1'
75});
76
77[true, () => {}, {}, ''].forEach((value) => {
78  assert.throws(() => {
79    fs.read(fd,
80            Buffer.allocUnsafe(expected.length),
81            0,
82            expected.length,
83            value,
84            common.mustNotCall());
85  }, {
86    code: 'ERR_INVALID_ARG_TYPE',
87    name: 'TypeError'
88  });
89});
90
91[0.5, 2 ** 53, 2n ** 63n].forEach((value) => {
92  assert.throws(() => {
93    fs.read(fd,
94            Buffer.allocUnsafe(expected.length),
95            0,
96            expected.length,
97            value,
98            common.mustNotCall());
99  }, {
100    code: 'ERR_OUT_OF_RANGE',
101    name: 'RangeError'
102  });
103});
104
105fs.read(fd,
106        Buffer.allocUnsafe(expected.length),
107        0,
108        expected.length,
109        0n,
110        common.mustSucceed());
111
112fs.read(fd,
113        Buffer.allocUnsafe(expected.length),
114        0,
115        expected.length,
116        2n ** 53n - 1n,
117        common.mustCall((err) => {
118          if (err) {
119            assert.strictEqual(err.code, 'EFBIG');
120          }
121        }));
122
123assert.throws(
124  () => fs.readSync(fd, expected.length, 0, 'utf-8'),
125  {
126    code: 'ERR_INVALID_ARG_TYPE',
127    name: 'TypeError',
128    message: 'The "buffer" argument must be an instance of Buffer, ' +
129             'TypedArray, or DataView. Received type number (4)'
130  }
131);
132
133[true, null, undefined, () => {}, {}].forEach((value) => {
134  assert.throws(() => {
135    fs.readSync(value,
136                Buffer.allocUnsafe(expected.length),
137                0,
138                expected.length,
139                0);
140  }, {
141    code: 'ERR_INVALID_ARG_TYPE',
142    name: 'TypeError'
143  });
144});
145
146assert.throws(() => {
147  fs.readSync(fd,
148              Buffer.allocUnsafe(expected.length),
149              -1,
150              expected.length,
151              0);
152}, {
153  code: 'ERR_OUT_OF_RANGE',
154  name: 'RangeError',
155});
156
157assert.throws(() => {
158  fs.readSync(fd,
159              Buffer.allocUnsafe(expected.length),
160              NaN,
161              expected.length,
162              0);
163}, {
164  code: 'ERR_OUT_OF_RANGE',
165  name: 'RangeError',
166  message: 'The value of "offset" is out of range. It must be an integer. ' +
167           'Received NaN'
168});
169
170assert.throws(() => {
171  fs.readSync(fd,
172              Buffer.allocUnsafe(expected.length),
173              0,
174              -1,
175              0);
176}, {
177  code: 'ERR_OUT_OF_RANGE',
178  name: 'RangeError',
179  message: 'The value of "length" is out of range. ' +
180           'It must be >= 0. Received -1'
181});
182
183assert.throws(() => {
184  fs.readSync(fd,
185              Buffer.allocUnsafe(expected.length),
186              0,
187              expected.length + 1,
188              0);
189}, {
190  code: 'ERR_OUT_OF_RANGE',
191  name: 'RangeError',
192  message: 'The value of "length" is out of range. ' +
193           'It must be <= 4. Received 5'
194});
195
196[true, () => {}, {}, ''].forEach((value) => {
197  assert.throws(() => {
198    fs.readSync(fd,
199                Buffer.allocUnsafe(expected.length),
200                0,
201                expected.length,
202                value);
203  }, {
204    code: 'ERR_INVALID_ARG_TYPE',
205    name: 'TypeError'
206  });
207});
208
209[0.5, 2 ** 53, 2n ** 63n].forEach((value) => {
210  assert.throws(() => {
211    fs.readSync(fd,
212                Buffer.allocUnsafe(expected.length),
213                0,
214                expected.length,
215                value);
216  }, {
217    code: 'ERR_OUT_OF_RANGE',
218    name: 'RangeError'
219  });
220});
221
222fs.readSync(fd,
223            Buffer.allocUnsafe(expected.length),
224            0,
225            expected.length,
226            0n);
227
228try {
229  fs.readSync(fd,
230              Buffer.allocUnsafe(expected.length),
231              0,
232              expected.length,
233              2n ** 53n - 1n);
234} catch (err) {
235  // On systems where max file size is below 2^53-1, we'd expect a EFBIG error.
236  // This is not using `assert.throws` because the above call should not raise
237  // any error on systems that allows file of that size.
238  if (err.code !== 'EFBIG') throw err;
239}
240