| 1 |
/* |
| 2 |
* Any copyright is dedicated to the Public Domain. |
| 3 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
| 4 |
*/ |
| 5 |
|
| 6 |
var gTestfile = "setImmutablePrototype.js"; |
| 7 |
//----------------------------------------------------------------------------- |
| 8 |
var BUGNUMBER = 1052139; |
| 9 |
var summary = |
| 10 |
"Implement JSAPI and a shell function to prevent modifying an extensible " + |
| 11 |
"object's [[Prototype]]"; |
| 12 |
|
| 13 |
print(BUGNUMBER + ": " + summary); |
| 14 |
|
| 15 |
/************** |
| 16 |
* BEGIN TEST * |
| 17 |
**************/ |
| 18 |
|
| 19 |
if (typeof evaluate !== "function") |
| 20 |
{ |
| 21 |
// Not totally faithful semantics, but approximately close enough for this |
| 22 |
// test's purposes. |
| 23 |
evaluate = eval; |
| 24 |
} |
| 25 |
|
| 26 |
var usingRealSetImmutablePrototype = true; |
| 27 |
|
| 28 |
if (typeof setImmutablePrototype !== "function") |
| 29 |
{ |
| 30 |
usingRealSetImmutablePrototype = false; |
| 31 |
|
| 32 |
if (typeof SpecialPowers !== "undefined") |
| 33 |
{ |
| 34 |
setImmutablePrototype = |
| 35 |
SpecialPowers.Cu.getJSTestingFunctions().setImmutablePrototype; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
if (typeof wrap !== "function") |
| 40 |
{ |
| 41 |
// good enough |
| 42 |
wrap = function(x) { return x; }; |
| 43 |
} |
| 44 |
|
| 45 |
function setViaProtoSetter(obj, newProto) |
| 46 |
{ |
| 47 |
var setter = |
| 48 |
Object.getOwnPropertyDescriptor(Object.prototype, "__proto__").set; |
| 49 |
setter.call(obj, newProto); |
| 50 |
} |
| 51 |
|
| 52 |
function checkPrototypeMutationFailure(obj, desc) |
| 53 |
{ |
| 54 |
var initialProto = Object.getPrototypeOf(obj); |
| 55 |
|
| 56 |
// disconnected from any [[Prototype]] chains for use on any object at all |
| 57 |
var newProto = Object.create(null); |
| 58 |
|
| 59 |
function tryMutate(func, method) |
| 60 |
{ |
| 61 |
try |
| 62 |
{ |
| 63 |
func(obj, newProto); |
| 64 |
throw new Error(desc + ": no error thrown, prototype " + |
| 65 |
(Object.getPrototypeOf(obj) === initialProto |
| 66 |
? "wasn't" |
| 67 |
: "was") + |
| 68 |
" changed"); |
| 69 |
} |
| 70 |
catch (e) |
| 71 |
{ |
| 72 |
// Note: This is always a TypeError from *this* global object, because |
| 73 |
// Object.setPrototypeOf and the __proto__ setter come from *this* |
| 74 |
// global object. |
| 75 |
assertEq(e instanceof TypeError, true, |
| 76 |
desc + ": should have thrown TypeError setting [[Prototype]] " + |
| 77 |
"via " + method + ", got " + e); |
| 78 |
assertEq(Object.getPrototypeOf(obj), initialProto, |
| 79 |
desc + ": shouldn't observe [[Prototype]] change"); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
tryMutate(Object.setPrototypeOf, "Object.setPrototypeOf"); |
| 84 |
tryMutate(setViaProtoSetter, "__proto__ setter"); |
| 85 |
} |
| 86 |
|
| 87 |
function runNormalTests(global) |
| 88 |
{ |
| 89 |
if (typeof setImmutablePrototype !== "function") |
| 90 |
{ |
| 91 |
print("no testable setImmutablePrototype function available, skipping tests"); |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
// regular old object, non-null [[Prototype]] |
| 96 |
|
| 97 |
var emptyLiteral = global.evaluate("({})"); |
| 98 |
assertEq(setImmutablePrototype(emptyLiteral), true); |
| 99 |
checkPrototypeMutationFailure(emptyLiteral, "empty literal"); |
| 100 |
|
| 101 |
// regular old object, null [[Prototype]] |
| 102 |
|
| 103 |
var nullProto = global.Object.create(null); |
| 104 |
assertEq(setImmutablePrototype(nullProto), true); |
| 105 |
checkPrototypeMutationFailure(nullProto, "nullProto"); |
| 106 |
|
| 107 |
// Shocker: SpecialPowers's little mind doesn't understand proxies. Abort. |
| 108 |
if (!usingRealSetImmutablePrototype) |
| 109 |
return; |
| 110 |
|
| 111 |
// direct proxies |
| 112 |
|
| 113 |
var emptyTarget = global.evaluate("({})"); |
| 114 |
var directProxy = new global.Proxy(emptyTarget, {}); |
| 115 |
assertEq(setImmutablePrototype(directProxy), true); |
| 116 |
checkPrototypeMutationFailure(directProxy, "direct proxy to empty target"); |
| 117 |
checkPrototypeMutationFailure(emptyTarget, "empty target"); |
| 118 |
|
| 119 |
var anotherTarget = global.evaluate("({})"); |
| 120 |
var anotherDirectProxy = new global.Proxy(anotherTarget, {}); |
| 121 |
assertEq(setImmutablePrototype(anotherTarget), true); |
| 122 |
checkPrototypeMutationFailure(anotherDirectProxy, "another direct proxy to empty target"); |
| 123 |
checkPrototypeMutationFailure(anotherTarget, "another empty target"); |
| 124 |
|
| 125 |
var nestedTarget = global.evaluate("({})"); |
| 126 |
var nestedProxy = new global.Proxy(new Proxy(nestedTarget, {}), {}); |
| 127 |
assertEq(setImmutablePrototype(nestedProxy), true); |
| 128 |
checkPrototypeMutationFailure(nestedProxy, "nested proxy to empty target"); |
| 129 |
checkPrototypeMutationFailure(nestedTarget, "nested target"); |
| 130 |
|
| 131 |
// revocable proxies |
| 132 |
|
| 133 |
var revocableTarget = global.evaluate("({})"); |
| 134 |
var revocable = global.Proxy.revocable(revocableTarget, {}); |
| 135 |
assertEq(setImmutablePrototype(revocable.proxy), true); |
| 136 |
checkPrototypeMutationFailure(revocableTarget, "revocable target"); |
| 137 |
checkPrototypeMutationFailure(revocable.proxy, "revocable proxy"); |
| 138 |
|
| 139 |
assertEq(revocable.revoke(), undefined); |
| 140 |
try |
| 141 |
{ |
| 142 |
setImmutablePrototype(revocable.proxy); |
| 143 |
throw new Error("expected to throw on revoked proxy"); |
| 144 |
} |
| 145 |
catch (e) |
| 146 |
{ |
| 147 |
// Note: In the cross-compartment case, this is a TypeError from |global|, |
| 148 |
// because the proxy's |setImmutablePrototype| method is what actually |
| 149 |
// throws here. That's why we check for TypeError from either global. |
| 150 |
// (Usually the method simply sets |*succeeded| to false and the |
| 151 |
// caller handles or throws as needed after that. But not here.) |
| 152 |
assertEq(e instanceof global.TypeError || e instanceof TypeError, true, |
| 153 |
"expected TypeError, instead threw " + e); |
| 154 |
} |
| 155 |
|
| 156 |
var anotherRevocableTarget = global.evaluate("({})"); |
| 157 |
assertEq(setImmutablePrototype(anotherRevocableTarget), true); |
| 158 |
checkPrototypeMutationFailure(anotherRevocableTarget, "another revocable target"); |
| 159 |
|
| 160 |
var anotherRevocable = global.Proxy.revocable(anotherRevocableTarget, {}); |
| 161 |
checkPrototypeMutationFailure(anotherRevocable.proxy, "another revocable target"); |
| 162 |
|
| 163 |
assertEq(anotherRevocable.revoke(), undefined); |
| 164 |
try |
| 165 |
{ |
| 166 |
var rv = setImmutablePrototype(anotherRevocable.proxy); |
| 167 |
throw new Error("expected to throw on another revoked proxy, returned " + rv); |
| 168 |
} |
| 169 |
catch (e) |
| 170 |
{ |
| 171 |
// NOTE: Again from |global| or |this|, as above. |
| 172 |
assertEq(e instanceof global.TypeError || e instanceof TypeError, true, |
| 173 |
"expected TypeError, instead threw " + e); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
var global = this; |
| 178 |
runNormalTests(global); // same-global |
| 179 |
|
| 180 |
if (typeof newGlobal === "function") |
| 181 |
{ |
| 182 |
var otherGlobal = newGlobal(); |
| 183 |
var subsumingNothing = newGlobal({ principal: 0 }); |
| 184 |
var subsumingEverything = newGlobal({ principal: ~0 }); |
| 185 |
|
| 186 |
runNormalTests(otherGlobal); // cross-global |
| 187 |
runNormalTests(subsumingNothing); |
| 188 |
runNormalTests(subsumingEverything); |
| 189 |
} |
| 190 |
|
| 191 |
/******************************************************************************/ |
| 192 |
|
| 193 |
if (typeof reportCompare === "function") |
| 194 |
reportCompare(true, true); |
| 195 |
|
| 196 |
print("Tests complete"); |