1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/xpconnect/tests/unit/test_bug809652.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,63 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +/* See https://bugzilla.mozilla.org/show_bug.cgi?id=813901 */ 1.9 + 1.10 +const Cu = Components.utils; 1.11 +const TypedArrays = [ Int8Array, Uint8Array, Int16Array, Uint16Array, 1.12 + Int32Array, Uint32Array, Float32Array, Float64Array, 1.13 + Uint8ClampedArray ]; 1.14 + 1.15 +// Make sure that the correct nativecall-y stuff is denied on security wrappers. 1.16 + 1.17 +function run_test() { 1.18 + 1.19 + var sb = new Cu.Sandbox('http://www.example.org'); 1.20 + sb.obj = {foo: 2}; 1.21 + 1.22 + /* Set up some typed arrays. */ 1.23 + sb.ab = new ArrayBuffer(8); 1.24 + for (var i = 0; i < 8; ++i) 1.25 + new Uint8Array(sb.ab)[i] = i * 10; 1.26 + sb.ta = []; 1.27 + TypedArrays.forEach(function(f) sb.ta.push(new f(sb.ab))); 1.28 + sb.dv = new DataView(sb.ab); 1.29 + 1.30 + /* Things that should throw. */ 1.31 + checkThrows("Object.prototype.__lookupSetter__('__proto__').call(obj, {});", sb); 1.32 + sb.re = /f/; 1.33 + checkThrows("RegExp.prototype.exec.call(re, 'abcdefg').index", sb); 1.34 + sb.d = new Date(); 1.35 + checkThrows("Date.prototype.setYear.call(d, 2011)", sb); 1.36 + sb.m = new Map(); 1.37 + checkThrows("(new Map()).clear.call(m)", sb); 1.38 + checkThrows("ArrayBuffer.prototype.__lookupGetter__('byteLength').call(ab);", sb); 1.39 + checkThrows("ArrayBuffer.prototype.slice.call(ab, 0);", sb); 1.40 + checkThrows("DataView.prototype.getInt8.call(dv, 0);", sb); 1.41 + 1.42 + /* Now that Date is on Xrays, these should all throw. */ 1.43 + checkThrows("Date.prototype.getYear.call(d)", sb); 1.44 + checkThrows("Date.prototype.valueOf.call(d)", sb); 1.45 + checkThrows("d.valueOf()", sb); 1.46 + checkThrows("Date.prototype.toString.call(d)", sb); 1.47 + checkThrows("d.toString()", sb); 1.48 + 1.49 + /* Typed arrays. */ 1.50 + function testForTypedArray(t) { 1.51 + sb.curr = t; 1.52 + do_check_eq(Cu.evalInSandbox("this[curr.constructor.name].prototype.subarray.call(curr, 0)[0]", sb), t[0]); 1.53 + do_check_eq(Cu.evalInSandbox("(new this[curr.constructor.name]).__lookupGetter__('length').call(curr)", sb), t.length); 1.54 + do_check_eq(Cu.evalInSandbox("(new this[curr.constructor.name]).__lookupGetter__('buffer').call(curr)", sb), sb.ab); 1.55 + do_check_eq(Cu.evalInSandbox("(new this[curr.constructor.name]).__lookupGetter__('byteOffset').call(curr)", sb), t.byteOffset); 1.56 + do_check_eq(Cu.evalInSandbox("(new this[curr.constructor.name]).__lookupGetter__('byteLength').call(curr)", sb), t.byteLength); 1.57 + } 1.58 + sb.ta.forEach(testForTypedArray); 1.59 +} 1.60 + 1.61 +function checkThrows(expression, sb) { 1.62 + var result = Cu.evalInSandbox('(function() { try { ' + expression + '; return "allowed"; } catch (e) { return e.toString(); }})();', sb); 1.63 + dump('result: ' + result + '\n\n\n'); 1.64 + do_check_true(!!/denied/.exec(result)); 1.65 +} 1.66 +