gecko-dev/dom/tests/mochitest/bugs/test_bug541530.html
Bobby Holley 7039a94106 Bug 733984 - Explicitly disallow shadowing on location wrappers. r=mrbkap
This was taken care of in other ways before, but we need to be more explicit about it now that we're doing more Xray stuff with Location wrappers.
2012-03-23 15:58:18 -07:00

106 lines
3.2 KiB
HTML

<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=541530
-->
<head>
<title>Test for Bug 411103</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=541530">Mozilla Bug 541530</a>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test">
<script class="testbody" type="text/javascript">
var orig = window;
window = {};
var origLocation = location;
ok(window === orig, "can't override window");
ok(window.location === location, "properties are properly aliased");
ok(document.location === location, "properties are properly aliased");
var canDefine = false;
try {
var foo;
__defineGetter__.call(foo, 'bar', function() {});
__defineSetter__.call(foo, 'bar', function() {});
canDefine = true;
} catch (e) {}
ok(canDefine, "Should have access to __defineGetter__ and __defineSetter__");
try {
__defineGetter__('window', function() {});
ok(false, "should not be able to defineGetter(window)");
} catch (e) {
}
try {
__defineGetter__.call(window, 'location', function(){});
ok(false, "should not be able to defineGetter(window.location)");
} catch (e) {
}
try {
__defineGetter__.call(window.location, 'href', function(){});
ok(false, "shouldn't be able to override location.href");
} catch (e) {
ok(/shadow/.exec(e.message), "Should be caught by the anti-shadow mechanism.");
}
// Try deleting the property.
delete window.location.href;
ok(typeof window.location.href !== 'undefined',
"shouldn't be able to delete the inherited property");
delete Object.getPrototypeOf(window.location).href;
ok(typeof window.location.href !== 'undefined',
"shouldn't be able to delete the property off of the prototype");
try {
__defineGetter__.call(Object.getPrototypeOf(window.location), 'href', function(){});
ok(false, "shouldn't be able to use the prototype");
} catch (e) {
}
try {
__defineSetter__.call(window.location, 'href', function(){});
ok(false, "overrode a setter for location.href?");
} catch (e) {
ok(/shadow/.exec(e.message), "Should be caught by the anti-shadow mechanism.");
}
try {
__defineGetter__.call(document, 'location', function(){});
ok(false, "shouldn't be able to override document.location");
} catch (e) {
}
// Make sure watch works correctly.
var watchTest = {a: 42};
var watchHandler = function(prop, oldval, newval) { return 22; }
Object.prototype.watch.call(watchTest, 'a', watchHandler);
watchTest.a = 1;
is(watchTest.a, 22, "Watch works correctly");
// Now make sure we can't watch location.
try {
Object.prototype.watch.call(location, 'href', function(prop, oldval, newval) {});
ok(false, "shouldn't be able to set a watchpoint on location");
} catch (e) {}
ok(window === orig, "can't override window");
ok(window.location === origLocation, "properties are properly aliased");
ok(document.location === origLocation, "properties are properly aliased");
location.href = 'javascript:ok(true, "was able to set location.href through a watchpoint")';
</script>
</pre>
</body>
</html>