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