4 https://bugzilla.mozilla.org/show_bug.cgi?id=880043
8 <title>Test for Bug
880043 Packaged apps installation and update
</title>
9 <script type=
"text/javascript" src=
"/MochiKit/MochiKit.js"></script>
10 <script type=
"text/javascript" src=
"/tests/SimpleTest/SimpleTest.js"></script>
11 <script type=
"text/javascript" src=
"test_packaged_app_common.js"></script>
12 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
16 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=880043">Mozilla Bug
880043</a>
18 <div id=
"content" style=
"display: none">
22 <script class=
"testbody" type=
"text/javascript">
26 const Ci = SpecialPowers.Ci;
27 const Cc = SpecialPowers.Cc;
28 const Cu = SpecialPowers.Cu;
33 var gAppName =
"Simple App";
34 var gInstallOrigin =
"http://mochi.test:8888/";
35 var gSJSPath =
"tests/dom/apps/tests/signed_app.sjs";
36 var gSJS = gInstallOrigin + gSJSPath;
37 var gPackagePath = gInstallOrigin +
"tests/dom/apps/tests/";
38 var gSignedAppOriginsStr =
"";
40 SimpleTest.waitForExplicitFinish();
42 function checkAppOnInstallError(aMiniManifestURL, aExpectedError) {
43 navigator.mozApps.mgmt.oninstall = function(evt) {
44 info(
"Got oninstall event");
45 gApp = evt.application;
46 gApp.ondownloaderror = function() {
47 is(gApp.downloadError.name, aExpectedError,
48 "Download fails with expected error: " + aExpectedError);
49 if (gApp.downloadError.name != aExpectedError) {
50 PackagedTestHelper.finish();
52 PackagedTestHelper.next();
55 gApp.ondownloadsuccess = function() {
56 ok(false,
"App download should fail");
57 PackagedTestHelper.finish();
60 var request = navigator.mozApps.installPackage(aMiniManifestURL);
61 request.onerror = function(evt) {
62 ok(false,
"Application should throw the error inside oninstall: " + evt.error.name);
63 PackagedTestHelper.finish();
65 request.onsuccess = function() {
66 info(
"Application install returns successfully");
70 function checkUninstallApp(aApp) {
71 var req = navigator.mozApps.mgmt.uninstall(aApp);
73 req.onsuccess = function() {
74 info(
"App uninstalled");
75 aApp.ondownloadsuccess = null;
76 aApp.ondownloaderror = null;
77 aApp.onprogress = null;
78 PackagedTestHelper.next();
80 req.onerror = function(evt) {
81 ok(false,
"App uninstallation should succeed (got unexpected " +
82 evt.target.error.name +
")");
83 PackagedTestHelper.finish();
90 info(
"Test Initial Setup");
91 gSignedAppOriginsStr = SpecialPowers.getCharPref(
"dom.mozApps.signed_apps_installable_from");
92 var signedAppOriginsStr = gSignedAppOriginsStr.concat(
"," + gInstallOrigin.slice(
0, -
1));
93 SpecialPowers.pushPrefEnv({'set': [['dom.mozApps.signed_apps_installable_from', signedAppOriginsStr]]}, function() {
94 var url = SimpleTest.getTestFileURL(
"chromeAddCert.js");
95 var script = SpecialPowers.loadChromeScript(url);
96 script.addMessageListener(
"addCertCompleted", function() {
97 SpecialPowers.setAllAppsLaunchable(true);
98 SpecialPowers.addPermission(
"webapps-manage", true, document);
99 info(
"Test CA Certificate Selected");
100 PackagedTestHelper.next();
106 info(
"autoConfirmAppInstall");
107 SpecialPowers.autoConfirmAppInstall(PackagedTestHelper.next);
110 info(
"autoConfirmAppUninstall");
111 SpecialPowers.autoConfirmAppUninstall(PackagedTestHelper.next);
114 info(
"== TEST == Install packaged app");
115 var miniManifestURL = gSJS +
"?" +
"app=valid&" +
"version=1";
117 navigator.mozApps.mgmt.oninstall = function(evt) {
118 info(
"Got oninstall event");
119 gApp = evt.application;
120 gApp.ondownloaderror = function() {
121 ok(false,
"Download should succeed (got error: " +
122 gApp.downloadError.name +
")");
123 PackagedTestHelper.finish();
125 gApp.ondownloadsuccess = function() {
126 info(
"App downloaded");
129 manifestURL: miniManifestURL,
130 installOrigin: gInstallOrigin.slice(
0, -
1),
132 installState:
"installed",
133 downloadAvailable: false,
135 readyToApplyDownload: false,
137 PackagedTestHelper.checkAppState(gApp,
1, expected,
138 true, false, PackagedTestHelper.next);
141 info(
"Installing app: " + miniManifestURL);
142 var request = navigator.mozApps.installPackage(miniManifestURL);
143 request.onerror = function(evt) {
144 ok(false,
"Application should have been correctly installed (error: " +
147 request.onsuccess = function() {
148 info(
"Application installed");
152 // Set up the server side to send a different package on the next call.
153 info(
"== TEST == Set state to invalid app");
154 var url = gSJS +
"?" +
"nextApp=unsigned";
155 var xhr = new XMLHttpRequest();
156 xhr.addEventListener(
"load", function() {
157 is(xhr.responseText,
"OK",
"nextApp=unsigned OK");
158 PackagedTestHelper.next();
160 xhr.addEventListener(
"error", event =
> {
161 ok(false,
"XHR error loading " + url +
": " + xhr.status +
" - " +
164 xhr.addEventListener(
"abort", event =
> {
165 ok(false,
"XHR abort loading " + url);
167 xhr.open('GET', url, true);
171 info(
"== TEST == Update app from an invalid source");
172 info(
"Updating app: " + gApp.manifest.name);
173 gApp.ondownloaderror = function() {
174 is(gApp.downloadError.name,
"INVALID_SIGNATURE");
175 PackagedTestHelper.next();
177 gApp.ondownloadsuccess = function() {
178 info(
"App downloaded");
179 ok(false,
"App should not download success");
180 PackagedTestHelper.finish();
182 info(
"App manifest: " + gApp.manifestURL);
184 // This update check will return a different mini manifest pointing to
185 // an invalid package.
186 var req = gApp.checkForUpdate();
187 req.onsuccess = function() {
190 req.onerror = PackagedTestHelper.mozAppsError;
193 info(
"== TEST == Uninstall a signed app");
195 checkUninstallApp(gApp);
198 info(
"== TEST == Install a corrupted package");
199 //Scenario: Corrupted package
200 var miniManifestURL = gSJS +
"?" +
"app=corrupt&" +
"version=1";
201 checkAppOnInstallError(miniManifestURL,
"APP_PACKAGE_CORRUPTED");
204 info(
"== TEST == Install a unsigned app from a trusted store");
205 //Scenario: Unsigned App from an origin that requires signed apps
206 var miniManifestURL = gSJS +
"?" +
"app=unknown_issuer&" +
"version=1";
207 checkAppOnInstallError(miniManifestURL,
"INVALID_SIGNATURE");
210 info(
"== TEST == Install packaged app with origin");
211 var miniManifestURL = gSJS +
"?" +
215 navigator.mozApps.mgmt.oninstall = function(evt) {
216 info(
"Got oninstall event");
217 gApp = evt.application;
218 gApp.ondownloaderror = function() {
219 ok(false,
"Download should succeed (got error: " +
220 gApp.downloadError.name +
")");
221 PackagedTestHelper.finish();
223 gApp.ondownloadsuccess = function() {
224 info(
"App downloaded");
227 manifestURL: miniManifestURL,
228 installOrigin: gInstallOrigin.slice(
0, -
1),
230 installState:
"installed",
231 downloadAvailable: false,
233 origin:
"app://test.origin.privileged.app",
234 readyToApplyDownload: false,
236 PackagedTestHelper.checkAppState(gApp,
1, expected,
237 true, false, PackagedTestHelper.next);
240 info(
"Installing app: " + miniManifestURL);
241 var request = navigator.mozApps.installPackage(miniManifestURL);
242 request.onerror = function(evt) {
243 ok(false,
"Application should have been correctly installed (error: " +
244 JSON.stringify(evt));
246 request.onsuccess = function() {
247 info(
"Application installed");
251 info(
"== TEST == Install app from an invalid source");
252 // Scenario: This is where an unexpected store is signing packages and
253 // attempting to install. Please note that after this test you cannot
254 // add new successful tests without changing the preference again
255 SpecialPowers.pushPrefEnv(
256 {'set': [['dom.mozApps.signed_apps_installable_from',
257 gSignedAppOriginsStr]]},
259 var miniManifestURL = gSJS +
"?" +
"app=valid&" +
"version=1";
260 checkAppOnInstallError(miniManifestURL,
"INSTALL_FROM_DENIED");
265 PackagedTestHelper.finish();
269 PackagedTestHelper.setSteps(steps);
270 PackagedTestHelper.gSJSPath = gSJSPath;
272 addLoadEvent(PackagedTestHelper.start);