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.

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

mercurial