js/src/tests/ecma_5/String/replace-throws-nonwritable-lastIndex-global.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Any copyright is dedicated to the Public Domain.
michael@0 3 * http://creativecommons.org/licenses/publicdomain/
michael@0 4 */
michael@0 5
michael@0 6 var BUGNUMBER = 501739;
michael@0 7 var summary =
michael@0 8 "String.prototype.replace should throw when called with a global RegExp " +
michael@0 9 "whose .lastIndex is non-writable";
michael@0 10
michael@0 11 print(BUGNUMBER + ": " + summary);
michael@0 12
michael@0 13 /**************
michael@0 14 * BEGIN TEST *
michael@0 15 **************/
michael@0 16
michael@0 17 var s = '0x2x4x6x8';
michael@0 18
michael@0 19 // First time with .lastIndex === 0, replacing to ''
michael@0 20
michael@0 21 var p1 = /x/g;
michael@0 22 Object.defineProperty(p1, "lastIndex", { writable: false });
michael@0 23
michael@0 24 try
michael@0 25 {
michael@0 26 s.replace(p1, '');
michael@0 27 throw "didn't throw";
michael@0 28 }
michael@0 29 catch (e)
michael@0 30 {
michael@0 31 assertEq(e instanceof TypeError, true,
michael@0 32 "should have thrown a TypeError, instead got: " + e);
michael@0 33 assertEq(p1.lastIndex, 0);
michael@0 34 }
michael@0 35
michael@0 36 // Second time with .lastIndex !== 0, replacing to ''
michael@0 37
michael@0 38 var p2 = /x/g;
michael@0 39 Object.defineProperty(p2, "lastIndex", { writable: false, value: 3 });
michael@0 40
michael@0 41 try
michael@0 42 {
michael@0 43 s.replace(p2, '');
michael@0 44 throw "didn't throw";
michael@0 45 }
michael@0 46 catch (e)
michael@0 47 {
michael@0 48 assertEq(e instanceof TypeError, true,
michael@0 49 "should have thrown a TypeError, instead got: " + e);
michael@0 50 assertEq(p2.lastIndex, 3);
michael@0 51 }
michael@0 52
michael@0 53 // Third time with .lastIndex === 0, replacing to 'y'
michael@0 54
michael@0 55 var p3 = /x/g;
michael@0 56 Object.defineProperty(p3, "lastIndex", { writable: false });
michael@0 57
michael@0 58 try
michael@0 59 {
michael@0 60 s.replace(p3, 'y');
michael@0 61 throw "didn't throw";
michael@0 62 }
michael@0 63 catch (e)
michael@0 64 {
michael@0 65 assertEq(e instanceof TypeError, true,
michael@0 66 "should have thrown a TypeError, instead got: " + e);
michael@0 67 assertEq(p3.lastIndex, 0);
michael@0 68 }
michael@0 69
michael@0 70 // Fourth time with .lastIndex !== 0, replacing to 'y'
michael@0 71
michael@0 72 var p4 = /x/g;
michael@0 73 Object.defineProperty(p4, "lastIndex", { writable: false, value: 3 });
michael@0 74
michael@0 75 try
michael@0 76 {
michael@0 77 s.replace(p4, '');
michael@0 78 throw "didn't throw";
michael@0 79 }
michael@0 80 catch (e)
michael@0 81 {
michael@0 82 assertEq(e instanceof TypeError, true,
michael@0 83 "should have thrown a TypeError, instead got: " + e);
michael@0 84 assertEq(p4.lastIndex, 3);
michael@0 85 }
michael@0 86
michael@0 87 // Fifth time with .lastIndex === 0, replacing to 'y', but no match
michael@0 88
michael@0 89 var p5 = /q/g;
michael@0 90 Object.defineProperty(p5, "lastIndex", { writable: false });
michael@0 91
michael@0 92 try
michael@0 93 {
michael@0 94 s.replace(p5, 'y');
michael@0 95 throw "didn't throw";
michael@0 96 }
michael@0 97 catch (e)
michael@0 98 {
michael@0 99 assertEq(e instanceof TypeError, true,
michael@0 100 "should have thrown a TypeError, instead got: " + e);
michael@0 101 assertEq(p5.lastIndex, 0);
michael@0 102 }
michael@0 103
michael@0 104 // Sixth time with .lastIndex !== 0, replacing to 'y', but no match
michael@0 105
michael@0 106 var p6 = /q/g;
michael@0 107 Object.defineProperty(p6, "lastIndex", { writable: false, value: 3 });
michael@0 108
michael@0 109 try
michael@0 110 {
michael@0 111 s.replace(p6, '');
michael@0 112 throw "didn't throw";
michael@0 113 }
michael@0 114 catch (e)
michael@0 115 {
michael@0 116 assertEq(e instanceof TypeError, true,
michael@0 117 "should have thrown a TypeError, instead got: " + e);
michael@0 118 assertEq(p6.lastIndex, 3);
michael@0 119 }
michael@0 120
michael@0 121 /******************************************************************************/
michael@0 122
michael@0 123 if (typeof reportCompare === "function")
michael@0 124 reportCompare(true, true);
michael@0 125
michael@0 126 print("Tests complete");

mercurial