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

const PREF_EM_HOTFIX_ID                = "extensions.hotfix.id";
const PREF_EM_HOTFIX_LASTVERSION       = "extensions.hotfix.lastVersion";
const PREF_EM_HOTFIX_URL               = "extensions.hotfix.url";
const PREF_EM_HOTFIX_CERTS             = "extensions.hotfix.certs.";
const PREF_EM_CERT_CHECKATTRIBUTES     = "extensions.hotfix.cert.checkAttributes";

const PREF_INSTALL_REQUIREBUILTINCERTS = "extensions.install.requireBuiltInCerts";
const PREF_UPDATE_REQUIREBUILTINCERTS  = "extensions.update.requireBuiltInCerts";

const PREF_APP_UPDATE_ENABLED          = "app.update.enabled";
const PREF_APP_UPDATE_URL              = "app.update.url";

const HOTFIX_ID = "hotfix@tests.mozilla.org";

/*
 * Register an addon install listener and return a promise that:
 *  resolves with the AddonInstall object if the install succeeds
 *  rejects with the AddonInstall if the install fails
 */
function promiseInstallListener() {
  return new Promise((resolve, reject) => {
    let listener = {
      onInstallEnded: ai => {
        AddonManager.removeInstallListener(listener);
        resolve(ai);
      },
      onDownloadCancelled: ai => {
        AddonManager.removeInstallListener(listener);
        reject(ai);
      }
    };
    AddonManager.addInstallListener(listener);
  });
}

function promiseSuccessfulInstall() {
  return promiseInstallListener().then(
    aInstall => {
      ok(true, "Should have seen the install complete");
      is(aInstall.addon.id, HOTFIX_ID, "Should have installed the right add-on");
      aInstall.addon.uninstall();
      Services.prefs.clearUserPref(PREF_EM_HOTFIX_LASTVERSION);
    },
    aInstall => {
      ok(false, "Should not have seen the download cancelled");
      is(aInstall.addon.id, HOTFIX_ID, "Should have seen the right add-on");
    });
}

function promiseFailedInstall() {
  return promiseInstallListener().then(
    aInstall => {
      ok(false, "Should not have seen the install complete");
      is(aInstall.addon.id, HOTFIX_ID, "Should have installed the right add-on");
      aInstall.addon.uninstall();
      Services.prefs.clearUserPref(PREF_EM_HOTFIX_LASTVERSION);
    },
    aInstall => {
      ok(true, "Should have seen the download cancelled");
      is(aInstall.addon.id, HOTFIX_ID, "Should have seen the right add-on");
    });
}

add_task(function setup() {
  var oldAusUrl = Services.prefs.getDefaultBranch(null).getCharPref(PREF_APP_UPDATE_URL);
  Services.prefs.getDefaultBranch(null).setCharPref(PREF_APP_UPDATE_URL, TESTROOT + "ausdummy.xml");
  Services.prefs.setBoolPref(PREF_APP_UPDATE_ENABLED, true);
  Services.prefs.setBoolPref(PREF_INSTALL_REQUIREBUILTINCERTS, false);
  Services.prefs.setBoolPref(PREF_UPDATE_REQUIREBUILTINCERTS, false);
  Services.prefs.setCharPref(PREF_EM_HOTFIX_ID, HOTFIX_ID);
  var oldURL = Services.prefs.getCharPref(PREF_EM_HOTFIX_URL);
  Services.prefs.setCharPref(PREF_EM_HOTFIX_URL, TESTROOT + "signed_hotfix.rdf");

  registerCleanupFunction(function() {
    Services.prefs.setBoolPref(PREF_APP_UPDATE_ENABLED, false);
    Services.prefs.getDefaultBranch(null).setCharPref(PREF_APP_UPDATE_URL, oldAusUrl);
    Services.prefs.clearUserPref(PREF_EM_HOTFIX_ID);
    Services.prefs.setCharPref(PREF_EM_HOTFIX_URL, oldURL);
    Services.prefs.clearUserPref(PREF_INSTALL_REQUIREBUILTINCERTS);
    Services.prefs.clearUserPref(PREF_UPDATE_REQUIREBUILTINCERTS);

    Services.prefs.clearUserPref(PREF_EM_CERT_CHECKATTRIBUTES);
    var prefs = Services.prefs.getChildList(PREF_EM_HOTFIX_CERTS);
    prefs.forEach(Services.prefs.clearUserPref);
  });
});

