• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Test that fs.copyFile() respects file permissions.
4// Ref: https://github.com/nodejs/node/issues/26936
5
6const common = require('../common');
7
8if (!common.isWindows && process.getuid() === 0)
9  common.skip('as this test should not be run as `root`');
10
11if (common.isIBMi)
12  common.skip('IBMi has a different access permission mechanism');
13
14const tmpdir = require('../common/tmpdir');
15tmpdir.refresh();
16
17const assert = require('assert');
18const fs = require('fs');
19const path = require('path');
20
21let n = 0;
22
23function beforeEach() {
24  n++;
25  const source = path.join(tmpdir.path, `source${n}`);
26  const dest = path.join(tmpdir.path, `dest${n}`);
27  fs.writeFileSync(source, 'source');
28  fs.writeFileSync(dest, 'dest');
29  fs.chmodSync(dest, '444');
30
31  const check = (err) => {
32    const expected = ['EACCES', 'EPERM'];
33    assert(expected.includes(err.code), `${err.code} not in ${expected}`);
34    assert.strictEqual(fs.readFileSync(dest, 'utf8'), 'dest');
35    return true;
36  };
37
38  return { source, dest, check };
39}
40
41// Test synchronous API.
42{
43  const { source, dest, check } = beforeEach();
44  assert.throws(() => { fs.copyFileSync(source, dest); }, check);
45}
46
47// Test promises API.
48{
49  const { source, dest, check } = beforeEach();
50  (async () => {
51    await assert.rejects(fs.promises.copyFile(source, dest), check);
52  })().then(common.mustCall());
53}
54
55// Test callback API.
56{
57  const { source, dest, check } = beforeEach();
58  fs.copyFile(source, dest, common.mustCall(check));
59}
60