toolkit/forgetaboutsite/test/unit/test_removeDataFromDomain.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/forgetaboutsite/test/unit/test_removeDataFromDomain.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,752 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 sts=2
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/**
    1.11 + * Test added with bug 460086 to test the behavior of the new API that was added
    1.12 + * to remove all traces of visiting a site.
    1.13 + */
    1.14 +
    1.15 +////////////////////////////////////////////////////////////////////////////////
    1.16 +//// Globals
    1.17 +
    1.18 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.19 +Cu.import("resource://gre/modules/Services.jsm");
    1.20 +Cu.import("resource://gre/modules/PlacesUtils.jsm");
    1.21 +Cu.import("resource://gre/modules/ForgetAboutSite.jsm");
    1.22 +
    1.23 +XPCOMUtils.defineLazyModuleGetter(this, "Promise",
    1.24 +                                  "resource://gre/modules/Promise.jsm");
    1.25 +
    1.26 +const COOKIE_EXPIRY = Math.round(Date.now() / 1000) + 60;
    1.27 +const COOKIE_NAME = "testcookie";
    1.28 +const COOKIE_PATH = "/";
    1.29 +
    1.30 +const LOGIN_USERNAME = "username";
    1.31 +const LOGIN_PASSWORD = "password";
    1.32 +const LOGIN_USERNAME_FIELD = "username_field";
    1.33 +const LOGIN_PASSWORD_FIELD = "password_field";
    1.34 +
    1.35 +const PERMISSION_TYPE = "test-perm";
    1.36 +const PERMISSION_VALUE = Ci.nsIPermissionManager.ALLOW_ACTION;
    1.37 +
    1.38 +const PREFERENCE_NAME = "test-pref";
    1.39 +
    1.40 +////////////////////////////////////////////////////////////////////////////////
    1.41 +//// Utility Functions
    1.42 +
    1.43 +/**
    1.44 + * Creates an nsIURI object for the given string representation of a URI.
    1.45 + *
    1.46 + * @param aURIString
    1.47 + *        The spec of the URI to create.
    1.48 + * @returns an nsIURI representing aURIString.
    1.49 + */
    1.50 +function uri(aURIString)
    1.51 +{
    1.52 +  return Cc["@mozilla.org/network/io-service;1"].
    1.53 +         getService(Ci.nsIIOService).
    1.54 +         newURI(aURIString, null, null);
    1.55 +}
    1.56 +
    1.57 +/**
    1.58 + * Asynchronously adds visits to a page.
    1.59 + *
    1.60 + * @param aPlaceInfo
    1.61 + *        Can be an nsIURI, in such a case a single LINK visit will be added.
    1.62 + *        Otherwise can be an object describing the visit to add, or an array
    1.63 + *        of these objects:
    1.64 + *          { uri: nsIURI of the page,
    1.65 + *            transition: one of the TRANSITION_* from nsINavHistoryService,
    1.66 + *            [optional] title: title of the page,
    1.67 + *            [optional] visitDate: visit date in microseconds from the epoch
    1.68 + *            [optional] referrer: nsIURI of the referrer for this visit
    1.69 + *          }
    1.70 + *
    1.71 + * @return {Promise}
    1.72 + * @resolves When all visits have been added successfully.
    1.73 + * @rejects JavaScript exception.
    1.74 + */
    1.75 +function promiseAddVisits(aPlaceInfo)
    1.76 +{
    1.77 +  let deferred = Promise.defer();
    1.78 +  let places = [];
    1.79 +  if (aPlaceInfo instanceof Ci.nsIURI) {
    1.80 +    places.push({ uri: aPlaceInfo });
    1.81 +  }
    1.82 +  else if (Array.isArray(aPlaceInfo)) {
    1.83 +    places = places.concat(aPlaceInfo);
    1.84 +  } else {
    1.85 +    places.push(aPlaceInfo)
    1.86 +  }
    1.87 +
    1.88 +  // Create mozIVisitInfo for each entry.
    1.89 +  let now = Date.now();
    1.90 +  for (let i = 0; i < places.length; i++) {
    1.91 +    if (!places[i].title) {
    1.92 +      places[i].title = "test visit for " + places[i].uri.spec;
    1.93 +    }
    1.94 +    places[i].visits = [{
    1.95 +      transitionType: places[i].transition === undefined ? Ci.nsINavHistoryService.TRANSITION_LINK
    1.96 +                                                         : places[i].transition,
    1.97 +      visitDate: places[i].visitDate || (now++) * 1000,
    1.98 +      referrerURI: places[i].referrer
    1.99 +    }];
   1.100 +  }
   1.101 +
   1.102 +  PlacesUtils.asyncHistory.updatePlaces(
   1.103 +    places,
   1.104 +    {
   1.105 +      handleError: function handleError(aResultCode, aPlaceInfo) {
   1.106 +        let ex = new Components.Exception("Unexpected error in adding visits.",
   1.107 +                                          aResultCode);
   1.108 +        deferred.reject(ex);
   1.109 +      },
   1.110 +      handleResult: function () {},
   1.111 +      handleCompletion: function handleCompletion() {
   1.112 +        deferred.resolve();
   1.113 +      }
   1.114 +    }
   1.115 +  );
   1.116 +
   1.117 +  return deferred.promise;
   1.118 +}
   1.119 +
   1.120 +
   1.121 +/**
   1.122 + * Asynchronously check a url is visited.
   1.123 + *
   1.124 + * @param aURI
   1.125 + *        The URI.
   1.126 + *
   1.127 + * @return {Promise}
   1.128 + * @resolves When the check has been added successfully.
   1.129 + * @rejects JavaScript exception.
   1.130 + */
   1.131 +function promiseIsURIVisited(aURI)
   1.132 +{
   1.133 +  let deferred = Promise.defer();
   1.134 +  PlacesUtils.asyncHistory.isURIVisited(aURI, function(aURI, aIsVisited) {
   1.135 +    deferred.resolve(aIsVisited);
   1.136 +  });
   1.137 +
   1.138 +  return deferred.promise;
   1.139 +}
   1.140 +
   1.141 +/**
   1.142 + * Add a cookie to the cookie service.
   1.143 + *
   1.144 + * @param aDomain
   1.145 + */
   1.146 +function add_cookie(aDomain)
   1.147 +{
   1.148 +  check_cookie_exists(aDomain, false);
   1.149 +  let cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
   1.150 +  cm.add(aDomain, COOKIE_PATH, COOKIE_NAME, "", false, false, false,
   1.151 +         COOKIE_EXPIRY);
   1.152 +  check_cookie_exists(aDomain, true);
   1.153 +}
   1.154 +
   1.155 +/**
   1.156 + * Checks to ensure that a cookie exists or not for a domain.
   1.157 + *
   1.158 + * @param aDomain
   1.159 + *        The domain to check for the cookie.
   1.160 + * @param aExists
   1.161 + *        True if the cookie should exist, false otherwise.
   1.162 + */
   1.163 +function check_cookie_exists(aDomain, aExists)
   1.164 +{
   1.165 +  let cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
   1.166 +  let cookie = {
   1.167 +    host: aDomain,
   1.168 +    name: COOKIE_NAME,
   1.169 +    path: COOKIE_PATH
   1.170 +  }
   1.171 +  let checker = aExists ? do_check_true : do_check_false;
   1.172 +  checker(cm.cookieExists(cookie));
   1.173 +}
   1.174 +
   1.175 +/**
   1.176 + * Adds a download to download history.
   1.177 + *
   1.178 + * @param aURIString
   1.179 + *        The string of the URI to add.
   1.180 + * @param aIsActive
   1.181 + *        If it should be set to an active state in the database.  This does not
   1.182 + *        make it show up in the list of active downloads however!
   1.183 + */
   1.184 +function add_download(aURIString, aIsActive)
   1.185 +{
   1.186 +  function makeGUID() {
   1.187 +    let guid = "";
   1.188 +    for (var i = 0; i < 12; i++)
   1.189 +      guid += Math.floor(Math.random() * 10);
   1.190 +    return guid;
   1.191 +  }
   1.192 +
   1.193 +  check_downloaded(aURIString, false);
   1.194 +  let db = Cc["@mozilla.org/download-manager;1"].
   1.195 +           getService(Ci.nsIDownloadManager).
   1.196 +           DBConnection;
   1.197 +  let stmt = db.createStatement(
   1.198 +    "INSERT INTO moz_downloads (source, state, guid) " +
   1.199 +    "VALUES (:source, :state, :guid)"
   1.200 +  );
   1.201 +  stmt.params.source = aURIString;
   1.202 +  stmt.params.state = aIsActive ? Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING :
   1.203 +                                  Ci.nsIDownloadManager.DOWNLOAD_FINISHED;
   1.204 +  stmt.params.guid = makeGUID();
   1.205 +  try {
   1.206 +    stmt.execute();
   1.207 +  }
   1.208 +  finally {
   1.209 +    stmt.finalize();
   1.210 +  }
   1.211 +  check_downloaded(aURIString, true);
   1.212 +}
   1.213 +
   1.214 +/**
   1.215 + * Checks to ensure a URI string is in download history or not.
   1.216 + *
   1.217 + * @param aURIString
   1.218 + *        The string of the URI to check.
   1.219 + * @param aIsDownloaded
   1.220 + *        True if the URI should be downloaded, false otherwise.
   1.221 + */
   1.222 +function check_downloaded(aURIString, aIsDownloaded)
   1.223 +{
   1.224 +  let db = Cc["@mozilla.org/download-manager;1"].
   1.225 +           getService(Ci.nsIDownloadManager).
   1.226 +           DBConnection;
   1.227 +  let stmt = db.createStatement(
   1.228 +    "SELECT * " +
   1.229 +    "FROM moz_downloads " +
   1.230 +    "WHERE source = :source"
   1.231 +  );
   1.232 +  stmt.params.source = aURIString;
   1.233 +
   1.234 +  let checker = aIsDownloaded ? do_check_true : do_check_false;
   1.235 +  try {
   1.236 +    checker(stmt.executeStep());
   1.237 +  }
   1.238 +  finally {
   1.239 +    stmt.finalize();
   1.240 +  }
   1.241 +}
   1.242 +
   1.243 +/**
   1.244 + * Adds a disabled host to the login manager.
   1.245 + *
   1.246 + * @param aHost
   1.247 + *        The host to add to the list of disabled hosts.
   1.248 + */
   1.249 +function add_disabled_host(aHost)
   1.250 +{
   1.251 +  check_disabled_host(aHost, false);
   1.252 +  let lm = Cc["@mozilla.org/login-manager;1"].
   1.253 +           getService(Ci.nsILoginManager);
   1.254 +  lm.setLoginSavingEnabled(aHost, false);
   1.255 +  check_disabled_host(aHost, true);
   1.256 +}
   1.257 +
   1.258 +/**
   1.259 + * Checks to see if a host is disabled for storing logins or not.
   1.260 + *
   1.261 + * @param aHost
   1.262 + *        The host to check if it is disabled.
   1.263 + * @param aIsDisabled
   1.264 + *        True if the host should be disabled, false otherwise.
   1.265 + */
   1.266 +function check_disabled_host(aHost, aIsDisabled)
   1.267 +{
   1.268 +  let lm = Cc["@mozilla.org/login-manager;1"].
   1.269 +           getService(Ci.nsILoginManager);
   1.270 +  let checker = aIsDisabled ? do_check_false : do_check_true;
   1.271 +  checker(lm.getLoginSavingEnabled(aHost));
   1.272 +}
   1.273 +
   1.274 +/**
   1.275 + * Adds a login for the specified host to the login manager.
   1.276 + *
   1.277 + * @param aHost
   1.278 + *        The host to add the login for.
   1.279 + */
   1.280 +function add_login(aHost)
   1.281 +{
   1.282 +  check_login_exists(aHost, false);
   1.283 +  let login = Cc["@mozilla.org/login-manager/loginInfo;1"].
   1.284 +              createInstance(Ci.nsILoginInfo);
   1.285 +  login.init(aHost, "", null, LOGIN_USERNAME, LOGIN_PASSWORD,
   1.286 +             LOGIN_USERNAME_FIELD, LOGIN_PASSWORD_FIELD);
   1.287 +  let lm = Cc["@mozilla.org/login-manager;1"].
   1.288 +           getService(Ci.nsILoginManager);
   1.289 +  lm.addLogin(login);
   1.290 +  check_login_exists(aHost, true);
   1.291 +}
   1.292 +
   1.293 +/**
   1.294 + * Checks to see if a login exists for a host.
   1.295 + *
   1.296 + * @param aHost
   1.297 + *        The host to check for the test login.
   1.298 + * @param aExists
   1.299 + *        True if the login should exist, false otherwise.
   1.300 + */
   1.301 +function check_login_exists(aHost, aExists)
   1.302 +{
   1.303 +  let lm = Cc["@mozilla.org/login-manager;1"].
   1.304 +           getService(Ci.nsILoginManager);
   1.305 +  let count = { value: 0 };
   1.306 +  lm.findLogins(count, aHost, "", null);
   1.307 +  do_check_eq(count.value, aExists ? 1 : 0);
   1.308 +}
   1.309 +
   1.310 +/**
   1.311 + * Adds a permission for the specified URI to the permission manager.
   1.312 + *
   1.313 + * @param aURI
   1.314 + *        The URI to add the test permission for.
   1.315 + */
   1.316 +function add_permission(aURI)
   1.317 +{
   1.318 +  check_permission_exists(aURI, false);
   1.319 +  let pm = Cc["@mozilla.org/permissionmanager;1"].
   1.320 +           getService(Ci.nsIPermissionManager);
   1.321 +  let principal = Cc["@mozilla.org/scriptsecuritymanager;1"]
   1.322 +                    .getService(Ci.nsIScriptSecurityManager)
   1.323 +                    .getNoAppCodebasePrincipal(aURI);
   1.324 +
   1.325 +  pm.addFromPrincipal(principal, PERMISSION_TYPE, PERMISSION_VALUE);
   1.326 +  check_permission_exists(aURI, true);
   1.327 +}
   1.328 +
   1.329 +/**
   1.330 + * Checks to see if a permission exists for the given URI.
   1.331 + *
   1.332 + * @param aURI
   1.333 + *        The URI to check if a permission exists.
   1.334 + * @param aExists
   1.335 + *        True if the permission should exist, false otherwise.
   1.336 + */
   1.337 +function check_permission_exists(aURI, aExists)
   1.338 +{
   1.339 +  let pm = Cc["@mozilla.org/permissionmanager;1"].
   1.340 +           getService(Ci.nsIPermissionManager);
   1.341 +  let principal = Cc["@mozilla.org/scriptsecuritymanager;1"]
   1.342 +                    .getService(Ci.nsIScriptSecurityManager)
   1.343 +                    .getNoAppCodebasePrincipal(aURI);
   1.344 +
   1.345 +  let perm = pm.testExactPermissionFromPrincipal(principal, PERMISSION_TYPE);
   1.346 +  let checker = aExists ? do_check_eq : do_check_neq;
   1.347 +  checker(perm, PERMISSION_VALUE);
   1.348 +}
   1.349 +
   1.350 +/**
   1.351 + * Adds a content preference for the specified URI.
   1.352 + *
   1.353 + * @param aURI
   1.354 + *        The URI to add a preference for.
   1.355 + */
   1.356 +function add_preference(aURI)
   1.357 +{
   1.358 +  let deferred = Promise.defer();
   1.359 +  let cp = Cc["@mozilla.org/content-pref/service;1"].
   1.360 +             getService(Ci.nsIContentPrefService2);
   1.361 +  cp.set(aURI.spec, PREFERENCE_NAME, "foo", null, {
   1.362 +    handleCompletion: function() deferred.resolve()
   1.363 +  });
   1.364 +  return deferred.promise;
   1.365 +}
   1.366 +
   1.367 +/**
   1.368 + * Checks to see if a content preference exists for the given URI.
   1.369 + *
   1.370 + * @param aURI
   1.371 + *        The URI to check if a preference exists.
   1.372 + */
   1.373 +function preference_exists(aURI)
   1.374 +{
   1.375 +  let deferred = Promise.defer();
   1.376 +  let cp = Cc["@mozilla.org/content-pref/service;1"].
   1.377 +             getService(Ci.nsIContentPrefService2);
   1.378 +  let exists = false;
   1.379 +  cp.getByDomainAndName(aURI.spec, PREFERENCE_NAME, null, {
   1.380 +    handleResult: function() exists = true,
   1.381 +    handleCompletion: function() deferred.resolve(exists)
   1.382 +  });
   1.383 +  return deferred.promise;
   1.384 +}
   1.385 +
   1.386 +////////////////////////////////////////////////////////////////////////////////
   1.387 +//// Test Functions
   1.388 +
   1.389 +// History
   1.390 +function test_history_cleared_with_direct_match()
   1.391 +{
   1.392 +  const TEST_URI = uri("http://mozilla.org/foo");
   1.393 +  do_check_false(yield promiseIsURIVisited(TEST_URI));
   1.394 +  yield promiseAddVisits(TEST_URI);
   1.395 +  do_check_true(yield promiseIsURIVisited(TEST_URI));
   1.396 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.397 +  do_check_false(yield promiseIsURIVisited(TEST_URI));
   1.398 +}
   1.399 +
   1.400 +function test_history_cleared_with_subdomain()
   1.401 +{
   1.402 +  const TEST_URI = uri("http://www.mozilla.org/foo");
   1.403 +  do_check_false(yield promiseIsURIVisited(TEST_URI));
   1.404 +  yield promiseAddVisits(TEST_URI);
   1.405 +  do_check_true(yield promiseIsURIVisited(TEST_URI));
   1.406 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.407 +  do_check_false(yield promiseIsURIVisited(TEST_URI));
   1.408 +}
   1.409 +
   1.410 +function test_history_not_cleared_with_uri_contains_domain()
   1.411 +{
   1.412 +  const TEST_URI = uri("http://ilovemozilla.org/foo");
   1.413 +  do_check_false(yield promiseIsURIVisited(TEST_URI));
   1.414 +  yield promiseAddVisits(TEST_URI);
   1.415 +  do_check_true(yield promiseIsURIVisited(TEST_URI));
   1.416 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.417 +  do_check_true(yield promiseIsURIVisited(TEST_URI));
   1.418 +
   1.419 +  // Clear history since we left something there from this test.
   1.420 +  PlacesUtils.bhistory.removeAllPages();
   1.421 +}
   1.422 +
   1.423 +// Cookie Service
   1.424 +function test_cookie_cleared_with_direct_match()
   1.425 +{
   1.426 +  const TEST_DOMAIN = "mozilla.org";
   1.427 +  add_cookie(TEST_DOMAIN);
   1.428 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.429 +  check_cookie_exists(TEST_DOMAIN, false);
   1.430 +}
   1.431 +
   1.432 +function test_cookie_cleared_with_subdomain()
   1.433 +{
   1.434 +  const TEST_DOMAIN = "www.mozilla.org";
   1.435 +  add_cookie(TEST_DOMAIN);
   1.436 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.437 +  check_cookie_exists(TEST_DOMAIN, false);
   1.438 +}
   1.439 +
   1.440 +function test_cookie_not_cleared_with_uri_contains_domain()
   1.441 +{
   1.442 +  const TEST_DOMAIN = "ilovemozilla.org";
   1.443 +  add_cookie(TEST_DOMAIN);
   1.444 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.445 +  check_cookie_exists(TEST_DOMAIN, true);
   1.446 +}
   1.447 +
   1.448 +// Download Manager
   1.449 +function test_download_history_cleared_with_direct_match()
   1.450 +{
   1.451 +  if (oldDownloadManagerDisabled()) {
   1.452 +    return;
   1.453 +  }
   1.454 +
   1.455 +  const TEST_URI = "http://mozilla.org/foo";
   1.456 +  add_download(TEST_URI, false);
   1.457 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.458 +  check_downloaded(TEST_URI, false);
   1.459 +}
   1.460 +
   1.461 +function test_download_history_cleared_with_subdomain()
   1.462 +{
   1.463 +  if (oldDownloadManagerDisabled()) {
   1.464 +    return;
   1.465 +  }
   1.466 +
   1.467 +  const TEST_URI = "http://www.mozilla.org/foo";
   1.468 +  add_download(TEST_URI, false);
   1.469 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.470 +  check_downloaded(TEST_URI, false);
   1.471 +}
   1.472 +
   1.473 +function test_download_history_not_cleared_with_active_direct_match()
   1.474 +{
   1.475 +  if (oldDownloadManagerDisabled()) {
   1.476 +    return;
   1.477 +  }
   1.478 +
   1.479 +  // Tests that downloads marked as active in the db are not deleted from the db
   1.480 +  const TEST_URI = "http://mozilla.org/foo";
   1.481 +  add_download(TEST_URI, true);
   1.482 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.483 +  check_downloaded(TEST_URI, true);
   1.484 +
   1.485 +  // Reset state
   1.486 +  let db = Cc["@mozilla.org/download-manager;1"].
   1.487 +           getService(Ci.nsIDownloadManager).
   1.488 +           DBConnection;
   1.489 +  db.executeSimpleSQL("DELETE FROM moz_downloads");
   1.490 +  check_downloaded(TEST_URI, false);
   1.491 +}
   1.492 +
   1.493 +// Login Manager
   1.494 +function test_login_manager_disabled_hosts_cleared_with_direct_match()
   1.495 +{
   1.496 +  const TEST_HOST = "http://mozilla.org";
   1.497 +  add_disabled_host(TEST_HOST);
   1.498 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.499 +  check_disabled_host(TEST_HOST, false);
   1.500 +}
   1.501 +
   1.502 +function test_login_manager_disabled_hosts_cleared_with_subdomain()
   1.503 +{
   1.504 +  const TEST_HOST = "http://www.mozilla.org";
   1.505 +  add_disabled_host(TEST_HOST);
   1.506 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.507 +  check_disabled_host(TEST_HOST, false);
   1.508 +}
   1.509 +
   1.510 +function test_login_manager_disabled_hosts_not_cleared_with_uri_contains_domain()
   1.511 +{
   1.512 +  const TEST_HOST = "http://ilovemozilla.org";
   1.513 +  add_disabled_host(TEST_HOST);
   1.514 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.515 +  check_disabled_host(TEST_HOST, true);
   1.516 +
   1.517 +  // Reset state
   1.518 +  let lm = Cc["@mozilla.org/login-manager;1"].
   1.519 +           getService(Ci.nsILoginManager);
   1.520 +  lm.setLoginSavingEnabled(TEST_HOST, true);
   1.521 +  check_disabled_host(TEST_HOST, false);
   1.522 +}
   1.523 +
   1.524 +function test_login_manager_logins_cleared_with_direct_match()
   1.525 +{
   1.526 +  const TEST_HOST = "http://mozilla.org";
   1.527 +  add_login(TEST_HOST);
   1.528 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.529 +  check_login_exists(TEST_HOST, false);
   1.530 +}
   1.531 +
   1.532 +function test_login_manager_logins_cleared_with_subdomain()
   1.533 +{
   1.534 +  const TEST_HOST = "http://www.mozilla.org";
   1.535 +  add_login(TEST_HOST);
   1.536 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.537 +  check_login_exists(TEST_HOST, false);
   1.538 +}
   1.539 +
   1.540 +function tets_login_manager_logins_not_cleared_with_uri_contains_domain()
   1.541 +{
   1.542 +  const TEST_HOST = "http://ilovemozilla.org";
   1.543 +  add_login(TEST_HOST);
   1.544 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.545 +  check_login_exists(TEST_HOST, true);
   1.546 +
   1.547 +  let lm = Cc["@mozilla.org/login-manager;1"].
   1.548 +           getService(Ci.nsILoginManager);
   1.549 +  lm.removeAllLogins();
   1.550 +  check_login_exists(TEST_HOST, false);
   1.551 +}
   1.552 +
   1.553 +// Permission Manager
   1.554 +function test_permission_manager_cleared_with_direct_match()
   1.555 +{
   1.556 +  const TEST_URI = uri("http://mozilla.org");
   1.557 +  add_permission(TEST_URI);
   1.558 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.559 +  check_permission_exists(TEST_URI, false);
   1.560 +}
   1.561 +
   1.562 +function test_permission_manager_cleared_with_subdomain()
   1.563 +{
   1.564 +  const TEST_URI = uri("http://www.mozilla.org");
   1.565 +  add_permission(TEST_URI);
   1.566 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.567 +  check_permission_exists(TEST_URI, false);
   1.568 +}
   1.569 +
   1.570 +function test_permission_manager_not_cleared_with_uri_contains_domain()
   1.571 +{
   1.572 +  const TEST_URI = uri("http://ilovemozilla.org");
   1.573 +  add_permission(TEST_URI);
   1.574 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.575 +  check_permission_exists(TEST_URI, true);
   1.576 +
   1.577 +  // Reset state
   1.578 +  let pm = Cc["@mozilla.org/permissionmanager;1"].
   1.579 +           getService(Ci.nsIPermissionManager);
   1.580 +  pm.removeAll();
   1.581 +  check_permission_exists(TEST_URI, false);
   1.582 +}
   1.583 +
   1.584 +function waitForPurgeNotification() {
   1.585 +  let deferred = Promise.defer();
   1.586 +
   1.587 +  let observer = {
   1.588 +    observe: function(aSubject, aTopic, aData)
   1.589 +    {
   1.590 +      Services.obs.removeObserver(observer, "browser:purge-domain-data");
   1.591 +      // test_storage_cleared needs this extra executeSoon because
   1.592 +      // the DOMStorage clean-up is also listening to this same observer
   1.593 +      // which is run synchronously.
   1.594 +      Services.tm.mainThread.dispatch(function() {
   1.595 +        deferred.resolve();
   1.596 +      }, Components.interfaces.nsIThread.DISPATCH_NORMAL);
   1.597 +    }
   1.598 +  };
   1.599 +  Services.obs.addObserver(observer, "browser:purge-domain-data", false);
   1.600 +
   1.601 +  return deferred.promise;
   1.602 +}
   1.603 +
   1.604 +// Content Preferences
   1.605 +function test_content_preferences_cleared_with_direct_match()
   1.606 +{
   1.607 +  const TEST_URI = uri("http://mozilla.org");
   1.608 +  do_check_false(yield preference_exists(TEST_URI));
   1.609 +  yield add_preference(TEST_URI);
   1.610 +  do_check_true(yield preference_exists(TEST_URI));
   1.611 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.612 +  yield waitForPurgeNotification();
   1.613 +  do_check_false(yield preference_exists(TEST_URI));
   1.614 +}
   1.615 +
   1.616 +function test_content_preferences_cleared_with_subdomain()
   1.617 +{
   1.618 +  const TEST_URI = uri("http://www.mozilla.org");
   1.619 +  do_check_false(yield preference_exists(TEST_URI));
   1.620 +  yield add_preference(TEST_URI);
   1.621 +  do_check_true(yield preference_exists(TEST_URI));
   1.622 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.623 +  yield waitForPurgeNotification();
   1.624 +  do_check_false(yield preference_exists(TEST_URI));
   1.625 +}
   1.626 +
   1.627 +function test_content_preferences_not_cleared_with_uri_contains_domain()
   1.628 +{
   1.629 +  const TEST_URI = uri("http://ilovemozilla.org");
   1.630 +  do_check_false(yield preference_exists(TEST_URI));
   1.631 +  yield add_preference(TEST_URI);
   1.632 +  do_check_true(yield preference_exists(TEST_URI));
   1.633 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.634 +  yield waitForPurgeNotification();
   1.635 +  do_check_true(yield preference_exists(TEST_URI));
   1.636 +
   1.637 +  // Reset state
   1.638 +  ForgetAboutSite.removeDataFromDomain("ilovemozilla.org");
   1.639 +  yield waitForPurgeNotification();
   1.640 +  do_check_false(yield preference_exists(TEST_URI));
   1.641 +}
   1.642 +
   1.643 +// Cache
   1.644 +function test_cache_cleared()
   1.645 +{
   1.646 +  // Because this test is asynchronous, it should be the last test
   1.647 +  do_check_true(tests[tests.length - 1] == arguments.callee);
   1.648 +
   1.649 +  // NOTE: We could be more extensive with this test and actually add an entry
   1.650 +  //       to the cache, and then make sure it is gone.  However, we trust that
   1.651 +  //       the API is well tested, and that when we get the observer
   1.652 +  //       notification, we have actually cleared the cache.
   1.653 +  // This seems to happen asynchronously...
   1.654 +  let os = Cc["@mozilla.org/observer-service;1"].
   1.655 +           getService(Ci.nsIObserverService);
   1.656 +  let observer = {
   1.657 +    observe: function(aSubject, aTopic, aData)
   1.658 +    {
   1.659 +      os.removeObserver(observer, "cacheservice:empty-cache");
   1.660 +      // Shutdown the download manager.
   1.661 +      Services.obs.notifyObservers(null, "quit-application", null);
   1.662 +      do_test_finished();
   1.663 +    }
   1.664 +  };
   1.665 +  os.addObserver(observer, "cacheservice:empty-cache", false);
   1.666 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.667 +  do_test_pending();
   1.668 +}
   1.669 +
   1.670 +function test_storage_cleared()
   1.671 +{
   1.672 +  function getStorageForURI(aURI)
   1.673 +  {
   1.674 +    let principal = Cc["@mozilla.org/scriptsecuritymanager;1"].
   1.675 +                    getService(Ci.nsIScriptSecurityManager).
   1.676 +                    getNoAppCodebasePrincipal(aURI);
   1.677 +    let dsm = Cc["@mozilla.org/dom/localStorage-manager;1"].
   1.678 +              getService(Ci.nsIDOMStorageManager);
   1.679 +    return dsm.createStorage(principal, "");
   1.680 +  }
   1.681 +
   1.682 +  let s = [
   1.683 +    getStorageForURI(uri("http://mozilla.org")),
   1.684 +    getStorageForURI(uri("http://my.mozilla.org")),
   1.685 +    getStorageForURI(uri("http://ilovemozilla.org")),
   1.686 +  ];
   1.687 +
   1.688 +  for (let i = 0; i < s.length; ++i) {
   1.689 +    let storage = s[i];
   1.690 +    storage.setItem("test", "value" + i);
   1.691 +    do_check_eq(storage.length, 1);
   1.692 +    do_check_eq(storage.key(0), "test");
   1.693 +    do_check_eq(storage.getItem("test"), "value" + i);
   1.694 +  }
   1.695 +
   1.696 +  ForgetAboutSite.removeDataFromDomain("mozilla.org");
   1.697 +  yield waitForPurgeNotification();
   1.698 +
   1.699 +  do_check_eq(s[0].getItem("test"), null);
   1.700 +  do_check_eq(s[0].length, 0);
   1.701 +  do_check_eq(s[1].getItem("test"), null);
   1.702 +  do_check_eq(s[1].length, 0);
   1.703 +  do_check_eq(s[2].getItem("test"), "value2");
   1.704 +  do_check_eq(s[2].length, 1);
   1.705 +}
   1.706 +
   1.707 +let tests = [
   1.708 +  // History
   1.709 +  test_history_cleared_with_direct_match,
   1.710 +  test_history_cleared_with_subdomain,
   1.711 +  test_history_not_cleared_with_uri_contains_domain,
   1.712 +
   1.713 +  // Cookie Service
   1.714 +  test_cookie_cleared_with_direct_match,
   1.715 +  test_cookie_cleared_with_subdomain,
   1.716 +  test_cookie_not_cleared_with_uri_contains_domain,
   1.717 +
   1.718 +  // Download Manager
   1.719 +  // Note: active downloads tested in test_removeDataFromDomain_activeDownloads.js
   1.720 +  test_download_history_cleared_with_direct_match,
   1.721 +  test_download_history_cleared_with_subdomain,
   1.722 +  test_download_history_not_cleared_with_active_direct_match,
   1.723 +
   1.724 +  // Login Manager
   1.725 +  test_login_manager_disabled_hosts_cleared_with_direct_match,
   1.726 +  test_login_manager_disabled_hosts_cleared_with_subdomain,
   1.727 +  test_login_manager_disabled_hosts_not_cleared_with_uri_contains_domain,
   1.728 +  test_login_manager_logins_cleared_with_direct_match,
   1.729 +  test_login_manager_logins_cleared_with_subdomain,
   1.730 +  tets_login_manager_logins_not_cleared_with_uri_contains_domain,
   1.731 +
   1.732 +  // Permission Manager
   1.733 +  test_permission_manager_cleared_with_direct_match,
   1.734 +  test_permission_manager_cleared_with_subdomain,
   1.735 +  test_permission_manager_not_cleared_with_uri_contains_domain,
   1.736 +
   1.737 +  // Content Preferences
   1.738 +  test_content_preferences_cleared_with_direct_match,
   1.739 +  test_content_preferences_cleared_with_subdomain,
   1.740 +  test_content_preferences_not_cleared_with_uri_contains_domain,
   1.741 +
   1.742 +  // Storage
   1.743 +  test_storage_cleared,
   1.744 +
   1.745 +  // Cache
   1.746 +  test_cache_cleared,
   1.747 +];
   1.748 +
   1.749 +function run_test()
   1.750 +{
   1.751 +  for (let i = 0; i < tests.length; i++)
   1.752 +    add_task(tests[i]);
   1.753 +
   1.754 +  run_next_test();
   1.755 +}

mercurial