11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// Test to assert the desired functioning of fs.read
51cb0ef41Sopenharmony_ci// when {offset:null} is passed as options parameter
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst common = require('../common');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst fs = require('fs');
101cb0ef41Sopenharmony_ciconst fsPromises = fs.promises;
111cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
121cb0ef41Sopenharmony_ciconst filepath = fixtures.path('x.txt');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst buf = Buffer.alloc(1);
151cb0ef41Sopenharmony_ci// Reading only one character, hence buffer of one byte is enough.
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci// Tests are done by making sure the first letter in buffer is
181cb0ef41Sopenharmony_ci// same as first letter in file.
191cb0ef41Sopenharmony_ci// 120 is the ascii code of letter x.
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci// Tests for callback API.
221cb0ef41Sopenharmony_cifs.open(filepath, 'r', common.mustSucceed((fd) => {
231cb0ef41Sopenharmony_ci  fs.read(fd, { offset: null, buffer: buf },
241cb0ef41Sopenharmony_ci          common.mustSucceed((bytesRead, buffer) => {
251cb0ef41Sopenharmony_ci            assert.strictEqual(buffer[0], 120);
261cb0ef41Sopenharmony_ci            fs.close(fd, common.mustSucceed(() => {}));
271cb0ef41Sopenharmony_ci          }));
281cb0ef41Sopenharmony_ci}));
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cifs.open(filepath, 'r', common.mustSucceed((fd) => {
311cb0ef41Sopenharmony_ci  fs.read(fd, buf, { offset: null },
321cb0ef41Sopenharmony_ci          common.mustSucceed((bytesRead, buffer) => {
331cb0ef41Sopenharmony_ci            assert.strictEqual(buffer[0], 120);
341cb0ef41Sopenharmony_ci            fs.close(fd, common.mustSucceed(() => {}));
351cb0ef41Sopenharmony_ci          }));
361cb0ef41Sopenharmony_ci}));
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cilet filehandle = null;
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci// Tests for promises api
411cb0ef41Sopenharmony_ci(async () => {
421cb0ef41Sopenharmony_ci  filehandle = await fsPromises.open(filepath, 'r');
431cb0ef41Sopenharmony_ci  const readObject = await filehandle.read(buf, { offset: null });
441cb0ef41Sopenharmony_ci  assert.strictEqual(readObject.buffer[0], 120);
451cb0ef41Sopenharmony_ci})()
461cb0ef41Sopenharmony_ci.finally(() => filehandle?.close())
471cb0ef41Sopenharmony_ci.then(common.mustCall());
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci// Undocumented: omitted position works the same as position === null
501cb0ef41Sopenharmony_ci(async () => {
511cb0ef41Sopenharmony_ci  filehandle = await fsPromises.open(filepath, 'r');
521cb0ef41Sopenharmony_ci  const readObject = await filehandle.read(buf, null, buf.length);
531cb0ef41Sopenharmony_ci  assert.strictEqual(readObject.buffer[0], 120);
541cb0ef41Sopenharmony_ci})()
551cb0ef41Sopenharmony_ci.finally(() => filehandle?.close())
561cb0ef41Sopenharmony_ci.then(common.mustCall());
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci(async () => {
591cb0ef41Sopenharmony_ci  filehandle = await fsPromises.open(filepath, 'r');
601cb0ef41Sopenharmony_ci  const readObject = await filehandle.read(buf, null, buf.length, 0);
611cb0ef41Sopenharmony_ci  assert.strictEqual(readObject.buffer[0], 120);
621cb0ef41Sopenharmony_ci})()
631cb0ef41Sopenharmony_ci.finally(() => filehandle?.close())
641cb0ef41Sopenharmony_ci.then(common.mustCall());
65