1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/xpconnect/tests/unit/test_exportFunction.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,113 @@ 1.4 +function run_test() { 1.5 + var Cu = Components.utils; 1.6 + var epsb = new Cu.Sandbox(["http://example.com", "http://example.org"], { wantExportHelpers: true }); 1.7 + var subsb = new Cu.Sandbox("http://example.com", { wantGlobalProperties: ["XMLHttpRequest"] }); 1.8 + var subsb2 = new Cu.Sandbox("http://example.com", { wantGlobalProperties: ["XMLHttpRequest"] }); 1.9 + var xorigsb = new Cu.Sandbox("http://test.com"); 1.10 + 1.11 + epsb.subsb = subsb; 1.12 + epsb.xorigsb = xorigsb; 1.13 + epsb.do_check_true = do_check_true; 1.14 + epsb.do_check_eq = do_check_eq; 1.15 + epsb.do_check_neq = do_check_neq; 1.16 + 1.17 + // Exporting should work if prinicipal of the source sandbox 1.18 + // subsumes the principal of the target sandbox. 1.19 + Cu.evalInSandbox("(" + function() { 1.20 + Object.prototype.protoProp = "common"; 1.21 + var wasCalled = false; 1.22 + var _this = this; 1.23 + this.funToExport = function(a, obj, native, mixed) { 1.24 + do_check_eq(a, 42); 1.25 + do_check_neq(obj, subsb.tobecloned); 1.26 + do_check_eq(obj.cloned, "cloned"); 1.27 + do_check_eq(obj.protoProp, "common"); 1.28 + do_check_eq(native, subsb.native); 1.29 + do_check_eq(_this, this); 1.30 + do_check_eq(mixed.xrayed, subsb.xrayed); 1.31 + do_check_eq(mixed.xrayed2, subsb.xrayed2); 1.32 + wasCalled = true; 1.33 + }; 1.34 + this.checkIfCalled = function() { 1.35 + do_check_true(wasCalled); 1.36 + wasCalled = false; 1.37 + } 1.38 + exportFunction(funToExport, subsb, { defineAs: "imported" }); 1.39 + }.toSource() + ")()", epsb); 1.40 + 1.41 + subsb.xrayed = Cu.evalInSandbox("(" + function () { 1.42 + return new XMLHttpRequest(); 1.43 + }.toSource() + ")()", subsb2); 1.44 + 1.45 + // Exported function should be able to be call from the 1.46 + // target sandbox. Native arguments should be just wrapped 1.47 + // every other argument should be cloned. 1.48 + Cu.evalInSandbox("(" + function () { 1.49 + native = new XMLHttpRequest(); 1.50 + xrayed2 = XPCNativeWrapper(new XMLHttpRequest()); 1.51 + mixed = { xrayed: xrayed, xrayed2: xrayed2 }; 1.52 + tobecloned = { cloned: "cloned" }; 1.53 + imported(42,tobecloned, native, mixed); 1.54 + }.toSource() + ")()", subsb); 1.55 + 1.56 + // Apply should work but the |this| argument should not be 1.57 + // possible to be changed. 1.58 + Cu.evalInSandbox("(" + function() { 1.59 + imported.apply("something", [42, tobecloned, native, mixed]); 1.60 + }.toSource() + ")()", subsb); 1.61 + 1.62 + Cu.evalInSandbox("(" + function() { 1.63 + checkIfCalled(); 1.64 + }.toSource() + ")()", epsb); 1.65 + 1.66 + // Exporting should throw if princpal of the source sandbox does 1.67 + // not subsume the principal of the target. 1.68 + Cu.evalInSandbox("(" + function() { 1.69 + try{ 1.70 + exportFunction(function() {}, this.xorigsb, { defineAs: "denied" }); 1.71 + do_check_true(false); 1.72 + } catch (e) { 1.73 + do_check_true(e.toString().indexOf('Permission denied') > -1); 1.74 + } 1.75 + }.toSource() + ")()", epsb); 1.76 + 1.77 + // Let's create an object in the target scope and add privileged 1.78 + // function to it as a property. 1.79 + Cu.evalInSandbox("(" + function() { 1.80 + var newContentObject = createObjectIn(subsb, { defineAs: "importedObject" }); 1.81 + exportFunction(funToExport, newContentObject, { defineAs: "privMethod" }); 1.82 + }.toSource() + ")()", epsb); 1.83 + 1.84 + Cu.evalInSandbox("(" + function () { 1.85 + importedObject.privMethod(42, tobecloned, native, mixed); 1.86 + }.toSource() + ")()", subsb); 1.87 + 1.88 + Cu.evalInSandbox("(" + function() { 1.89 + checkIfCalled(); 1.90 + }.toSource() + ")()", epsb); 1.91 + 1.92 + // exportFunction and createObjectIn should be available from Cu too. 1.93 + var newContentObject = Cu.createObjectIn(subsb, { defineAs: "importedObject2" }); 1.94 + var wasCalled = false; 1.95 + Cu.exportFunction(function(arg) { wasCalled = arg.wasCalled; }, 1.96 + newContentObject, { defineAs: "privMethod" }); 1.97 + 1.98 + Cu.evalInSandbox("(" + function () { 1.99 + importedObject2.privMethod({wasCalled: true}); 1.100 + }.toSource() + ")()", subsb); 1.101 + 1.102 + // 3rd argument of exportFunction should be optional. 1.103 + Cu.evalInSandbox("(" + function() { 1.104 + subsb.imported2 = exportFunction(funToExport, subsb); 1.105 + }.toSource() + ")()", epsb); 1.106 + 1.107 + Cu.evalInSandbox("(" + function () { 1.108 + imported2(42, tobecloned, native, mixed); 1.109 + }.toSource() + ")()", subsb); 1.110 + 1.111 + Cu.evalInSandbox("(" + function() { 1.112 + checkIfCalled(); 1.113 + }.toSource() + ")()", epsb); 1.114 + 1.115 + do_check_true(wasCalled, true); 1.116 +}