Wed, 31 Dec 2014 06:09:35 +0100
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 | |
michael@0 | 5 | // Test ThirdPartyUtil methods. See mozIThirdPartyUtil. |
michael@0 | 6 | |
michael@0 | 7 | Components.utils.import("resource://gre/modules/NetUtil.jsm"); |
michael@0 | 8 | |
michael@0 | 9 | let Cc = Components.classes; |
michael@0 | 10 | let Ci = Components.interfaces; |
michael@0 | 11 | let NS_ERROR_INVALID_ARG = Components.results.NS_ERROR_INVALID_ARG; |
michael@0 | 12 | |
michael@0 | 13 | function do_check_throws(f, result, stack) |
michael@0 | 14 | { |
michael@0 | 15 | if (!stack) { |
michael@0 | 16 | try { |
michael@0 | 17 | // We might not have a 'Components' object. |
michael@0 | 18 | stack = Components.stack.caller; |
michael@0 | 19 | } catch (e) { |
michael@0 | 20 | } |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | try { |
michael@0 | 24 | f(); |
michael@0 | 25 | } catch (exc) { |
michael@0 | 26 | do_check_eq(exc.result, result); |
michael@0 | 27 | return; |
michael@0 | 28 | } |
michael@0 | 29 | do_throw("expected " + result + " exception, none thrown", stack); |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | function run_test() { |
michael@0 | 33 | let util = Cc["@mozilla.org/thirdpartyutil;1"].getService(Ci.mozIThirdPartyUtil); |
michael@0 | 34 | |
michael@0 | 35 | // Create URIs and channels pointing to foo.com and bar.com. |
michael@0 | 36 | // We will use these to put foo.com into first and third party contexts. |
michael@0 | 37 | let spec1 = "http://foo.com/foo.html"; |
michael@0 | 38 | let spec2 = "http://bar.com/bar.html"; |
michael@0 | 39 | let uri1 = NetUtil.newURI(spec1); |
michael@0 | 40 | let uri2 = NetUtil.newURI(spec2); |
michael@0 | 41 | let channel1 = NetUtil.newChannel(uri1); |
michael@0 | 42 | let channel2 = NetUtil.newChannel(uri2); |
michael@0 | 43 | |
michael@0 | 44 | // Create some file:// URIs. |
michael@0 | 45 | let filespec1 = "file://foo.txt"; |
michael@0 | 46 | let filespec2 = "file://bar.txt"; |
michael@0 | 47 | let fileuri1 = NetUtil.newURI(filespec1); |
michael@0 | 48 | let fileuri2 = NetUtil.newURI(filespec2); |
michael@0 | 49 | let filechannel1 = NetUtil.newChannel(fileuri1); |
michael@0 | 50 | let filechannel2 = NetUtil.newChannel(fileuri2); |
michael@0 | 51 | |
michael@0 | 52 | // Test isThirdPartyURI. |
michael@0 | 53 | do_check_false(util.isThirdPartyURI(uri1, uri1)); |
michael@0 | 54 | do_check_true(util.isThirdPartyURI(uri1, uri2)); |
michael@0 | 55 | do_check_true(util.isThirdPartyURI(uri2, uri1)); |
michael@0 | 56 | do_check_false(util.isThirdPartyURI(fileuri1, fileuri1)); |
michael@0 | 57 | do_check_false(util.isThirdPartyURI(fileuri1, fileuri2)); |
michael@0 | 58 | do_check_true(util.isThirdPartyURI(uri1, fileuri1)); |
michael@0 | 59 | do_check_throws(function() { util.isThirdPartyURI(uri1, null); }, |
michael@0 | 60 | NS_ERROR_INVALID_ARG); |
michael@0 | 61 | do_check_throws(function() { util.isThirdPartyURI(null, uri1); }, |
michael@0 | 62 | NS_ERROR_INVALID_ARG); |
michael@0 | 63 | do_check_throws(function() { util.isThirdPartyURI(null, null); }, |
michael@0 | 64 | NS_ERROR_INVALID_ARG); |
michael@0 | 65 | |
michael@0 | 66 | // We can't test isThirdPartyWindow since we can't really set up a window |
michael@0 | 67 | // heirarchy. We leave that to mochitests. |
michael@0 | 68 | |
michael@0 | 69 | // Test isThirdPartyChannel. As above, we can't test the bits that require |
michael@0 | 70 | // a load context or window heirarchy. |
michael@0 | 71 | do_check_throws(function() { util.isThirdPartyChannel(null); }, |
michael@0 | 72 | NS_ERROR_INVALID_ARG); |
michael@0 | 73 | do_check_throws(function() { util.isThirdPartyChannel(channel1); }, |
michael@0 | 74 | NS_ERROR_INVALID_ARG); |
michael@0 | 75 | do_check_throws(function() { util.isThirdPartyChannel(channel1, uri1); }, |
michael@0 | 76 | NS_ERROR_INVALID_ARG); |
michael@0 | 77 | do_check_true(util.isThirdPartyChannel(channel1, uri2)); |
michael@0 | 78 | let httpchannel1 = channel1.QueryInterface(Ci.nsIHttpChannelInternal); |
michael@0 | 79 | httpchannel1.forceAllowThirdPartyCookie = true; |
michael@0 | 80 | do_check_false(util.isThirdPartyChannel(channel1)); |
michael@0 | 81 | do_check_false(util.isThirdPartyChannel(channel1, uri1)); |
michael@0 | 82 | do_check_true(util.isThirdPartyChannel(channel1, uri2)); |
michael@0 | 83 | } |
michael@0 | 84 |