Line data Source code
1 : // Copyright 2013 the V8 project authors. All rights reserved.
2 : // Redistribution and use in source and binary forms, with or without
3 : // modification, are permitted provided that the following conditions are
4 : // met:
5 : //
6 : // * Redistributions of source code must retain the above copyright
7 : // notice, this list of conditions and the following disclaimer.
8 : // * Redistributions in binary form must reproduce the above
9 : // copyright notice, this list of conditions and the following
10 : // disclaimer in the documentation and/or other materials provided
11 : // with the distribution.
12 : // * Neither the name of Google Inc. nor the names of its
13 : // contributors may be used to endorse or promote products derived
14 : // from this software without specific prior written permission.
15 : //
16 : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 : // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 : // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 : // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 : // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 : // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 : // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 : // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 : // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 : // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 : // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 :
28 : #include "src/api.h"
29 : #include "src/objects-inl.h"
30 : #include "src/v8.h"
31 : #include "test/cctest/cctest.h"
32 :
33 : using ::v8::Array;
34 : using ::v8::Context;
35 : using ::v8::Local;
36 : using ::v8::Value;
37 :
38 : // This test fails if properties on the prototype of the global object appear
39 : // as declared globals.
40 23724 : TEST(StrictUndeclaredGlobalVariable) {
41 6 : v8::HandleScope scope(CcTest::isolate());
42 6 : v8::Local<v8::String> var_name = v8_str("x");
43 12 : LocalContext context;
44 12 : v8::TryCatch try_catch(CcTest::isolate());
45 : v8::Local<v8::Script> script = v8_compile("\"use strict\"; x = 42;");
46 6 : v8::Local<v8::Object> proto = v8::Object::New(CcTest::isolate());
47 : v8::Local<v8::Object> global =
48 12 : context->Global()->GetPrototype().As<v8::Object>();
49 24 : proto->Set(context.local(), var_name, v8_num(100)).FromJust();
50 12 : global->SetPrototype(context.local(), proto).FromJust();
51 12 : CHECK(script->Run(context.local()).IsEmpty());
52 6 : CHECK(try_catch.HasCaught());
53 12 : v8::String::Utf8Value exception(CcTest::isolate(), try_catch.Exception());
54 12 : CHECK_EQ(0, strcmp("ReferenceError: x is not defined", *exception));
55 6 : }
56 :
57 :
58 23724 : TEST(KeysGlobalObject_Regress2764) {
59 6 : LocalContext env1;
60 12 : v8::HandleScope scope(env1->GetIsolate());
61 :
62 : // Create second environment.
63 6 : v8::Local<Context> env2 = Context::New(env1->GetIsolate());
64 :
65 6 : Local<Value> token = v8_str("foo");
66 :
67 : // Set same security token for env1 and env2.
68 6 : env1->SetSecurityToken(token);
69 6 : env2->SetSecurityToken(token);
70 :
71 : // Create a reference to env2 global from env1 global.
72 : env1->Global()
73 30 : ->Set(env1.local(), v8_str("global2"), env2->Global())
74 12 : .FromJust();
75 : // Set some global variables in global2
76 30 : env2->Global()->Set(env2, v8_str("a"), v8_str("a")).FromJust();
77 30 : env2->Global()->Set(env2, v8_str("42"), v8_str("42")).FromJust();
78 :
79 : // List all entries from global2.
80 : Local<Array> result;
81 : result = Local<Array>::Cast(CompileRun("Object.keys(global2)"));
82 6 : CHECK_EQ(2u, result->Length());
83 24 : CHECK(
84 : v8_str("42")
85 : ->Equals(env1.local(), result->Get(env1.local(), 0).ToLocalChecked())
86 : .FromJust());
87 24 : CHECK(
88 : v8_str("a")
89 : ->Equals(env1.local(), result->Get(env1.local(), 1).ToLocalChecked())
90 : .FromJust());
91 :
92 : result =
93 : Local<Array>::Cast(CompileRun("Object.getOwnPropertyNames(global2)"));
94 6 : CHECK_LT(2u, result->Length());
95 : // Check that all elements are in the property names
96 : ExpectTrue("-1 < Object.getOwnPropertyNames(global2).indexOf('42')");
97 : ExpectTrue("-1 < Object.getOwnPropertyNames(global2).indexOf('a')");
98 :
99 : // Hold on to global from env2 and detach global from env2.
100 6 : env2->DetachGlobal();
101 :
102 : // List again all entries from the detached global2.
103 : result = Local<Array>::Cast(CompileRun("Object.keys(global2)"));
104 6 : CHECK_EQ(0u, result->Length());
105 : result =
106 : Local<Array>::Cast(CompileRun("Object.getOwnPropertyNames(global2)"));
107 12 : CHECK_EQ(0u, result->Length());
108 6 : }
109 :
110 23724 : TEST(KeysGlobalObject_SetPrototype) {
111 6 : LocalContext env1;
112 12 : v8::HandleScope scope(env1->GetIsolate());
113 :
114 : // Create second environment.
115 6 : v8::Local<Context> env2 = Context::New(env1->GetIsolate());
116 :
117 6 : Local<Value> token = v8_str("foo");
118 :
119 : // Set same security token for env1 and env2.
120 6 : env1->SetSecurityToken(token);
121 6 : env2->SetSecurityToken(token);
122 :
123 : // Create a reference to env2 global from env1 global.
124 : env1->Global()
125 12 : ->GetPrototype()
126 12 : .As<v8::Object>()
127 18 : ->SetPrototype(env1.local(), env2->Global()->GetPrototype())
128 12 : .FromJust();
129 : // Set some global variables in global2
130 30 : env2->Global()->Set(env2, v8_str("a"), v8_str("a")).FromJust();
131 30 : env2->Global()->Set(env2, v8_str("42"), v8_str("42")).FromJust();
132 :
133 : // List all entries from global2.
134 6 : ExpectTrue("a == 'a'");
135 71160 : }
|