toolkit/forgetaboutsite/test/unit/test_removeDataFromDomain.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim: sw=2 ts=2 sts=2
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /**
michael@0 8 * Test added with bug 460086 to test the behavior of the new API that was added
michael@0 9 * to remove all traces of visiting a site.
michael@0 10 */
michael@0 11
michael@0 12 ////////////////////////////////////////////////////////////////////////////////
michael@0 13 //// Globals
michael@0 14
michael@0 15 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 16 Cu.import("resource://gre/modules/Services.jsm");
michael@0 17 Cu.import("resource://gre/modules/PlacesUtils.jsm");
michael@0 18 Cu.import("resource://gre/modules/ForgetAboutSite.jsm");
michael@0 19
michael@0 20 XPCOMUtils.defineLazyModuleGetter(this, "Promise",
michael@0 21 "resource://gre/modules/Promise.jsm");
michael@0 22
michael@0 23 const COOKIE_EXPIRY = Math.round(Date.now() / 1000) + 60;
michael@0 24 const COOKIE_NAME = "testcookie";
michael@0 25 const COOKIE_PATH = "/";
michael@0 26
michael@0 27 const LOGIN_USERNAME = "username";
michael@0 28 const LOGIN_PASSWORD = "password";
michael@0 29 const LOGIN_USERNAME_FIELD = "username_field";
michael@0 30 const LOGIN_PASSWORD_FIELD = "password_field";
michael@0 31
michael@0 32 const PERMISSION_TYPE = "test-perm";
michael@0 33 const PERMISSION_VALUE = Ci.nsIPermissionManager.ALLOW_ACTION;
michael@0 34
michael@0 35 const PREFERENCE_NAME = "test-pref";
michael@0 36
michael@0 37 ////////////////////////////////////////////////////////////////////////////////
michael@0 38 //// Utility Functions
michael@0 39
michael@0 40 /**
michael@0 41 * Creates an nsIURI object for the given string representation of a URI.
michael@0 42 *
michael@0 43 * @param aURIString
michael@0 44 * The spec of the URI to create.
michael@0 45 * @returns an nsIURI representing aURIString.
michael@0 46 */
michael@0 47 function uri(aURIString)
michael@0 48 {
michael@0 49 return Cc["@mozilla.org/network/io-service;1"].
michael@0 50 getService(Ci.nsIIOService).
michael@0 51 newURI(aURIString, null, null);
michael@0 52 }
michael@0 53
michael@0 54 /**
michael@0 55 * Asynchronously adds visits to a page.
michael@0 56 *
michael@0 57 * @param aPlaceInfo
michael@0 58 * Can be an nsIURI, in such a case a single LINK visit will be added.
michael@0 59 * Otherwise can be an object describing the visit to add, or an array
michael@0 60 * of these objects:
michael@0 61 * { uri: nsIURI of the page,
michael@0 62 * transition: one of the TRANSITION_* from nsINavHistoryService,
michael@0 63 * [optional] title: title of the page,
michael@0 64 * [optional] visitDate: visit date in microseconds from the epoch
michael@0 65 * [optional] referrer: nsIURI of the referrer for this visit
michael@0 66 * }
michael@0 67 *
michael@0 68 * @return {Promise}
michael@0 69 * @resolves When all visits have been added successfully.
michael@0 70 * @rejects JavaScript exception.
michael@0 71 */
michael@0 72 function promiseAddVisits(aPlaceInfo)
michael@0 73 {
michael@0 74 let deferred = Promise.defer();
michael@0 75 let places = [];
michael@0 76 if (aPlaceInfo instanceof Ci.nsIURI) {
michael@0 77 places.push({ uri: aPlaceInfo });
michael@0 78 }
michael@0 79 else if (Array.isArray(aPlaceInfo)) {
michael@0 80 places = places.concat(aPlaceInfo);
michael@0 81 } else {
michael@0 82 places.push(aPlaceInfo)
michael@0 83 }
michael@0 84
michael@0 85 // Create mozIVisitInfo for each entry.
michael@0 86 let now = Date.now();
michael@0 87 for (let i = 0; i < places.length; i++) {
michael@0 88 if (!places[i].title) {
michael@0 89 places[i].title = "test visit for " + places[i].uri.spec;
michael@0 90 }
michael@0 91 places[i].visits = [{
michael@0 92 transitionType: places[i].transition === undefined ? Ci.nsINavHistoryService.TRANSITION_LINK
michael@0 93 : places[i].transition,
michael@0 94 visitDate: places[i].visitDate || (now++) * 1000,
michael@0 95 referrerURI: places[i].referrer
michael@0 96 }];
michael@0 97 }
michael@0 98
michael@0 99 PlacesUtils.asyncHistory.updatePlaces(
michael@0 100 places,
michael@0 101 {
michael@0 102 handleError: function handleError(aResultCode, aPlaceInfo) {
michael@0 103 let ex = new Components.Exception("Unexpected error in adding visits.",
michael@0 104 aResultCode);
michael@0 105 deferred.reject(ex);
michael@0 106 },
michael@0 107 handleResult: function () {},
michael@0 108 handleCompletion: function handleCompletion() {
michael@0 109 deferred.resolve();
michael@0 110 }
michael@0 111 }
michael@0 112 );
michael@0 113
michael@0 114 return deferred.promise;
michael@0 115 }
michael@0 116
michael@0 117
michael@0 118 /**
michael@0 119 * Asynchronously check a url is visited.
michael@0 120 *
michael@0 121 * @param aURI
michael@0 122 * The URI.
michael@0 123 *
michael@0 124 * @return {Promise}
michael@0 125 * @resolves When the check has been added successfully.
michael@0 126 * @rejects JavaScript exception.
michael@0 127 */
michael@0 128 function promiseIsURIVisited(aURI)
michael@0 129 {
michael@0 130 let deferred = Promise.defer();
michael@0 131 PlacesUtils.asyncHistory.isURIVisited(aURI, function(aURI, aIsVisited) {
michael@0 132 deferred.resolve(aIsVisited);
michael@0 133 });
michael@0 134
michael@0 135 return deferred.promise;
michael@0 136 }
michael@0 137
michael@0 138 /**
michael@0 139 * Add a cookie to the cookie service.
michael@0 140 *
michael@0 141 * @param aDomain
michael@0 142 */
michael@0 143 function add_cookie(aDomain)
michael@0 144 {
michael@0 145 check_cookie_exists(aDomain, false);
michael@0 146 let cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
michael@0 147 cm.add(aDomain, COOKIE_PATH, COOKIE_NAME, "", false, false, false,
michael@0 148 COOKIE_EXPIRY);
michael@0 149 check_cookie_exists(aDomain, true);
michael@0 150 }
michael@0 151
michael@0 152 /**
michael@0 153 * Checks to ensure that a cookie exists or not for a domain.
michael@0 154 *
michael@0 155 * @param aDomain
michael@0 156 * The domain to check for the cookie.
michael@0 157 * @param aExists
michael@0 158 * True if the cookie should exist, false otherwise.
michael@0 159 */
michael@0 160 function check_cookie_exists(aDomain, aExists)
michael@0 161 {
michael@0 162 let cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
michael@0 163 let cookie = {
michael@0 164 host: aDomain,
michael@0 165 name: COOKIE_NAME,
michael@0 166 path: COOKIE_PATH
michael@0 167 }
michael@0 168 let checker = aExists ? do_check_true : do_check_false;
michael@0 169 checker(cm.cookieExists(cookie));
michael@0 170 }
michael@0 171
michael@0 172 /**
michael@0 173 * Adds a download to download history.
michael@0 174 *
michael@0 175 * @param aURIString
michael@0 176 * The string of the URI to add.
michael@0 177 * @param aIsActive
michael@0 178 * If it should be set to an active state in the database. This does not
michael@0 179 * make it show up in the list of active downloads however!
michael@0 180 */
michael@0 181 function add_download(aURIString, aIsActive)
michael@0 182 {
michael@0 183 function makeGUID() {
michael@0 184 let guid = "";
michael@0 185 for (var i = 0; i < 12; i++)
michael@0 186 guid += Math.floor(Math.random() * 10);
michael@0 187 return guid;
michael@0 188 }
michael@0 189
michael@0 190 check_downloaded(aURIString, false);
michael@0 191 let db = Cc["@mozilla.org/download-manager;1"].
michael@0 192 getService(Ci.nsIDownloadManager).
michael@0 193 DBConnection;
michael@0 194 let stmt = db.createStatement(
michael@0 195 "INSERT INTO moz_downloads (source, state, guid) " +
michael@0 196 "VALUES (:source, :state, :guid)"
michael@0 197 );
michael@0 198 stmt.params.source = aURIString;
michael@0 199 stmt.params.state = aIsActive ? Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING :
michael@0 200 Ci.nsIDownloadManager.DOWNLOAD_FINISHED;
michael@0 201 stmt.params.guid = makeGUID();
michael@0 202 try {
michael@0 203 stmt.execute();
michael@0 204 }
michael@0 205 finally {
michael@0 206 stmt.finalize();
michael@0 207 }
michael@0 208 check_downloaded(aURIString, true);
michael@0 209 }
michael@0 210
michael@0 211 /**
michael@0 212 * Checks to ensure a URI string is in download history or not.
michael@0 213 *
michael@0 214 * @param aURIString
michael@0 215 * The string of the URI to check.
michael@0 216 * @param aIsDownloaded
michael@0 217 * True if the URI should be downloaded, false otherwise.
michael@0 218 */
michael@0 219 function check_downloaded(aURIString, aIsDownloaded)
michael@0 220 {
michael@0 221 let db = Cc["@mozilla.org/download-manager;1"].
michael@0 222 getService(Ci.nsIDownloadManager).
michael@0 223 DBConnection;
michael@0 224 let stmt = db.createStatement(
michael@0 225 "SELECT * " +
michael@0 226 "FROM moz_downloads " +
michael@0 227 "WHERE source = :source"
michael@0 228 );
michael@0 229 stmt.params.source = aURIString;
michael@0 230
michael@0 231 let checker = aIsDownloaded ? do_check_true : do_check_false;
michael@0 232 try {
michael@0 233 checker(stmt.executeStep());
michael@0 234 }
michael@0 235 finally {
michael@0 236 stmt.finalize();
michael@0 237 }
michael@0 238 }
michael@0 239
michael@0 240 /**
michael@0 241 * Adds a disabled host to the login manager.
michael@0 242 *
michael@0 243 * @param aHost
michael@0 244 * The host to add to the list of disabled hosts.
michael@0 245 */
michael@0 246 function add_disabled_host(aHost)
michael@0 247 {
michael@0 248 check_disabled_host(aHost, false);
michael@0 249 let lm = Cc["@mozilla.org/login-manager;1"].
michael@0 250 getService(Ci.nsILoginManager);
michael@0 251 lm.setLoginSavingEnabled(aHost, false);
michael@0 252 check_disabled_host(aHost, true);
michael@0 253 }
michael@0 254
michael@0 255 /**
michael@0 256 * Checks to see if a host is disabled for storing logins or not.
michael@0 257 *
michael@0 258 * @param aHost
michael@0 259 * The host to check if it is disabled.
michael@0 260 * @param aIsDisabled
michael@0 261 * True if the host should be disabled, false otherwise.
michael@0 262 */
michael@0 263 function check_disabled_host(aHost, aIsDisabled)
michael@0 264 {
michael@0 265 let lm = Cc["@mozilla.org/login-manager;1"].
michael@0 266 getService(Ci.nsILoginManager);
michael@0 267 let checker = aIsDisabled ? do_check_false : do_check_true;
michael@0 268 checker(lm.getLoginSavingEnabled(aHost));
michael@0 269 }
michael@0 270
michael@0 271 /**
michael@0 272 * Adds a login for the specified host to the login manager.
michael@0 273 *
michael@0 274 * @param aHost
michael@0 275 * The host to add the login for.
michael@0 276 */
michael@0 277 function add_login(aHost)
michael@0 278 {
michael@0 279 check_login_exists(aHost, false);
michael@0 280 let login = Cc["@mozilla.org/login-manager/loginInfo;1"].
michael@0 281 createInstance(Ci.nsILoginInfo);
michael@0 282 login.init(aHost, "", null, LOGIN_USERNAME, LOGIN_PASSWORD,
michael@0 283 LOGIN_USERNAME_FIELD, LOGIN_PASSWORD_FIELD);
michael@0 284 let lm = Cc["@mozilla.org/login-manager;1"].
michael@0 285 getService(Ci.nsILoginManager);
michael@0 286 lm.addLogin(login);
michael@0 287 check_login_exists(aHost, true);
michael@0 288 }
michael@0 289
michael@0 290 /**
michael@0 291 * Checks to see if a login exists for a host.
michael@0 292 *
michael@0 293 * @param aHost
michael@0 294 * The host to check for the test login.
michael@0 295 * @param aExists
michael@0 296 * True if the login should exist, false otherwise.
michael@0 297 */
michael@0 298 function check_login_exists(aHost, aExists)
michael@0 299 {
michael@0 300 let lm = Cc["@mozilla.org/login-manager;1"].
michael@0 301 getService(Ci.nsILoginManager);
michael@0 302 let count = { value: 0 };
michael@0 303 lm.findLogins(count, aHost, "", null);
michael@0 304 do_check_eq(count.value, aExists ? 1 : 0);
michael@0 305 }
michael@0 306
michael@0 307 /**
michael@0 308 * Adds a permission for the specified URI to the permission manager.
michael@0 309 *
michael@0 310 * @param aURI
michael@0 311 * The URI to add the test permission for.
michael@0 312 */
michael@0 313 function add_permission(aURI)
michael@0 314 {
michael@0 315 check_permission_exists(aURI, false);
michael@0 316 let pm = Cc["@mozilla.org/permissionmanager;1"].
michael@0 317 getService(Ci.nsIPermissionManager);
michael@0 318 let principal = Cc["@mozilla.org/scriptsecuritymanager;1"]
michael@0 319 .getService(Ci.nsIScriptSecurityManager)
michael@0 320 .getNoAppCodebasePrincipal(aURI);
michael@0 321
michael@0 322 pm.addFromPrincipal(principal, PERMISSION_TYPE, PERMISSION_VALUE);
michael@0 323 check_permission_exists(aURI, true);
michael@0 324 }
michael@0 325
michael@0 326 /**
michael@0 327 * Checks to see if a permission exists for the given URI.
michael@0 328 *
michael@0 329 * @param aURI
michael@0 330 * The URI to check if a permission exists.
michael@0 331 * @param aExists
michael@0 332 * True if the permission should exist, false otherwise.
michael@0 333 */
michael@0 334 function check_permission_exists(aURI, aExists)
michael@0 335 {
michael@0 336 let pm = Cc["@mozilla.org/permissionmanager;1"].
michael@0 337 getService(Ci.nsIPermissionManager);
michael@0 338 let principal = Cc["@mozilla.org/scriptsecuritymanager;1"]
michael@0 339 .getService(Ci.nsIScriptSecurityManager)
michael@0 340 .getNoAppCodebasePrincipal(aURI);
michael@0 341
michael@0 342 let perm = pm.testExactPermissionFromPrincipal(principal, PERMISSION_TYPE);
michael@0 343 let checker = aExists ? do_check_eq : do_check_neq;
michael@0 344 checker(perm, PERMISSION_VALUE);
michael@0 345 }
michael@0 346
michael@0 347 /**
michael@0 348 * Adds a content preference for the specified URI.
michael@0 349 *
michael@0 350 * @param aURI
michael@0 351 * The URI to add a preference for.
michael@0 352 */
michael@0 353 function add_preference(aURI)
michael@0 354 {
michael@0 355 let deferred = Promise.defer();
michael@0 356 let cp = Cc["@mozilla.org/content-pref/service;1"].
michael@0 357 getService(Ci.nsIContentPrefService2);
michael@0 358 cp.set(aURI.spec, PREFERENCE_NAME, "foo", null, {
michael@0 359 handleCompletion: function() deferred.resolve()
michael@0 360 });
michael@0 361 return deferred.promise;
michael@0 362 }
michael@0 363
michael@0 364 /**
michael@0 365 * Checks to see if a content preference exists for the given URI.
michael@0 366 *
michael@0 367 * @param aURI
michael@0 368 * The URI to check if a preference exists.
michael@0 369 */
michael@0 370 function preference_exists(aURI)
michael@0 371 {
michael@0 372 let deferred = Promise.defer();
michael@0 373 let cp = Cc["@mozilla.org/content-pref/service;1"].
michael@0 374 getService(Ci.nsIContentPrefService2);
michael@0 375 let exists = false;
michael@0 376 cp.getByDomainAndName(aURI.spec, PREFERENCE_NAME, null, {
michael@0 377 handleResult: function() exists = true,
michael@0 378 handleCompletion: function() deferred.resolve(exists)
michael@0 379 });
michael@0 380 return deferred.promise;
michael@0 381 }
michael@0 382
michael@0 383 ////////////////////////////////////////////////////////////////////////////////
michael@0 384 //// Test Functions
michael@0 385
michael@0 386 // History
michael@0 387 function test_history_cleared_with_direct_match()
michael@0 388 {
michael@0 389 const TEST_URI = uri("http://mozilla.org/foo");
michael@0 390 do_check_false(yield promiseIsURIVisited(TEST_URI));
michael@0 391 yield promiseAddVisits(TEST_URI);
michael@0 392 do_check_true(yield promiseIsURIVisited(TEST_URI));
michael@0 393 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 394 do_check_false(yield promiseIsURIVisited(TEST_URI));
michael@0 395 }
michael@0 396
michael@0 397 function test_history_cleared_with_subdomain()
michael@0 398 {
michael@0 399 const TEST_URI = uri("http://www.mozilla.org/foo");
michael@0 400 do_check_false(yield promiseIsURIVisited(TEST_URI));
michael@0 401 yield promiseAddVisits(TEST_URI);
michael@0 402 do_check_true(yield promiseIsURIVisited(TEST_URI));
michael@0 403 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 404 do_check_false(yield promiseIsURIVisited(TEST_URI));
michael@0 405 }
michael@0 406
michael@0 407 function test_history_not_cleared_with_uri_contains_domain()
michael@0 408 {
michael@0 409 const TEST_URI = uri("http://ilovemozilla.org/foo");
michael@0 410 do_check_false(yield promiseIsURIVisited(TEST_URI));
michael@0 411 yield promiseAddVisits(TEST_URI);
michael@0 412 do_check_true(yield promiseIsURIVisited(TEST_URI));
michael@0 413 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 414 do_check_true(yield promiseIsURIVisited(TEST_URI));
michael@0 415
michael@0 416 // Clear history since we left something there from this test.
michael@0 417 PlacesUtils.bhistory.removeAllPages();
michael@0 418 }
michael@0 419
michael@0 420 // Cookie Service
michael@0 421 function test_cookie_cleared_with_direct_match()
michael@0 422 {
michael@0 423 const TEST_DOMAIN = "mozilla.org";
michael@0 424 add_cookie(TEST_DOMAIN);
michael@0 425 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 426 check_cookie_exists(TEST_DOMAIN, false);
michael@0 427 }
michael@0 428
michael@0 429 function test_cookie_cleared_with_subdomain()
michael@0 430 {
michael@0 431 const TEST_DOMAIN = "www.mozilla.org";
michael@0 432 add_cookie(TEST_DOMAIN);
michael@0 433 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 434 check_cookie_exists(TEST_DOMAIN, false);
michael@0 435 }
michael@0 436
michael@0 437 function test_cookie_not_cleared_with_uri_contains_domain()
michael@0 438 {
michael@0 439 const TEST_DOMAIN = "ilovemozilla.org";
michael@0 440 add_cookie(TEST_DOMAIN);
michael@0 441 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 442 check_cookie_exists(TEST_DOMAIN, true);
michael@0 443 }
michael@0 444
michael@0 445 // Download Manager
michael@0 446 function test_download_history_cleared_with_direct_match()
michael@0 447 {
michael@0 448 if (oldDownloadManagerDisabled()) {
michael@0 449 return;
michael@0 450 }
michael@0 451
michael@0 452 const TEST_URI = "http://mozilla.org/foo";
michael@0 453 add_download(TEST_URI, false);
michael@0 454 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 455 check_downloaded(TEST_URI, false);
michael@0 456 }
michael@0 457
michael@0 458 function test_download_history_cleared_with_subdomain()
michael@0 459 {
michael@0 460 if (oldDownloadManagerDisabled()) {
michael@0 461 return;
michael@0 462 }
michael@0 463
michael@0 464 const TEST_URI = "http://www.mozilla.org/foo";
michael@0 465 add_download(TEST_URI, false);
michael@0 466 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 467 check_downloaded(TEST_URI, false);
michael@0 468 }
michael@0 469
michael@0 470 function test_download_history_not_cleared_with_active_direct_match()
michael@0 471 {
michael@0 472 if (oldDownloadManagerDisabled()) {
michael@0 473 return;
michael@0 474 }
michael@0 475
michael@0 476 // Tests that downloads marked as active in the db are not deleted from the db
michael@0 477 const TEST_URI = "http://mozilla.org/foo";
michael@0 478 add_download(TEST_URI, true);
michael@0 479 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 480 check_downloaded(TEST_URI, true);
michael@0 481
michael@0 482 // Reset state
michael@0 483 let db = Cc["@mozilla.org/download-manager;1"].
michael@0 484 getService(Ci.nsIDownloadManager).
michael@0 485 DBConnection;
michael@0 486 db.executeSimpleSQL("DELETE FROM moz_downloads");
michael@0 487 check_downloaded(TEST_URI, false);
michael@0 488 }
michael@0 489
michael@0 490 // Login Manager
michael@0 491 function test_login_manager_disabled_hosts_cleared_with_direct_match()
michael@0 492 {
michael@0 493 const TEST_HOST = "http://mozilla.org";
michael@0 494 add_disabled_host(TEST_HOST);
michael@0 495 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 496 check_disabled_host(TEST_HOST, false);
michael@0 497 }
michael@0 498
michael@0 499 function test_login_manager_disabled_hosts_cleared_with_subdomain()
michael@0 500 {
michael@0 501 const TEST_HOST = "http://www.mozilla.org";
michael@0 502 add_disabled_host(TEST_HOST);
michael@0 503 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 504 check_disabled_host(TEST_HOST, false);
michael@0 505 }
michael@0 506
michael@0 507 function test_login_manager_disabled_hosts_not_cleared_with_uri_contains_domain()
michael@0 508 {
michael@0 509 const TEST_HOST = "http://ilovemozilla.org";
michael@0 510 add_disabled_host(TEST_HOST);
michael@0 511 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 512 check_disabled_host(TEST_HOST, true);
michael@0 513
michael@0 514 // Reset state
michael@0 515 let lm = Cc["@mozilla.org/login-manager;1"].
michael@0 516 getService(Ci.nsILoginManager);
michael@0 517 lm.setLoginSavingEnabled(TEST_HOST, true);
michael@0 518 check_disabled_host(TEST_HOST, false);
michael@0 519 }
michael@0 520
michael@0 521 function test_login_manager_logins_cleared_with_direct_match()
michael@0 522 {
michael@0 523 const TEST_HOST = "http://mozilla.org";
michael@0 524 add_login(TEST_HOST);
michael@0 525 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 526 check_login_exists(TEST_HOST, false);
michael@0 527 }
michael@0 528
michael@0 529 function test_login_manager_logins_cleared_with_subdomain()
michael@0 530 {
michael@0 531 const TEST_HOST = "http://www.mozilla.org";
michael@0 532 add_login(TEST_HOST);
michael@0 533 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 534 check_login_exists(TEST_HOST, false);
michael@0 535 }
michael@0 536
michael@0 537 function tets_login_manager_logins_not_cleared_with_uri_contains_domain()
michael@0 538 {
michael@0 539 const TEST_HOST = "http://ilovemozilla.org";
michael@0 540 add_login(TEST_HOST);
michael@0 541 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 542 check_login_exists(TEST_HOST, true);
michael@0 543
michael@0 544 let lm = Cc["@mozilla.org/login-manager;1"].
michael@0 545 getService(Ci.nsILoginManager);
michael@0 546 lm.removeAllLogins();
michael@0 547 check_login_exists(TEST_HOST, false);
michael@0 548 }
michael@0 549
michael@0 550 // Permission Manager
michael@0 551 function test_permission_manager_cleared_with_direct_match()
michael@0 552 {
michael@0 553 const TEST_URI = uri("http://mozilla.org");
michael@0 554 add_permission(TEST_URI);
michael@0 555 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 556 check_permission_exists(TEST_URI, false);
michael@0 557 }
michael@0 558
michael@0 559 function test_permission_manager_cleared_with_subdomain()
michael@0 560 {
michael@0 561 const TEST_URI = uri("http://www.mozilla.org");
michael@0 562 add_permission(TEST_URI);
michael@0 563 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 564 check_permission_exists(TEST_URI, false);
michael@0 565 }
michael@0 566
michael@0 567 function test_permission_manager_not_cleared_with_uri_contains_domain()
michael@0 568 {
michael@0 569 const TEST_URI = uri("http://ilovemozilla.org");
michael@0 570 add_permission(TEST_URI);
michael@0 571 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 572 check_permission_exists(TEST_URI, true);
michael@0 573
michael@0 574 // Reset state
michael@0 575 let pm = Cc["@mozilla.org/permissionmanager;1"].
michael@0 576 getService(Ci.nsIPermissionManager);
michael@0 577 pm.removeAll();
michael@0 578 check_permission_exists(TEST_URI, false);
michael@0 579 }
michael@0 580
michael@0 581 function waitForPurgeNotification() {
michael@0 582 let deferred = Promise.defer();
michael@0 583
michael@0 584 let observer = {
michael@0 585 observe: function(aSubject, aTopic, aData)
michael@0 586 {
michael@0 587 Services.obs.removeObserver(observer, "browser:purge-domain-data");
michael@0 588 // test_storage_cleared needs this extra executeSoon because
michael@0 589 // the DOMStorage clean-up is also listening to this same observer
michael@0 590 // which is run synchronously.
michael@0 591 Services.tm.mainThread.dispatch(function() {
michael@0 592 deferred.resolve();
michael@0 593 }, Components.interfaces.nsIThread.DISPATCH_NORMAL);
michael@0 594 }
michael@0 595 };
michael@0 596 Services.obs.addObserver(observer, "browser:purge-domain-data", false);
michael@0 597
michael@0 598 return deferred.promise;
michael@0 599 }
michael@0 600
michael@0 601 // Content Preferences
michael@0 602 function test_content_preferences_cleared_with_direct_match()
michael@0 603 {
michael@0 604 const TEST_URI = uri("http://mozilla.org");
michael@0 605 do_check_false(yield preference_exists(TEST_URI));
michael@0 606 yield add_preference(TEST_URI);
michael@0 607 do_check_true(yield preference_exists(TEST_URI));
michael@0 608 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 609 yield waitForPurgeNotification();
michael@0 610 do_check_false(yield preference_exists(TEST_URI));
michael@0 611 }
michael@0 612
michael@0 613 function test_content_preferences_cleared_with_subdomain()
michael@0 614 {
michael@0 615 const TEST_URI = uri("http://www.mozilla.org");
michael@0 616 do_check_false(yield preference_exists(TEST_URI));
michael@0 617 yield add_preference(TEST_URI);
michael@0 618 do_check_true(yield preference_exists(TEST_URI));
michael@0 619 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 620 yield waitForPurgeNotification();
michael@0 621 do_check_false(yield preference_exists(TEST_URI));
michael@0 622 }
michael@0 623
michael@0 624 function test_content_preferences_not_cleared_with_uri_contains_domain()
michael@0 625 {
michael@0 626 const TEST_URI = uri("http://ilovemozilla.org");
michael@0 627 do_check_false(yield preference_exists(TEST_URI));
michael@0 628 yield add_preference(TEST_URI);
michael@0 629 do_check_true(yield preference_exists(TEST_URI));
michael@0 630 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 631 yield waitForPurgeNotification();
michael@0 632 do_check_true(yield preference_exists(TEST_URI));
michael@0 633
michael@0 634 // Reset state
michael@0 635 ForgetAboutSite.removeDataFromDomain("ilovemozilla.org");
michael@0 636 yield waitForPurgeNotification();
michael@0 637 do_check_false(yield preference_exists(TEST_URI));
michael@0 638 }
michael@0 639
michael@0 640 // Cache
michael@0 641 function test_cache_cleared()
michael@0 642 {
michael@0 643 // Because this test is asynchronous, it should be the last test
michael@0 644 do_check_true(tests[tests.length - 1] == arguments.callee);
michael@0 645
michael@0 646 // NOTE: We could be more extensive with this test and actually add an entry
michael@0 647 // to the cache, and then make sure it is gone. However, we trust that
michael@0 648 // the API is well tested, and that when we get the observer
michael@0 649 // notification, we have actually cleared the cache.
michael@0 650 // This seems to happen asynchronously...
michael@0 651 let os = Cc["@mozilla.org/observer-service;1"].
michael@0 652 getService(Ci.nsIObserverService);
michael@0 653 let observer = {
michael@0 654 observe: function(aSubject, aTopic, aData)
michael@0 655 {
michael@0 656 os.removeObserver(observer, "cacheservice:empty-cache");
michael@0 657 // Shutdown the download manager.
michael@0 658 Services.obs.notifyObservers(null, "quit-application", null);
michael@0 659 do_test_finished();
michael@0 660 }
michael@0 661 };
michael@0 662 os.addObserver(observer, "cacheservice:empty-cache", false);
michael@0 663 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 664 do_test_pending();
michael@0 665 }
michael@0 666
michael@0 667 function test_storage_cleared()
michael@0 668 {
michael@0 669 function getStorageForURI(aURI)
michael@0 670 {
michael@0 671 let principal = Cc["@mozilla.org/scriptsecuritymanager;1"].
michael@0 672 getService(Ci.nsIScriptSecurityManager).
michael@0 673 getNoAppCodebasePrincipal(aURI);
michael@0 674 let dsm = Cc["@mozilla.org/dom/localStorage-manager;1"].
michael@0 675 getService(Ci.nsIDOMStorageManager);
michael@0 676 return dsm.createStorage(principal, "");
michael@0 677 }
michael@0 678
michael@0 679 let s = [
michael@0 680 getStorageForURI(uri("http://mozilla.org")),
michael@0 681 getStorageForURI(uri("http://my.mozilla.org")),
michael@0 682 getStorageForURI(uri("http://ilovemozilla.org")),
michael@0 683 ];
michael@0 684
michael@0 685 for (let i = 0; i < s.length; ++i) {
michael@0 686 let storage = s[i];
michael@0 687 storage.setItem("test", "value" + i);
michael@0 688 do_check_eq(storage.length, 1);
michael@0 689 do_check_eq(storage.key(0), "test");
michael@0 690 do_check_eq(storage.getItem("test"), "value" + i);
michael@0 691 }
michael@0 692
michael@0 693 ForgetAboutSite.removeDataFromDomain("mozilla.org");
michael@0 694 yield waitForPurgeNotification();
michael@0 695
michael@0 696 do_check_eq(s[0].getItem("test"), null);
michael@0 697 do_check_eq(s[0].length, 0);
michael@0 698 do_check_eq(s[1].getItem("test"), null);
michael@0 699 do_check_eq(s[1].length, 0);
michael@0 700 do_check_eq(s[2].getItem("test"), "value2");
michael@0 701 do_check_eq(s[2].length, 1);
michael@0 702 }
michael@0 703
michael@0 704 let tests = [
michael@0 705 // History
michael@0 706 test_history_cleared_with_direct_match,
michael@0 707 test_history_cleared_with_subdomain,
michael@0 708 test_history_not_cleared_with_uri_contains_domain,
michael@0 709
michael@0 710 // Cookie Service
michael@0 711 test_cookie_cleared_with_direct_match,
michael@0 712 test_cookie_cleared_with_subdomain,
michael@0 713 test_cookie_not_cleared_with_uri_contains_domain,
michael@0 714
michael@0 715 // Download Manager
michael@0 716 // Note: active downloads tested in test_removeDataFromDomain_activeDownloads.js
michael@0 717 test_download_history_cleared_with_direct_match,
michael@0 718 test_download_history_cleared_with_subdomain,
michael@0 719 test_download_history_not_cleared_with_active_direct_match,
michael@0 720
michael@0 721 // Login Manager
michael@0 722 test_login_manager_disabled_hosts_cleared_with_direct_match,
michael@0 723 test_login_manager_disabled_hosts_cleared_with_subdomain,
michael@0 724 test_login_manager_disabled_hosts_not_cleared_with_uri_contains_domain,
michael@0 725 test_login_manager_logins_cleared_with_direct_match,
michael@0 726 test_login_manager_logins_cleared_with_subdomain,
michael@0 727 tets_login_manager_logins_not_cleared_with_uri_contains_domain,
michael@0 728
michael@0 729 // Permission Manager
michael@0 730 test_permission_manager_cleared_with_direct_match,
michael@0 731 test_permission_manager_cleared_with_subdomain,
michael@0 732 test_permission_manager_not_cleared_with_uri_contains_domain,
michael@0 733
michael@0 734 // Content Preferences
michael@0 735 test_content_preferences_cleared_with_direct_match,
michael@0 736 test_content_preferences_cleared_with_subdomain,
michael@0 737 test_content_preferences_not_cleared_with_uri_contains_domain,
michael@0 738
michael@0 739 // Storage
michael@0 740 test_storage_cleared,
michael@0 741
michael@0 742 // Cache
michael@0 743 test_cache_cleared,
michael@0 744 ];
michael@0 745
michael@0 746 function run_test()
michael@0 747 {
michael@0 748 for (let i = 0; i < tests.length; i++)
michael@0 749 add_task(tests[i]);
michael@0 750
michael@0 751 run_next_test();
michael@0 752 }

mercurial