do_test_removeDataFromDomain.js
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: sw=2 ts=2 sts=2
3  * ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is Private Browsing Tests.
17  *
18  * The Initial Developer of the Original Code is
19  * Mozilla Corporation.
20  * Portions created by the Initial Developer are Copyright (C) 2008
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
25  *
26  * Alternatively, the contents of this file may be used under the terms of
27  * either of the GNU General Public License Version 2 or later (the "GPL"),
28  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29  * in which case the provisions of the GPL or the LGPL are applicable instead
30  * of those above. If you wish to allow use of your version of this file only
31  * under the terms of either the GPL or the LGPL, and not to allow others to
32  * use your version of this file under the terms of the MPL, indicate your
33  * decision by deleting the provisions above and replace them with the notice
34  * and other provisions required by the GPL or the LGPL. If you do not delete
35  * the provisions above, a recipient may use your version of this file under
36  * the terms of any one of the MPL, the GPL or the LGPL.
37  *
38  * ***** END LICENSE BLOCK ***** */
39 
45 
49  getService(Ci.nsIPrivateBrowsingService);
50 
51 const COOKIE_EXPIRY = Math.round(Date.now() / 1000) + 60;
52 const COOKIE_NAME = "testcookie";
53 const COOKIE_PATH = "/";
54 
55 const LOGIN_USERNAME = "username";
56 const LOGIN_PASSWORD = "password";
57 const LOGIN_USERNAME_FIELD = "username_field";
58 const LOGIN_PASSWORD_FIELD = "password_field";
59 
60 const PERMISSION_TYPE = "test-perm";
61 const PERMISSION_VALUE = Ci.nsIPermissionManager.ALLOW_ACTION;
62 
63 const PREFERENCE_NAME = "test-pref";
64 
67 
75 function uri(aURIString)
76 {
77  return Cc["@mozilla.org/network/io-service;1"].
78  getService(Ci.nsIIOService).
79  newURI(aURIString, null, null);
80 }
81 
88 function add_visit(aURI)
89 {
90  check_visited(aURI, false);
91  let bh = Cc["@mozilla.org/browser/global-history;2"].
92  getService(Ci.nsIBrowserHistory);
93  bh.addPageWithDetails(aURI, aURI.spec, Date.now() * 1000);
94  check_visited(aURI, true);
95 }
96 
105 function check_visited(aURI, aIsVisited)
106 {
107  let gh = Cc["@mozilla.org/browser/global-history;2"].
108  getService(Ci.nsIGlobalHistory2);
109  let checker = aIsVisited ? do_check_true : do_check_false;
110  checker(gh.isVisited(aURI));
111 }
112 
118 function add_cookie(aDomain)
119 {
120  check_cookie_exists(aDomain, false);
121  let cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
122  cm.add(aDomain, COOKIE_PATH, COOKIE_NAME, "", false, false, false,
123  COOKIE_EXPIRY);
124  check_cookie_exists(aDomain, true);
125 }
126 
135 function check_cookie_exists(aDomain, aExists)
136 {
137  let cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
138  let cookie = {
139  host: aDomain,
140  name: COOKIE_NAME,
142  }
143  let checker = aExists ? do_check_true : do_check_false;
144  checker(cm.cookieExists(cookie));
145 }
146 
156 function add_download(aURIString, aIsActive)
157 {
158  check_downloaded(aURIString, false);
159  let db = Cc["@mozilla.org/download-manager;1"].
160  getService(Ci.nsIDownloadManager).
161  DBConnection;
162  let stmt = db.createStatement(
163  "INSERT INTO moz_downloads (source, state) " +
164  "VALUES (:source, :state)"
165  );
166  stmt.params.source = aURIString;
167  stmt.params.state = aIsActive ? Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING :
168  Ci.nsIDownloadManager.DOWNLOAD_FINISHED;
169  try {
170  stmt.execute();
171  }
172  finally {
173  stmt.finalize();
174  }
175  check_downloaded(aURIString, true);
176 }
177 
186 function check_downloaded(aURIString, aIsDownloaded)
187 {
188  let db = Cc["@mozilla.org/download-manager;1"].
189  getService(Ci.nsIDownloadManager).
190  DBConnection;
191  let stmt = db.createStatement(
192  "SELECT * " +
193  "FROM moz_downloads " +
194  "WHERE source = :source"
195  );
196  stmt.params.source = aURIString;
197 
198  let checker = aIsDownloaded ? do_check_true : do_check_false;
199  try {
200  checker(stmt.step());
201  }
202  finally {
203  stmt.finalize();
204  }
205 }
206 
214 {
215  check_disabled_host(aHost, false);
216  let lm = Cc["@mozilla.org/login-manager;1"].
217  getService(Ci.nsILoginManager);
218  lm.setLoginSavingEnabled(aHost, false);
219  check_disabled_host(aHost, true);
220 }
221 
230 function check_disabled_host(aHost, aIsDisabled)
231 {
232  let lm = Cc["@mozilla.org/login-manager;1"].
233  getService(Ci.nsILoginManager);
234  let checker = aIsDisabled ? do_check_false : do_check_true;
235  checker(lm.getLoginSavingEnabled(aHost));
236 }
237 
244 function add_login(aHost)
245 {
246  check_login_exists(aHost, false);
247  let login = Cc["@mozilla.org/login-manager/loginInfo;1"].
248  createInstance(Ci.nsILoginInfo);
249  login.init(aHost, "", null, LOGIN_USERNAME, LOGIN_PASSWORD,
251  let lm = Cc["@mozilla.org/login-manager;1"].
252  getService(Ci.nsILoginManager);
253  lm.addLogin(login);
254  check_login_exists(aHost, true);
255 }
256 
265 function check_login_exists(aHost, aExists)
266 {
267  let lm = Cc["@mozilla.org/login-manager;1"].
268  getService(Ci.nsILoginManager);
269  let count = { value: 0 };
270  lm.findLogins(count, aHost, "", null);
271  do_check_eq(count.value, aExists ? 1 : 0);
272 }
273 
280 function add_permission(aURI)
281 {
282  check_permission_exists(aURI, false);
283  let pm = Cc["@mozilla.org/permissionmanager;1"].
284  getService(Ci.nsIPermissionManager);
285  pm.add(aURI, PERMISSION_TYPE, PERMISSION_VALUE);
286  check_permission_exists(aURI, true);
287 }
288 
297 function check_permission_exists(aURI, aExists)
298 {
299  let pm = Cc["@mozilla.org/permissionmanager;1"].
300  getService(Ci.nsIPermissionManager);
301  let perm = pm.testExactPermission(aURI, PERMISSION_TYPE);
302  let checker = aExists ? do_check_eq : do_check_neq;
303  checker(perm, PERMISSION_VALUE);
304 }
305 
312 function add_preference(aURI)
313 {
314  check_preference_exists(aURI, false);
315  let cp = Cc["@mozilla.org/content-pref/service;1"].
316  getService(Ci.nsIContentPrefService);
317  cp.setPref(aURI, PREFERENCE_NAME, "foo");
318  check_preference_exists(aURI, true);
319 }
320 
329 function check_preference_exists(aURI, aExists)
330 {
331  let cp = Cc["@mozilla.org/content-pref/service;1"].
332  getService(Ci.nsIContentPrefService);
333  let checker = aExists ? do_check_true : do_check_false;
334  checker(cp.hasPref(aURI, PREFERENCE_NAME));
335 }
336 
339 
340 // History
342 {
343  const TEST_URI = uri("http://mozilla.org/foo");
345  pb.removeDataFromDomain("mozilla.org");
346  check_visited(TEST_URI, false);
347 }
348 
350 {
351  const TEST_URI = uri("http://www.mozilla.org/foo");
353  pb.removeDataFromDomain("mozilla.org");
354  check_visited(TEST_URI, false);
355 }
356 
358 {
359  const TEST_URI = uri("http://ilovemozilla.org/foo");
361  pb.removeDataFromDomain("mozilla.org");
362  check_visited(TEST_URI, true);
363 
364  // Clear history since we left something there from this test.
365  let bh = Cc["@mozilla.org/browser/global-history;2"].
366  getService(Ci.nsIBrowserHistory);
367  bh.removeAllPages();
368 }
369 
370 // Cookie Service
372 {
373  const TEST_DOMAIN = "mozilla.org";
374  add_cookie(TEST_DOMAIN);
375  pb.removeDataFromDomain("mozilla.org");
376  check_cookie_exists(TEST_DOMAIN, false);
377 }
378 
380 {
381  const TEST_DOMAIN = "www.mozilla.org";
382  add_cookie(TEST_DOMAIN);
383  pb.removeDataFromDomain("mozilla.org");
384  check_cookie_exists(TEST_DOMAIN, false);
385 }
386 
388 {
389  const TEST_DOMAIN = "ilovemozilla.org";
390  add_cookie(TEST_DOMAIN);
391  pb.removeDataFromDomain("mozilla.org");
392  check_cookie_exists(TEST_DOMAIN, true);
393 }
394 
395 // Download Manager
397 {
398  const TEST_URI = "http://mozilla.org/foo";
399  add_download(TEST_URI, false);
400  pb.removeDataFromDomain("mozilla.org");
401  check_downloaded(TEST_URI, false);
402 }
403 
405 {
406  const TEST_URI = "http://www.mozilla.org/foo";
407  add_download(TEST_URI, false);
408  pb.removeDataFromDomain("mozilla.org");
409  check_downloaded(TEST_URI, false);
410 }
411 
413 {
414  // Tests that downloads marked as active in the db are not deleted from the db
415  const TEST_URI = "http://mozilla.org/foo";
416  add_download(TEST_URI, true);
417  pb.removeDataFromDomain("mozilla.org");
418  check_downloaded(TEST_URI, true);
419 
420  // Reset state
421  let db = Cc["@mozilla.org/download-manager;1"].
422  getService(Ci.nsIDownloadManager).
423  DBConnection;
424  db.executeSimpleSQL("DELETE FROM moz_downloads");
425  check_downloaded(TEST_URI, false);
426 }
427 
428 // Login Manager
430 {
431  const TEST_HOST = "http://mozilla.org";
432  add_disabled_host(TEST_HOST);
433  pb.removeDataFromDomain("mozilla.org");
434  check_disabled_host(TEST_HOST, false);
435 }
436 
438 {
439  const TEST_HOST = "http://www.mozilla.org";
440  add_disabled_host(TEST_HOST);
441  pb.removeDataFromDomain("mozilla.org");
442  check_disabled_host(TEST_HOST, false);
443 }
444 
446 {
447  const TEST_HOST = "http://ilovemozilla.org";
448  add_disabled_host(TEST_HOST);
449  pb.removeDataFromDomain("mozilla.org");
450  check_disabled_host(TEST_HOST, true);
451 
452  // Reset state
453  let lm = Cc["@mozilla.org/login-manager;1"].
454  getService(Ci.nsILoginManager);
455  lm.setLoginSavingEnabled(TEST_HOST, true);
456  check_disabled_host(TEST_HOST, false);
457 }
458 
460 {
461  const TEST_HOST = "http://mozilla.org";
462  add_login(TEST_HOST);
463  pb.removeDataFromDomain("mozilla.org");
464  check_login_exists(TEST_HOST, false);
465 }
466 
468 {
469  const TEST_HOST = "http://www.mozilla.org";
470  add_login(TEST_HOST);
471  pb.removeDataFromDomain("mozilla.org");
472  check_login_exists(TEST_HOST, false);
473 }
474 
476 {
477  const TEST_HOST = "http://ilovemozilla.org";
478  add_login(TEST_HOST);
479  pb.removeDataFromDomain("mozilla.org");
480  check_login_exists(TEST_HOST, true);
481 
482  let lm = Cc["@mozilla.org/login-manager;1"].
483  getService(Ci.nsILoginManager);
484  lm.removeAllLogins();
485  check_login_exists(TEST_HOST, false);
486 }
487 
488 // Permission Manager
490 {
491  const TEST_URI = uri("http://mozilla.org");
493  pb.removeDataFromDomain("mozilla.org");
495 }
496 
498 {
499  const TEST_URI = uri("http://www.mozilla.org");
501  pb.removeDataFromDomain("mozilla.org");
503 }
504 
506 {
507  const TEST_URI = uri("http://ilovemozilla.org");
509  pb.removeDataFromDomain("mozilla.org");
511 
512  // Reset state
513  let pm = Cc["@mozilla.org/permissionmanager;1"].
514  getService(Ci.nsIPermissionManager);
515  pm.removeAll();
517 }
518 
519 // Content Preferences
521 {
522  const TEST_URI = uri("http://mozilla.org");
524  pb.removeDataFromDomain("mozilla.org");
526 }
527 
529 {
530  const TEST_URI = uri("http://www.mozilla.org");
532  pb.removeDataFromDomain("mozilla.org");
534 }
535 
537 {
538  const TEST_URI = uri("http://ilovemozilla.org");
540  pb.removeDataFromDomain("mozilla.org");
542 
543  // Reset state
544  let cp = Cc["@mozilla.org/content-pref/service;1"].
545  getService(Ci.nsIContentPrefService);
546  cp.removePref(TEST_URI, PREFERENCE_NAME);
548 }
549 
550 // Cache
552 {
553  // Because this test is asynchronous, it should be the last test
554  do_check_eq(tests[tests.length - 1], arguments.callee);
555 
556  // NOTE: We could be more extensive with this test and actually add an entry
557  // to the cache, and then make sure it is gone. However, we trust that
558  // the API is well tested, and that when we get the observer
559  // notification, we have actually cleared the cache.
560  // This seems to happen asynchronously...
561  let os = Cc["@mozilla.org/observer-service;1"].
562  getService(Ci.nsIObserverService);
563  let observer = {
564  observe: function(aSubject, aTopic, aData)
565  {
566  os.removeObserver(observer, "cacheservice:empty-cache");
567  shutdownPlaces();
568  do_test_finished();
569  }
570  };
571  os.addObserver(observer, "cacheservice:empty-cache", false);
572  pb.removeDataFromDomain("mozilla.org");
573  do_test_pending();
574 }
575 
576 let tests = [
577  // History
581 
582  // Cookie Service
586 
587  // Download Manager
588  // Note: active downloads tested in test_removeDataFromDomain_activeDownloads.js
592 
593  // Login Manager
600 
601  // Permission Manager
605 
606  // Content Preferences
610 
611  // Cache
613 ];
614 
615 function do_test()
616 {
617  for (let i = 0; i < tests.length; i++)
618  tests[i]();
619 }
function uri(aURIString)
function test_login_manager_disabled_hosts_cleared_with_subdomain()
const Cc
do_check_eq(typeof PlacesUtils,"object")
function test_download_history_cleared_with_subdomain()
function check_preference_exists(aURI, aExists)
function check_downloaded(aURIString, aIsDownloaded)
function do_test()
function test_content_preferences_cleared_with_subdomain()
function test_download_history_not_cleared_with_active_direct_match()
function test_cookie_cleared_with_direct_match()
function shutdownPlaces()
const PERMISSION_VALUE
function add_permission(aURI)
function add_visit(aURI)
function test_login_manager_disabled_hosts_cleared_with_direct_match()
function check_disabled_host(aHost, aIsDisabled)
_updateCookies aHost
function check_cookie_exists(aDomain, aExists)
function check_visited(aURI, aIsVisited)
sidebarFactory createInstance
Definition: nsSidebar.js:351
function test_permission_manager_not_cleared_with_uri_contains_domain()
getService(Ci.sbIFaceplateManager)
function check_permission_exists(aURI, aExists)
function test_content_preferecnes_not_cleared_with_uri_contains_domain()
function test_history_cleared_with_direct_match()
function test_login_manager_disabled_hosts_not_cleared_with_uri_contains_domain()
var count
Definition: test_bug7406.js:32
const PREFERENCE_NAME
function test_login_manager_logins_cleared_with_subdomain()
function check_login_exists(aHost, aExists)
function test_login_manager_logins_cleared_with_direct_match()
function test_history_not_cleared_with_uri_contains_domain()
const LOGIN_USERNAME_FIELD
function test_cache_cleared()
return null
Definition: FeedWriter.js:1143
function test_history_cleared_with_subdomain()
function newURI(aURLString)
const LOGIN_PASSWORD_FIELD
return!aWindow arguments!aWindow arguments[0]
function test_download_history_cleared_with_direct_match()
var os
const LOGIN_PASSWORD
countRef value
Definition: FeedWriter.js:1423
function add_disabled_host(aHost)
function add_download(aURIString, aIsActive)
function test_permission_manager_cleared_with_direct_match()
function add_cookie(aDomain)
function add_preference(aURI)
function tets_login_manager_logins_not_cleared_with_uri_contains_domain()
function test_cookie_cleared_with_subdomain()
function test_cookie_not_cleared_with_uri_contains_domain()
const Ci
function add_login(aHost)
var PRIVATEBROWSING_CONTRACT_ID
const PERMISSION_TYPE
_getSelectedPageStyle s i
function test_content_preferences_cleared_with_direct_match()
let observer
const LOGIN_USERNAME
var cm
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe
function test_permission_manager_cleared_with_subdomain()