extensions/cookie/test/test_app_uninstall_cookies.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/cookie/test/test_app_uninstall_cookies.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,181 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=783408
     1.8 +-->
     1.9 +<head>
    1.10 +  <meta charset="utf-8">
    1.11 +  <title>Tests that uninstalling app removes the cookies</title>
    1.12 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    1.13 +  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
    1.14 +  <script src="channel_utils.js"></script>
    1.15 +</head>
    1.16 +<body>
    1.17 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=786296">Mozilla Bug 783408</a>
    1.18 +<p id="display"></p>
    1.19 +<div id="content">
    1.20 +
    1.21 +</div>
    1.22 +<pre id="test">
    1.23 +<script type="application/javascript;version=1.7">
    1.24 +
    1.25 +/** Test for Bug 783408 **/
    1.26 +
    1.27 +const Ci = Components.interfaces;
    1.28 +const Cc = Components.classes;
    1.29 +const Cu = Components.utils;
    1.30 +
    1.31 +SimpleTest.waitForExplicitFinish();
    1.32 +
    1.33 +Cu.import("resource://testing-common/httpd.js");
    1.34 +var httpserver = new HttpServer();
    1.35 +
    1.36 +var cookieSetPath = "/setcookie";
    1.37 +var cookieCheckPath = "/checkcookie";
    1.38 +
    1.39 +var permManager = Cc["@mozilla.org/permissionmanager;1"]
    1.40 +                    .getService(Ci.nsIPermissionManager);
    1.41 +var cookieMng = Cc["@mozilla.org/cookiemanager;1"]
    1.42 +                  .getService(Ci.nsICookieManager2);
    1.43 +var appsService = Cc['@mozilla.org/AppsService;1']
    1.44 +                    .getService(Ci.nsIAppsService);
    1.45 +
    1.46 +var cookies = [
    1.47 +  { cookieName: 'LCC_App_BrowF_PrivF',
    1.48 +    loadContext: null },
    1.49 +  { cookieName: 'LCC_App_BrowT_PrivF',
    1.50 +    loadContext: null },
    1.51 +  { cookieName: 'AppUninstall_Witness',
    1.52 +    loadContext: new LoadContextCallback(0, false, false, 1) },
    1.53 +];
    1.54 +var counter = 0;
    1.55 +
    1.56 +function getCookiesCountForApp(aAppId) {
    1.57 +  var nbCookies = 0;
    1.58 +  var enumerator = cookieMng.getCookiesForApp(aAppId, false);
    1.59 +
    1.60 +  while (enumerator.hasMoreElements()) {
    1.61 +    enumerator.getNext();
    1.62 +    nbCookies++;
    1.63 +  }
    1.64 +
    1.65 +  return nbCookies;
    1.66 +}
    1.67 +
    1.68 +function getCookiesCount() {
    1.69 +  var nbCookies = 0;
    1.70 +  var enumerator = cookieMng.enumerator;
    1.71 +
    1.72 +  while (enumerator.hasMoreElements()) {
    1.73 +    enumerator.getNext();
    1.74 +    nbCookies++;
    1.75 +  }
    1.76 +
    1.77 +  return nbCookies;
    1.78 +}
    1.79 +
    1.80 +function cookieSetHandler(metadata, response) {
    1.81 +  var cookieName = metadata.getHeader("foo-set-cookie");
    1.82 +
    1.83 +  response.setStatusLine(metadata.httpVersion, 200, "Ok");
    1.84 +  response.setHeader("Set-Cookie", cookieName + "=1; Path=/", false);
    1.85 +  response.setHeader("Content-Type", "text/plain");
    1.86 +  response.bodyOutputStream.write("Ok", "Ok".length);
    1.87 +}
    1.88 +
    1.89 +function cookieCheckHandler(metadata, response) {
    1.90 +  var cookies = metadata.getHeader("Cookie");
    1.91 +
    1.92 +  response.setStatusLine(metadata.httpVersion, 200, "Ok");
    1.93 +  response.setHeader("foo-saw-cookies", cookies, false);
    1.94 +  response.setHeader("Content-Type", "text/plain");
    1.95 +  response.bodyOutputStream.write("Ok", "Ok".length);
    1.96 +}
    1.97 +
    1.98 +function setupChannel(path) {
    1.99 +  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
   1.100 +  var chan = ios.newChannel("http://localhost:4444" + path, "", null);
   1.101 +  chan.notificationCallbacks = cookies[counter].loadContext;
   1.102 +  chan.QueryInterface(Ci.nsIHttpChannel);
   1.103 +  return chan;
   1.104 +}
   1.105 +
   1.106 +function setCookie() {
   1.107 +  var channel = setupChannel(cookieSetPath);
   1.108 +  channel.setRequestHeader("foo-set-cookie", cookies[counter].cookieName, false);
   1.109 +  channel.asyncOpen(new ChannelListener(setNextCookie, null), null);
   1.110 +}
   1.111 +
   1.112 +function setNextCookie(request, data, context) {
   1.113 +  if (++counter == cookies.length) {
   1.114 +    // all cookies set: switch to checking them
   1.115 +    counter = 0;
   1.116 +    checkCookie();
   1.117 +  } else {
   1.118 +    setCookie();
   1.119 +  }
   1.120 +}
   1.121 +
   1.122 +permManager.addFromPrincipal(window.document.nodePrincipal, "webapps-manage",
   1.123 +                             Ci.nsIPermissionManager.ALLOW_ACTION);
   1.124 +
   1.125 +SimpleTest.registerCleanupFunction(() =>
   1.126 +  permManager.removeFromPrincipal(window.document.nodePrincipal, "webapps-manage",
   1.127 +                                  Ci.nsIPermissionManager.ALLOW_ACTION)
   1.128 +);
   1.129 +
   1.130 +var gManifestURL = "http://www.example.com/chrome/dom/tests/mochitest/webapps/apps/basic.webapp";
   1.131 +
   1.132 +var gTestAppId = 0;
   1.133 +var gCurrentCookiesCount = 0;
   1.134 +
   1.135 +function onInstall() {
   1.136 +  gTestAppId = appsService.getAppLocalIdByManifestURL(gManifestURL);
   1.137 +
   1.138 +  cookies[0].loadContext = new LoadContextCallback(gTestAppId, false, false, 1);
   1.139 +  cookies[1].loadContext = new LoadContextCallback(gTestAppId, true, false, 1);
   1.140 +
   1.141 +  is(getCookiesCountForApp(gTestAppId), 0, "App should have no cookies");
   1.142 +
   1.143 +  httpserver.registerPathHandler(cookieSetPath, cookieSetHandler);
   1.144 +  httpserver.registerPathHandler(cookieCheckPath, cookieCheckHandler);
   1.145 +  httpserver.start(4444);
   1.146 +
   1.147 +  setCookie();
   1.148 +}
   1.149 +
   1.150 +function checkCookie() {
   1.151 +  var appCookiesCount = getCookiesCountForApp(gTestAppId);
   1.152 +  is(appCookiesCount, 2, "App should have two cookies");
   1.153 +
   1.154 +  gCurrentCookiesCount = getCookiesCount() - appCookiesCount;
   1.155 +
   1.156 +  // Not installed means not installed as native app.
   1.157 +  navigator.mozApps.mgmt.getNotInstalled().onsuccess = function() {
   1.158 +    for (i in this.result) {
   1.159 +      var app = this.result[i];
   1.160 +      if (app.manifestURL == gManifestURL) {
   1.161 +        navigator.mozApps.mgmt.uninstall(app).onsuccess = function() {
   1.162 +          is(getCookiesCountForApp(gTestAppId), 0, "App should have no cookies");
   1.163 +
   1.164 +          is(getCookiesCount(), gCurrentCookiesCount,
   1.165 +             "Number of cookies should not have changed");
   1.166 +
   1.167 +          httpserver.stop(function() {
   1.168 +            SimpleTest.finish();
   1.169 +          });
   1.170 +        };
   1.171 +      }
   1.172 +    }
   1.173 +  };
   1.174 +}
   1.175 +
   1.176 +SpecialPowers.pushPrefEnv({set: [['network.cookie.cookieBehavior', 0]]}, () =>
   1.177 +  SpecialPowers.autoConfirmAppInstall(() =>
   1.178 +    navigator.mozApps.install(gManifestURL, null).onsuccess = onInstall
   1.179 +  )
   1.180 +);
   1.181 +</script>
   1.182 +</pre>
   1.183 +</body>
   1.184 +</html>

mercurial