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.chmod method.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst fs = require('fs');
91cb0ef41Sopenharmony_ciconst { open } = fs.promises;
101cb0ef41Sopenharmony_ciconst path = require('path');
111cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
121cb0ef41Sopenharmony_ciconst assert = require('assert');
131cb0ef41Sopenharmony_ciconst tmpDir = tmpdir.path;
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_citmpdir.refresh();
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciasync function validateFilePermission() {
181cb0ef41Sopenharmony_ci  const filePath = path.resolve(tmpDir, 'tmp-chmod.txt');
191cb0ef41Sopenharmony_ci  const fileHandle = await open(filePath, 'w+', 0o444);
201cb0ef41Sopenharmony_ci  // File created with r--r--r-- 444
211cb0ef41Sopenharmony_ci  const statsBeforeMod = fs.statSync(filePath);
221cb0ef41Sopenharmony_ci  assert.strictEqual(statsBeforeMod.mode & 0o444, 0o444);
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  let expectedAccess;
251cb0ef41Sopenharmony_ci  const newPermissions = 0o765;
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  if (common.isWindows) {
281cb0ef41Sopenharmony_ci    // Chmod in Windows will only toggle read only/write access. The
291cb0ef41Sopenharmony_ci    // fs.Stats.mode in Windows is computed using read/write
301cb0ef41Sopenharmony_ci    // bits (not exec). Read-only at best returns 444; r/w 666.
311cb0ef41Sopenharmony_ci    // Refer: /deps/uv/src/win/fs.cfs;
321cb0ef41Sopenharmony_ci    expectedAccess = 0o664;
331cb0ef41Sopenharmony_ci  } else {
341cb0ef41Sopenharmony_ci    expectedAccess = newPermissions;
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  // Change the permissions to rwxr--r-x
381cb0ef41Sopenharmony_ci  await fileHandle.chmod(newPermissions);
391cb0ef41Sopenharmony_ci  const statsAfterMod = fs.statSync(filePath);
401cb0ef41Sopenharmony_ci  assert.deepStrictEqual(statsAfterMod.mode & expectedAccess, expectedAccess);
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  await fileHandle.close();
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_civalidateFilePermission().then(common.mustCall());
46