summaryrefslogtreecommitdiffstats
path: root/toolkit/components/backgroundtasks/tests/xpcshell/test_backgroundtask_profile_is_slim.js
blob: a6d37c9f1c1b2544f605a858ba67c372c1d674d1 (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
/* -*- 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/. */

// Invoke `profile_is_slim` background task, have it *not* remove its
// temporary profile, and verify that the temporary profile doesn't
// contain unexpected files and/or directories.
//
// N.b.: the background task invocation is a production Firefox
// running in automation; that means it can only connect to localhost
// (and it is not configured to connect to mochi.test, etc).
// Therefore we run our own server for it to connect to.

const { HttpServer } = ChromeUtils.importESModule(
  "resource://testing-common/httpd.sys.mjs"
);
let server;

setupProfileService();

let successCount = 0;
function success(metadata, response) {
  response.setStatusLine(metadata.httpVersion, 200, "OK");
  let str = "success";
  response.write(str, str.length);
  successCount += 1;
}

add_setup(() => {
  server = new HttpServer();
  server.registerPathHandler("/success", success);
  server.start(-1);

  registerCleanupFunction(async () => {
    await server.stop();
  });
});

add_task(async function test_backgroundtask_profile_is_slim() {
  let profileService = Cc["@mozilla.org/toolkit/profile-service;1"].getService(
    Ci.nsIToolkitProfileService
  );

  let profD = do_get_profile();
  profD.append("test_profile_is_slim");
  let profile = profileService.createUniqueProfile(
    profD,
    "test_profile_is_slim"
  );

  registerCleanupFunction(() => {
    profile.remove(true);
  });

  let exitCode = await do_backgroundtask("profile_is_slim", {
    extraArgs: [`http://localhost:${server.identity.primaryPort}/success`],
    extraEnv: {
      XRE_PROFILE_PATH: profile.rootDir.path,
      MOZ_BACKGROUNDTASKS_NO_REMOVE_PROFILE: "1",
    },
  });

  Assert.equal(
    0,
    exitCode,
    "The fetch background task exited with exit code 0"
  );
  Assert.equal(
    1,
    successCount,
    "The fetch background task reached the test server 1 time"
  );

  assertProfileIsSlim(profile.rootDir);
});

const expected = [
  { relPath: "lock", condition: AppConstants.platform == "linux" },
  {
    relPath: ".parentlock",
    condition:
      AppConstants.platform == "linux" || AppConstants.platform == "macosx",
  },
  {
    relPath: "parent.lock",
    condition:
      AppConstants.platform == "win" || AppConstants.platform == "macosx",
  },
  // TODO: verify that directory is empty.
  { relPath: "cache2", isDirectory: true },
  { relPath: "compatibility.ini" },
  { relPath: "crashes", isDirectory: true },
  { relPath: "data", isDirectory: true },
  { relPath: "local", isDirectory: true },
  { relPath: "minidumps", isDirectory: true },
  // `mozinfo.json` is an artifact of the testing infrastructure.
  { relPath: "mozinfo.json" },
  { relPath: "pkcs11.txt" },
  { relPath: "prefs.js" },
  { relPath: "security_state", isDirectory: true },
  // TODO: verify that directory is empty.
  { relPath: "startupCache", isDirectory: true },
  { relPath: "times.json" },
];

async function assertProfileIsSlim(profile) {
  Assert.ok(profile.exists(), `Profile directory does exist: ${profile.path}`);

  // We collect unexpected results, log them all, and then assert
  // there are none to provide the most feedback per iteration.
  let unexpected = [];

  let typeLabel = { true: "directory", false: "file" };

  for (let entry of profile.directoryEntries) {
    // `getRelativePath` always uses '/' as path separator.
    let relPath = entry.getRelativePath(profile);

    info(`Witnessed directory entry '${relPath}'.`);

    let expectation = expected.find(
      it => it.relPath == relPath && (!("condition" in it) || it.condition)
    );
    if (!expectation) {
      info(`UNEXPECTED: Directory entry '${relPath}' is NOT expected!`);
      unexpected.push(relPath);
    } else if (
      typeLabel[!!expectation.isDirectory] != typeLabel[entry.isDirectory()]
    ) {
      info(`UNEXPECTED: Directory entry '${relPath}' is NOT expected type!`);
      unexpected.push(relPath);
    }
  }

  Assert.deepEqual([], unexpected, "Expected nothing to report");
}