summaryrefslogtreecommitdiffstats
path: root/toolkit/components/osfile/tests/xpcshell/test_osfile_win_async_setPermissions.js
blob: 990d722f5c434b4513aec3858fec4910cde610a9 (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
104
105
106
107
108
109
110
111
112
113
114
/* 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 1022816)
 * The manifest tests on Windows.
 */

// Sequence of setPermission parameters.
var testSequence = [
  [ { winAttributes: { readOnly: true, system: true, hidden: true } },
    { readOnly: true, system: true, hidden: true } ],
  [ { winAttributes: { readOnly: false } },
    { readOnly: false, system: true, hidden: true } ],
  [ { winAttributes: { system: false } },
    { readOnly: false, system: false, hidden: true } ],
  [ { winAttributes: { hidden: false } },
    { readOnly: false, system: false, hidden: false } ],
  [ { winAttributes: {readOnly: true, system: false, hidden: false} },
    { readOnly: true, system: false, hidden: false } ],
  [ { winAttributes: {readOnly: false, system: true, hidden: false} },
    { readOnly: false, system: true, hidden: false } ],
  [ { winAttributes: {readOnly: false, system: false, hidden: true} },
    { readOnly: false, system: false, hidden: true } ],
];

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

  try {
    for (let [options, attributesExpected] 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_print("Got stat winAttributes: " + JSON.stringify(stat.winAttributes));

      do_check_eq(stat.winAttributes.readOnly, attributesExpected.readOnly);
      do_check_eq(stat.winAttributes.system, attributesExpected.system);
      do_check_eq(stat.winAttributes.hidden, attributesExpected.hidden);

    }
  } 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_win_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, attributesExpected] of testSequence) {
        if (options !== null) {
          do_print("Setting permissions to " + JSON.stringify(options));
          yield fd.setPermissions(options);
        }

        let stat = yield fd.stat();
        do_print("Got stat winAttributes: " + JSON.stringify(stat.winAttributes));
        do_check_eq(stat.winAttributes.readOnly, attributesExpected.readOnly);
        do_check_eq(stat.winAttributes.system, attributesExpected.system);
        do_check_eq(stat.winAttributes.hidden, attributesExpected.hidden);
      }
    } finally {
      yield fd.close();
    }
  } finally {
    yield OS.File.remove(path);
  }
});

// Test application to Check setPermissions on a non-existant file path.
add_task(function* test_non_existant_file_path_setPermissions() {
  let path = OS.Path.join(OS.Constants.Path.tmpDir,
                          "test_osfile_win_async_setPermissions_path.tmp");
  Assert.rejects(OS.File.setPermissions(path, {winAttributes: {readOnly: true}}),
                 /The system cannot find the file specified/,
                 "setPermissions failed as expected on a non-existant file path");
});

// Test application to Check setPermissions on a invalid file handle.
add_task(function* test_closed_file_handle_setPermissions() {
  let path = OS.Path.join(OS.Constants.Path.tmpDir,
                          "test_osfile_win_async_setPermissions_path.tmp");
  yield OS.File.writeAtomic(path, new Uint8Array(1));

  try {
    let fd = yield OS.File.open(path, { write: true });
    yield fd.close();
    Assert.rejects(fd.setPermissions(path, {winAttributes: {readOnly: true}}),
                   /The handle is invalid/,
                   "setPermissions failed as expected on a invalid file handle");
  } finally {
    yield OS.File.remove(path);
  }
});

function run_test() {
  run_next_test();
}