|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <!-- |
|
4 https://bugzilla.mozilla.org/show_bug.cgi?id=783408 |
|
5 --> |
|
6 <head> |
|
7 <meta charset="utf-8"> |
|
8 <title>Tests that uninstalling app removes the cookies</title> |
|
9 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> |
|
10 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?> |
|
11 <script src="channel_utils.js"></script> |
|
12 </head> |
|
13 <body> |
|
14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=786296">Mozilla Bug 783408</a> |
|
15 <p id="display"></p> |
|
16 <div id="content"> |
|
17 |
|
18 </div> |
|
19 <pre id="test"> |
|
20 <script type="application/javascript;version=1.7"> |
|
21 |
|
22 /** Test for Bug 783408 **/ |
|
23 |
|
24 const Ci = Components.interfaces; |
|
25 const Cc = Components.classes; |
|
26 const Cu = Components.utils; |
|
27 |
|
28 SimpleTest.waitForExplicitFinish(); |
|
29 |
|
30 Cu.import("resource://testing-common/httpd.js"); |
|
31 var httpserver = new HttpServer(); |
|
32 |
|
33 var cookieSetPath = "/setcookie"; |
|
34 var cookieCheckPath = "/checkcookie"; |
|
35 |
|
36 var permManager = Cc["@mozilla.org/permissionmanager;1"] |
|
37 .getService(Ci.nsIPermissionManager); |
|
38 var cookieMng = Cc["@mozilla.org/cookiemanager;1"] |
|
39 .getService(Ci.nsICookieManager2); |
|
40 var appsService = Cc['@mozilla.org/AppsService;1'] |
|
41 .getService(Ci.nsIAppsService); |
|
42 |
|
43 var cookies = [ |
|
44 { cookieName: 'LCC_App_BrowF_PrivF', |
|
45 loadContext: null }, |
|
46 { cookieName: 'LCC_App_BrowT_PrivF', |
|
47 loadContext: null }, |
|
48 { cookieName: 'AppUninstall_Witness', |
|
49 loadContext: new LoadContextCallback(0, false, false, 1) }, |
|
50 ]; |
|
51 var counter = 0; |
|
52 |
|
53 function getCookiesCountForApp(aAppId) { |
|
54 var nbCookies = 0; |
|
55 var enumerator = cookieMng.getCookiesForApp(aAppId, false); |
|
56 |
|
57 while (enumerator.hasMoreElements()) { |
|
58 enumerator.getNext(); |
|
59 nbCookies++; |
|
60 } |
|
61 |
|
62 return nbCookies; |
|
63 } |
|
64 |
|
65 function getCookiesCount() { |
|
66 var nbCookies = 0; |
|
67 var enumerator = cookieMng.enumerator; |
|
68 |
|
69 while (enumerator.hasMoreElements()) { |
|
70 enumerator.getNext(); |
|
71 nbCookies++; |
|
72 } |
|
73 |
|
74 return nbCookies; |
|
75 } |
|
76 |
|
77 function cookieSetHandler(metadata, response) { |
|
78 var cookieName = metadata.getHeader("foo-set-cookie"); |
|
79 |
|
80 response.setStatusLine(metadata.httpVersion, 200, "Ok"); |
|
81 response.setHeader("Set-Cookie", cookieName + "=1; Path=/", false); |
|
82 response.setHeader("Content-Type", "text/plain"); |
|
83 response.bodyOutputStream.write("Ok", "Ok".length); |
|
84 } |
|
85 |
|
86 function cookieCheckHandler(metadata, response) { |
|
87 var cookies = metadata.getHeader("Cookie"); |
|
88 |
|
89 response.setStatusLine(metadata.httpVersion, 200, "Ok"); |
|
90 response.setHeader("foo-saw-cookies", cookies, false); |
|
91 response.setHeader("Content-Type", "text/plain"); |
|
92 response.bodyOutputStream.write("Ok", "Ok".length); |
|
93 } |
|
94 |
|
95 function setupChannel(path) { |
|
96 var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); |
|
97 var chan = ios.newChannel("http://localhost:4444" + path, "", null); |
|
98 chan.notificationCallbacks = cookies[counter].loadContext; |
|
99 chan.QueryInterface(Ci.nsIHttpChannel); |
|
100 return chan; |
|
101 } |
|
102 |
|
103 function setCookie() { |
|
104 var channel = setupChannel(cookieSetPath); |
|
105 channel.setRequestHeader("foo-set-cookie", cookies[counter].cookieName, false); |
|
106 channel.asyncOpen(new ChannelListener(setNextCookie, null), null); |
|
107 } |
|
108 |
|
109 function setNextCookie(request, data, context) { |
|
110 if (++counter == cookies.length) { |
|
111 // all cookies set: switch to checking them |
|
112 counter = 0; |
|
113 checkCookie(); |
|
114 } else { |
|
115 setCookie(); |
|
116 } |
|
117 } |
|
118 |
|
119 permManager.addFromPrincipal(window.document.nodePrincipal, "webapps-manage", |
|
120 Ci.nsIPermissionManager.ALLOW_ACTION); |
|
121 |
|
122 SimpleTest.registerCleanupFunction(() => |
|
123 permManager.removeFromPrincipal(window.document.nodePrincipal, "webapps-manage", |
|
124 Ci.nsIPermissionManager.ALLOW_ACTION) |
|
125 ); |
|
126 |
|
127 var gManifestURL = "http://www.example.com/chrome/dom/tests/mochitest/webapps/apps/basic.webapp"; |
|
128 |
|
129 var gTestAppId = 0; |
|
130 var gCurrentCookiesCount = 0; |
|
131 |
|
132 function onInstall() { |
|
133 gTestAppId = appsService.getAppLocalIdByManifestURL(gManifestURL); |
|
134 |
|
135 cookies[0].loadContext = new LoadContextCallback(gTestAppId, false, false, 1); |
|
136 cookies[1].loadContext = new LoadContextCallback(gTestAppId, true, false, 1); |
|
137 |
|
138 is(getCookiesCountForApp(gTestAppId), 0, "App should have no cookies"); |
|
139 |
|
140 httpserver.registerPathHandler(cookieSetPath, cookieSetHandler); |
|
141 httpserver.registerPathHandler(cookieCheckPath, cookieCheckHandler); |
|
142 httpserver.start(4444); |
|
143 |
|
144 setCookie(); |
|
145 } |
|
146 |
|
147 function checkCookie() { |
|
148 var appCookiesCount = getCookiesCountForApp(gTestAppId); |
|
149 is(appCookiesCount, 2, "App should have two cookies"); |
|
150 |
|
151 gCurrentCookiesCount = getCookiesCount() - appCookiesCount; |
|
152 |
|
153 // Not installed means not installed as native app. |
|
154 navigator.mozApps.mgmt.getNotInstalled().onsuccess = function() { |
|
155 for (i in this.result) { |
|
156 var app = this.result[i]; |
|
157 if (app.manifestURL == gManifestURL) { |
|
158 navigator.mozApps.mgmt.uninstall(app).onsuccess = function() { |
|
159 is(getCookiesCountForApp(gTestAppId), 0, "App should have no cookies"); |
|
160 |
|
161 is(getCookiesCount(), gCurrentCookiesCount, |
|
162 "Number of cookies should not have changed"); |
|
163 |
|
164 httpserver.stop(function() { |
|
165 SimpleTest.finish(); |
|
166 }); |
|
167 }; |
|
168 } |
|
169 } |
|
170 }; |
|
171 } |
|
172 |
|
173 SpecialPowers.pushPrefEnv({set: [['network.cookie.cookieBehavior', 0]]}, () => |
|
174 SpecialPowers.autoConfirmAppInstall(() => |
|
175 navigator.mozApps.install(gManifestURL, null).onsuccess = onInstall |
|
176 ) |
|
177 ); |
|
178 </script> |
|
179 </pre> |
|
180 </body> |
|
181 </html> |