js/src/jsapi-tests/testResolveRecursion.cpp

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 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 */
michael@0 4 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 #include "jsapi-tests/tests.h"
michael@0 9
michael@0 10 /*
michael@0 11 * Test that resolve hook recursion for the same object and property is
michael@0 12 * prevented.
michael@0 13 */
michael@0 14
michael@0 15 BEGIN_TEST(testResolveRecursion)
michael@0 16 {
michael@0 17 static const JSClass my_resolve_class = {
michael@0 18 "MyResolve",
michael@0 19 JSCLASS_NEW_RESOLVE | JSCLASS_HAS_PRIVATE,
michael@0 20
michael@0 21 JS_PropertyStub, // add
michael@0 22 JS_DeletePropertyStub, // delete
michael@0 23 JS_PropertyStub, // get
michael@0 24 JS_StrictPropertyStub, // set
michael@0 25 JS_EnumerateStub,
michael@0 26 (JSResolveOp) my_resolve,
michael@0 27 JS_ConvertStub
michael@0 28 };
michael@0 29
michael@0 30 obj1 = obj2 = nullptr;
michael@0 31 JS::AddObjectRoot(cx, &obj1);
michael@0 32 JS::AddObjectRoot(cx, &obj2);
michael@0 33
michael@0 34 obj1 = JS_NewObject(cx, &my_resolve_class, JS::NullPtr(), JS::NullPtr());
michael@0 35 CHECK(obj1);
michael@0 36 obj2 = JS_NewObject(cx, &my_resolve_class, JS::NullPtr(), JS::NullPtr());
michael@0 37 CHECK(obj2);
michael@0 38 JS_SetPrivate(obj1, this);
michael@0 39 JS_SetPrivate(obj2, this);
michael@0 40
michael@0 41 JS::RootedValue obj1Val(cx, ObjectValue(*obj1));
michael@0 42 JS::RootedValue obj2Val(cx, ObjectValue(*obj2));
michael@0 43 CHECK(JS_DefineProperty(cx, global, "obj1", obj1Val, 0));
michael@0 44 CHECK(JS_DefineProperty(cx, global, "obj2", obj2Val, 0));
michael@0 45
michael@0 46 resolveEntryCount = 0;
michael@0 47 resolveExitCount = 0;
michael@0 48
michael@0 49 /* Start the essence of the test via invoking the first resolve hook. */
michael@0 50 JS::RootedValue v(cx);
michael@0 51 EVAL("obj1.x", &v);
michael@0 52 CHECK_SAME(v, JSVAL_FALSE);
michael@0 53 CHECK_EQUAL(resolveEntryCount, 4);
michael@0 54 CHECK_EQUAL(resolveExitCount, 4);
michael@0 55
michael@0 56 obj1 = nullptr;
michael@0 57 obj2 = nullptr;
michael@0 58 JS::RemoveObjectRoot(cx, &obj1);
michael@0 59 JS::RemoveObjectRoot(cx, &obj2);
michael@0 60 return true;
michael@0 61 }
michael@0 62
michael@0 63 JS::Heap<JSObject *> obj1;
michael@0 64 JS::Heap<JSObject *> obj2;
michael@0 65 unsigned resolveEntryCount;
michael@0 66 unsigned resolveExitCount;
michael@0 67
michael@0 68 struct AutoIncrCounters {
michael@0 69
michael@0 70 AutoIncrCounters(cls_testResolveRecursion *t) : t(t) {
michael@0 71 t->resolveEntryCount++;
michael@0 72 }
michael@0 73
michael@0 74 ~AutoIncrCounters() {
michael@0 75 t->resolveExitCount++;
michael@0 76 }
michael@0 77
michael@0 78 cls_testResolveRecursion *t;
michael@0 79 };
michael@0 80
michael@0 81 bool
michael@0 82 doResolve(JS::HandleObject obj, JS::HandleId id, JS::MutableHandleObject objp)
michael@0 83 {
michael@0 84 CHECK_EQUAL(resolveExitCount, 0);
michael@0 85 AutoIncrCounters incr(this);
michael@0 86 CHECK_EQUAL(obj, obj1 || obj == obj2);
michael@0 87
michael@0 88 CHECK(JSID_IS_STRING(id));
michael@0 89
michael@0 90 JSFlatString *str = JS_FlattenString(cx, JSID_TO_STRING(id));
michael@0 91 CHECK(str);
michael@0 92 JS::RootedValue v(cx);
michael@0 93 if (JS_FlatStringEqualsAscii(str, "x")) {
michael@0 94 if (obj == obj1) {
michael@0 95 /* First resolve hook invocation. */
michael@0 96 CHECK_EQUAL(resolveEntryCount, 1);
michael@0 97 EVAL("obj2.y = true", &v);
michael@0 98 CHECK_SAME(v, JSVAL_TRUE);
michael@0 99 CHECK(JS_DefinePropertyById(cx, obj, id, JSVAL_FALSE, nullptr, nullptr, 0));
michael@0 100 objp.set(obj);
michael@0 101 return true;
michael@0 102 }
michael@0 103 if (obj == obj2) {
michael@0 104 CHECK_EQUAL(resolveEntryCount, 4);
michael@0 105 objp.set(nullptr);
michael@0 106 return true;
michael@0 107 }
michael@0 108 } else if (JS_FlatStringEqualsAscii(str, "y")) {
michael@0 109 if (obj == obj2) {
michael@0 110 CHECK_EQUAL(resolveEntryCount, 2);
michael@0 111 CHECK(JS_DefinePropertyById(cx, obj, id, JSVAL_NULL, nullptr, nullptr, 0));
michael@0 112 EVAL("obj1.x", &v);
michael@0 113 CHECK(JSVAL_IS_VOID(v));
michael@0 114 EVAL("obj1.y", &v);
michael@0 115 CHECK_SAME(v, JSVAL_ZERO);
michael@0 116 objp.set(obj);
michael@0 117 return true;
michael@0 118 }
michael@0 119 if (obj == obj1) {
michael@0 120 CHECK_EQUAL(resolveEntryCount, 3);
michael@0 121 EVAL("obj1.x", &v);
michael@0 122 CHECK(JSVAL_IS_VOID(v));
michael@0 123 EVAL("obj1.y", &v);
michael@0 124 CHECK(JSVAL_IS_VOID(v));
michael@0 125 EVAL("obj2.y", &v);
michael@0 126 CHECK(JSVAL_IS_NULL(v));
michael@0 127 EVAL("obj2.x", &v);
michael@0 128 CHECK(JSVAL_IS_VOID(v));
michael@0 129 EVAL("obj1.y = 0", &v);
michael@0 130 CHECK_SAME(v, JSVAL_ZERO);
michael@0 131 objp.set(obj);
michael@0 132 return true;
michael@0 133 }
michael@0 134 }
michael@0 135 CHECK(false);
michael@0 136 return false;
michael@0 137 }
michael@0 138
michael@0 139 static bool
michael@0 140 my_resolve(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleObject objp)
michael@0 141 {
michael@0 142 return static_cast<cls_testResolveRecursion *>(JS_GetPrivate(obj))->
michael@0 143 doResolve(obj, id, objp);
michael@0 144 }
michael@0 145
michael@0 146 END_TEST(testResolveRecursion)

mercurial