test_thirdpartyutil.js (3381B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ 3 */ 4 5 // Test ThirdPartyUtil methods. See mozIThirdPartyUtil. 6 7 var prefs = Services.prefs; 8 9 // Since this test creates a TYPE_DOCUMENT channel via javascript, it will 10 // end up using the wrong LoadInfo constructor. Setting this pref will disable 11 // the ContentPolicyType assertion in the constructor. 12 prefs.setBoolPref("network.loadinfo.skip_type_assertion", true); 13 14 var NS_ERROR_INVALID_ARG = Cr.NS_ERROR_INVALID_ARG; 15 16 function do_check_throws(f, result, stack) { 17 if (!stack) { 18 try { 19 // We might not have a 'Components' object. 20 stack = Components.stack.caller; 21 } catch (e) {} 22 } 23 24 try { 25 f(); 26 } catch (exc) { 27 Assert.equal(exc.result, result); 28 return; 29 } 30 do_throw("expected " + result + " exception, none thrown", stack); 31 } 32 33 function run_test() { 34 let util = Cc["@mozilla.org/thirdpartyutil;1"].getService( 35 Ci.mozIThirdPartyUtil 36 ); 37 38 // Create URIs and channels pointing to foo.com and bar.com. 39 // We will use these to put foo.com into first and third party contexts. 40 let spec1 = "http://foo.com/foo.html"; 41 let spec2 = "http://bar.com/bar.html"; 42 let uri1 = NetUtil.newURI(spec1); 43 let uri2 = NetUtil.newURI(spec2); 44 const contentPolicyType = Ci.nsIContentPolicy.TYPE_DOCUMENT; 45 let channel1 = NetUtil.newChannel({ 46 uri: uri1, 47 loadUsingSystemPrincipal: true, 48 contentPolicyType, 49 }); 50 NetUtil.newChannel({ 51 uri: uri2, 52 loadUsingSystemPrincipal: true, 53 contentPolicyType, 54 }); 55 56 // Create some file:// URIs. 57 let filespec1 = "file://foo.txt"; 58 let filespec2 = "file://bar.txt"; 59 let fileuri1 = NetUtil.newURI(filespec1); 60 let fileuri2 = NetUtil.newURI(filespec2); 61 NetUtil.newChannel({ uri: fileuri1, loadUsingSystemPrincipal: true }); 62 NetUtil.newChannel({ uri: fileuri2, loadUsingSystemPrincipal: true }); 63 64 // Test isThirdPartyURI. 65 Assert.ok(!util.isThirdPartyURI(uri1, uri1)); 66 Assert.ok(util.isThirdPartyURI(uri1, uri2)); 67 Assert.ok(util.isThirdPartyURI(uri2, uri1)); 68 Assert.ok(!util.isThirdPartyURI(fileuri1, fileuri1)); 69 Assert.ok(!util.isThirdPartyURI(fileuri1, fileuri2)); 70 Assert.ok(util.isThirdPartyURI(uri1, fileuri1)); 71 do_check_throws(function () { 72 util.isThirdPartyURI(uri1, null); 73 }, NS_ERROR_INVALID_ARG); 74 do_check_throws(function () { 75 util.isThirdPartyURI(null, uri1); 76 }, NS_ERROR_INVALID_ARG); 77 do_check_throws(function () { 78 util.isThirdPartyURI(null, null); 79 }, NS_ERROR_INVALID_ARG); 80 81 // We can't test isThirdPartyWindow since we can't really set up a window 82 // hierarchy. We leave that to mochitests. 83 84 // Test isThirdPartyChannel. As above, we can't test the bits that require 85 // a load context or window heirarchy. Because of bug 1259873, we assume 86 // that these are not third-party. 87 do_check_throws(function () { 88 util.isThirdPartyChannel(null); 89 }, NS_ERROR_INVALID_ARG); 90 Assert.ok(!util.isThirdPartyChannel(channel1)); 91 Assert.ok(!util.isThirdPartyChannel(channel1, uri1)); 92 Assert.ok(util.isThirdPartyChannel(channel1, uri2)); 93 94 let httpchannel1 = channel1.QueryInterface(Ci.nsIHttpChannelInternal); 95 httpchannel1.forceAllowThirdPartyCookie = true; 96 Assert.ok(!util.isThirdPartyChannel(channel1)); 97 Assert.ok(!util.isThirdPartyChannel(channel1, uri1)); 98 Assert.ok(util.isThirdPartyChannel(channel1, uri2)); 99 }