js/xpconnect/tests/unit/test_exportFunction.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 function run_test() {
     2   var Cu = Components.utils;
     3   var epsb = new Cu.Sandbox(["http://example.com", "http://example.org"], { wantExportHelpers: true });
     4   var subsb = new Cu.Sandbox("http://example.com", { wantGlobalProperties: ["XMLHttpRequest"] });
     5   var subsb2 = new Cu.Sandbox("http://example.com", { wantGlobalProperties: ["XMLHttpRequest"] });
     6   var xorigsb = new Cu.Sandbox("http://test.com");
     8   epsb.subsb = subsb;
     9   epsb.xorigsb = xorigsb;
    10   epsb.do_check_true = do_check_true;
    11   epsb.do_check_eq = do_check_eq;
    12   epsb.do_check_neq = do_check_neq;
    14   // Exporting should work if prinicipal of the source sandbox
    15   // subsumes the principal of the target sandbox.
    16   Cu.evalInSandbox("(" + function() {
    17     Object.prototype.protoProp = "common";
    18     var wasCalled = false;
    19     var _this = this;
    20     this.funToExport = function(a, obj, native, mixed) {
    21       do_check_eq(a, 42);
    22       do_check_neq(obj, subsb.tobecloned);
    23       do_check_eq(obj.cloned, "cloned");
    24       do_check_eq(obj.protoProp, "common");
    25       do_check_eq(native, subsb.native);
    26       do_check_eq(_this, this);
    27       do_check_eq(mixed.xrayed, subsb.xrayed);
    28       do_check_eq(mixed.xrayed2, subsb.xrayed2);
    29       wasCalled = true;
    30     };
    31     this.checkIfCalled = function() {
    32       do_check_true(wasCalled);
    33       wasCalled = false;
    34     }
    35     exportFunction(funToExport, subsb, { defineAs: "imported" });
    36   }.toSource() + ")()", epsb);
    38   subsb.xrayed = Cu.evalInSandbox("(" + function () {
    39       return new XMLHttpRequest();
    40   }.toSource() + ")()", subsb2);
    42   // Exported function should be able to be call from the
    43   // target sandbox. Native arguments should be just wrapped
    44   // every other argument should be cloned.
    45   Cu.evalInSandbox("(" + function () {
    46     native = new XMLHttpRequest();
    47     xrayed2 = XPCNativeWrapper(new XMLHttpRequest());
    48     mixed = { xrayed: xrayed, xrayed2: xrayed2 };
    49     tobecloned = { cloned: "cloned" };
    50     imported(42,tobecloned, native, mixed);
    51   }.toSource() + ")()", subsb);
    53   // Apply should work but the |this| argument should not be
    54   // possible to be changed.
    55   Cu.evalInSandbox("(" + function() {
    56     imported.apply("something", [42, tobecloned, native, mixed]);
    57   }.toSource() + ")()", subsb);
    59   Cu.evalInSandbox("(" + function() {
    60     checkIfCalled();
    61   }.toSource() + ")()", epsb);
    63   // Exporting should throw if princpal of the source sandbox does
    64   // not subsume the principal of the target.
    65   Cu.evalInSandbox("(" + function() {
    66     try{
    67       exportFunction(function() {}, this.xorigsb, { defineAs: "denied" });
    68       do_check_true(false);
    69     } catch (e) {
    70       do_check_true(e.toString().indexOf('Permission denied') > -1);
    71     }
    72   }.toSource() + ")()", epsb);
    74   // Let's create an object in the target scope and add privileged
    75   // function to it as a property.
    76   Cu.evalInSandbox("(" + function() {
    77     var newContentObject = createObjectIn(subsb, { defineAs: "importedObject" });
    78     exportFunction(funToExport, newContentObject, { defineAs: "privMethod" });
    79   }.toSource() + ")()", epsb);
    81   Cu.evalInSandbox("(" + function () {
    82     importedObject.privMethod(42, tobecloned, native, mixed);
    83   }.toSource() + ")()", subsb);
    85   Cu.evalInSandbox("(" + function() {
    86     checkIfCalled();
    87   }.toSource() + ")()", epsb);
    89   // exportFunction and createObjectIn should be available from Cu too.
    90   var newContentObject = Cu.createObjectIn(subsb, { defineAs: "importedObject2" });
    91   var wasCalled = false;
    92   Cu.exportFunction(function(arg) { wasCalled = arg.wasCalled; },
    93                     newContentObject, { defineAs: "privMethod" });
    95   Cu.evalInSandbox("(" + function () {
    96     importedObject2.privMethod({wasCalled: true});
    97   }.toSource() + ")()", subsb);
    99   // 3rd argument of exportFunction should be optional.
   100   Cu.evalInSandbox("(" + function() {
   101     subsb.imported2 = exportFunction(funToExport, subsb);
   102   }.toSource() + ")()", epsb);
   104   Cu.evalInSandbox("(" + function () {
   105     imported2(42, tobecloned, native, mixed);
   106   }.toSource() + ")()", subsb);
   108   Cu.evalInSandbox("(" + function() {
   109     checkIfCalled();
   110   }.toSource() + ")()", epsb);
   112   do_check_true(wasCalled, true);
   113 }

mercurial