• 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.mustCall((err) => {
33      assert.ifError(err);
34      assert.strictEqual(fs.statSync(file).mode & 0o777, mode);
35    }));
36  }
37
38  {
39    const file = path.join(tmpdir.path, `chmodSync-${suffix}.txt`);
40    fs.writeFileSync(file, 'test', 'utf-8');
41
42    fs.chmodSync(file, input);
43    assert.strictEqual(fs.statSync(file).mode & 0o777, mode);
44  }
45
46  {
47    const file = path.join(tmpdir.path, `fchmod-async-${suffix}.txt`);
48    fs.writeFileSync(file, 'test', 'utf-8');
49    fs.open(file, 'w', common.mustCall((err, fd) => {
50      assert.ifError(err);
51
52      fs.fchmod(fd, input, common.mustCall((err) => {
53        assert.ifError(err);
54        assert.strictEqual(fs.fstatSync(fd).mode & 0o777, mode);
55        fs.close(fd, assert.ifError);
56      }));
57    }));
58  }
59
60  {
61    const file = path.join(tmpdir.path, `fchmodSync-${suffix}.txt`);
62    fs.writeFileSync(file, 'test', 'utf-8');
63    const fd = fs.openSync(file, 'w');
64
65    fs.fchmodSync(fd, input);
66    assert.strictEqual(fs.fstatSync(fd).mode & 0o777, mode);
67
68    fs.close(fd, assert.ifError);
69  }
70
71  if (fs.lchmod) {
72    const link = path.join(tmpdir.path, `lchmod-src-${suffix}`);
73    const file = path.join(tmpdir.path, `lchmod-dest-${suffix}`);
74    fs.writeFileSync(file, 'test', 'utf-8');
75    fs.symlinkSync(file, link);
76
77    fs.lchmod(link, input, common.mustCall((err) => {
78      assert.ifError(err);
79      assert.strictEqual(fs.lstatSync(link).mode & 0o777, mode);
80    }));
81  }
82
83  if (fs.lchmodSync) {
84    const link = path.join(tmpdir.path, `lchmodSync-src-${suffix}`);
85    const file = path.join(tmpdir.path, `lchmodSync-dest-${suffix}`);
86    fs.writeFileSync(file, 'test', 'utf-8');
87    fs.symlinkSync(file, link);
88
89    fs.lchmodSync(link, input);
90    assert.strictEqual(fs.lstatSync(link).mode & 0o777, mode);
91  }
92}
93
94test(mode, true);
95test(mode, false);
96