• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// This tests that the lower bits of mode > 0o777 still works in fs APIs.
4
5const common = require('../common');
6const assert = require('assert');
7const path = require('path');
8const fs = require('fs');
9
10let mode;
11// On Windows chmod is only able to manipulate write permission
12if (common.isWindows) {
13  mode = 0o444;  // read-only
14} else {
15  mode = 0o777;
16}
17
18const maskToIgnore = 0o10000;
19
20const tmpdir = require('../common/tmpdir');
21tmpdir.refresh();
22
23function test(mode, asString) {
24  const suffix = asString ? 'str' : 'num';
25  const input = asString ?
26    (mode | maskToIgnore).toString(8) : (mode | maskToIgnore);
27
28  {
29    const file = path.join(tmpdir.path, `chmod-async-${suffix}.txt`);
30    fs.writeFileSync(file, 'test', 'utf-8');
31
32    fs.chmod(file, input, common.mustSucceed(() => {
33      assert.strictEqual(fs.statSync(file).mode & 0o777, mode);
34    }));
35  }
36
37  {
38    const file = path.join(tmpdir.path, `chmodSync-${suffix}.txt`);
39    fs.writeFileSync(file, 'test', 'utf-8');
40
41    fs.chmodSync(file, input);
42    assert.strictEqual(fs.statSync(file).mode & 0o777, mode);
43  }
44
45  {
46    const file = path.join(tmpdir.path, `fchmod-async-${suffix}.txt`);
47    fs.writeFileSync(file, 'test', 'utf-8');
48    fs.open(file, 'w', common.mustSucceed((fd) => {
49      fs.fchmod(fd, input, common.mustSucceed(() => {
50        assert.strictEqual(fs.fstatSync(fd).mode & 0o777, mode);
51        fs.close(fd, assert.ifError);
52      }));
53    }));
54  }
55
56  {
57    const file = path.join(tmpdir.path, `fchmodSync-${suffix}.txt`);
58    fs.writeFileSync(file, 'test', 'utf-8');
59    const fd = fs.openSync(file, 'w');
60
61    fs.fchmodSync(fd, input);
62    assert.strictEqual(fs.fstatSync(fd).mode & 0o777, mode);
63
64    fs.close(fd, assert.ifError);
65  }
66
67  if (fs.lchmod) {
68    const link = path.join(tmpdir.path, `lchmod-src-${suffix}`);
69    const file = path.join(tmpdir.path, `lchmod-dest-${suffix}`);
70    fs.writeFileSync(file, 'test', 'utf-8');
71    fs.symlinkSync(file, link);
72
73    fs.lchmod(link, input, common.mustSucceed(() => {
74      assert.strictEqual(fs.lstatSync(link).mode & 0o777, mode);
75    }));
76  }
77
78  if (fs.lchmodSync) {
79    const link = path.join(tmpdir.path, `lchmodSync-src-${suffix}`);
80    const file = path.join(tmpdir.path, `lchmodSync-dest-${suffix}`);
81    fs.writeFileSync(file, 'test', 'utf-8');
82    fs.symlinkSync(file, link);
83
84    fs.lchmodSync(link, input);
85    assert.strictEqual(fs.lstatSync(link).mode & 0o777, mode);
86  }
87}
88
89test(mode, true);
90test(mode, false);
91