extensions/cookie/test/unit/test_cookies_privatebrowsing.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 // Test private browsing mode.
     6 let test_generator = do_run_test();
     8 function run_test() {
     9   do_test_pending();
    10   do_run_generator(test_generator);
    11 }
    13 function finish_test() {
    14   do_execute_soon(function() {
    15     test_generator.close();
    16     do_test_finished();
    17   });
    18 }
    20 function make_channel(url) {
    21   var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    22   var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel);
    23   return chan;
    24 }
    26 function do_run_test() {
    27   // Set up a profile.
    28   let profile = do_get_profile();
    30   // Test with cookies enabled.
    31   Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
    33   // Create URIs pointing to foo.com and bar.com.
    34   let uri1 = NetUtil.newURI("http://foo.com/foo.html");
    35   let uri2 = NetUtil.newURI("http://bar.com/bar.html");
    37   // Set a cookie for host 1.
    38   Services.cookies.setCookieString(uri1, null, "oh=hai; max-age=1000", null);
    39   do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
    41   // Enter private browsing mode, set a cookie for host 2, and check the counts.
    42   var chan1 = make_channel(uri1.spec);
    43   chan1.QueryInterface(Ci.nsIPrivateBrowsingChannel);
    44   chan1.setPrivate(true);
    46   var chan2 = make_channel(uri2.spec);
    47   chan2.QueryInterface(Ci.nsIPrivateBrowsingChannel);
    48   chan2.setPrivate(true);
    50   Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", chan2);
    51   do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null);
    52   do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai");
    54   // Remove cookies and check counts.
    55   Services.obs.notifyObservers(null, "last-pb-context-exited", null);
    56   do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null);
    57   do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null);
    59   Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", chan2);
    60   do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai");
    62   // Leave private browsing mode and check counts.
    63   Services.obs.notifyObservers(null, "last-pb-context-exited", null);
    64   do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
    65   do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
    67   // Fake a profile change.
    68   do_close_profile(test_generator);
    69   yield;
    70   do_load_profile();
    72   // Check that the right cookie persisted.
    73   do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
    74   do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
    76   // Enter private browsing mode, set a cookie for host 2, and check the counts.
    77   do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null);
    78   do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null);
    79   Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", chan2);
    80   do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai");
    82   // Fake a profile change.
    83   do_close_profile(test_generator);
    84   yield;
    85   do_load_profile();
    87   // We're still in private browsing mode, but should have a new session.
    88   // Check counts.
    89   do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null);
    90   do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null);
    92   // Leave private browsing mode and check counts.
    93   Services.obs.notifyObservers(null, "last-pb-context-exited", null);
    94   do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
    95   do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
    97   // Enter private browsing mode.
    99   // Fake a profile change, but wait for async read completion.
   100   do_close_profile(test_generator);
   101   yield;
   102   do_load_profile(test_generator);
   103   yield;
   105   // We're still in private browsing mode, but should have a new session.
   106   // Check counts.
   107   do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null);
   108   do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null);
   110   // Leave private browsing mode and check counts.
   111   Services.obs.notifyObservers(null, "last-pb-context-exited", null);
   112   do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
   113   do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
   115   finish_test();
   116 }

mercurial