2019-03-27 06:32:20 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Test that fs.copyFile() respects file permissions.
|
|
|
|
// Ref: https://github.com/nodejs/node/issues/26936
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
|
2019-04-24 04:28:34 +00:00
|
|
|
if (!common.isWindows && process.getuid() === 0)
|
|
|
|
common.skip('as this test should not be run as `root`');
|
|
|
|
|
2019-12-06 07:08:57 +00:00
|
|
|
if (common.isIBMi)
|
|
|
|
common.skip('IBMi has a different access permission mechanism');
|
|
|
|
|
2019-03-27 06:32:20 +00:00
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
tmpdir.refresh();
|
|
|
|
|
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
let n = 0;
|
|
|
|
|
|
|
|
function beforeEach() {
|
|
|
|
n++;
|
|
|
|
const source = path.join(tmpdir.path, `source${n}`);
|
|
|
|
const dest = path.join(tmpdir.path, `dest${n}`);
|
|
|
|
fs.writeFileSync(source, 'source');
|
|
|
|
fs.writeFileSync(dest, 'dest');
|
|
|
|
fs.chmodSync(dest, '444');
|
|
|
|
|
|
|
|
const check = (err) => {
|
2019-04-15 15:43:56 +00:00
|
|
|
const expected = ['EACCES', 'EPERM'];
|
|
|
|
assert(expected.includes(err.code), `${err.code} not in ${expected}`);
|
2019-03-27 06:32:20 +00:00
|
|
|
assert.strictEqual(fs.readFileSync(dest, 'utf8'), 'dest');
|
2019-03-31 05:35:59 +00:00
|
|
|
return true;
|
2019-03-27 06:32:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return { source, dest, check };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test synchronous API.
|
|
|
|
{
|
|
|
|
const { source, dest, check } = beforeEach();
|
|
|
|
assert.throws(() => { fs.copyFileSync(source, dest); }, check);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test promises API.
|
|
|
|
{
|
|
|
|
const { source, dest, check } = beforeEach();
|
2019-03-31 05:35:59 +00:00
|
|
|
(async () => {
|
|
|
|
await assert.rejects(fs.promises.copyFile(source, dest), check);
|
2020-07-14 15:45:39 +00:00
|
|
|
})().then(common.mustCall());
|
2019-03-27 06:32:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test callback API.
|
|
|
|
{
|
|
|
|
const { source, dest, check } = beforeEach();
|
|
|
|
fs.copyFile(source, dest, common.mustCall(check));
|
|
|
|
}
|