summaryrefslogtreecommitdiffstats
path: root/toolkit/components/backgroundtasks/tests/xpcshell/test_backgroundtask_removeDirectory.js
blob: 57da9ac175ab3f906dad7306c70c1968fab2c396 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
 * vim: sw=4 ts=4 sts=4 et
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

// This test exercises functionality and also ensures the exit codes,
// which are a public API, do not change over time.
const { EXIT_CODE } = ChromeUtils.importESModule(
  "resource://gre/modules/BackgroundTasksManager.sys.mjs"
);

const LEAF_NAME = "newCacheFolder";

add_task(async function test_simple() {
  // This test creates a simple folder in the profile and passes it to
  // the background task to delete

  let dir = do_get_profile();
  dir.append(LEAF_NAME);
  dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);
  equal(dir.exists(), true);

  let extraDir = do_get_profile();
  extraDir.append("test.abc");
  extraDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);
  equal(extraDir.exists(), true);

  let outputLines = [];
  let exitCode = await do_backgroundtask("removeDirectory", {
    extraArgs: [do_get_profile().path, LEAF_NAME, "10", ".abc"],
    onStdoutLine: line => outputLines.push(line),
  });
  equal(exitCode, EXIT_CODE.SUCCESS);
  equal(dir.exists(), false);
  equal(extraDir.exists(), false);

  if (AppConstants.platform !== "win") {
    // Check specific logs because there can still be some logs in certain conditions,
    // e.g. in code coverage (see bug 1831778 and bug 1804833)
    ok(
      outputLines.every(l => !l.includes("*** You are running in")),
      "Should not have logs by default"
    );
  }
});

add_task(async function test_no_extension() {
  // Test that not passing the extension is fine

  let dir = do_get_profile();
  dir.append(LEAF_NAME);
  dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);
  equal(dir.exists(), true);

  let exitCode = await do_backgroundtask("removeDirectory", {
    extraArgs: [do_get_profile().path, LEAF_NAME, "10"],
  });
  equal(exitCode, EXIT_CODE.SUCCESS);
  equal(dir.exists(), false);
});

add_task(async function test_createAfter() {
  // First we create the task then we create the directory.
  // The task will only wait for 10 seconds.
  let dir = do_get_profile();
  dir.append(LEAF_NAME);

  let task = do_backgroundtask("removeDirectory", {
    extraArgs: [do_get_profile().path, LEAF_NAME, "10", ""],
  });

  dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);

  let exitCode = await task;
  equal(exitCode, EXIT_CODE.SUCCESS);
  equal(dir.exists(), false);
});

add_task(async function test_folderNameWithSpaces() {
  // We pass in a leaf name with spaces just to make sure the command line
  // argument parsing works properly.
  let dir = do_get_profile();
  let leafNameWithSpaces = `${LEAF_NAME} space`;
  dir.append(leafNameWithSpaces);

  let task = do_backgroundtask("removeDirectory", {
    extraArgs: [do_get_profile().path, leafNameWithSpaces, "10", ""],
  });

  dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);

  let exitCode = await task;
  equal(exitCode, EXIT_CODE.SUCCESS);
  equal(dir.exists(), false);
});

add_task(async function test_missing_folder() {
  // We never create the directory.
  // We only wait for 1 second

  let dir = do_get_profile();
  dir.append(LEAF_NAME);
  let exitCode = await do_backgroundtask("removeDirectory", {
    extraArgs: [do_get_profile().path, LEAF_NAME, "1", ""],
  });

  equal(exitCode, EXIT_CODE.SUCCESS);
  equal(dir.exists(), false);
});

add_task(
  { skip_if: () => mozinfo.os != "win" },
  async function test_ro_file_in_folder() {
    let dir = do_get_profile();
    dir.append(LEAF_NAME);
    dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);
    equal(dir.exists(), true);

    let file = dir.clone();
    file.append("ro_file");
    file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o644);
    file.QueryInterface(Ci.nsILocalFileWin).readOnly = true;

    // Make sure that we can move with a readonly file in the directory
    dir.moveTo(null, "newName");

    // This will fail because of the readonly file.
    let exitCode = await do_backgroundtask("removeDirectory", {
      extraArgs: [do_get_profile().path, "newName", "1", ""],
    });

    equal(exitCode, EXIT_CODE.EXCEPTION);

    dir = do_get_profile();
    dir.append("newName");
    file = dir.clone();
    file.append("ro_file");
    equal(file.exists(), true);

    // Remove readonly attribute and try again.
    // As a followup we might want to force an old cache dir to be deleted, even if it has readonly files in it.
    file.QueryInterface(Ci.nsILocalFileWin).readOnly = false;
    dir.remove(true);
    equal(file.exists(), false);
    equal(dir.exists(), false);
  }
);

