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            if (common.isIBMi)
120              assert.strictEqual(err.code, 'EOVERFLOW');
121            else
122              assert.strictEqual(err.code, 'EFBIG');
123          }
124        }));
125
126assert.throws(
127  () => fs.readSync(fd, expected.length, 0, 'utf-8'),
128  {
129    code: 'ERR_INVALID_ARG_TYPE',
130    name: 'TypeError',
131    message: 'The "buffer" argument must be an instance of Buffer, ' +
132             'TypedArray, or DataView. Received type number (4)'
133  }
134);
135
136[true, null, undefined, () => {}, {}].forEach((value) => {
137  assert.throws(() => {
138    fs.readSync(value,
139                Buffer.allocUnsafe(expected.length),
140                0,
141                expected.length,
142                0);
143  }, {
144    code: 'ERR_INVALID_ARG_TYPE',
145    name: 'TypeError'
146  });
147});
148
149assert.throws(() => {
150  fs.readSync(fd,
151              Buffer.allocUnsafe(expected.length),
152              -1,
153              expected.length,
154              0);
155}, {
156  code: 'ERR_OUT_OF_RANGE',
157  name: 'RangeError',
158});
159
160assert.throws(() => {
161  fs.readSync(fd,
162              Buffer.allocUnsafe(expected.length),
163              NaN,
164              expected.length,
165              0);
166}, {
167  code: 'ERR_OUT_OF_RANGE',
168  name: 'RangeError',
169  message: 'The value of "offset" is out of range. It must be an integer. ' +
170           'Received NaN'
171});
172
173assert.throws(() => {
174  fs.readSync(fd,
175              Buffer.allocUnsafe(expected.length),
176              0,
177              -1,
178              0);
179}, {
180  code: 'ERR_OUT_OF_RANGE',
181  name: 'RangeError',
182  message: 'The value of "length" is out of range. ' +
183           'It must be >= 0. Received -1'
184});
185
186assert.throws(() => {
187  fs.readSync(fd,
188              Buffer.allocUnsafe(expected.length),
189              0,
190              expected.length + 1,
191              0);
192}, {
193  code: 'ERR_OUT_OF_RANGE',
194  name: 'RangeError',
195  message: 'The value of "length" is out of range. ' +
196           'It must be <= 4. Received 5'
197});
198
199[true, () => {}, {}, ''].forEach((value) => {
200  assert.throws(() => {
201    fs.readSync(fd,
202                Buffer.allocUnsafe(expected.length),
203                0,
204                expected.length,
205                value);
206  }, {
207    code: 'ERR_INVALID_ARG_TYPE',
208    name: 'TypeError'
209  });
210});
211
212[0.5, 2 ** 53, 2n ** 63n].forEach((value) => {
213  assert.throws(() => {
214    fs.readSync(fd,
215                Buffer.allocUnsafe(expected.length),
216                0,
217                expected.length,
218                value);
219  }, {
220    code: 'ERR_OUT_OF_RANGE',
221    name: 'RangeError'
222  });
223});
224
225fs.readSync(fd,
226            Buffer.allocUnsafe(expected.length),
227            0,
228            expected.length,
229            0n);
230
231try {
232  fs.readSync(fd,
233              Buffer.allocUnsafe(expected.length),
234              0,
235              expected.length,
236              2n ** 53n - 1n);
237} catch (err) {
238  // On systems where max file size is below 2^53-1, we'd expect a EFBIG error.
239  // This is not using `assert.throws` because the above call should not raise
240  // any error on systems that allows file of that size.
241  if (err.code !== 'EFBIG' && !(common.isIBMi && err.code === 'EOVERFLOW'))
242    throw err;
243}
244