extensions/cookie/test/unit/test_cookies_persistence.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.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 // test for cookie persistence across sessions, for the cases:
michael@0 5 // 1) network.cookie.lifetimePolicy = 0 (expire naturally)
michael@0 6 // 2) network.cookie.lifetimePolicy = 2 (expire at end of session)
michael@0 7
michael@0 8 let test_generator = do_run_test();
michael@0 9
michael@0 10 function run_test() {
michael@0 11 do_test_pending();
michael@0 12 test_generator.next();
michael@0 13 }
michael@0 14
michael@0 15 function finish_test() {
michael@0 16 do_execute_soon(function() {
michael@0 17 test_generator.close();
michael@0 18 do_test_finished();
michael@0 19 });
michael@0 20 }
michael@0 21
michael@0 22 function do_run_test() {
michael@0 23 // Set up a profile.
michael@0 24 let profile = do_get_profile();
michael@0 25
michael@0 26 // Create URIs and channels pointing to foo.com and bar.com.
michael@0 27 // We will use these to put foo.com into first and third party contexts.
michael@0 28 var spec1 = "http://foo.com/foo.html";
michael@0 29 var spec2 = "http://bar.com/bar.html";
michael@0 30 var uri1 = NetUtil.newURI(spec1);
michael@0 31 var uri2 = NetUtil.newURI(spec2);
michael@0 32 var channel1 = NetUtil.newChannel(uri1);
michael@0 33 var channel2 = NetUtil.newChannel(uri2);
michael@0 34
michael@0 35 // Force the channel URI to be used when determining the originating URI of
michael@0 36 // the channel.
michael@0 37 var httpchannel1 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
michael@0 38 var httpchannel2 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
michael@0 39 httpchannel1.forceAllowThirdPartyCookie = true;
michael@0 40 httpchannel2.forceAllowThirdPartyCookie = true;
michael@0 41
michael@0 42 // test with cookies enabled, and third party cookies persistent.
michael@0 43 Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
michael@0 44 Services.prefs.setBoolPref("network.cookie.thirdparty.sessionOnly", false);
michael@0 45 do_set_cookies(uri1, channel1, false, [1, 2, 3, 4]);
michael@0 46 do_set_cookies(uri2, channel2, true, [1, 2, 3, 4]);
michael@0 47
michael@0 48 // fake a profile change
michael@0 49 do_close_profile(test_generator);
michael@0 50 yield;
michael@0 51 do_load_profile();
michael@0 52 do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 4);
michael@0 53 do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);
michael@0 54
michael@0 55 // Again, but don't wait for the async close to complete. This should always
michael@0 56 // work, since we blocked on close above and haven't kicked off any writes
michael@0 57 // since then.
michael@0 58 do_close_profile();
michael@0 59 do_load_profile();
michael@0 60 do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 4);
michael@0 61 do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);
michael@0 62
michael@0 63 // cleanse them
michael@0 64 do_close_profile(test_generator, "shutdown-cleanse");
michael@0 65 yield;
michael@0 66 do_load_profile();
michael@0 67 do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 0);
michael@0 68 do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);
michael@0 69
michael@0 70 // test with cookies set to session-only
michael@0 71 Services.prefs.setIntPref("network.cookie.lifetimePolicy", 2);
michael@0 72 do_set_cookies(uri1, channel1, false, [1, 2, 3, 4]);
michael@0 73 do_set_cookies(uri2, channel2, true, [1, 2, 3, 4]);
michael@0 74
michael@0 75 // fake a profile change
michael@0 76 do_close_profile(test_generator);
michael@0 77 yield;
michael@0 78 do_load_profile();
michael@0 79 do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 0);
michael@0 80 do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);
michael@0 81
michael@0 82 finish_test();
michael@0 83 }
michael@0 84

mercurial