summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_update_strictcompat.js
blob: 14192657dddcba31ae2bfca7bfe9a5f3ea6a740d (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

// This verifies that add-on update checks work in conjunction with
// strict compatibility settings.

const PREF_GETADDONS_CACHE_ENABLED = "extensions.getAddons.cache.enabled";

// The test extension uses an insecure update url.
Services.prefs.setBoolPref(PREF_EM_CHECK_UPDATE_SECURITY, false);
Services.prefs.setBoolPref(PREF_EM_STRICT_COMPATIBILITY, false);

const appId = "toolkit@mozilla.org";

testserver = createHttpServer({ hosts: ["example.com"] });
testserver.registerDirectory("/data/", do_get_file("data"));

add_task(async function setup() {
  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1");
  AddonTestUtils.updateReason = AddonManager.UPDATE_WHEN_USER_REQUESTED;

  Services.prefs.setCharPref(
    PREF_GETADDONS_BYIDS,
    "http://example.com/data/test_update_addons.json"
  );
  Services.prefs.setBoolPref(PREF_GETADDONS_CACHE_ENABLED, true);
});

// Test that the update check correctly observes the
// extensions.strictCompatibility pref.
add_task(async function test_update_strict() {
  const ID = "addon9@tests.mozilla.org";
  let xpi = await createAddon({
    id: ID,
    updateURL: "http://example.com/update.json",
    targetApplications: [
      {
        id: "xpcshell@tests.mozilla.org",
        minVersion: "0.1",
        maxVersion: "0.2",
      },
    ],
  });
  await manuallyInstall(xpi, AddonTestUtils.profileExtensions, ID);

  await promiseStartupManager();

  await AddonRepository.backgroundUpdateCheck();

  let UPDATE = {
    addons: {
      [ID]: {
        updates: [
          {
            version: "2.0",
            update_link: "http://example.com/addons/test_update9_2.xpi",
            applications: {
              gecko: {
                strict_min_version: "1",
                advisory_max_version: "1",
              },
            },
          },

          // Incompatible when strict compatibility is enabled
          {
            version: "3.0",
            update_link: "http://example.com/addons/test_update9_3.xpi",
            applications: {
              gecko: {
                strict_min_version: "0.9",
                advisory_max_version: "0.9",
              },
            },
          },

          // Addon for future version of app
          {
            version: "4.0",
            update_link: "http://example.com/addons/test_update9_5.xpi",
            applications: {
              gecko: {
                strict_min_version: "5",
                advisory_max_version: "6",
              },
            },
          },
        ],
      },
    },
  };

  AddonTestUtils.registerJSON(testserver, "/update.json", UPDATE);

  let addon = await AddonManager.getAddonByID(ID);
  let { updateAvailable } = await promiseFindAddonUpdates(addon);

  Assert.notEqual(updateAvailable, null, "Got update");
  Assert.equal(
    updateAvailable.version,
    "3.0",
    "The correct update was selected"
  );
  await addon.uninstall();

  await promiseShutdownManager();
});

// Tests that compatibility updates are applied to addons when the updated
// compatibility data wouldn't match with strict compatibility enabled.
add_task(async function test_update_strict2() {
  const ID = "addon10@tests.mozilla.org";
  let xpi = createAddon({
    id: ID,
    updateURL: "http://example.com/update.json",
    targetApplications: [
      {
        id: appId,
        minVersion: "0.1",
        maxVersion: "0.2",
      },
    ],
  });
  await manuallyInstall(xpi, AddonTestUtils.profileExtensions, ID);

  await promiseStartupManager();
  await AddonRepository.backgroundUpdateCheck();

  const UPDATE = {
    addons: {
      [ID]: {
        updates: [
          {
            version: "1.0",
            update_link: "http://example.com/addons/test_update10.xpi",
            applications: {
              gecko: {
                strict_min_version: "0.1",
                advisory_max_version: "0.4",
              },
            },
          },
        ],
      },
    },
  };

  AddonTestUtils.registerJSON(testserver, "/update.json", UPDATE);

  let addon = await AddonManager.getAddonByID(ID);
  notEqual(addon, null);

  let result = await promiseFindAddonUpdates(addon);
  ok(result.compatibilityUpdate, "Should have seen a compatibility update");
  ok(!result.updateAvailable, "Should not have seen a version update");

  await addon.uninstall();
  await promiseShutdownManager();
});

// Test that the update check correctly observes when an addon opts-in to
// strict compatibility checking.
add_task(async function test_update_strict_optin() {
  const ID = "addon11@tests.mozilla.org";
  let xpi = await createAddon({
    id: ID,
    updateURL: "http://example.com/update.json",
    targetApplications: [
      {
        id: appId,
        minVersion: "0.1",
        maxVersion: "0.2",
      },
    ],
  });
  await manuallyInstall(xpi, AddonTestUtils.profileExtensions, ID);

  await promiseStartupManager();

  await AddonRepository.backgroundUpdateCheck();

  const UPDATE = {
    addons: {
      [ID]: {
        updates: [
          {
            version: "2.0",
            update_link: "http://example.com/addons/test_update11.xpi",
            applications: {
              gecko: {
                strict_min_version: "0.1",
                strict_max_version: "0.2",
              },
            },
          },
        ],
      },
    },
  };

  AddonTestUtils.registerJSON(testserver, "/update.json", UPDATE);

  let addon = await AddonManager.getAddonByID(ID);
  notEqual(addon, null);

  let result = await AddonTestUtils.promiseFindAddonUpdates(addon);
  ok(
    !result.compatibilityUpdate,
    "Should not have seen a compatibility update"
  );
  ok(!result.updateAvailable, "Should not have seen a version update");

  await addon.uninstall();
  await promiseShutdownManager();
});