summaryrefslogtreecommitdiffstats
path: root/dom/system/tests/ioutils/test_ioutils_create_unique.html
blob: e447964343d098ce957414f2af3e32d5b3429395 (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
<!-- 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");

    function octalFormat(n) {
      let s = n.toString(8);
      while (s.length < 3) {
        s = `0${s}`;
      }
      return `0o${s}`;
    }

    async function check(method, path, prefix, type, perms) {
      const filename = PathUtils.filename(path);

      ok(filename.startsWith(prefix), `IOUtils.${method} uses the prefix`);
      ok(await IOUtils.exists(path), `IOUtils.${method} creates a file`);

      const stat = await IOUtils.stat(path);
      is(stat.type, type, `IOUtils.${method} creates a "${type}" file`);

      is(
        octalFormat(stat.permissions),
        octalFormat(perms),
        `IOUtils.${method} creates a file with the correct permissions`
      );
    }

    add_task(async function test_createUnique() {
      const tempDir = PathUtils.join(
        PathUtils.tempDir,
        "test_createUnique.tmp.d"
      );

      const filesToChmod = [];

      SimpleTest.registerCleanupFunction(async function test_createUnique_cleanup() {
        for (const file of filesToChmod) {
          if (await IOUtils.exists(file)) {
            await IOUtils.setPermissions(file, 0o666);
          }
        }

        await IOUtils.remove(tempDir, { recursive: true });
      });

      const isWindows = Services.appinfo.OS === "WINNT";

      info("Creating a unique directory")
      const dir = await IOUtils.createUniqueDirectory(tempDir, "unique-dir", 0o600);
      await check("createUniqueDirectory", dir, "unique-dir", "directory", isWindows ? 0o666 : 0o600);

      info("Creating a unique directory with the same prefix")
      const dir2 = await IOUtils.createUniqueDirectory(tempDir, "unique-dir", 0o700);
      await check("createUniqueDirectory", dir2, "unique-dir", "directory", isWindows ? 0o666 : 0o700);
      ok(dir !== dir2, "IOUtils.createUniqueDirectory creates unique paths");

      info("Creating a unique file");
      const file = await IOUtils.createUniqueFile(tempDir, "unique-file", 0o641);
      await check("createUniqueFile", file, "unique-file", "regular", isWindows ? 0o666 : 0o641);

      info("Creating a unique file with the same prefix");
      const file2 = await IOUtils.createUniqueFile(tempDir, "unique-file", 0o400);
      filesToChmod.push(file2);
      await check("createUniqueFile", file2, "unique-file", "regular", isWindows ? 0o444 : 0o400);
    });
  </script>
</head>

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

</html>