summaryrefslogtreecommitdiffstats
path: root/toolkit/components/osfile/tests/xpcshell/test_osfile_async_setPermissions.js
blob: ab8bf7dd9b2ca95f630d228247190cba98e6d847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * A test to ensure that OS.File.setPermissions and
 * OS.File.prototype.setPermissions are all working correctly.
 * (see bug 1001849)
 * These functions are currently Unix-specific.  The manifest skips
 * the test on Windows.
 */

/**
 * Helper function for test logging: prints a POSIX file permission mode as an
 * octal number, with a leading '0' per C (not JS) convention.  When the
 * numeric value is 0o777 or lower, it is padded on the left with zeroes to
 * four digits wide.
 * Sample outputs:  0022, 0644, 04755.
 */
function format_mode(mode) {
  if (mode <= 0o777) {
    return ("0000" + mode.toString(8)).slice(-4);
  } else {
    return "0" + mode.toString(8);
  }
}

const _umask = OS.Constants.Sys.umask;
do_print("umask: " + format_mode(_umask));

/**
 * Compute the mode that a file should have after applying the umask,
 * whatever it happens to be.
 */
function apply_umask(mode) {
  return mode & ~_umask;
}

// Sequence of setPermission parameters and expected file mode.  The first test
// checks the permissions when the file is first created.
var testSequence = [
  [null,                                        apply_umask(0o600)],
  [{ unixMode: 0o4777 },                        apply_umask(0o4777)],
  [{ unixMode: 0o4777, unixHonorUmask: false }, 0o4777],
  [{ unixMode: 0o4777, unixHonorUmask: true },  apply_umask(0o4777)],
  [undefined,                                   apply_umask(0o600)],
  [{ unixMode: 0o666 },                         apply_umask(0o666)],
  [{ unixMode: 0o600 },                         apply_umask(0o600)],
  [{ unixMode: 0 },                             0],
  [{},                                          apply_umask(0o600)],
];

// Test application to paths.
add_task(function* test_path_setPermissions() {
  let path = OS.Path.join(OS.Constants.Path.tmpDir,
                          "test_osfile_async_setPermissions_path.tmp");
  yield OS.File.writeAtomic(path, new Uint8Array(1));

  try {
    for (let [options, expectedMode] of testSequence) {
      if (options !== null) {
        do_print("Setting permissions to " + JSON.stringify(options));
        yield OS.File.setPermissions(path, options);
      }

      let stat = yield OS.File.stat(path);
      do_check_eq(format_mode(stat.unixMode), format_mode(expectedMode));
    }
  } finally {
    yield OS.File.remove(path);
  }
});

// Test application to open files.
add_task(function* test_file_setPermissions() {
  let path = OS.Path.join(OS.Constants.Path.tmpDir,
                              "test_osfile_async_setPermissions_file.tmp");
  yield OS.File.writeAtomic(path, new Uint8Array(1));

  try {
    let fd = yield OS.File.open(path, { write: true });
    try {
      for (let [options, expectedMode] of testSequence) {
        if (options !== null) {
          do_print("Setting permissions to " + JSON.stringify(options));
          yield fd.setPermissions(options);
        }

        let stat = yield fd.stat();
        do_check_eq(format_mode(stat.unixMode), format_mode(expectedMode));
      }
    } finally {
      yield fd.close();
    }
  } finally {
    yield OS.File.remove(path);
  }
});

function run_test() {
  run_next_test();
}