michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: // Test ThirdPartyUtil methods. See mozIThirdPartyUtil. michael@0: michael@0: Components.utils.import("resource://gre/modules/NetUtil.jsm"); michael@0: michael@0: let Cc = Components.classes; michael@0: let Ci = Components.interfaces; michael@0: let NS_ERROR_INVALID_ARG = Components.results.NS_ERROR_INVALID_ARG; michael@0: michael@0: function do_check_throws(f, result, stack) michael@0: { michael@0: if (!stack) { michael@0: try { michael@0: // We might not have a 'Components' object. michael@0: stack = Components.stack.caller; michael@0: } catch (e) { michael@0: } michael@0: } michael@0: michael@0: try { michael@0: f(); michael@0: } catch (exc) { michael@0: do_check_eq(exc.result, result); michael@0: return; michael@0: } michael@0: do_throw("expected " + result + " exception, none thrown", stack); michael@0: } michael@0: michael@0: function run_test() { michael@0: let util = Cc["@mozilla.org/thirdpartyutil;1"].getService(Ci.mozIThirdPartyUtil); michael@0: 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: let spec1 = "http://foo.com/foo.html"; michael@0: let spec2 = "http://bar.com/bar.html"; michael@0: let uri1 = NetUtil.newURI(spec1); michael@0: let uri2 = NetUtil.newURI(spec2); michael@0: let channel1 = NetUtil.newChannel(uri1); michael@0: let channel2 = NetUtil.newChannel(uri2); michael@0: michael@0: // Create some file:// URIs. michael@0: let filespec1 = "file://foo.txt"; michael@0: let filespec2 = "file://bar.txt"; michael@0: let fileuri1 = NetUtil.newURI(filespec1); michael@0: let fileuri2 = NetUtil.newURI(filespec2); michael@0: let filechannel1 = NetUtil.newChannel(fileuri1); michael@0: let filechannel2 = NetUtil.newChannel(fileuri2); michael@0: michael@0: // Test isThirdPartyURI. michael@0: do_check_false(util.isThirdPartyURI(uri1, uri1)); michael@0: do_check_true(util.isThirdPartyURI(uri1, uri2)); michael@0: do_check_true(util.isThirdPartyURI(uri2, uri1)); michael@0: do_check_false(util.isThirdPartyURI(fileuri1, fileuri1)); michael@0: do_check_false(util.isThirdPartyURI(fileuri1, fileuri2)); michael@0: do_check_true(util.isThirdPartyURI(uri1, fileuri1)); michael@0: do_check_throws(function() { util.isThirdPartyURI(uri1, null); }, michael@0: NS_ERROR_INVALID_ARG); michael@0: do_check_throws(function() { util.isThirdPartyURI(null, uri1); }, michael@0: NS_ERROR_INVALID_ARG); michael@0: do_check_throws(function() { util.isThirdPartyURI(null, null); }, michael@0: NS_ERROR_INVALID_ARG); michael@0: michael@0: // We can't test isThirdPartyWindow since we can't really set up a window michael@0: // heirarchy. We leave that to mochitests. michael@0: michael@0: // Test isThirdPartyChannel. As above, we can't test the bits that require michael@0: // a load context or window heirarchy. michael@0: do_check_throws(function() { util.isThirdPartyChannel(null); }, michael@0: NS_ERROR_INVALID_ARG); michael@0: do_check_throws(function() { util.isThirdPartyChannel(channel1); }, michael@0: NS_ERROR_INVALID_ARG); michael@0: do_check_throws(function() { util.isThirdPartyChannel(channel1, uri1); }, michael@0: NS_ERROR_INVALID_ARG); michael@0: do_check_true(util.isThirdPartyChannel(channel1, uri2)); michael@0: let httpchannel1 = channel1.QueryInterface(Ci.nsIHttpChannelInternal); michael@0: httpchannel1.forceAllowThirdPartyCookie = true; michael@0: do_check_false(util.isThirdPartyChannel(channel1)); michael@0: do_check_false(util.isThirdPartyChannel(channel1, uri1)); michael@0: do_check_true(util.isThirdPartyChannel(channel1, uri2)); michael@0: } michael@0: