Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/JsValue.cpp

Issue 10310030: Convert references to FileSystem & Co. into shared pointers (avoid use after free) (Closed)
Patch Set: Created April 18, 2013, 11:59 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 #include <AdblockPlus.h> 1 #include <AdblockPlus.h>
2 #include <gtest/gtest.h> 2 #include <gtest/gtest.h>
3 3
4 class ThrowingErrorCallback : public AdblockPlus::ErrorCallback
5 {
6 public:
7 void operator()(const std::string& message)
8 {
9 throw std::runtime_error("Unexpected error: " + message);
10 }
11 };
12
13 TEST(JsValueTest, UndefinedValue) 4 TEST(JsValueTest, UndefinedValue)
14 { 5 {
15 ThrowingErrorCallback errorCallback; 6 AdblockPlus::JsEngine jsEngine;
16 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback);
17 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("undefined"); 7 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("undefined");
18 ASSERT_TRUE(value->IsUndefined()); 8 ASSERT_TRUE(value->IsUndefined());
19 ASSERT_FALSE(value->IsNull()); 9 ASSERT_FALSE(value->IsNull());
20 ASSERT_FALSE(value->IsString()); 10 ASSERT_FALSE(value->IsString());
21 ASSERT_FALSE(value->IsBool()); 11 ASSERT_FALSE(value->IsBool());
22 ASSERT_FALSE(value->IsNumber()); 12 ASSERT_FALSE(value->IsNumber());
23 ASSERT_FALSE(value->IsObject()); 13 ASSERT_FALSE(value->IsObject());
24 ASSERT_FALSE(value->IsArray()); 14 ASSERT_FALSE(value->IsArray());
25 ASSERT_FALSE(value->IsFunction()); 15 ASSERT_FALSE(value->IsFunction());
26 ASSERT_EQ("undefined", value->AsString()); 16 ASSERT_EQ("undefined", value->AsString());
27 ASSERT_FALSE(value->AsBool()); 17 ASSERT_FALSE(value->AsBool());
28 ASSERT_ANY_THROW(value->AsList()); 18 ASSERT_ANY_THROW(value->AsList());
29 ASSERT_ANY_THROW(value->GetProperty("foo")); 19 ASSERT_ANY_THROW(value->GetProperty("foo"));
30 ASSERT_ANY_THROW(value->SetProperty("foo", false)); 20 ASSERT_ANY_THROW(value->SetProperty("foo", false));
31 ASSERT_ANY_THROW(value->GetClassName()); 21 ASSERT_ANY_THROW(value->GetClassName());
32 ASSERT_ANY_THROW(value->GetOwnPropertyNames()); 22 ASSERT_ANY_THROW(value->GetOwnPropertyNames());
33 ASSERT_ANY_THROW(value->Call()); 23 ASSERT_ANY_THROW(value->Call());
34 } 24 }
35 25
36 TEST(JsValueTest, NullValue) 26 TEST(JsValueTest, NullValue)
37 { 27 {
38 ThrowingErrorCallback errorCallback; 28 AdblockPlus::JsEngine jsEngine;
39 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback);
40 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("null"); 29 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("null");
41 ASSERT_FALSE(value->IsUndefined()); 30 ASSERT_FALSE(value->IsUndefined());
42 ASSERT_TRUE(value->IsNull()); 31 ASSERT_TRUE(value->IsNull());
43 ASSERT_FALSE(value->IsString()); 32 ASSERT_FALSE(value->IsString());
44 ASSERT_FALSE(value->IsBool()); 33 ASSERT_FALSE(value->IsBool());
45 ASSERT_FALSE(value->IsNumber()); 34 ASSERT_FALSE(value->IsNumber());
46 ASSERT_FALSE(value->IsObject()); 35 ASSERT_FALSE(value->IsObject());
47 ASSERT_FALSE(value->IsArray()); 36 ASSERT_FALSE(value->IsArray());
48 ASSERT_FALSE(value->IsFunction()); 37 ASSERT_FALSE(value->IsFunction());
49 ASSERT_EQ("null", value->AsString()); 38 ASSERT_EQ("null", value->AsString());
50 ASSERT_FALSE(value->AsBool()); 39 ASSERT_FALSE(value->AsBool());
51 ASSERT_ANY_THROW(value->AsList()); 40 ASSERT_ANY_THROW(value->AsList());
52 ASSERT_ANY_THROW(value->GetProperty("foo")); 41 ASSERT_ANY_THROW(value->GetProperty("foo"));
53 ASSERT_ANY_THROW(value->SetProperty("foo", false)); 42 ASSERT_ANY_THROW(value->SetProperty("foo", false));
54 ASSERT_ANY_THROW(value->GetClassName()); 43 ASSERT_ANY_THROW(value->GetClassName());
55 ASSERT_ANY_THROW(value->GetOwnPropertyNames()); 44 ASSERT_ANY_THROW(value->GetOwnPropertyNames());
56 ASSERT_ANY_THROW(value->Call()); 45 ASSERT_ANY_THROW(value->Call());
57 } 46 }
58 47
59 TEST(JsValueTest, StringValue) 48 TEST(JsValueTest, StringValue)
60 { 49 {
61 ThrowingErrorCallback errorCallback; 50 AdblockPlus::JsEngine jsEngine;
62 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback);
63 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("'123'"); 51 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("'123'");
64 ASSERT_FALSE(value->IsUndefined()); 52 ASSERT_FALSE(value->IsUndefined());
65 ASSERT_FALSE(value->IsNull()); 53 ASSERT_FALSE(value->IsNull());
66 ASSERT_TRUE(value->IsString()); 54 ASSERT_TRUE(value->IsString());
67 ASSERT_FALSE(value->IsBool()); 55 ASSERT_FALSE(value->IsBool());
68 ASSERT_FALSE(value->IsNumber()); 56 ASSERT_FALSE(value->IsNumber());
69 ASSERT_FALSE(value->IsObject()); 57 ASSERT_FALSE(value->IsObject());
70 ASSERT_FALSE(value->IsArray()); 58 ASSERT_FALSE(value->IsArray());
71 ASSERT_FALSE(value->IsFunction()); 59 ASSERT_FALSE(value->IsFunction());
72 ASSERT_EQ("123", value->AsString()); 60 ASSERT_EQ("123", value->AsString());
73 ASSERT_EQ(123, value->AsInt()); 61 ASSERT_EQ(123, value->AsInt());
74 ASSERT_TRUE(value->AsBool()); 62 ASSERT_TRUE(value->AsBool());
75 ASSERT_ANY_THROW(value->AsList()); 63 ASSERT_ANY_THROW(value->AsList());
76 ASSERT_ANY_THROW(value->GetProperty("foo")); 64 ASSERT_ANY_THROW(value->GetProperty("foo"));
77 ASSERT_ANY_THROW(value->SetProperty("foo", false)); 65 ASSERT_ANY_THROW(value->SetProperty("foo", false));
78 ASSERT_ANY_THROW(value->GetClassName()); 66 ASSERT_ANY_THROW(value->GetClassName());
79 ASSERT_ANY_THROW(value->GetOwnPropertyNames()); 67 ASSERT_ANY_THROW(value->GetOwnPropertyNames());
80 ASSERT_ANY_THROW(value->Call()); 68 ASSERT_ANY_THROW(value->Call());
81 } 69 }
82 70
83 TEST(JsValueTest, IntValue) 71 TEST(JsValueTest, IntValue)
84 { 72 {
85 ThrowingErrorCallback errorCallback; 73 AdblockPlus::JsEngine jsEngine;
86 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback);
87 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("123"); 74 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("123");
88 ASSERT_FALSE(value->IsUndefined()); 75 ASSERT_FALSE(value->IsUndefined());
89 ASSERT_FALSE(value->IsNull()); 76 ASSERT_FALSE(value->IsNull());
90 ASSERT_FALSE(value->IsString()); 77 ASSERT_FALSE(value->IsString());
91 ASSERT_FALSE(value->IsBool()); 78 ASSERT_FALSE(value->IsBool());
92 ASSERT_TRUE(value->IsNumber()); 79 ASSERT_TRUE(value->IsNumber());
93 ASSERT_FALSE(value->IsObject()); 80 ASSERT_FALSE(value->IsObject());
94 ASSERT_FALSE(value->IsArray()); 81 ASSERT_FALSE(value->IsArray());
95 ASSERT_FALSE(value->IsFunction()); 82 ASSERT_FALSE(value->IsFunction());
96 ASSERT_EQ("123", value->AsString()); 83 ASSERT_EQ("123", value->AsString());
97 ASSERT_EQ(123, value->AsInt()); 84 ASSERT_EQ(123, value->AsInt());
98 ASSERT_TRUE(value->AsBool()); 85 ASSERT_TRUE(value->AsBool());
99 ASSERT_ANY_THROW(value->AsList()); 86 ASSERT_ANY_THROW(value->AsList());
100 ASSERT_ANY_THROW(value->GetProperty("foo")); 87 ASSERT_ANY_THROW(value->GetProperty("foo"));
101 ASSERT_ANY_THROW(value->SetProperty("foo", false)); 88 ASSERT_ANY_THROW(value->SetProperty("foo", false));
102 ASSERT_ANY_THROW(value->GetClassName()); 89 ASSERT_ANY_THROW(value->GetClassName());
103 ASSERT_ANY_THROW(value->GetOwnPropertyNames()); 90 ASSERT_ANY_THROW(value->GetOwnPropertyNames());
104 ASSERT_ANY_THROW(value->Call()); 91 ASSERT_ANY_THROW(value->Call());
105 } 92 }
106 93
107 TEST(JsValueTest, BoolValue) 94 TEST(JsValueTest, BoolValue)
108 { 95 {
109 ThrowingErrorCallback errorCallback; 96 AdblockPlus::JsEngine jsEngine;
110 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback);
111 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("true"); 97 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("true");
112 ASSERT_FALSE(value->IsUndefined()); 98 ASSERT_FALSE(value->IsUndefined());
113 ASSERT_FALSE(value->IsNull()); 99 ASSERT_FALSE(value->IsNull());
114 ASSERT_FALSE(value->IsString()); 100 ASSERT_FALSE(value->IsString());
115 ASSERT_TRUE(value->IsBool()); 101 ASSERT_TRUE(value->IsBool());
116 ASSERT_FALSE(value->IsNumber()); 102 ASSERT_FALSE(value->IsNumber());
117 ASSERT_FALSE(value->IsObject()); 103 ASSERT_FALSE(value->IsObject());
118 ASSERT_FALSE(value->IsArray()); 104 ASSERT_FALSE(value->IsArray());
119 ASSERT_FALSE(value->IsFunction()); 105 ASSERT_FALSE(value->IsFunction());
120 ASSERT_EQ("true", value->AsString()); 106 ASSERT_EQ("true", value->AsString());
121 ASSERT_TRUE(value->AsBool()); 107 ASSERT_TRUE(value->AsBool());
122 ASSERT_ANY_THROW(value->AsList()); 108 ASSERT_ANY_THROW(value->AsList());
123 ASSERT_ANY_THROW(value->GetProperty("foo")); 109 ASSERT_ANY_THROW(value->GetProperty("foo"));
124 ASSERT_ANY_THROW(value->SetProperty("foo", false)); 110 ASSERT_ANY_THROW(value->SetProperty("foo", false));
125 ASSERT_ANY_THROW(value->GetClassName()); 111 ASSERT_ANY_THROW(value->GetClassName());
126 ASSERT_ANY_THROW(value->GetOwnPropertyNames()); 112 ASSERT_ANY_THROW(value->GetOwnPropertyNames());
127 ASSERT_ANY_THROW(value->Call()); 113 ASSERT_ANY_THROW(value->Call());
128 } 114 }
129 115
130 TEST(JsValueTest, ObjectValue) 116 TEST(JsValueTest, ObjectValue)
131 { 117 {
132 ThrowingErrorCallback errorCallback; 118 AdblockPlus::JsEngine jsEngine;
133 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback);
134 const std::string source("\ 119 const std::string source("\
135 function Foo() {\ 120 function Foo() {\
136 this.x = 2;\ 121 this.x = 2;\
137 this.toString = function() {return 'foo';};\ 122 this.toString = function() {return 'foo';};\
138 this.valueOf = function() {return 123;};\ 123 this.valueOf = function() {return 123;};\
139 };\ 124 };\
140 new Foo()"); 125 new Foo()");
141 AdblockPlus::JsValuePtr value = jsEngine.Evaluate(source); 126 AdblockPlus::JsValuePtr value = jsEngine.Evaluate(source);
142 ASSERT_FALSE(value->IsUndefined()); 127 ASSERT_FALSE(value->IsUndefined());
143 ASSERT_FALSE(value->IsNull()); 128 ASSERT_FALSE(value->IsNull());
(...skipping 12 matching lines...) Expand all
156 ASSERT_EQ(12, value->GetProperty("x")->AsInt()); 141 ASSERT_EQ(12, value->GetProperty("x")->AsInt());
157 value->SetProperty("x", jsEngine.NewValue(15)); 142 value->SetProperty("x", jsEngine.NewValue(15));
158 ASSERT_EQ(15, value->GetProperty("x")->AsInt()); 143 ASSERT_EQ(15, value->GetProperty("x")->AsInt());
159 ASSERT_EQ("Foo", value->GetClassName()); 144 ASSERT_EQ("Foo", value->GetClassName());
160 ASSERT_EQ(3u, value->GetOwnPropertyNames().size()); 145 ASSERT_EQ(3u, value->GetOwnPropertyNames().size());
161 ASSERT_ANY_THROW(value->Call()); 146 ASSERT_ANY_THROW(value->Call());
162 } 147 }
163 148
164 TEST(JsValueTest, ArrayValue) 149 TEST(JsValueTest, ArrayValue)
165 { 150 {
166 ThrowingErrorCallback errorCallback; 151 AdblockPlus::JsEngine jsEngine;
167 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback);
168 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("[5,8,12]"); 152 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("[5,8,12]");
169 ASSERT_FALSE(value->IsUndefined()); 153 ASSERT_FALSE(value->IsUndefined());
170 ASSERT_FALSE(value->IsNull()); 154 ASSERT_FALSE(value->IsNull());
171 ASSERT_FALSE(value->IsString()); 155 ASSERT_FALSE(value->IsString());
172 ASSERT_FALSE(value->IsBool()); 156 ASSERT_FALSE(value->IsBool());
173 ASSERT_FALSE(value->IsNumber()); 157 ASSERT_FALSE(value->IsNumber());
174 ASSERT_TRUE(value->IsObject()); 158 ASSERT_TRUE(value->IsObject());
175 ASSERT_TRUE(value->IsArray()); 159 ASSERT_TRUE(value->IsArray());
176 ASSERT_FALSE(value->IsFunction()); 160 ASSERT_FALSE(value->IsFunction());
177 ASSERT_EQ("5,8,12", value->AsString()); 161 ASSERT_EQ("5,8,12", value->AsString());
178 ASSERT_TRUE(value->AsBool()); 162 ASSERT_TRUE(value->AsBool());
179 ASSERT_EQ(3u, value->AsList().size()); 163 ASSERT_EQ(3u, value->AsList().size());
180 ASSERT_EQ(8, value->AsList()[1]->AsInt()); 164 ASSERT_EQ(8, value->AsList()[1]->AsInt());
181 ASSERT_EQ(3, value->GetProperty("length")->AsInt()); 165 ASSERT_EQ(3, value->GetProperty("length")->AsInt());
182 ASSERT_EQ("Array", value->GetClassName()); 166 ASSERT_EQ("Array", value->GetClassName());
183 ASSERT_ANY_THROW(value->Call()); 167 ASSERT_ANY_THROW(value->Call());
184 } 168 }
185 169
186 TEST(JsValueTest, FunctionValue) 170 TEST(JsValueTest, FunctionValue)
187 { 171 {
188 ThrowingErrorCallback errorCallback; 172 AdblockPlus::JsEngine jsEngine;
189 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback);
190 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("(function(foo, bar) {return this.x + '/' + foo + '/' + bar;})"); 173 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("(function(foo, bar) {return this.x + '/' + foo + '/' + bar;})");
191 ASSERT_FALSE(value->IsUndefined()); 174 ASSERT_FALSE(value->IsUndefined());
192 ASSERT_FALSE(value->IsNull()); 175 ASSERT_FALSE(value->IsNull());
193 ASSERT_FALSE(value->IsString()); 176 ASSERT_FALSE(value->IsString());
194 ASSERT_FALSE(value->IsBool()); 177 ASSERT_FALSE(value->IsBool());
195 ASSERT_FALSE(value->IsNumber()); 178 ASSERT_FALSE(value->IsNumber());
196 ASSERT_TRUE(value->IsObject()); 179 ASSERT_TRUE(value->IsObject());
197 ASSERT_FALSE(value->IsArray()); 180 ASSERT_FALSE(value->IsArray());
198 ASSERT_TRUE(value->IsFunction()); 181 ASSERT_TRUE(value->IsFunction());
199 ASSERT_TRUE(value->AsBool()); 182 ASSERT_TRUE(value->AsBool());
200 ASSERT_ANY_THROW(value->AsList()); 183 ASSERT_ANY_THROW(value->AsList());
201 ASSERT_EQ(2, value->GetProperty("length")->AsInt()); 184 ASSERT_EQ(2, value->GetProperty("length")->AsInt());
202 185
203 AdblockPlus::JsValuePtr thisPtr = jsEngine.Evaluate("({x:2})"); 186 AdblockPlus::JsValuePtr thisPtr = jsEngine.Evaluate("({x:2})");
204 AdblockPlus::JsValueList params; 187 AdblockPlus::JsValueList params;
205 params.push_back(jsEngine.NewValue(5)); 188 params.push_back(jsEngine.NewValue(5));
206 params.push_back(jsEngine.NewValue("xyz")); 189 params.push_back(jsEngine.NewValue("xyz"));
207 ASSERT_EQ("2/5/xyz", value->Call(params, thisPtr)->AsString()); 190 ASSERT_EQ("2/5/xyz", value->Call(params, thisPtr)->AsString());
208 } 191 }
209 192
210 TEST(JsValueTest, ThrowingCoversion) 193 TEST(JsValueTest, ThrowingCoversion)
211 { 194 {
212 ThrowingErrorCallback errorCallback; 195 AdblockPlus::JsEngine jsEngine;
213 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback);
214 const std::string source("\ 196 const std::string source("\
215 function Foo() {\ 197 function Foo() {\
216 this.toString = function() {throw 'test1';};\ 198 this.toString = function() {throw 'test1';};\
217 this.valueOf = function() {throw 'test2';};\ 199 this.valueOf = function() {throw 'test2';};\
218 };\ 200 };\
219 new Foo()"); 201 new Foo()");
220 AdblockPlus::JsValuePtr value = jsEngine.Evaluate(source); 202 AdblockPlus::JsValuePtr value = jsEngine.Evaluate(source);
221 ASSERT_EQ("", value->AsString()); 203 ASSERT_EQ("", value->AsString());
222 ASSERT_EQ(0, value->AsInt()); 204 ASSERT_EQ(0, value->AsInt());
223 } 205 }
OLDNEW
« src/JsEngine.cpp ('K') | « test/JsEngine.cpp ('k') | test/WebRequest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld