content/base/test/unit/test_thirdpartyutil.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial