11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// The following tests validate base functionality for the fs.promises
61cb0ef41Sopenharmony_ci// FileHandle.read method.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst fs = require('fs');
91cb0ef41Sopenharmony_ciconst { open } = fs.promises;
101cb0ef41Sopenharmony_ciconst path = require('path');
111cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
121cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
131cb0ef41Sopenharmony_ciconst assert = require('assert');
141cb0ef41Sopenharmony_ciconst tmpDir = tmpdir.path;
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciasync function read(fileHandle, buffer, offset, length, position, options) {
171cb0ef41Sopenharmony_ci  return options.useConf ?
181cb0ef41Sopenharmony_ci    fileHandle.read({ buffer, offset, length, position }) :
191cb0ef41Sopenharmony_ci    fileHandle.read(buffer, offset, length, position);
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciasync function validateRead(data, file, options) {
231cb0ef41Sopenharmony_ci  const filePath = path.resolve(tmpDir, file);
241cb0ef41Sopenharmony_ci  const buffer = Buffer.from(data, 'utf8');
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  const fd = fs.openSync(filePath, 'w+');
271cb0ef41Sopenharmony_ci  const fileHandle = await open(filePath, 'w+');
281cb0ef41Sopenharmony_ci  const streamFileHandle = await open(filePath, 'w+');
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  fs.writeSync(fd, buffer, 0, buffer.length);
311cb0ef41Sopenharmony_ci  fs.closeSync(fd);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  fileHandle.on('close', common.mustCall());
341cb0ef41Sopenharmony_ci  const readAsyncHandle =
351cb0ef41Sopenharmony_ci    await read(fileHandle, Buffer.alloc(11), 0, 11, 0, options);
361cb0ef41Sopenharmony_ci  assert.deepStrictEqual(data.length, readAsyncHandle.bytesRead);
371cb0ef41Sopenharmony_ci  if (data.length)
381cb0ef41Sopenharmony_ci    assert.deepStrictEqual(buffer, readAsyncHandle.buffer);
391cb0ef41Sopenharmony_ci  await fileHandle.close();
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  const stream = fs.createReadStream(null, { fd: streamFileHandle });
421cb0ef41Sopenharmony_ci  let streamData = Buffer.alloc(0);
431cb0ef41Sopenharmony_ci  for await (const chunk of stream)
441cb0ef41Sopenharmony_ci    streamData = Buffer.from(chunk);
451cb0ef41Sopenharmony_ci  assert.deepStrictEqual(buffer, streamData);
461cb0ef41Sopenharmony_ci  if (data.length)
471cb0ef41Sopenharmony_ci    assert.deepStrictEqual(streamData, readAsyncHandle.buffer);
481cb0ef41Sopenharmony_ci  await streamFileHandle.close();
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ciasync function validateLargeRead(options) {
521cb0ef41Sopenharmony_ci  // Reading beyond file length (3 in this case) should return no data.
531cb0ef41Sopenharmony_ci  // This is a test for a bug where reads > uint32 would return data
541cb0ef41Sopenharmony_ci  // from the current position in the file.
551cb0ef41Sopenharmony_ci  const filePath = fixtures.path('x.txt');
561cb0ef41Sopenharmony_ci  const fileHandle = await open(filePath, 'r');
571cb0ef41Sopenharmony_ci  const pos = 0xffffffff + 1; // max-uint32 + 1
581cb0ef41Sopenharmony_ci  const readHandle =
591cb0ef41Sopenharmony_ci    await read(fileHandle, Buffer.alloc(1), 0, 1, pos, options);
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  assert.strictEqual(readHandle.bytesRead, 0);
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ciasync function validateReadNoParams() {
651cb0ef41Sopenharmony_ci  const filePath = fixtures.path('x.txt');
661cb0ef41Sopenharmony_ci  const fileHandle = await open(filePath, 'r');
671cb0ef41Sopenharmony_ci  // Should not throw
681cb0ef41Sopenharmony_ci  await fileHandle.read();
691cb0ef41Sopenharmony_ci}
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci// Validates that the zero position is respected after the position has been
721cb0ef41Sopenharmony_ci// moved. The test iterates over the xyz chars twice making sure that the values
731cb0ef41Sopenharmony_ci// are read from the correct position.
741cb0ef41Sopenharmony_ciasync function validateReadWithPositionZero() {
751cb0ef41Sopenharmony_ci  const opts = { useConf: true };
761cb0ef41Sopenharmony_ci  const filePath = fixtures.path('x.txt');
771cb0ef41Sopenharmony_ci  const fileHandle = await open(filePath, 'r');
781cb0ef41Sopenharmony_ci  const expectedSequence = ['x', 'y', 'z'];
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  for (let i = 0; i < expectedSequence.length * 2; i++) {
811cb0ef41Sopenharmony_ci    const len = 1;
821cb0ef41Sopenharmony_ci    const pos = i % 3;
831cb0ef41Sopenharmony_ci    const buf = Buffer.alloc(len);
841cb0ef41Sopenharmony_ci    const { bytesRead } = await read(fileHandle, buf, 0, len, pos, opts);
851cb0ef41Sopenharmony_ci    assert.strictEqual(bytesRead, len);
861cb0ef41Sopenharmony_ci    assert.strictEqual(buf.toString(), expectedSequence[pos]);
871cb0ef41Sopenharmony_ci  }
881cb0ef41Sopenharmony_ci}
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ciasync function validateReadLength(len) {
911cb0ef41Sopenharmony_ci  const buf = Buffer.alloc(4);
921cb0ef41Sopenharmony_ci  const opts = { useConf: true };
931cb0ef41Sopenharmony_ci  const filePath = fixtures.path('x.txt');
941cb0ef41Sopenharmony_ci  const fileHandle = await open(filePath, 'r');
951cb0ef41Sopenharmony_ci  const { bytesRead } = await read(fileHandle, buf, 0, len, 0, opts);
961cb0ef41Sopenharmony_ci  assert.strictEqual(bytesRead, len);
971cb0ef41Sopenharmony_ci}
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci(async function() {
1011cb0ef41Sopenharmony_ci  tmpdir.refresh();
1021cb0ef41Sopenharmony_ci  await validateRead('Hello world', 'read-file', { useConf: false });
1031cb0ef41Sopenharmony_ci  await validateRead('', 'read-empty-file', { useConf: false });
1041cb0ef41Sopenharmony_ci  await validateRead('Hello world', 'read-file-conf', { useConf: true });
1051cb0ef41Sopenharmony_ci  await validateRead('', 'read-empty-file-conf', { useConf: true });
1061cb0ef41Sopenharmony_ci  await validateLargeRead({ useConf: false });
1071cb0ef41Sopenharmony_ci  await validateLargeRead({ useConf: true });
1081cb0ef41Sopenharmony_ci  await validateReadNoParams();
1091cb0ef41Sopenharmony_ci  await validateReadWithPositionZero();
1101cb0ef41Sopenharmony_ci  await validateReadLength(0);
1111cb0ef41Sopenharmony_ci  await validateReadLength(1);
1121cb0ef41Sopenharmony_ci})().then(common.mustCall());
113