11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst path = require('path');
81cb0ef41Sopenharmony_ciconst { writeFile, readFile } = require('fs').promises;
91cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
101cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
111cb0ef41Sopenharmony_ciconst fsBinding = internalBinding('fs');
121cb0ef41Sopenharmony_citmpdir.refresh();
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst fn = path.join(tmpdir.path, 'large-file');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci// Creating large buffer with random content
171cb0ef41Sopenharmony_ciconst largeBuffer = Buffer.from(
181cb0ef41Sopenharmony_ci  Array.from({ length: 1024 ** 2 + 19 }, (_, index) => index)
191cb0ef41Sopenharmony_ci);
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciasync function createLargeFile() {
221cb0ef41Sopenharmony_ci  // Writing buffer to a file then try to read it
231cb0ef41Sopenharmony_ci  await writeFile(fn, largeBuffer);
241cb0ef41Sopenharmony_ci}
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciasync function validateReadFile() {
271cb0ef41Sopenharmony_ci  const readBuffer = await readFile(fn);
281cb0ef41Sopenharmony_ci  assert.strictEqual(readBuffer.equals(largeBuffer), true);
291cb0ef41Sopenharmony_ci}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciasync function validateReadFileProc() {
321cb0ef41Sopenharmony_ci  // Test to make sure reading a file under the /proc directory works. Adapted
331cb0ef41Sopenharmony_ci  // from test-fs-read-file-sync-hostname.js.
341cb0ef41Sopenharmony_ci  // Refs:
351cb0ef41Sopenharmony_ci  // - https://groups.google.com/forum/#!topic/nodejs-dev/rxZ_RoH1Gn0
361cb0ef41Sopenharmony_ci  // - https://github.com/nodejs/node/issues/21331
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  // Test is Linux-specific.
391cb0ef41Sopenharmony_ci  if (!common.isLinux)
401cb0ef41Sopenharmony_ci    return;
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  const hostname = await readFile('/proc/sys/kernel/hostname');
431cb0ef41Sopenharmony_ci  assert.ok(hostname.length > 0);
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cifunction validateReadFileAbortLogicBefore() {
471cb0ef41Sopenharmony_ci  const signal = AbortSignal.abort();
481cb0ef41Sopenharmony_ci  assert.rejects(readFile(fn, { signal }), {
491cb0ef41Sopenharmony_ci    name: 'AbortError'
501cb0ef41Sopenharmony_ci  });
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_cifunction validateReadFileAbortLogicDuring() {
541cb0ef41Sopenharmony_ci  const controller = new AbortController();
551cb0ef41Sopenharmony_ci  const signal = controller.signal;
561cb0ef41Sopenharmony_ci  process.nextTick(() => controller.abort());
571cb0ef41Sopenharmony_ci  assert.rejects(readFile(fn, { signal }), {
581cb0ef41Sopenharmony_ci    name: 'AbortError'
591cb0ef41Sopenharmony_ci  });
601cb0ef41Sopenharmony_ci}
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ciasync function validateWrongSignalParam() {
631cb0ef41Sopenharmony_ci  // Verify that if something different than Abortcontroller.signal
641cb0ef41Sopenharmony_ci  // is passed, ERR_INVALID_ARG_TYPE is thrown
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  await assert.rejects(async () => {
671cb0ef41Sopenharmony_ci    const callback = common.mustNotCall();
681cb0ef41Sopenharmony_ci    await readFile(fn, { signal: 'hello' }, callback);
691cb0ef41Sopenharmony_ci  }, { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' });
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ciasync function validateZeroByteLiar() {
741cb0ef41Sopenharmony_ci  const originalFStat = fsBinding.fstat;
751cb0ef41Sopenharmony_ci  fsBinding.fstat = common.mustCall(
761cb0ef41Sopenharmony_ci    () => (/* stat fields */ [0, 1, 2, 3, 4, 5, 6, 7, 0 /* size */])
771cb0ef41Sopenharmony_ci  );
781cb0ef41Sopenharmony_ci  const readBuffer = await readFile(fn);
791cb0ef41Sopenharmony_ci  assert.strictEqual(readBuffer.toString(), largeBuffer.toString());
801cb0ef41Sopenharmony_ci  fsBinding.fstat = originalFStat;
811cb0ef41Sopenharmony_ci}
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci(async () => {
841cb0ef41Sopenharmony_ci  await createLargeFile();
851cb0ef41Sopenharmony_ci  await validateReadFile();
861cb0ef41Sopenharmony_ci  await validateReadFileProc();
871cb0ef41Sopenharmony_ci  await validateReadFileAbortLogicBefore();
881cb0ef41Sopenharmony_ci  await validateReadFileAbortLogicDuring();
891cb0ef41Sopenharmony_ci  await validateWrongSignalParam();
901cb0ef41Sopenharmony_ci  await validateZeroByteLiar();
911cb0ef41Sopenharmony_ci})().then(common.mustCall());
92