js/src/jsapi-tests/testTrap.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.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     2  * vim: set ts=8 sts=4 et sw=4 tw=99:
     3  */
     4 /* This Source Code Form is subject to the terms of the Mozilla Public
     5  * License, v. 2.0. If a copy of the MPL was not distributed with this
     6  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     8 #include "js/OldDebugAPI.h"
     9 #include "jsapi-tests/tests.h"
    11 static int emptyTrapCallCount = 0;
    13 static JSTrapStatus
    14 EmptyTrapHandler(JSContext *cx, JSScript *script, jsbytecode *pc, jsval *rval,
    15                  jsval closureArg)
    16 {
    17     JS::RootedValue closure(cx, closureArg);
    18     JS_GC(JS_GetRuntime(cx));
    19     if (JSVAL_IS_STRING(closure))
    20         ++emptyTrapCallCount;
    21     return JSTRAP_CONTINUE;
    22 }
    24 BEGIN_TEST(testTrap_gc)
    25 {
    26     static const char source[] =
    27 "var i = 0;\n"
    28 "var sum = 0;\n"
    29 "while (i < 10) {\n"
    30 "    sum += i;\n"
    31 "    ++i;\n"
    32 "}\n"
    33 "({ result: sum });\n"
    34         ;
    36     // compile
    37     JS::CompileOptions options(cx);
    38     options.setFileAndLine(__FILE__, 1);
    39     JS::RootedScript script(cx, JS_CompileScript(cx, global, source,
    40                                                  strlen(source), options));
    41     CHECK(script);
    43     // execute
    44     JS::RootedValue v2(cx);
    45     CHECK(JS_ExecuteScript(cx, global, script, &v2));
    46     CHECK(v2.isObject());
    47     CHECK_EQUAL(emptyTrapCallCount, 0);
    49     // Enable debug mode
    50     CHECK(JS_SetDebugMode(cx, true));
    52     static const char trapClosureText[] = "some trap closure";
    54     // scope JSScript  usage to make sure that it is not used after
    55     // JS_ExecuteScript. This way we avoid using Anchor.
    56     JS::RootedString trapClosure(cx);
    57     {
    58         jsbytecode *line2 = JS_LineNumberToPC(cx, script, 1);
    59         CHECK(line2);
    61         jsbytecode *line6 = JS_LineNumberToPC(cx, script, 5);
    62         CHECK(line2);
    64         trapClosure = JS_NewStringCopyZ(cx, trapClosureText);
    65         CHECK(trapClosure);
    66         JS::RootedValue closureValue(cx, JS::StringValue(trapClosure));
    67         JS_SetTrap(cx, script, line2, EmptyTrapHandler, closureValue);
    68         JS_SetTrap(cx, script, line6, EmptyTrapHandler, closureValue);
    70         JS_GC(rt);
    72         CHECK(JS_FlatStringEqualsAscii(JS_ASSERT_STRING_IS_FLAT(trapClosure), trapClosureText));
    73     }
    75     // execute
    76     CHECK(JS_ExecuteScript(cx, global, script, &v2));
    77     CHECK_EQUAL(emptyTrapCallCount, 11);
    79     JS_GC(rt);
    81     CHECK(JS_FlatStringEqualsAscii(JS_ASSERT_STRING_IS_FLAT(trapClosure), trapClosureText));
    83     return true;
    84 }
    85 END_TEST(testTrap_gc)

mercurial