nodejs/test/parallel/test-fs-mkdir-mode-mask.js
Joyee Cheung a18e130e59
lib: mask mode_t type of arguments with 0o777
- Introduce the `validateAndMaskMode` validator that
  validates `mode_t` arguments and mask them with 0o777
  if they are 32-bit unsigned integer or octal string
  to be more consistent with POSIX APIs.
- Use the validator in fs APIs and process.umask for
  consistency.
- Add tests for 32-bit unsigned modes larger than 0o777.

PR-URL: https://github.com/nodejs/node/pull/20636
Fixes: https://github.com/nodejs/node/issues/20498
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
2018-05-17 17:14:35 +08:00

46 lines
1.0 KiB
JavaScript

'use strict';
// This tests that mode > 0o777 will be masked off with 0o777 in fs.mkdir().
const common = require('../common');
const assert = require('assert');
const path = require('path');
const fs = require('fs');
let mode;
if (common.isWindows) {
common.skip('mode is not supported in mkdir on Windows');
return;
} else {
mode = 0o644;
}
const maskToIgnore = 0o10000;
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
function test(mode, asString) {
const suffix = asString ? 'str' : 'num';
const input = asString ?
(mode | maskToIgnore).toString(8) : (mode | maskToIgnore);
{
const dir = path.join(tmpdir.path, `mkdirSync-${suffix}`);
fs.mkdirSync(dir, input);
assert.strictEqual(fs.statSync(dir).mode & 0o777, mode);
}
{
const dir = path.join(tmpdir.path, `mkdir-${suffix}`);
fs.mkdir(dir, input, common.mustCall((err) => {
assert.ifError(err);
assert.strictEqual(fs.statSync(dir).mode & 0o777, mode);
}));
}
}
test(mode, true);
test(mode, false);