summaryrefslogtreecommitdiffstats
path: root/toolkit/components/osfile/tests/xpcshell/test_osfile_win_async_setPermissions.js
blob: b2708274c24020a1c5ee94621e9eacd50e5da8d3 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* 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(async function test_path_setPermissions() {
  let path = OS.Path.join(
    OS.Constants.Path.tmpDir,
    "test_osfile_win_async_setPermissions_path.tmp"
  );
  await OS.File.writeAtomic(path, new Uint8Array(1));

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

      let stat = await OS.File.stat(path);
      info("Got stat winAttributes: " + JSON.stringify(stat.winAttributes));

      Assert.equal(stat.winAttributes.readOnly, attributesExpected.readOnly);
      Assert.equal(stat.winAttributes.system, attributesExpected.system);
      Assert.equal(stat.winAttributes.hidden, attributesExpected.hidden);
    }
  } finally {
    await OS.File.remove(path);
  }
});

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

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

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

// Test application to Check setPermissions on a non-existant file path.
add_task(async function test_non_existant_file_path_setPermissions() {
  let path = OS.Path.join(
    OS.Constants.Path.tmpDir,
    "test_osfile_win_async_setPermissions_path.tmp"
  );
  await 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(async function test_closed_file_handle_setPermissions() {
  let path = OS.Path.join(
    OS.Constants.Path.tmpDir,
    "test_osfile_win_async_setPermissions_path.tmp"
  );
  await OS.File.writeAtomic(path, new Uint8Array(1));

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