summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_system_update_checkSizeHash.js
blob: 9d5ea45ce64cfa37be2c1b69e977c5ebcca986e6 (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
// Tests that system add-on upgrades work.

createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "2");

let distroDir = FileUtils.getDir("ProfD", ["sysfeatures", "empty"], true);
registerDirectory("XREAppFeat", distroDir);

AddonTestUtils.usePrivilegedSignatures = id => "system";

/**
 * Defines the set of initial conditions to run each test against. Each should
 * define the following properties:
 *
 * setup:        A task to setup the profile into the initial state.
 * initialState: The initial expected system add-on state after setup has run.
 */
const TEST_CONDITIONS = {
  // Runs tests with no updated or default system add-ons initially installed
  blank: {
    setup() {
      clearSystemAddonUpdatesDir();
      distroDir.leafName = "empty";
    },
    initialState: [
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
    ],
  },
  // Runs tests with default system add-ons installed
  withAppSet: {
    setup() {
      clearSystemAddonUpdatesDir();
      distroDir.leafName = "prefilled";
    },
    initialState: [
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: "2.0" },
      { isUpgrade: false, version: "2.0" },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
    ],
  },

  // Runs tests with updated system add-ons installed
  withProfileSet: {
    async setup() {
      await buildPrefilledUpdatesDir();
      distroDir.leafName = "empty";
    },
    initialState: [
      { isUpgrade: false, version: null },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
    ],
  },

  // Runs tests with both default and updated system add-ons installed
  withBothSets: {
    async setup() {
      await buildPrefilledUpdatesDir();
      distroDir.leafName = "hidden";
    },
    initialState: [
      { isUpgrade: false, version: "1.0" },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: true, version: "2.0" },
      { isUpgrade: false, version: null },
      { isUpgrade: false, version: null },
    ],
  },
};

/**
 * The tests to run. Each test must define an updateList or test. The following
 * properties are used:
 *
 * updateList: The set of add-ons the server should respond with.
 * test:       A function to run to perform the update check (replaces
 *             updateList)
 * fails:      An optional property, if true the update check is expected to
 *             fail.
 * finalState: An optional property, the expected final state of system add-ons,
 *             if missing the test condition's initialState is used.
 */
const TESTS = {
  // Correct sizes and hashes should work
  checkSizeHash: {
    // updateList is populated in setup()
    updateList: [],
    finalState: {
      blank: [
        { isUpgrade: false, version: null },
        { isUpgrade: true, version: "3.0" },
        { isUpgrade: true, version: "3.0" },
        { isUpgrade: false, version: null },
        { isUpgrade: true, version: "1.0" },
      ],
      withAppSet: [
        { isUpgrade: false, version: null },
        { isUpgrade: true, version: "3.0" },
        { isUpgrade: true, version: "3.0" },
        { isUpgrade: false, version: null },
        { isUpgrade: true, version: "1.0" },
      ],
      withProfileSet: [
        { isUpgrade: false, version: null },
        { isUpgrade: true, version: "3.0" },
        { isUpgrade: true, version: "3.0" },
        { isUpgrade: false, version: null },
        { isUpgrade: true, version: "1.0" },
      ],
      withBothSets: [
        { isUpgrade: false, version: "1.0" },
        { isUpgrade: true, version: "3.0" },
        { isUpgrade: true, version: "3.0" },
        { isUpgrade: false, version: null },
        { isUpgrade: true, version: "1.0" },
      ],
    },
  },
};

add_task(async function setup() {
  await initSystemAddonDirs();

  // Initialise the profile
  await overrideBuiltIns({ system: [] });
  await promiseStartupManager();
  await promiseShutdownManager();

  let list = TESTS.checkSizeHash.updateList;
  let xpi = await getSystemAddonXPI(2, "3.0");
  list.push({
    id: "system2@tests.mozilla.org",
    version: "3.0",
    path: "system2_3.xpi",
    xpi,
    size: xpi.fileSize,
  });

  xpi = await getSystemAddonXPI(3, "3.0");
  let [hashFunction, hashValue] = do_get_file_hash(xpi, "sha1").split(":");
  list.push({
    id: "system3@tests.mozilla.org",
    version: "3.0",
    path: "system3_3.xpi",
    xpi,
    hashFunction,
    hashValue,
  });

  xpi = await getSystemAddonXPI(5, "1.0");
  [hashFunction, hashValue] = do_get_file_hash(xpi, "sha1").split(":");
  list.push({
    id: "system5@tests.mozilla.org",
    version: "1.0",
    path: "system5_1.xpi",
    size: xpi.fileSize,
    xpi,
    hashFunction,
    hashValue,
  });
});

add_task(async function () {
  for (let setupName of Object.keys(TEST_CONDITIONS)) {
    for (let testName of Object.keys(TESTS)) {
      info("Running test " + setupName + " " + testName);

      let setup = TEST_CONDITIONS[setupName];
      let test = TESTS[testName];

      await execSystemAddonTest(setupName, setup, test, distroDir);
    }
  }
});