test_bug793969.html (2386B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=793969 5 --> 6 <head> 7 <meta charset="utf-8"> 8 <title>Test for Bug 793969</title> 9 <script src="/tests/SimpleTest/SimpleTest.js"></script> 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 11 </head> 12 <body> 13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=793969">Mozilla Bug 793969</a> 14 <p id="display"></p> 15 <div id="content" style="display: none"> 16 17 </div> 18 <pre id="test"> 19 <script type="application/javascript"> 20 21 /** Test for Bug 793969 **/ 22 function checkThrows(f, desc, skipMessageCheck) { 23 try { 24 f(); 25 ok(false, "Should have thrown for " + desc); 26 } catch (e) { 27 ok(true, "threw correctly"); 28 if (!skipMessageCheck) 29 ok(/denied/.exec(e) || 30 /can't redefine non-configurable property/.exec(e), 31 "Correctly threw a security exception: " + e); 32 } 33 } 34 35 // NB: These sets will be no-ops (throw in strict mode) because setting an inherited readonly value prop has those semantics. 36 checkThrows(function() { "use strict"; location.valueOf = 'hah'; }, 'Shadow with string', /* skipMessageCheck = */ true); 37 checkThrows(function() { "use strict"; location.valueOf = function() { return {a: 'hah'};} }, 'Shadow with function', /* skipMessageCheck = */ true); 38 checkThrows(function() { Object.defineProperty(location, 'valueOf', { value: function() { return 'hah'; } }); }, 'defineProperty with value'); 39 checkThrows(function() { delete location.valueOf; Object.defineProperty(location, 'valueOf', { value: function() { return 'hah'; } }); }, 'delete + defineProperty with value'); 40 checkThrows(function() { Object.defineProperty(location, 'valueOf', { get: function() { return 'hah'; } }); }, 'defineProperty with getter'); 41 checkThrows(function() { delete location.valueOf; Object.defineProperty(location, 'valueOf', { get: function() { return 'hah'; } }); }, 'delete + defineProperty with getter'); 42 43 Object.prototype.valueOf = function() { return 'hah'; }; 44 is(({}).valueOf(), 'hah', "Shadowing on Object.prototype works for vanilla objects"); 45 is(location.valueOf(), location, "Shadowing on Object.prototype and Location.prototype doesn't for location objects"); 46 47 location[Symbol.toPrimitive] = function() { return 'hah'; } 48 is(location + "", location.toString(), "Should't be able to shadow with toPrimitive"); 49 50 </script> 51 </pre> 52 </body> 53 </html>