js/src/tests/ecma_5/JSON/cyclic-stringify.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 // Any copyright is dedicated to the Public Domain.
michael@0 2 // http://creativecommons.org/licenses/publicdomain/
michael@0 3
michael@0 4 //-----------------------------------------------------------------------------
michael@0 5 var BUGNUMBER = 578273;
michael@0 6 var summary =
michael@0 7 "ES5: Properly detect cycles in JSON.stringify (throw TypeError, check for " +
michael@0 8 "cycles rather than imprecisely rely on recursion limits)";
michael@0 9
michael@0 10 print(BUGNUMBER + ": " + summary);
michael@0 11
michael@0 12 /**************
michael@0 13 * BEGIN TEST *
michael@0 14 **************/
michael@0 15
michael@0 16 // objects
michael@0 17
michael@0 18 var count = 0;
michael@0 19 var desc =
michael@0 20 {
michael@0 21 get: function() { count++; return obj; },
michael@0 22 enumerable: true,
michael@0 23 configurable: true
michael@0 24 };
michael@0 25 var obj = Object.defineProperty({ p1: 0 }, "p2", desc);
michael@0 26
michael@0 27 try
michael@0 28 {
michael@0 29 var str = JSON.stringify(obj);
michael@0 30 assertEq(false, true, "should have thrown, got " + str);
michael@0 31 }
michael@0 32 catch (e)
michael@0 33 {
michael@0 34 assertEq(e instanceof TypeError, true,
michael@0 35 "wrong error type: " + e.constructor.name);
michael@0 36 assertEq(count, 1,
michael@0 37 "cyclic data structures not detected immediately");
michael@0 38 }
michael@0 39
michael@0 40 count = 0;
michael@0 41 var obj2 = Object.defineProperty({}, "obj", desc);
michael@0 42 try
michael@0 43 {
michael@0 44 var str = JSON.stringify(obj2);
michael@0 45 assertEq(false, true, "should have thrown, got " + str);
michael@0 46 }
michael@0 47 catch (e)
michael@0 48 {
michael@0 49 assertEq(e instanceof TypeError, true,
michael@0 50 "wrong error type: " + e.constructor.name);
michael@0 51 assertEq(count, 2,
michael@0 52 "cyclic data structures not detected immediately");
michael@0 53 }
michael@0 54
michael@0 55
michael@0 56 // arrays
michael@0 57
michael@0 58 var count = 0;
michael@0 59 var desc =
michael@0 60 {
michael@0 61 get: function() { count++; return arr; },
michael@0 62 enumerable: true,
michael@0 63 configurable: true
michael@0 64 };
michael@0 65 var arr = Object.defineProperty([], "0", desc);
michael@0 66
michael@0 67 try
michael@0 68 {
michael@0 69 var str = JSON.stringify(arr);
michael@0 70 assertEq(false, true, "should have thrown, got " + str);
michael@0 71 }
michael@0 72 catch (e)
michael@0 73 {
michael@0 74 assertEq(e instanceof TypeError, true,
michael@0 75 "wrong error type: " + e.constructor.name);
michael@0 76 assertEq(count, 1,
michael@0 77 "cyclic data structures not detected immediately");
michael@0 78 }
michael@0 79
michael@0 80 count = 0;
michael@0 81 var arr2 = Object.defineProperty([], "0", desc);
michael@0 82 try
michael@0 83 {
michael@0 84 var str = JSON.stringify(arr2);
michael@0 85 assertEq(false, true, "should have thrown, got " + str);
michael@0 86 }
michael@0 87 catch (e)
michael@0 88 {
michael@0 89 assertEq(e instanceof TypeError, true,
michael@0 90 "wrong error type: " + e.constructor.name);
michael@0 91 assertEq(count, 2,
michael@0 92 "cyclic data structures not detected immediately");
michael@0 93 }
michael@0 94
michael@0 95 /******************************************************************************/
michael@0 96
michael@0 97 if (typeof reportCompare === "function")
michael@0 98 reportCompare(true, true);
michael@0 99
michael@0 100 print("Tests complete");

mercurial