• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const path = require('path');
6const fs = require('fs');
7const tmpdir = require('../common/tmpdir');
8
9tmpdir.refresh();
10
11const expected = 'ümlaut. Лорем 運務ホソモ指及 आपको करने विकास 紙読決多密所 أضف';
12
13let cnt = 0;
14const getFileName = () => path.join(tmpdir.path, `readv_${++cnt}.txt`);
15const exptectedBuff = Buffer.from(expected);
16
17const allocateEmptyBuffers = (combinedLength) => {
18  const bufferArr = [];
19  // Allocate two buffers, each half the size of exptectedBuff
20  bufferArr[0] = Buffer.alloc(Math.floor(combinedLength / 2)),
21  bufferArr[1] = Buffer.alloc(combinedLength - bufferArr[0].length);
22
23  return bufferArr;
24};
25
26const getCallback = (fd, bufferArr) => {
27  return common.mustCall((err, bytesRead, buffers) => {
28    assert.ifError(err);
29
30    assert.deepStrictEqual(bufferArr, buffers);
31    const expectedLength = exptectedBuff.length;
32    assert.deepStrictEqual(bytesRead, expectedLength);
33    fs.closeSync(fd);
34
35    assert(Buffer.concat(bufferArr).equals(exptectedBuff));
36  });
37};
38
39// fs.readv with array of buffers with all parameters
40{
41  const filename = getFileName();
42  const fd = fs.openSync(filename, 'w+');
43  fs.writeSync(fd, exptectedBuff);
44
45  const bufferArr = allocateEmptyBuffers(exptectedBuff.length);
46  const callback = getCallback(fd, bufferArr);
47
48  fs.readv(fd, bufferArr, 0, callback);
49}
50
51// fs.readv with array of buffers without position
52{
53  const filename = getFileName();
54  fs.writeFileSync(filename, exptectedBuff);
55  const fd = fs.openSync(filename, 'r');
56
57  const bufferArr = allocateEmptyBuffers(exptectedBuff.length);
58  const callback = getCallback(fd, bufferArr);
59
60  fs.readv(fd, bufferArr, callback);
61}
62
63/**
64 * Testing with incorrect arguments
65 */
66const wrongInputs = [false, 'test', {}, [{}], ['sdf'], null, undefined];
67
68{
69  const filename = getFileName(2);
70  fs.writeFileSync(filename, exptectedBuff);
71  const fd = fs.openSync(filename, 'r');
72
73
74  wrongInputs.forEach((wrongInput) => {
75    assert.throws(
76      () => fs.readv(fd, wrongInput, null, common.mustNotCall()), {
77        code: 'ERR_INVALID_ARG_TYPE',
78        name: 'TypeError'
79      }
80    );
81  });
82
83  fs.closeSync(fd);
84}
85
86{
87  // fs.readv with wrong fd argument
88  wrongInputs.forEach((wrongInput) => {
89    assert.throws(
90      () => fs.readv(wrongInput, common.mustNotCall()),
91      {
92        code: 'ERR_INVALID_ARG_TYPE',
93        name: 'TypeError'
94      }
95    );
96  });
97}
98