11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
31cb0ef41Sopenharmony_ci//
41cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
51cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
61cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
71cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
81cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
91cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
101cb0ef41Sopenharmony_ci// following conditions:
111cb0ef41Sopenharmony_ci//
121cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
131cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
141cb0ef41Sopenharmony_ci//
151cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
161cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
171cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
181cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
191cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
201cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
211cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci'use strict';
241cb0ef41Sopenharmony_ciconst common = require('../common');
251cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
261cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
271cb0ef41Sopenharmony_ciconst assert = require('assert');
281cb0ef41Sopenharmony_ciconst fs = require('fs');
291cb0ef41Sopenharmony_ciconst path = require('path');
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_citmpdir.refresh();
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciconst nonexistentFile = path.join(tmpdir.path, 'non-existent');
351cb0ef41Sopenharmony_ciconst nonexistentDir = path.join(tmpdir.path, 'non-existent', 'foo', 'bar');
361cb0ef41Sopenharmony_ciconst existingFile = path.join(tmpdir.path, 'existingFile.js');
371cb0ef41Sopenharmony_ciconst existingFile2 = path.join(tmpdir.path, 'existingFile2.js');
381cb0ef41Sopenharmony_ciconst existingDir = path.join(tmpdir.path, 'dir');
391cb0ef41Sopenharmony_ciconst existingDir2 = fixtures.path('keys');
401cb0ef41Sopenharmony_cifs.mkdirSync(existingDir);
411cb0ef41Sopenharmony_cifs.writeFileSync(existingFile, 'test', 'utf-8');
421cb0ef41Sopenharmony_cifs.writeFileSync(existingFile2, 'test', 'utf-8');
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciconst { COPYFILE_EXCL } = fs.constants;
461cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
471cb0ef41Sopenharmony_ciconst {
481cb0ef41Sopenharmony_ci  UV_EBADF,
491cb0ef41Sopenharmony_ci  UV_EEXIST,
501cb0ef41Sopenharmony_ci  UV_EINVAL,
511cb0ef41Sopenharmony_ci  UV_ENOENT,
521cb0ef41Sopenharmony_ci  UV_ENOTDIR,
531cb0ef41Sopenharmony_ci  UV_ENOTEMPTY,
541cb0ef41Sopenharmony_ci  UV_EPERM
551cb0ef41Sopenharmony_ci} = internalBinding('uv');
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci// Template tag function for escaping special characters in strings so that:
581cb0ef41Sopenharmony_ci// new RegExp(re`${str}`).test(str) === true
591cb0ef41Sopenharmony_cifunction re(literals, ...values) {
601cb0ef41Sopenharmony_ci  const escapeRE = /[\\^$.*+?()[\]{}|=!<>:-]/g;
611cb0ef41Sopenharmony_ci  let result = literals[0].replace(escapeRE, '\\$&');
621cb0ef41Sopenharmony_ci  for (const [i, value] of values.entries()) {
631cb0ef41Sopenharmony_ci    result += value.replace(escapeRE, '\\$&');
641cb0ef41Sopenharmony_ci    result += literals[i + 1].replace(escapeRE, '\\$&');
651cb0ef41Sopenharmony_ci  }
661cb0ef41Sopenharmony_ci  return result;
671cb0ef41Sopenharmony_ci}
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci// stat
701cb0ef41Sopenharmony_ci{
711cb0ef41Sopenharmony_ci  const validateError = (err) => {
721cb0ef41Sopenharmony_ci    assert.strictEqual(nonexistentFile, err.path);
731cb0ef41Sopenharmony_ci    assert.strictEqual(
741cb0ef41Sopenharmony_ci      err.message,
751cb0ef41Sopenharmony_ci      `ENOENT: no such file or directory, stat '${nonexistentFile}'`);
761cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
771cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
781cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'stat');
791cb0ef41Sopenharmony_ci    return true;
801cb0ef41Sopenharmony_ci  };
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  fs.stat(nonexistentFile, common.mustCall(validateError));
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  assert.throws(
851cb0ef41Sopenharmony_ci    () => fs.statSync(nonexistentFile),
861cb0ef41Sopenharmony_ci    validateError
871cb0ef41Sopenharmony_ci  );
881cb0ef41Sopenharmony_ci}
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci// lstat
911cb0ef41Sopenharmony_ci{
921cb0ef41Sopenharmony_ci  const validateError = (err) => {
931cb0ef41Sopenharmony_ci    assert.strictEqual(nonexistentFile, err.path);
941cb0ef41Sopenharmony_ci    assert.strictEqual(
951cb0ef41Sopenharmony_ci      err.message,
961cb0ef41Sopenharmony_ci      `ENOENT: no such file or directory, lstat '${nonexistentFile}'`);
971cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
981cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
991cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'lstat');
1001cb0ef41Sopenharmony_ci    return true;
1011cb0ef41Sopenharmony_ci  };
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci  fs.lstat(nonexistentFile, common.mustCall(validateError));
1041cb0ef41Sopenharmony_ci  assert.throws(
1051cb0ef41Sopenharmony_ci    () => fs.lstatSync(nonexistentFile),
1061cb0ef41Sopenharmony_ci    validateError
1071cb0ef41Sopenharmony_ci  );
1081cb0ef41Sopenharmony_ci}
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci// fstat
1111cb0ef41Sopenharmony_ci{
1121cb0ef41Sopenharmony_ci  const validateError = (err) => {
1131cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'EBADF: bad file descriptor, fstat');
1141cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_EBADF);
1151cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'EBADF');
1161cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'fstat');
1171cb0ef41Sopenharmony_ci    return true;
1181cb0ef41Sopenharmony_ci  };
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci  common.runWithInvalidFD((fd) => {
1211cb0ef41Sopenharmony_ci    fs.fstat(fd, common.mustCall(validateError));
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci    assert.throws(
1241cb0ef41Sopenharmony_ci      () => fs.fstatSync(fd),
1251cb0ef41Sopenharmony_ci      validateError
1261cb0ef41Sopenharmony_ci    );
1271cb0ef41Sopenharmony_ci  });
1281cb0ef41Sopenharmony_ci}
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci// realpath
1311cb0ef41Sopenharmony_ci{
1321cb0ef41Sopenharmony_ci  const validateError = (err) => {
1331cb0ef41Sopenharmony_ci    assert.strictEqual(nonexistentFile, err.path);
1341cb0ef41Sopenharmony_ci    assert.strictEqual(
1351cb0ef41Sopenharmony_ci      err.message,
1361cb0ef41Sopenharmony_ci      `ENOENT: no such file or directory, lstat '${nonexistentFile}'`);
1371cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
1381cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
1391cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'lstat');
1401cb0ef41Sopenharmony_ci    return true;
1411cb0ef41Sopenharmony_ci  };
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci  fs.realpath(nonexistentFile, common.mustCall(validateError));
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci  assert.throws(
1461cb0ef41Sopenharmony_ci    () => fs.realpathSync(nonexistentFile),
1471cb0ef41Sopenharmony_ci    validateError
1481cb0ef41Sopenharmony_ci  );
1491cb0ef41Sopenharmony_ci}
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci// native realpath
1521cb0ef41Sopenharmony_ci{
1531cb0ef41Sopenharmony_ci  const validateError = (err) => {
1541cb0ef41Sopenharmony_ci    assert.strictEqual(nonexistentFile, err.path);
1551cb0ef41Sopenharmony_ci    assert.strictEqual(
1561cb0ef41Sopenharmony_ci      err.message,
1571cb0ef41Sopenharmony_ci      `ENOENT: no such file or directory, realpath '${nonexistentFile}'`);
1581cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
1591cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
1601cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'realpath');
1611cb0ef41Sopenharmony_ci    return true;
1621cb0ef41Sopenharmony_ci  };
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci  fs.realpath.native(nonexistentFile, common.mustCall(validateError));
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_ci  assert.throws(
1671cb0ef41Sopenharmony_ci    () => fs.realpathSync.native(nonexistentFile),
1681cb0ef41Sopenharmony_ci    validateError
1691cb0ef41Sopenharmony_ci  );
1701cb0ef41Sopenharmony_ci}
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci// readlink
1731cb0ef41Sopenharmony_ci{
1741cb0ef41Sopenharmony_ci  const validateError = (err) => {
1751cb0ef41Sopenharmony_ci    assert.strictEqual(nonexistentFile, err.path);
1761cb0ef41Sopenharmony_ci    assert.strictEqual(
1771cb0ef41Sopenharmony_ci      err.message,
1781cb0ef41Sopenharmony_ci      `ENOENT: no such file or directory, readlink '${nonexistentFile}'`);
1791cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
1801cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
1811cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'readlink');
1821cb0ef41Sopenharmony_ci    return true;
1831cb0ef41Sopenharmony_ci  };
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ci  fs.readlink(nonexistentFile, common.mustCall(validateError));
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_ci  assert.throws(
1881cb0ef41Sopenharmony_ci    () => fs.readlinkSync(nonexistentFile),
1891cb0ef41Sopenharmony_ci    validateError
1901cb0ef41Sopenharmony_ci  );
1911cb0ef41Sopenharmony_ci}
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ci// Link nonexistent file
1941cb0ef41Sopenharmony_ci{
1951cb0ef41Sopenharmony_ci  const validateError = (err) => {
1961cb0ef41Sopenharmony_ci    assert.strictEqual(nonexistentFile, err.path);
1971cb0ef41Sopenharmony_ci    // Could be resolved to an absolute path
1981cb0ef41Sopenharmony_ci    assert.ok(err.dest.endsWith('foo'),
1991cb0ef41Sopenharmony_ci              `expect ${err.dest} to end with 'foo'`);
2001cb0ef41Sopenharmony_ci    const regexp = new RegExp('^ENOENT: no such file or directory, link ' +
2011cb0ef41Sopenharmony_ci                              re`'${nonexistentFile}' -> ` + '\'.*foo\'');
2021cb0ef41Sopenharmony_ci    assert.match(err.message, regexp);
2031cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
2041cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
2051cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'link');
2061cb0ef41Sopenharmony_ci    return true;
2071cb0ef41Sopenharmony_ci  };
2081cb0ef41Sopenharmony_ci
2091cb0ef41Sopenharmony_ci  fs.link(nonexistentFile, 'foo', common.mustCall(validateError));
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci  assert.throws(
2121cb0ef41Sopenharmony_ci    () => fs.linkSync(nonexistentFile, 'foo'),
2131cb0ef41Sopenharmony_ci    validateError
2141cb0ef41Sopenharmony_ci  );
2151cb0ef41Sopenharmony_ci}
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ci// link existing file
2181cb0ef41Sopenharmony_ci{
2191cb0ef41Sopenharmony_ci  const validateError = (err) => {
2201cb0ef41Sopenharmony_ci    assert.strictEqual(existingFile, err.path);
2211cb0ef41Sopenharmony_ci    assert.strictEqual(existingFile2, err.dest);
2221cb0ef41Sopenharmony_ci    assert.strictEqual(
2231cb0ef41Sopenharmony_ci      err.message,
2241cb0ef41Sopenharmony_ci      `EEXIST: file already exists, link '${existingFile}' -> ` +
2251cb0ef41Sopenharmony_ci      `'${existingFile2}'`);
2261cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_EEXIST);
2271cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'EEXIST');
2281cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'link');
2291cb0ef41Sopenharmony_ci    return true;
2301cb0ef41Sopenharmony_ci  };
2311cb0ef41Sopenharmony_ci
2321cb0ef41Sopenharmony_ci  fs.link(existingFile, existingFile2, common.mustCall(validateError));
2331cb0ef41Sopenharmony_ci
2341cb0ef41Sopenharmony_ci  assert.throws(
2351cb0ef41Sopenharmony_ci    () => fs.linkSync(existingFile, existingFile2),
2361cb0ef41Sopenharmony_ci    validateError
2371cb0ef41Sopenharmony_ci  );
2381cb0ef41Sopenharmony_ci}
2391cb0ef41Sopenharmony_ci
2401cb0ef41Sopenharmony_ci// symlink
2411cb0ef41Sopenharmony_ci{
2421cb0ef41Sopenharmony_ci  const validateError = (err) => {
2431cb0ef41Sopenharmony_ci    assert.strictEqual(existingFile, err.path);
2441cb0ef41Sopenharmony_ci    assert.strictEqual(existingFile2, err.dest);
2451cb0ef41Sopenharmony_ci    assert.strictEqual(
2461cb0ef41Sopenharmony_ci      err.message,
2471cb0ef41Sopenharmony_ci      `EEXIST: file already exists, symlink '${existingFile}' -> ` +
2481cb0ef41Sopenharmony_ci      `'${existingFile2}'`);
2491cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_EEXIST);
2501cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'EEXIST');
2511cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'symlink');
2521cb0ef41Sopenharmony_ci    return true;
2531cb0ef41Sopenharmony_ci  };
2541cb0ef41Sopenharmony_ci
2551cb0ef41Sopenharmony_ci  fs.symlink(existingFile, existingFile2, common.mustCall(validateError));
2561cb0ef41Sopenharmony_ci
2571cb0ef41Sopenharmony_ci  assert.throws(
2581cb0ef41Sopenharmony_ci    () => fs.symlinkSync(existingFile, existingFile2),
2591cb0ef41Sopenharmony_ci    validateError
2601cb0ef41Sopenharmony_ci  );
2611cb0ef41Sopenharmony_ci}
2621cb0ef41Sopenharmony_ci
2631cb0ef41Sopenharmony_ci// unlink
2641cb0ef41Sopenharmony_ci{
2651cb0ef41Sopenharmony_ci  const validateError = (err) => {
2661cb0ef41Sopenharmony_ci    assert.strictEqual(nonexistentFile, err.path);
2671cb0ef41Sopenharmony_ci    assert.strictEqual(
2681cb0ef41Sopenharmony_ci      err.message,
2691cb0ef41Sopenharmony_ci      `ENOENT: no such file or directory, unlink '${nonexistentFile}'`);
2701cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
2711cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
2721cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'unlink');
2731cb0ef41Sopenharmony_ci    return true;
2741cb0ef41Sopenharmony_ci  };
2751cb0ef41Sopenharmony_ci
2761cb0ef41Sopenharmony_ci  fs.unlink(nonexistentFile, common.mustCall(validateError));
2771cb0ef41Sopenharmony_ci
2781cb0ef41Sopenharmony_ci  assert.throws(
2791cb0ef41Sopenharmony_ci    () => fs.unlinkSync(nonexistentFile),
2801cb0ef41Sopenharmony_ci    validateError
2811cb0ef41Sopenharmony_ci  );
2821cb0ef41Sopenharmony_ci}
2831cb0ef41Sopenharmony_ci
2841cb0ef41Sopenharmony_ci// rename
2851cb0ef41Sopenharmony_ci{
2861cb0ef41Sopenharmony_ci  const validateError = (err) => {
2871cb0ef41Sopenharmony_ci    assert.strictEqual(nonexistentFile, err.path);
2881cb0ef41Sopenharmony_ci    // Could be resolved to an absolute path
2891cb0ef41Sopenharmony_ci    assert.ok(err.dest.endsWith('foo'),
2901cb0ef41Sopenharmony_ci              `expect ${err.dest} to end with 'foo'`);
2911cb0ef41Sopenharmony_ci    const regexp = new RegExp('ENOENT: no such file or directory, rename ' +
2921cb0ef41Sopenharmony_ci                              re`'${nonexistentFile}' -> ` + '\'.*foo\'');
2931cb0ef41Sopenharmony_ci    assert.match(err.message, regexp);
2941cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
2951cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
2961cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'rename');
2971cb0ef41Sopenharmony_ci    return true;
2981cb0ef41Sopenharmony_ci  };
2991cb0ef41Sopenharmony_ci
3001cb0ef41Sopenharmony_ci  const destFile = path.join(tmpdir.path, 'foo');
3011cb0ef41Sopenharmony_ci  fs.rename(nonexistentFile, destFile, common.mustCall(validateError));
3021cb0ef41Sopenharmony_ci
3031cb0ef41Sopenharmony_ci  assert.throws(
3041cb0ef41Sopenharmony_ci    () => fs.renameSync(nonexistentFile, destFile),
3051cb0ef41Sopenharmony_ci    validateError
3061cb0ef41Sopenharmony_ci  );
3071cb0ef41Sopenharmony_ci}
3081cb0ef41Sopenharmony_ci
3091cb0ef41Sopenharmony_ci// Rename non-empty directory
3101cb0ef41Sopenharmony_ci{
3111cb0ef41Sopenharmony_ci  const validateError = (err) => {
3121cb0ef41Sopenharmony_ci    assert.strictEqual(existingDir, err.path);
3131cb0ef41Sopenharmony_ci    assert.strictEqual(existingDir2, err.dest);
3141cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'rename');
3151cb0ef41Sopenharmony_ci    // Could be ENOTEMPTY, EEXIST, or EPERM, depending on the platform
3161cb0ef41Sopenharmony_ci    if (err.code === 'ENOTEMPTY') {
3171cb0ef41Sopenharmony_ci      assert.strictEqual(
3181cb0ef41Sopenharmony_ci        err.message,
3191cb0ef41Sopenharmony_ci        `ENOTEMPTY: directory not empty, rename '${existingDir}' -> ` +
3201cb0ef41Sopenharmony_ci        `'${existingDir2}'`);
3211cb0ef41Sopenharmony_ci      assert.strictEqual(err.errno, UV_ENOTEMPTY);
3221cb0ef41Sopenharmony_ci    } else if (err.code === 'EXDEV') {  // Not on the same mounted filesystem
3231cb0ef41Sopenharmony_ci      assert.strictEqual(
3241cb0ef41Sopenharmony_ci        err.message,
3251cb0ef41Sopenharmony_ci        `EXDEV: cross-device link not permitted, rename '${existingDir}' -> ` +
3261cb0ef41Sopenharmony_ci            `'${existingDir2}'`);
3271cb0ef41Sopenharmony_ci    } else if (err.code === 'EEXIST') {  // smartos and aix
3281cb0ef41Sopenharmony_ci      assert.strictEqual(
3291cb0ef41Sopenharmony_ci        err.message,
3301cb0ef41Sopenharmony_ci        `EEXIST: file already exists, rename '${existingDir}' -> ` +
3311cb0ef41Sopenharmony_ci        `'${existingDir2}'`);
3321cb0ef41Sopenharmony_ci      assert.strictEqual(err.errno, UV_EEXIST);
3331cb0ef41Sopenharmony_ci    } else {  // windows
3341cb0ef41Sopenharmony_ci      assert.strictEqual(
3351cb0ef41Sopenharmony_ci        err.message,
3361cb0ef41Sopenharmony_ci        `EPERM: operation not permitted, rename '${existingDir}' -> ` +
3371cb0ef41Sopenharmony_ci        `'${existingDir2}'`);
3381cb0ef41Sopenharmony_ci      assert.strictEqual(err.errno, UV_EPERM);
3391cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 'EPERM');
3401cb0ef41Sopenharmony_ci    }
3411cb0ef41Sopenharmony_ci    return true;
3421cb0ef41Sopenharmony_ci  };
3431cb0ef41Sopenharmony_ci
3441cb0ef41Sopenharmony_ci  fs.rename(existingDir, existingDir2, common.mustCall(validateError));
3451cb0ef41Sopenharmony_ci
3461cb0ef41Sopenharmony_ci  assert.throws(
3471cb0ef41Sopenharmony_ci    () => fs.renameSync(existingDir, existingDir2),
3481cb0ef41Sopenharmony_ci    validateError
3491cb0ef41Sopenharmony_ci  );
3501cb0ef41Sopenharmony_ci}
3511cb0ef41Sopenharmony_ci
3521cb0ef41Sopenharmony_ci// rmdir
3531cb0ef41Sopenharmony_ci{
3541cb0ef41Sopenharmony_ci  const validateError = (err) => {
3551cb0ef41Sopenharmony_ci    assert.strictEqual(nonexistentFile, err.path);
3561cb0ef41Sopenharmony_ci    assert.strictEqual(
3571cb0ef41Sopenharmony_ci      err.message,
3581cb0ef41Sopenharmony_ci      `ENOENT: no such file or directory, rmdir '${nonexistentFile}'`);
3591cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
3601cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
3611cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'rmdir');
3621cb0ef41Sopenharmony_ci    return true;
3631cb0ef41Sopenharmony_ci  };
3641cb0ef41Sopenharmony_ci
3651cb0ef41Sopenharmony_ci  fs.rmdir(nonexistentFile, common.mustCall(validateError));
3661cb0ef41Sopenharmony_ci
3671cb0ef41Sopenharmony_ci  assert.throws(
3681cb0ef41Sopenharmony_ci    () => fs.rmdirSync(nonexistentFile),
3691cb0ef41Sopenharmony_ci    validateError
3701cb0ef41Sopenharmony_ci  );
3711cb0ef41Sopenharmony_ci}
3721cb0ef41Sopenharmony_ci
3731cb0ef41Sopenharmony_ci// rmdir a file
3741cb0ef41Sopenharmony_ci{
3751cb0ef41Sopenharmony_ci  const validateError = (err) => {
3761cb0ef41Sopenharmony_ci    assert.strictEqual(existingFile, err.path);
3771cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'rmdir');
3781cb0ef41Sopenharmony_ci    if (err.code === 'ENOTDIR') {
3791cb0ef41Sopenharmony_ci      assert.strictEqual(
3801cb0ef41Sopenharmony_ci        err.message,
3811cb0ef41Sopenharmony_ci        `ENOTDIR: not a directory, rmdir '${existingFile}'`);
3821cb0ef41Sopenharmony_ci      assert.strictEqual(err.errno, UV_ENOTDIR);
3831cb0ef41Sopenharmony_ci    } else {  // windows
3841cb0ef41Sopenharmony_ci      assert.strictEqual(
3851cb0ef41Sopenharmony_ci        err.message,
3861cb0ef41Sopenharmony_ci        `ENOENT: no such file or directory, rmdir '${existingFile}'`);
3871cb0ef41Sopenharmony_ci      assert.strictEqual(err.errno, UV_ENOENT);
3881cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 'ENOENT');
3891cb0ef41Sopenharmony_ci    }
3901cb0ef41Sopenharmony_ci    return true;
3911cb0ef41Sopenharmony_ci  };
3921cb0ef41Sopenharmony_ci
3931cb0ef41Sopenharmony_ci  fs.rmdir(existingFile, common.mustCall(validateError));
3941cb0ef41Sopenharmony_ci
3951cb0ef41Sopenharmony_ci  assert.throws(
3961cb0ef41Sopenharmony_ci    () => fs.rmdirSync(existingFile),
3971cb0ef41Sopenharmony_ci    validateError
3981cb0ef41Sopenharmony_ci  );
3991cb0ef41Sopenharmony_ci}
4001cb0ef41Sopenharmony_ci
4011cb0ef41Sopenharmony_ci// mkdir
4021cb0ef41Sopenharmony_ci{
4031cb0ef41Sopenharmony_ci  const validateError = (err) => {
4041cb0ef41Sopenharmony_ci    assert.strictEqual(existingFile, err.path);
4051cb0ef41Sopenharmony_ci    assert.strictEqual(
4061cb0ef41Sopenharmony_ci      err.message,
4071cb0ef41Sopenharmony_ci      `EEXIST: file already exists, mkdir '${existingFile}'`);
4081cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_EEXIST);
4091cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'EEXIST');
4101cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'mkdir');
4111cb0ef41Sopenharmony_ci    return true;
4121cb0ef41Sopenharmony_ci  };
4131cb0ef41Sopenharmony_ci
4141cb0ef41Sopenharmony_ci  fs.mkdir(existingFile, 0o666, common.mustCall(validateError));
4151cb0ef41Sopenharmony_ci
4161cb0ef41Sopenharmony_ci  assert.throws(
4171cb0ef41Sopenharmony_ci    () => fs.mkdirSync(existingFile, 0o666),
4181cb0ef41Sopenharmony_ci    validateError
4191cb0ef41Sopenharmony_ci  );
4201cb0ef41Sopenharmony_ci}
4211cb0ef41Sopenharmony_ci
4221cb0ef41Sopenharmony_ci// chmod
4231cb0ef41Sopenharmony_ci{
4241cb0ef41Sopenharmony_ci  const validateError = (err) => {
4251cb0ef41Sopenharmony_ci    assert.strictEqual(nonexistentFile, err.path);
4261cb0ef41Sopenharmony_ci    assert.strictEqual(
4271cb0ef41Sopenharmony_ci      err.message,
4281cb0ef41Sopenharmony_ci      `ENOENT: no such file or directory, chmod '${nonexistentFile}'`);
4291cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
4301cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
4311cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'chmod');
4321cb0ef41Sopenharmony_ci    return true;
4331cb0ef41Sopenharmony_ci  };
4341cb0ef41Sopenharmony_ci
4351cb0ef41Sopenharmony_ci  fs.chmod(nonexistentFile, 0o666, common.mustCall(validateError));
4361cb0ef41Sopenharmony_ci
4371cb0ef41Sopenharmony_ci  assert.throws(
4381cb0ef41Sopenharmony_ci    () => fs.chmodSync(nonexistentFile, 0o666),
4391cb0ef41Sopenharmony_ci    validateError
4401cb0ef41Sopenharmony_ci  );
4411cb0ef41Sopenharmony_ci}
4421cb0ef41Sopenharmony_ci
4431cb0ef41Sopenharmony_ci// open
4441cb0ef41Sopenharmony_ci{
4451cb0ef41Sopenharmony_ci  const validateError = (err) => {
4461cb0ef41Sopenharmony_ci    assert.strictEqual(nonexistentFile, err.path);
4471cb0ef41Sopenharmony_ci    assert.strictEqual(
4481cb0ef41Sopenharmony_ci      err.message,
4491cb0ef41Sopenharmony_ci      `ENOENT: no such file or directory, open '${nonexistentFile}'`);
4501cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
4511cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
4521cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'open');
4531cb0ef41Sopenharmony_ci    return true;
4541cb0ef41Sopenharmony_ci  };
4551cb0ef41Sopenharmony_ci
4561cb0ef41Sopenharmony_ci  fs.open(nonexistentFile, 'r', 0o666, common.mustCall(validateError));
4571cb0ef41Sopenharmony_ci
4581cb0ef41Sopenharmony_ci  assert.throws(
4591cb0ef41Sopenharmony_ci    () => fs.openSync(nonexistentFile, 'r', 0o666),
4601cb0ef41Sopenharmony_ci    validateError
4611cb0ef41Sopenharmony_ci  );
4621cb0ef41Sopenharmony_ci}
4631cb0ef41Sopenharmony_ci
4641cb0ef41Sopenharmony_ci
4651cb0ef41Sopenharmony_ci// close
4661cb0ef41Sopenharmony_ci{
4671cb0ef41Sopenharmony_ci  const validateError = (err) => {
4681cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'EBADF: bad file descriptor, close');
4691cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_EBADF);
4701cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'EBADF');
4711cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'close');
4721cb0ef41Sopenharmony_ci    return true;
4731cb0ef41Sopenharmony_ci  };
4741cb0ef41Sopenharmony_ci
4751cb0ef41Sopenharmony_ci  common.runWithInvalidFD((fd) => {
4761cb0ef41Sopenharmony_ci    fs.close(fd, common.mustCall(validateError));
4771cb0ef41Sopenharmony_ci
4781cb0ef41Sopenharmony_ci    assert.throws(
4791cb0ef41Sopenharmony_ci      () => fs.closeSync(fd),
4801cb0ef41Sopenharmony_ci      validateError
4811cb0ef41Sopenharmony_ci    );
4821cb0ef41Sopenharmony_ci  });
4831cb0ef41Sopenharmony_ci}
4841cb0ef41Sopenharmony_ci
4851cb0ef41Sopenharmony_ci// readFile
4861cb0ef41Sopenharmony_ci{
4871cb0ef41Sopenharmony_ci  const validateError = (err) => {
4881cb0ef41Sopenharmony_ci    assert.strictEqual(nonexistentFile, err.path);
4891cb0ef41Sopenharmony_ci    assert.strictEqual(
4901cb0ef41Sopenharmony_ci      err.message,
4911cb0ef41Sopenharmony_ci      `ENOENT: no such file or directory, open '${nonexistentFile}'`);
4921cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
4931cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
4941cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'open');
4951cb0ef41Sopenharmony_ci    return true;
4961cb0ef41Sopenharmony_ci  };
4971cb0ef41Sopenharmony_ci
4981cb0ef41Sopenharmony_ci  fs.readFile(nonexistentFile, common.mustCall(validateError));
4991cb0ef41Sopenharmony_ci
5001cb0ef41Sopenharmony_ci  assert.throws(
5011cb0ef41Sopenharmony_ci    () => fs.readFileSync(nonexistentFile),
5021cb0ef41Sopenharmony_ci    validateError
5031cb0ef41Sopenharmony_ci  );
5041cb0ef41Sopenharmony_ci}
5051cb0ef41Sopenharmony_ci
5061cb0ef41Sopenharmony_ci// readdir
5071cb0ef41Sopenharmony_ci{
5081cb0ef41Sopenharmony_ci  const validateError = (err) => {
5091cb0ef41Sopenharmony_ci    assert.strictEqual(nonexistentFile, err.path);
5101cb0ef41Sopenharmony_ci    assert.strictEqual(
5111cb0ef41Sopenharmony_ci      err.message,
5121cb0ef41Sopenharmony_ci      `ENOENT: no such file or directory, scandir '${nonexistentFile}'`);
5131cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
5141cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
5151cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'scandir');
5161cb0ef41Sopenharmony_ci    return true;
5171cb0ef41Sopenharmony_ci  };
5181cb0ef41Sopenharmony_ci
5191cb0ef41Sopenharmony_ci  fs.readdir(nonexistentFile, common.mustCall(validateError));
5201cb0ef41Sopenharmony_ci
5211cb0ef41Sopenharmony_ci  assert.throws(
5221cb0ef41Sopenharmony_ci    () => fs.readdirSync(nonexistentFile),
5231cb0ef41Sopenharmony_ci    validateError
5241cb0ef41Sopenharmony_ci  );
5251cb0ef41Sopenharmony_ci}
5261cb0ef41Sopenharmony_ci
5271cb0ef41Sopenharmony_ci// ftruncate
5281cb0ef41Sopenharmony_ci{
5291cb0ef41Sopenharmony_ci  const validateError = (err) => {
5301cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'ftruncate');
5311cb0ef41Sopenharmony_ci    // Could be EBADF or EINVAL, depending on the platform
5321cb0ef41Sopenharmony_ci    if (err.code === 'EBADF') {
5331cb0ef41Sopenharmony_ci      assert.strictEqual(err.message, 'EBADF: bad file descriptor, ftruncate');
5341cb0ef41Sopenharmony_ci      assert.strictEqual(err.errno, UV_EBADF);
5351cb0ef41Sopenharmony_ci    } else {
5361cb0ef41Sopenharmony_ci      assert.strictEqual(err.message, 'EINVAL: invalid argument, ftruncate');
5371cb0ef41Sopenharmony_ci      assert.strictEqual(err.errno, UV_EINVAL);
5381cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 'EINVAL');
5391cb0ef41Sopenharmony_ci    }
5401cb0ef41Sopenharmony_ci    return true;
5411cb0ef41Sopenharmony_ci  };
5421cb0ef41Sopenharmony_ci
5431cb0ef41Sopenharmony_ci  common.runWithInvalidFD((fd) => {
5441cb0ef41Sopenharmony_ci    fs.ftruncate(fd, 4, common.mustCall(validateError));
5451cb0ef41Sopenharmony_ci
5461cb0ef41Sopenharmony_ci    assert.throws(
5471cb0ef41Sopenharmony_ci      () => fs.ftruncateSync(fd, 4),
5481cb0ef41Sopenharmony_ci      validateError
5491cb0ef41Sopenharmony_ci    );
5501cb0ef41Sopenharmony_ci  });
5511cb0ef41Sopenharmony_ci}
5521cb0ef41Sopenharmony_ci
5531cb0ef41Sopenharmony_ci// fdatasync
5541cb0ef41Sopenharmony_ci{
5551cb0ef41Sopenharmony_ci  const validateError = (err) => {
5561cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'EBADF: bad file descriptor, fdatasync');
5571cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_EBADF);
5581cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'EBADF');
5591cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'fdatasync');
5601cb0ef41Sopenharmony_ci    return true;
5611cb0ef41Sopenharmony_ci  };
5621cb0ef41Sopenharmony_ci
5631cb0ef41Sopenharmony_ci  common.runWithInvalidFD((fd) => {
5641cb0ef41Sopenharmony_ci    fs.fdatasync(fd, common.mustCall(validateError));
5651cb0ef41Sopenharmony_ci
5661cb0ef41Sopenharmony_ci    assert.throws(
5671cb0ef41Sopenharmony_ci      () => fs.fdatasyncSync(fd),
5681cb0ef41Sopenharmony_ci      validateError
5691cb0ef41Sopenharmony_ci    );
5701cb0ef41Sopenharmony_ci  });
5711cb0ef41Sopenharmony_ci}
5721cb0ef41Sopenharmony_ci
5731cb0ef41Sopenharmony_ci// fsync
5741cb0ef41Sopenharmony_ci{
5751cb0ef41Sopenharmony_ci  const validateError = (err) => {
5761cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'EBADF: bad file descriptor, fsync');
5771cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_EBADF);
5781cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'EBADF');
5791cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'fsync');
5801cb0ef41Sopenharmony_ci    return true;
5811cb0ef41Sopenharmony_ci  };
5821cb0ef41Sopenharmony_ci
5831cb0ef41Sopenharmony_ci  common.runWithInvalidFD((fd) => {
5841cb0ef41Sopenharmony_ci    fs.fsync(fd, common.mustCall(validateError));
5851cb0ef41Sopenharmony_ci
5861cb0ef41Sopenharmony_ci    assert.throws(
5871cb0ef41Sopenharmony_ci      () => fs.fsyncSync(fd),
5881cb0ef41Sopenharmony_ci      validateError
5891cb0ef41Sopenharmony_ci    );
5901cb0ef41Sopenharmony_ci  });
5911cb0ef41Sopenharmony_ci}
5921cb0ef41Sopenharmony_ci
5931cb0ef41Sopenharmony_ci// chown
5941cb0ef41Sopenharmony_ciif (!common.isWindows) {
5951cb0ef41Sopenharmony_ci  const validateError = (err) => {
5961cb0ef41Sopenharmony_ci    assert.strictEqual(nonexistentFile, err.path);
5971cb0ef41Sopenharmony_ci    assert.strictEqual(
5981cb0ef41Sopenharmony_ci      err.message,
5991cb0ef41Sopenharmony_ci      `ENOENT: no such file or directory, chown '${nonexistentFile}'`);
6001cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
6011cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
6021cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'chown');
6031cb0ef41Sopenharmony_ci    return true;
6041cb0ef41Sopenharmony_ci  };
6051cb0ef41Sopenharmony_ci
6061cb0ef41Sopenharmony_ci  fs.chown(nonexistentFile, process.getuid(), process.getgid(),
6071cb0ef41Sopenharmony_ci           common.mustCall(validateError));
6081cb0ef41Sopenharmony_ci
6091cb0ef41Sopenharmony_ci  assert.throws(
6101cb0ef41Sopenharmony_ci    () => fs.chownSync(nonexistentFile,
6111cb0ef41Sopenharmony_ci                       process.getuid(), process.getgid()),
6121cb0ef41Sopenharmony_ci    validateError
6131cb0ef41Sopenharmony_ci  );
6141cb0ef41Sopenharmony_ci}
6151cb0ef41Sopenharmony_ci
6161cb0ef41Sopenharmony_ci// utimes
6171cb0ef41Sopenharmony_ciif (!common.isAIX) {
6181cb0ef41Sopenharmony_ci  const validateError = (err) => {
6191cb0ef41Sopenharmony_ci    assert.strictEqual(nonexistentFile, err.path);
6201cb0ef41Sopenharmony_ci    assert.strictEqual(
6211cb0ef41Sopenharmony_ci      err.message,
6221cb0ef41Sopenharmony_ci      `ENOENT: no such file or directory, utime '${nonexistentFile}'`);
6231cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
6241cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
6251cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'utime');
6261cb0ef41Sopenharmony_ci    return true;
6271cb0ef41Sopenharmony_ci  };
6281cb0ef41Sopenharmony_ci
6291cb0ef41Sopenharmony_ci  fs.utimes(nonexistentFile, new Date(), new Date(),
6301cb0ef41Sopenharmony_ci            common.mustCall(validateError));
6311cb0ef41Sopenharmony_ci
6321cb0ef41Sopenharmony_ci  assert.throws(
6331cb0ef41Sopenharmony_ci    () => fs.utimesSync(nonexistentFile, new Date(), new Date()),
6341cb0ef41Sopenharmony_ci    validateError
6351cb0ef41Sopenharmony_ci  );
6361cb0ef41Sopenharmony_ci}
6371cb0ef41Sopenharmony_ci
6381cb0ef41Sopenharmony_ci// mkdtemp
6391cb0ef41Sopenharmony_ci{
6401cb0ef41Sopenharmony_ci  const validateError = (err) => {
6411cb0ef41Sopenharmony_ci    const pathPrefix = new RegExp('^' + re`${nonexistentDir}`);
6421cb0ef41Sopenharmony_ci    assert.match(err.path, pathPrefix);
6431cb0ef41Sopenharmony_ci
6441cb0ef41Sopenharmony_ci    const prefix = new RegExp('^ENOENT: no such file or directory, mkdtemp ' +
6451cb0ef41Sopenharmony_ci                              re`'${nonexistentDir}`);
6461cb0ef41Sopenharmony_ci    assert.match(err.message, prefix);
6471cb0ef41Sopenharmony_ci
6481cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
6491cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
6501cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'mkdtemp');
6511cb0ef41Sopenharmony_ci    return true;
6521cb0ef41Sopenharmony_ci  };
6531cb0ef41Sopenharmony_ci
6541cb0ef41Sopenharmony_ci  fs.mkdtemp(nonexistentDir, common.mustCall(validateError));
6551cb0ef41Sopenharmony_ci
6561cb0ef41Sopenharmony_ci  assert.throws(
6571cb0ef41Sopenharmony_ci    () => fs.mkdtempSync(nonexistentDir),
6581cb0ef41Sopenharmony_ci    validateError
6591cb0ef41Sopenharmony_ci  );
6601cb0ef41Sopenharmony_ci}
6611cb0ef41Sopenharmony_ci
6621cb0ef41Sopenharmony_ci// Check copyFile with invalid modes.
6631cb0ef41Sopenharmony_ci{
6641cb0ef41Sopenharmony_ci  const validateError = {
6651cb0ef41Sopenharmony_ci    code: 'ERR_OUT_OF_RANGE',
6661cb0ef41Sopenharmony_ci  };
6671cb0ef41Sopenharmony_ci
6681cb0ef41Sopenharmony_ci  assert.throws(
6691cb0ef41Sopenharmony_ci    () => fs.copyFile(existingFile, nonexistentFile, -1, () => {}),
6701cb0ef41Sopenharmony_ci    validateError
6711cb0ef41Sopenharmony_ci  );
6721cb0ef41Sopenharmony_ci  assert.throws(
6731cb0ef41Sopenharmony_ci    () => fs.copyFileSync(existingFile, nonexistentFile, -1),
6741cb0ef41Sopenharmony_ci    validateError
6751cb0ef41Sopenharmony_ci  );
6761cb0ef41Sopenharmony_ci}
6771cb0ef41Sopenharmony_ci
6781cb0ef41Sopenharmony_ci// copyFile: destination exists but the COPYFILE_EXCL flag is provided.
6791cb0ef41Sopenharmony_ci{
6801cb0ef41Sopenharmony_ci  const validateError = (err) => {
6811cb0ef41Sopenharmony_ci    if (err.code === 'ENOENT') {  // Could be ENOENT or EEXIST
6821cb0ef41Sopenharmony_ci      assert.strictEqual(err.message,
6831cb0ef41Sopenharmony_ci                         'ENOENT: no such file or directory, copyfile ' +
6841cb0ef41Sopenharmony_ci                         `'${existingFile}' -> '${existingFile2}'`);
6851cb0ef41Sopenharmony_ci      assert.strictEqual(err.errno, UV_ENOENT);
6861cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 'ENOENT');
6871cb0ef41Sopenharmony_ci      assert.strictEqual(err.syscall, 'copyfile');
6881cb0ef41Sopenharmony_ci    } else {
6891cb0ef41Sopenharmony_ci      assert.strictEqual(err.message,
6901cb0ef41Sopenharmony_ci                         'EEXIST: file already exists, copyfile ' +
6911cb0ef41Sopenharmony_ci                         `'${existingFile}' -> '${existingFile2}'`);
6921cb0ef41Sopenharmony_ci      assert.strictEqual(err.errno, UV_EEXIST);
6931cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 'EEXIST');
6941cb0ef41Sopenharmony_ci      assert.strictEqual(err.syscall, 'copyfile');
6951cb0ef41Sopenharmony_ci    }
6961cb0ef41Sopenharmony_ci    return true;
6971cb0ef41Sopenharmony_ci  };
6981cb0ef41Sopenharmony_ci
6991cb0ef41Sopenharmony_ci  fs.copyFile(existingFile, existingFile2, COPYFILE_EXCL,
7001cb0ef41Sopenharmony_ci              common.mustCall(validateError));
7011cb0ef41Sopenharmony_ci
7021cb0ef41Sopenharmony_ci  assert.throws(
7031cb0ef41Sopenharmony_ci    () => fs.copyFileSync(existingFile, existingFile2, COPYFILE_EXCL),
7041cb0ef41Sopenharmony_ci    validateError
7051cb0ef41Sopenharmony_ci  );
7061cb0ef41Sopenharmony_ci}
7071cb0ef41Sopenharmony_ci
7081cb0ef41Sopenharmony_ci// copyFile: the source does not exist.
7091cb0ef41Sopenharmony_ci{
7101cb0ef41Sopenharmony_ci  const validateError = (err) => {
7111cb0ef41Sopenharmony_ci    assert.strictEqual(err.message,
7121cb0ef41Sopenharmony_ci                       'ENOENT: no such file or directory, copyfile ' +
7131cb0ef41Sopenharmony_ci                       `'${nonexistentFile}' -> '${existingFile2}'`);
7141cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_ENOENT);
7151cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ENOENT');
7161cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'copyfile');
7171cb0ef41Sopenharmony_ci    return true;
7181cb0ef41Sopenharmony_ci  };
7191cb0ef41Sopenharmony_ci
7201cb0ef41Sopenharmony_ci  fs.copyFile(nonexistentFile, existingFile2, COPYFILE_EXCL,
7211cb0ef41Sopenharmony_ci              common.mustCall(validateError));
7221cb0ef41Sopenharmony_ci
7231cb0ef41Sopenharmony_ci  assert.throws(
7241cb0ef41Sopenharmony_ci    () => fs.copyFileSync(nonexistentFile, existingFile2, COPYFILE_EXCL),
7251cb0ef41Sopenharmony_ci    validateError
7261cb0ef41Sopenharmony_ci  );
7271cb0ef41Sopenharmony_ci}
7281cb0ef41Sopenharmony_ci
7291cb0ef41Sopenharmony_ci// read
7301cb0ef41Sopenharmony_ci{
7311cb0ef41Sopenharmony_ci  const validateError = (err) => {
7321cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'EBADF: bad file descriptor, read');
7331cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_EBADF);
7341cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'EBADF');
7351cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'read');
7361cb0ef41Sopenharmony_ci    return true;
7371cb0ef41Sopenharmony_ci  };
7381cb0ef41Sopenharmony_ci
7391cb0ef41Sopenharmony_ci  common.runWithInvalidFD((fd) => {
7401cb0ef41Sopenharmony_ci    const buf = Buffer.alloc(5);
7411cb0ef41Sopenharmony_ci    fs.read(fd, buf, 0, 1, 1, common.mustCall(validateError));
7421cb0ef41Sopenharmony_ci
7431cb0ef41Sopenharmony_ci    assert.throws(
7441cb0ef41Sopenharmony_ci      () => fs.readSync(fd, buf, 0, 1, 1),
7451cb0ef41Sopenharmony_ci      validateError
7461cb0ef41Sopenharmony_ci    );
7471cb0ef41Sopenharmony_ci  });
7481cb0ef41Sopenharmony_ci}
7491cb0ef41Sopenharmony_ci
7501cb0ef41Sopenharmony_ci// fchmod
7511cb0ef41Sopenharmony_ci{
7521cb0ef41Sopenharmony_ci  const validateError = (err) => {
7531cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'EBADF: bad file descriptor, fchmod');
7541cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_EBADF);
7551cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'EBADF');
7561cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'fchmod');
7571cb0ef41Sopenharmony_ci    return true;
7581cb0ef41Sopenharmony_ci  };
7591cb0ef41Sopenharmony_ci
7601cb0ef41Sopenharmony_ci  common.runWithInvalidFD((fd) => {
7611cb0ef41Sopenharmony_ci    fs.fchmod(fd, 0o666, common.mustCall(validateError));
7621cb0ef41Sopenharmony_ci
7631cb0ef41Sopenharmony_ci    assert.throws(
7641cb0ef41Sopenharmony_ci      () => fs.fchmodSync(fd, 0o666),
7651cb0ef41Sopenharmony_ci      validateError
7661cb0ef41Sopenharmony_ci    );
7671cb0ef41Sopenharmony_ci  });
7681cb0ef41Sopenharmony_ci}
7691cb0ef41Sopenharmony_ci
7701cb0ef41Sopenharmony_ci// fchown
7711cb0ef41Sopenharmony_ciif (!common.isWindows) {
7721cb0ef41Sopenharmony_ci  const validateError = (err) => {
7731cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'EBADF: bad file descriptor, fchown');
7741cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_EBADF);
7751cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'EBADF');
7761cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'fchown');
7771cb0ef41Sopenharmony_ci    return true;
7781cb0ef41Sopenharmony_ci  };
7791cb0ef41Sopenharmony_ci
7801cb0ef41Sopenharmony_ci  common.runWithInvalidFD((fd) => {
7811cb0ef41Sopenharmony_ci    fs.fchown(fd, process.getuid(), process.getgid(),
7821cb0ef41Sopenharmony_ci              common.mustCall(validateError));
7831cb0ef41Sopenharmony_ci
7841cb0ef41Sopenharmony_ci    assert.throws(
7851cb0ef41Sopenharmony_ci      () => fs.fchownSync(fd, process.getuid(), process.getgid()),
7861cb0ef41Sopenharmony_ci      validateError
7871cb0ef41Sopenharmony_ci    );
7881cb0ef41Sopenharmony_ci  });
7891cb0ef41Sopenharmony_ci}
7901cb0ef41Sopenharmony_ci
7911cb0ef41Sopenharmony_ci// write buffer
7921cb0ef41Sopenharmony_ci{
7931cb0ef41Sopenharmony_ci  const validateError = (err) => {
7941cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'EBADF: bad file descriptor, write');
7951cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_EBADF);
7961cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'EBADF');
7971cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'write');
7981cb0ef41Sopenharmony_ci    return true;
7991cb0ef41Sopenharmony_ci  };
8001cb0ef41Sopenharmony_ci
8011cb0ef41Sopenharmony_ci  common.runWithInvalidFD((fd) => {
8021cb0ef41Sopenharmony_ci    const buf = Buffer.alloc(5);
8031cb0ef41Sopenharmony_ci    fs.write(fd, buf, 0, 1, 1, common.mustCall(validateError));
8041cb0ef41Sopenharmony_ci
8051cb0ef41Sopenharmony_ci    assert.throws(
8061cb0ef41Sopenharmony_ci      () => fs.writeSync(fd, buf, 0, 1, 1),
8071cb0ef41Sopenharmony_ci      validateError
8081cb0ef41Sopenharmony_ci    );
8091cb0ef41Sopenharmony_ci  });
8101cb0ef41Sopenharmony_ci}
8111cb0ef41Sopenharmony_ci
8121cb0ef41Sopenharmony_ci// write string
8131cb0ef41Sopenharmony_ci{
8141cb0ef41Sopenharmony_ci  const validateError = (err) => {
8151cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'EBADF: bad file descriptor, write');
8161cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_EBADF);
8171cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'EBADF');
8181cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'write');
8191cb0ef41Sopenharmony_ci    return true;
8201cb0ef41Sopenharmony_ci  };
8211cb0ef41Sopenharmony_ci
8221cb0ef41Sopenharmony_ci  common.runWithInvalidFD((fd) => {
8231cb0ef41Sopenharmony_ci    fs.write(fd, 'test', 1, common.mustCall(validateError));
8241cb0ef41Sopenharmony_ci
8251cb0ef41Sopenharmony_ci    assert.throws(
8261cb0ef41Sopenharmony_ci      () => fs.writeSync(fd, 'test', 1),
8271cb0ef41Sopenharmony_ci      validateError
8281cb0ef41Sopenharmony_ci    );
8291cb0ef41Sopenharmony_ci  });
8301cb0ef41Sopenharmony_ci}
8311cb0ef41Sopenharmony_ci
8321cb0ef41Sopenharmony_ci
8331cb0ef41Sopenharmony_ci// futimes
8341cb0ef41Sopenharmony_ciif (!common.isAIX) {
8351cb0ef41Sopenharmony_ci  const validateError = (err) => {
8361cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'EBADF: bad file descriptor, futime');
8371cb0ef41Sopenharmony_ci    assert.strictEqual(err.errno, UV_EBADF);
8381cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'EBADF');
8391cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'futime');
8401cb0ef41Sopenharmony_ci    return true;
8411cb0ef41Sopenharmony_ci  };
8421cb0ef41Sopenharmony_ci
8431cb0ef41Sopenharmony_ci  common.runWithInvalidFD((fd) => {
8441cb0ef41Sopenharmony_ci    fs.futimes(fd, new Date(), new Date(), common.mustCall(validateError));
8451cb0ef41Sopenharmony_ci
8461cb0ef41Sopenharmony_ci    assert.throws(
8471cb0ef41Sopenharmony_ci      () => fs.futimesSync(fd, new Date(), new Date()),
8481cb0ef41Sopenharmony_ci      validateError
8491cb0ef41Sopenharmony_ci    );
8501cb0ef41Sopenharmony_ci  });
8511cb0ef41Sopenharmony_ci}
852