content/base/test/unit/test_thirdpartyutil.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/unit/test_thirdpartyutil.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,84 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.6 + */
     1.7 +
     1.8 +// Test ThirdPartyUtil methods. See mozIThirdPartyUtil.
     1.9 +
    1.10 +Components.utils.import("resource://gre/modules/NetUtil.jsm");
    1.11 +
    1.12 +let Cc = Components.classes;
    1.13 +let Ci = Components.interfaces;
    1.14 +let NS_ERROR_INVALID_ARG = Components.results.NS_ERROR_INVALID_ARG;
    1.15 +
    1.16 +function do_check_throws(f, result, stack)
    1.17 +{
    1.18 +  if (!stack) {
    1.19 +    try {
    1.20 +      // We might not have a 'Components' object.
    1.21 +      stack = Components.stack.caller;
    1.22 +    } catch (e) {
    1.23 +    }
    1.24 +  }
    1.25 +
    1.26 +  try {
    1.27 +    f();
    1.28 +  } catch (exc) {
    1.29 +    do_check_eq(exc.result, result);
    1.30 +    return;
    1.31 +  }
    1.32 +  do_throw("expected " + result + " exception, none thrown", stack);
    1.33 +}
    1.34 +
    1.35 +function run_test() {
    1.36 +  let util = Cc["@mozilla.org/thirdpartyutil;1"].getService(Ci.mozIThirdPartyUtil);
    1.37 +
    1.38 +  // Create URIs and channels pointing to foo.com and bar.com.
    1.39 +  // We will use these to put foo.com into first and third party contexts.
    1.40 +  let spec1 = "http://foo.com/foo.html";
    1.41 +  let spec2 = "http://bar.com/bar.html";
    1.42 +  let uri1 = NetUtil.newURI(spec1);
    1.43 +  let uri2 = NetUtil.newURI(spec2);
    1.44 +  let channel1 = NetUtil.newChannel(uri1);
    1.45 +  let channel2 = NetUtil.newChannel(uri2);
    1.46 +
    1.47 +  // Create some file:// URIs.
    1.48 +  let filespec1 = "file://foo.txt";
    1.49 +  let filespec2 = "file://bar.txt";
    1.50 +  let fileuri1 = NetUtil.newURI(filespec1);
    1.51 +  let fileuri2 = NetUtil.newURI(filespec2);
    1.52 +  let filechannel1 = NetUtil.newChannel(fileuri1);
    1.53 +  let filechannel2 = NetUtil.newChannel(fileuri2);
    1.54 +
    1.55 +  // Test isThirdPartyURI.
    1.56 +  do_check_false(util.isThirdPartyURI(uri1, uri1));
    1.57 +  do_check_true(util.isThirdPartyURI(uri1, uri2));
    1.58 +  do_check_true(util.isThirdPartyURI(uri2, uri1));
    1.59 +  do_check_false(util.isThirdPartyURI(fileuri1, fileuri1));
    1.60 +  do_check_false(util.isThirdPartyURI(fileuri1, fileuri2));
    1.61 +  do_check_true(util.isThirdPartyURI(uri1, fileuri1));
    1.62 +  do_check_throws(function() { util.isThirdPartyURI(uri1, null); },
    1.63 +    NS_ERROR_INVALID_ARG);
    1.64 +  do_check_throws(function() { util.isThirdPartyURI(null, uri1); },
    1.65 +    NS_ERROR_INVALID_ARG);
    1.66 +  do_check_throws(function() { util.isThirdPartyURI(null, null); },
    1.67 +    NS_ERROR_INVALID_ARG);
    1.68 +
    1.69 +  // We can't test isThirdPartyWindow since we can't really set up a window
    1.70 +  // heirarchy. We leave that to mochitests.
    1.71 +
    1.72 +  // Test isThirdPartyChannel. As above, we can't test the bits that require
    1.73 +  // a load context or window heirarchy.
    1.74 +  do_check_throws(function() { util.isThirdPartyChannel(null); },
    1.75 +    NS_ERROR_INVALID_ARG);
    1.76 +  do_check_throws(function() { util.isThirdPartyChannel(channel1); },
    1.77 +    NS_ERROR_INVALID_ARG);
    1.78 +  do_check_throws(function() { util.isThirdPartyChannel(channel1, uri1); },
    1.79 +    NS_ERROR_INVALID_ARG);
    1.80 +  do_check_true(util.isThirdPartyChannel(channel1, uri2));
    1.81 +  let httpchannel1 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
    1.82 +  httpchannel1.forceAllowThirdPartyCookie = true;
    1.83 +  do_check_false(util.isThirdPartyChannel(channel1));
    1.84 +  do_check_false(util.isThirdPartyChannel(channel1, uri1));
    1.85 +  do_check_true(util.isThirdPartyChannel(channel1, uri2));
    1.86 +}
    1.87 +

mercurial