michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: // test third party cookie blocking, for the cases: michael@0: // 1) with null channel michael@0: // 2) with channel, but with no docshell parent michael@0: michael@0: function run_test() { michael@0: // Create URIs and channels pointing to foo.com and bar.com. michael@0: // We will use these to put foo.com into first and third party contexts. michael@0: var spec1 = "http://foo.com/foo.html"; michael@0: var spec2 = "http://bar.com/bar.html"; michael@0: var uri1 = NetUtil.newURI(spec1); michael@0: var uri2 = NetUtil.newURI(spec2); michael@0: var channel1 = NetUtil.newChannel(uri1); michael@0: var channel2 = NetUtil.newChannel(uri2); michael@0: michael@0: // test with cookies enabled michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", 0); michael@0: do_set_cookies(uri1, channel1, true, [1, 2, 3, 4]); michael@0: Services.cookies.removeAll(); michael@0: do_set_cookies(uri1, channel2, true, [1, 2, 3, 4]); michael@0: Services.cookies.removeAll(); michael@0: michael@0: // test with third party cookies blocked michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", 1); michael@0: do_set_cookies(uri1, channel1, true, [0, 0, 0, 0]); michael@0: Services.cookies.removeAll(); michael@0: do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]); michael@0: Services.cookies.removeAll(); michael@0: michael@0: // Force the channel URI to be used when determining the originating URI of michael@0: // the channel. michael@0: var httpchannel1 = channel1.QueryInterface(Ci.nsIHttpChannelInternal); michael@0: var httpchannel2 = channel2.QueryInterface(Ci.nsIHttpChannelInternal); michael@0: httpchannel1.forceAllowThirdPartyCookie = true; michael@0: httpchannel2.forceAllowThirdPartyCookie = true; michael@0: michael@0: // test with cookies enabled michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", 0); michael@0: do_set_cookies(uri1, channel1, true, [1, 2, 3, 4]); michael@0: Services.cookies.removeAll(); michael@0: do_set_cookies(uri1, channel2, true, [1, 2, 3, 4]); michael@0: Services.cookies.removeAll(); michael@0: michael@0: // test with third party cookies blocked michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", 1); michael@0: do_set_cookies(uri1, channel1, true, [0, 1, 1, 2]); michael@0: Services.cookies.removeAll(); michael@0: do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]); michael@0: Services.cookies.removeAll(); michael@0: michael@0: // test with third party cookies limited michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", 3); michael@0: do_set_cookies(uri1, channel1, true, [0, 1, 2, 3]); michael@0: Services.cookies.removeAll(); michael@0: do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]); michael@0: Services.cookies.removeAll(); michael@0: do_set_single_http_cookie(uri1, channel1, 1); michael@0: do_set_cookies(uri1, channel2, true, [2, 3, 4, 5]); michael@0: Services.cookies.removeAll(); michael@0: michael@0: // Test per-site 3rd party cookie blocking with cookies enabled michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", 0); michael@0: var kPermissionType = "cookie"; michael@0: var ALLOW_FIRST_PARTY_ONLY = 9; michael@0: // ALLOW_FIRST_PARTY_ONLY overrides michael@0: Services.perms.add(uri1, kPermissionType, ALLOW_FIRST_PARTY_ONLY); michael@0: do_set_cookies(uri1, channel1, true, [0, 1, 1, 2]); michael@0: Services.cookies.removeAll(); michael@0: do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]); michael@0: Services.cookies.removeAll(); michael@0: michael@0: // Test per-site 3rd party cookie blocking with 3rd party cookies disabled michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", 1); michael@0: do_set_cookies(uri1, channel1, true, [0, 1, 1, 2]); michael@0: Services.cookies.removeAll(); michael@0: // No preference has been set for uri2, but it should act as if michael@0: // ALLOW_FIRST_PARTY_ONLY has been set michael@0: do_set_cookies(uri2, channel2, true, [0, 1, 1, 2]); michael@0: Services.cookies.removeAll(); michael@0: do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]); michael@0: Services.cookies.removeAll(); michael@0: michael@0: // Test per-site 3rd party cookie blocking with 3rd party cookies limited michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", 3); michael@0: do_set_cookies(uri1, channel1, true, [0, 1, 1, 2]); michael@0: Services.cookies.removeAll(); michael@0: // No preference has been set for uri2, but it should act as if michael@0: // LIMIT_THIRD_PARTY has been set michael@0: do_set_cookies(uri2, channel2, true, [0, 1, 2, 3]); michael@0: Services.cookies.removeAll(); michael@0: do_set_single_http_cookie(uri2, channel2, 1); michael@0: do_set_cookies(uri2, channel2, true, [2, 3, 4, 5]); michael@0: Services.cookies.removeAll(); michael@0: do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]); michael@0: Services.cookies.removeAll(); michael@0: do_set_single_http_cookie(uri1, channel1, 1); michael@0: do_set_cookies(uri1, channel2, true, [1, 1, 1, 1]); michael@0: Services.cookies.removeAll(); michael@0: michael@0: // Test per-site 3rd party cookie limiting with cookies enabled michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", 0); michael@0: var kPermissionType = "cookie"; michael@0: var LIMIT_THIRD_PARTY = 10; michael@0: // LIMIT_THIRD_PARTY overrides michael@0: Services.perms.add(uri1, kPermissionType, LIMIT_THIRD_PARTY); michael@0: do_set_cookies(uri1, channel1, true, [0, 1, 2, 3]); michael@0: Services.cookies.removeAll(); michael@0: do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]); michael@0: Services.cookies.removeAll(); michael@0: do_set_single_http_cookie(uri1, channel1, 1); michael@0: do_set_cookies(uri1, channel2, true, [2, 3, 4, 5]); michael@0: Services.cookies.removeAll(); michael@0: michael@0: // Test per-site 3rd party cookie limiting with 3rd party cookies disabled michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", 1); michael@0: do_set_cookies(uri1, channel1, true, [0, 1, 2, 3]); michael@0: Services.cookies.removeAll(); michael@0: // No preference has been set for uri2, but it should act as if michael@0: // ALLOW_FIRST_PARTY_ONLY has been set michael@0: do_set_cookies(uri2, channel2, true, [0, 1, 1, 2]); michael@0: Services.cookies.removeAll(); michael@0: do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]); michael@0: Services.cookies.removeAll(); michael@0: do_set_single_http_cookie(uri1, channel1, 1); michael@0: do_set_cookies(uri1, channel2, true, [2, 3, 4, 5]); michael@0: Services.cookies.removeAll(); michael@0: michael@0: // Test per-site 3rd party cookie limiting with 3rd party cookies limited michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", 3); michael@0: do_set_cookies(uri1, channel1, true, [0, 1, 2, 3]); michael@0: Services.cookies.removeAll(); michael@0: // No preference has been set for uri2, but it should act as if michael@0: // LIMIT_THIRD_PARTY has been set michael@0: do_set_cookies(uri2, channel2, true, [0, 1, 2, 3]); michael@0: Services.cookies.removeAll(); michael@0: do_set_single_http_cookie(uri2, channel2, 1); michael@0: do_set_cookies(uri2, channel2, true, [2, 3, 4, 5]); michael@0: Services.cookies.removeAll(); michael@0: do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]); michael@0: Services.cookies.removeAll(); michael@0: do_set_single_http_cookie(uri1, channel1, 1); michael@0: do_set_cookies(uri1, channel2, true, [2, 3, 4, 5]); michael@0: Services.cookies.removeAll(); michael@0: } michael@0: