summaryrefslogtreecommitdiffstats
path: root/toolkit/components/osfile/tests/xpcshell/test_osfile_async_append.js
blob: 8fabbad1edd112f08c51f931fd2dfdbcd154fcae (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
"use strict";

info("starting tests");

const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");

/**
 * A test to check that the |append| mode flag is correctly implemented.
 * (see bug 925865)
 */

function setup_mode(mode) {
  // Complete mode.
  let realMode = {
    read: true,
    write: true,
  };
  for (let k in mode) {
    realMode[k] = mode[k];
  }
  return realMode;
}

// Test append mode.
async function test_append(mode) {
  let path = OS.Path.join(
    OS.Constants.Path.tmpDir,
    "test_osfile_async_append.tmp"
  );

  // Clear any left-over files from previous runs.
  await removeTestFile(path);

  try {
    mode = setup_mode(mode);
    mode.append = true;
    if (mode.trunc) {
      // Pre-fill file with some data to see if |trunc| actually works.
      await OS.File.writeAtomic(path, new Uint8Array(500));
    }
    let file = await OS.File.open(path, mode);
    try {
      await file.write(new Uint8Array(1000));
      await file.setPosition(0, OS.File.POS_START);
      await file.read(100);
      // Should be at offset 100, length 1000 now.
      await file.write(new Uint8Array(100));
      // Should be at offset 1100, length 1100 now.
      let stat = await file.stat();
      Assert.equal(1100, stat.size);
    } finally {
      await file.close();
    }
  } catch (ex) {
    await removeTestFile(path);
  }
}

// Test no-append mode.
async function test_no_append(mode) {
  let path = OS.Path.join(
    OS.Constants.Path.tmpDir,
    "test_osfile_async_noappend.tmp"
  );

  // Clear any left-over files from previous runs.
  await removeTestFile(path);

  try {
    mode = setup_mode(mode);
    mode.append = false;
    if (mode.trunc) {
      // Pre-fill file with some data to see if |trunc| actually works.
      await OS.File.writeAtomic(path, new Uint8Array(500));
    }
    let file = await OS.File.open(path, mode);
    try {
      await file.write(new Uint8Array(1000));
      await file.setPosition(0, OS.File.POS_START);
      await file.read(100);
      // Should be at offset 100, length 1000 now.
      await file.write(new Uint8Array(100));
      // Should be at offset 200, length 1000 now.
      let stat = await file.stat();
      Assert.equal(1000, stat.size);
    } finally {
      await file.close();
    }
  } finally {
    await removeTestFile(path);
  }
}

var test_flags = [{}, { create: true }, { trunc: true }];
function run_test() {
  do_test_pending();

  for (let t of test_flags) {
    add_task(test_append.bind(null, t));
    add_task(test_no_append.bind(null, t));
  }
  add_task(do_test_finished);

  run_next_test();
}