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