add_task(function* check_no_cert_checks() {
  Services.prefs.setBoolPref(PREF_EM_CERT_CHECKATTRIBUTES, false);
  yield Promise.all([
    promiseSuccessfulInstall(),
    AddonManagerPrivate.backgroundUpdateCheck()
  ]);
});

add_task(function* check_wrong_cert_fingerprint() {
  Services.prefs.setBoolPref(PREF_EM_CERT_CHECKATTRIBUTES, true);
  Services.prefs.setCharPref(PREF_EM_HOTFIX_CERTS + "1.sha1Fingerprint", "foo");

  yield Promise.all([
    promiseFailedInstall(),
    AddonManagerPrivate.backgroundUpdateCheck()
  ]);
  Services.prefs.clearUserPref(PREF_EM_HOTFIX_CERTS + "1.sha1Fingerprint");
});

add_task(function* check_right_cert_fingerprint() {
  Services.prefs.setBoolPref(PREF_EM_CERT_CHECKATTRIBUTES, true);
  Services.prefs.setCharPref(PREF_EM_HOTFIX_CERTS + "1.sha1Fingerprint", "3E:B9:4E:07:12:FE:3C:01:41:46:13:46:FC:84:52:1A:8C:BE:1D:A2");

  yield Promise.all([
    promiseSuccessfulInstall(),
    AddonManagerPrivate.backgroundUpdateCheck()
  ]);

  Services.prefs.clearUserPref(PREF_EM_HOTFIX_CERTS + "1.sha1Fingerprint");
});

add_task(function* check_multi_cert_fingerprint_1() {
  Services.prefs.setBoolPref(PREF_EM_CERT_CHECKATTRIBUTES, true);
  Services.prefs.setCharPref(PREF_EM_HOTFIX_CERTS + "1.sha1Fingerprint", "3E:B9:4E:07:12:FE:3C:01:41:46:13:46:FC:84:52:1A:8C:BE:1D:A2");
  Services.prefs.setCharPref(PREF_EM_HOTFIX_CERTS + "2.sha1Fingerprint", "foo");

  yield Promise.all([
    promiseSuccessfulInstall(),
    AddonManagerPrivate.backgroundUpdateCheck()
  ]);

  Services.prefs.clearUserPref(PREF_EM_HOTFIX_CERTS + "1.sha1Fingerprint");
  Services.prefs.clearUserPref(PREF_EM_HOTFIX_CERTS + "2.sha1Fingerprint");
});

add_task(function* check_multi_cert_fingerprint_2() {
  Services.prefs.setBoolPref(PREF_EM_CERT_CHECKATTRIBUTES, true);
  Services.prefs.setCharPref(PREF_EM_HOTFIX_CERTS + "1.sha1Fingerprint", "foo");
  Services.prefs.setCharPref(PREF_EM_HOTFIX_CERTS + "2.sha1Fingerprint", "3E:B9:4E:07:12:FE:3C:01:41:46:13:46:FC:84:52:1A:8C:BE:1D:A2");

  yield Promise.all([
    promiseSuccessfulInstall(),
    AddonManagerPrivate.backgroundUpdateCheck()
  ]);

  Services.prefs.clearUserPref(PREF_EM_HOTFIX_CERTS + "1.sha1Fingerprint");
  Services.prefs.clearUserPref(PREF_EM_HOTFIX_CERTS + "2.sha1Fingerprint");
});

add_task(function* check_no_cert_no_checks() {
  Services.prefs.setBoolPref(PREF_EM_CERT_CHECKATTRIBUTES, false);
  Services.prefs.setCharPref(PREF_EM_HOTFIX_URL, TESTROOT + "unsigned_hotfix.rdf");

  yield Promise.all([
    promiseSuccessfulInstall(),
    AddonManagerPrivate.backgroundUpdateCheck()
  ]);
});

add_task(function* check_no_cert_cert_fingerprint_check() {
  Services.prefs.setBoolPref(PREF_EM_CERT_CHECKATTRIBUTES, true);
  Services.prefs.setCharPref(PREF_EM_HOTFIX_CERTS + "1.sha1Fingerprint", "3E:B9:4E:07:12:FE:3C:01:41:46:13:46:FC:84:52:1A:8C:BE:1D:A2");

  yield Promise.all([
    promiseFailedInstall(),
    AddonManagerPrivate.backgroundUpdateCheck()
  ]);

  Services.prefs.clearUserPref(PREF_EM_HOTFIX_CERTS + "1.sha1Fingerprint");
});