summaryrefslogtreecommitdiffstats
path: root/dom/system/tests/ioutils/test_ioutils_windows_file_attributes.html
blob: b452b4f6db161fe1c568f0a4511ecd1258829e6d (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/ -->
<!DOCTYPE HTML>
<html>

<head>
  <meta charset="utf-8">
  <title>Test the IOUtils file I/O API</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
  <script src="file_ioutils_test_fixtures.js"></script>
  <script>
    "use strict";

    const { Assert } = ChromeUtils.import("resource://testing-common/Assert.jsm");

    add_task(async function getSetWindowsAttributes() {
      const tmpDir = PathUtils.join(PathUtils.tempDir, "ioutils-windows-attributes.tmp.d");
      await createDir(tmpDir);
      ok(await dirExists(tmpDir), `Expected ${tmpDir} to be a directory`);

      const filePath = PathUtils.join(tmpDir, "file.tmp");
      await createFile(filePath);
      ok(await fileExists(filePath), `Expected ${filePath} to exist`);

      {
        info("Getting attributes for newly created file.");
        const attrs = await IOUtils.getWindowsAttributes(filePath);

        ok(attrs.readOnly === false, `Newly created file ${filePath} is not a read-only file`);
        ok(attrs.hidden === false, `Newly created file ${filePath} is not a hidden file`);
        ok(attrs.system === false, `Newly created file ${filePath} is not a system file`);
      }

      {
        info("Setting read-only on an existing file.");
        await IOUtils.setWindowsAttributes(filePath, { readOnly: true })
        const attrs = await IOUtils.getWindowsAttributes(filePath);

        ok(attrs.readOnly === true, `Updated file ${filePath} is a read-only file`);
        ok(attrs.hidden === false, `Updated file ${filePath} is not a hidden file`);
        ok(attrs.system === false, `Updated file ${filePath} is not a system file`);
      }

      info("Attempting to write to a read-only file.");

      await Assert.rejects(
        IOUtils.writeUTF8(filePath, "hello, world"),
        /NotAllowedError: Could not open the file at .+ for writing/,
        "IOUtils::writeUTF8 on a read-only file fails."
      );

      {
        info("Setting hidden on an existing file.");
        await IOUtils.setWindowsAttributes(filePath, { hidden: true })
        const attrs = await IOUtils.getWindowsAttributes(filePath);

        ok(attrs.readOnly === true, `Updated file ${filePath} is still a read-only file`);
        ok(attrs.hidden === true, `Updated file ${filePath} is a hidden file`);
        ok(attrs.system === false, `Updated file ${filePath} is not a system file`);
      }

      {
        info("Setting system on an existing file.");
        await IOUtils.setWindowsAttributes(filePath, { system: true })
        const attrs = await IOUtils.getWindowsAttributes(filePath);

        ok(attrs.readOnly === true, `Updated file ${filePath} is still a read-only file`);
        ok(attrs.hidden === true, `Updated file ${filePath} is still a hidden file`);
        ok(attrs.system === true, `Updated file ${filePath} is a system file`);
      }

      {
        info("Clearing all Windows attributes on an existing file.");
        await IOUtils.setWindowsAttributes(filePath, { readOnly: false, hidden: false, system: false });
        const attrs = await IOUtils.getWindowsAttributes(filePath);

        ok(attrs.readOnly === false, `Updated file ${filePath} is not a read-only file`);
        ok(attrs.hidden === false, `Updated file ${filePath} is not a hidden file`);
        ok(attrs.system === false, `Updated file ${filePath} is not a system file`);
      }

      {
        info("Setting all Windows attributes on an existing file.");
        await IOUtils.setWindowsAttributes(filePath, { readOnly: true, hidden: true, system: true });
        const attrs = await IOUtils.getWindowsAttributes(filePath);

        ok(attrs.readOnly === true, `Updated file ${filePath} is a read-only file`);
        ok(attrs.hidden === true, `Updated file ${filePath} is a hidden file`);
        ok(attrs.system === true, `Updated file ${filePath} is a system file`);
      }

      {
        info("Clearing read-only on an existing file.");
        await IOUtils.setWindowsAttributes(filePath, { readOnly: false });
        const attrs = await IOUtils.getWindowsAttributes(filePath);

        ok(attrs.readOnly === false, `Updated file ${filePath} is no longer a read-only file`);
        ok(attrs.hidden === true, `Updated file ${filePath} is still a hidden file`);
        ok(attrs.system === true, `Updated file ${filePath} is still a system file`);
      }

      {
        info("Clearing hidden on an existing file.");
        await IOUtils.setWindowsAttributes(filePath, { hidden: false });
        const attrs = await IOUtils.getWindowsAttributes(filePath);

        ok(attrs.readOnly === false, `Updated file ${filePath} is still not a read-only file`);
        ok(attrs.hidden === false, `Updated file ${filePath} is no longer a hidden file`);
        ok(attrs.system === true, `Updated file ${filePath} is still a system file`);
      }

      {
        info("Clearing system on an existing file.");
        await IOUtils.setWindowsAttributes(filePath, { system: false });
        const attrs = await IOUtils.getWindowsAttributes(filePath);

        ok(attrs.readOnly === false, `Updated file ${filePath} is still not a read-only file`);
        ok(attrs.hidden === false, `Updated file ${filePath} is sitll not a hidden file`);
        ok(attrs.system === false, `Updated file ${filePath} is no longer a system file`);
      }

      await cleanup(tmpDir);
    });

  </script>
</head>

<body>
  <p id="display"></p>
  <div id="content" style="display: none"></div>
  <pre id="test"></pre>
</body>

</html>