add_task(async function test_purgeFile() {
  // Test that only directories are purged by the background task
  let file = do_get_profile();
  file.append(LEAF_NAME);
  file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o644);
  equal(file.exists(), true);

  let exitCode = await do_backgroundtask("removeDirectory", {
    extraArgs: [do_get_profile().path, LEAF_NAME, "2", ""],
  });
  equal(exitCode, EXIT_CODE.EXCEPTION);
  equal(file.exists(), true);

  file.remove(true);
  let dir = file.clone();
  dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);
  equal(dir.exists(), true);
  file = do_get_profile();
  file.append("test.abc");
  file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o644);
  equal(file.exists(), true);
  let dir2 = do_get_profile();
  dir2.append("dir.abc");
  dir2.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);
  equal(dir2.exists(), true);

  exitCode = await do_backgroundtask("removeDirectory", {
    extraArgs: [do_get_profile().path, LEAF_NAME, "2", ".abc"],
  });

  equal(exitCode, EXIT_CODE.SUCCESS);
  equal(dir.exists(), false);
  equal(dir2.exists(), false);
  equal(file.exists(), true);
  file.remove(true);
});

add_task(async function test_two_tasks() {
  let dir1 = do_get_profile();
  dir1.append("leaf1.abc");

  let dir2 = do_get_profile();
  dir2.append("leaf2.abc");

  let tasks = [];
  tasks.push(
    do_backgroundtask("removeDirectory", {
      extraArgs: [do_get_profile().path, dir1.leafName, "5", ".abc"],
    })
  );
  dir1.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);
  tasks.push(
    do_backgroundtask("removeDirectory", {
      extraArgs: [do_get_profile().path, dir2.leafName, "5", ".abc"],
    })
  );
  dir2.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);

  let [r1, r2] = await Promise.all(tasks);

  equal(r1, EXIT_CODE.SUCCESS);
  equal(r2, EXIT_CODE.SUCCESS);
  equal(dir1.exists(), false);
  equal(dir2.exists(), false);
});

// This test creates a large number of tasks and directories.  Spawning a huge
// number of tasks concurrently (say, 50 or 100) appears to cause problems;
// since doing so is rather artificial, we haven't tracked this down.
const TASK_COUNT = 20;
add_task(async function test_aLotOfTasks() {
  let dirs = [];
  let tasks = [];

  for (let i = 0; i < TASK_COUNT; i++) {
    let dir = do_get_profile();
    dir.append(`leaf${i}.abc`);

    tasks.push(
      do_backgroundtask("removeDirectory", {
        extraArgs: [do_get_profile().path, dir.leafName, "5", ".abc"],
        extraEnv: { MOZ_LOG: "BackgroundTasks:5" },
      })
    );

    dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);
    dirs.push(dir);
  }

  let results = await Promise.all(tasks);
  for (let i in results) {
    equal(results[i], EXIT_CODE.SUCCESS, `Task ${i} should succeed`);
    equal(dirs[i].exists(), false, `leaf${i}.abc should not exist`);
  }
});

add_task(async function test_suffix() {
  // Make sure only the directories with the specified suffix will be removed

  let leaf = do_get_profile();
  leaf.append(LEAF_NAME);
  leaf.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);

  let abc = do_get_profile();
  abc.append("foo.abc");
  abc.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);
  equal(abc.exists(), true);

  let bcd = do_get_profile();
  bcd.append("foo.bcd");
  bcd.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);
  equal(bcd.exists(), true);

  // XXX: This should be able to skip passing LEAF_NAME (passing "" instead),
  // but bug 1853920 and bug 1853921 causes incorrect arguments in that case.
  let task = do_backgroundtask("removeDirectory", {
    extraArgs: [do_get_profile().path, LEAF_NAME, "10", ".abc"],
  });

  let exitCode = await task;
  equal(exitCode, EXIT_CODE.SUCCESS);
  equal(abc.exists(), false);
  equal(bcd.exists(), true);
});

add_task(async function test_suffix_wildcard() {
  // wildcard as a suffix should remove every subdirectories

  let leaf = do_get_profile();
  leaf.append(LEAF_NAME);
  leaf.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);

  let abc = do_get_profile();
  abc.append("foo.abc");
  abc.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);

  let cde = do_get_profile();
  cde.append("foo.cde");
  cde.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744);

  // XXX: This should be able to skip passing LEAF_NAME (passing "" instead),
  // but bug 1853920 and bug 1853921 causes incorrect arguments in that case.
  let task = do_backgroundtask("removeDirectory", {
    extraArgs: [do_get_profile().path, LEAF_NAME, "10", "*"],
  });

  let exitCode = await task;
  equal(exitCode, EXIT_CODE.SUCCESS);
  equal(cde.exists(), false);
  equal(cde.exists(), false);
});