Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(379)

Side by Side Diff: net/proxy/proxy_resolver_v8_unittest.cc

Issue 2109503009: Refactor net tests to use GMock matchers for checking net::Error results (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Revert changes to contents.txt files Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/compiler_specific.h" 5 #include "base/compiler_specific.h"
6 #include "base/files/file_util.h" 6 #include "base/files/file_util.h"
7 #include "base/path_service.h" 7 #include "base/path_service.h"
8 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "net/base/completion_callback.h" 11 #include "net/base/completion_callback.h"
12 #include "net/base/net_errors.h" 12 #include "net/base/net_errors.h"
13 #include "net/proxy/proxy_info.h" 13 #include "net/proxy/proxy_info.h"
14 #include "net/proxy/proxy_resolver_script_data.h" 14 #include "net/proxy/proxy_resolver_script_data.h"
15 #include "net/proxy/proxy_resolver_v8.h" 15 #include "net/proxy/proxy_resolver_v8.h"
16 #include "net/test/gtest_util.h"
17 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/gurl.h" 19 #include "url/gurl.h"
18 20
21 using net::test::IsError;
22 using net::test::IsOk;
23
19 namespace net { 24 namespace net {
20 namespace { 25 namespace {
21 26
22 // Javascript bindings for ProxyResolverV8, which returns mock values. 27 // Javascript bindings for ProxyResolverV8, which returns mock values.
23 // Each time one of the bindings is called into, we push the input into a 28 // Each time one of the bindings is called into, we push the input into a
24 // list, for later verification. 29 // list, for later verification.
25 class MockJSBindings : public ProxyResolverV8::JSBindings { 30 class MockJSBindings : public ProxyResolverV8::JSBindings {
26 public: 31 public:
27 MockJSBindings() 32 MockJSBindings()
28 : my_ip_address_count(0), 33 : my_ip_address_count(0),
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 private: 138 private:
134 MockJSBindings js_bindings_; 139 MockJSBindings js_bindings_;
135 std::unique_ptr<ProxyResolverV8> resolver_; 140 std::unique_ptr<ProxyResolverV8> resolver_;
136 }; 141 };
137 142
138 // Doesn't really matter what these values are for many of the tests. 143 // Doesn't really matter what these values are for many of the tests.
139 const GURL kQueryUrl("http://www.google.com"); 144 const GURL kQueryUrl("http://www.google.com");
140 const GURL kPacUrl; 145 const GURL kPacUrl;
141 146
142 TEST_F(ProxyResolverV8Test, Direct) { 147 TEST_F(ProxyResolverV8Test, Direct) {
143 ASSERT_EQ(OK, CreateResolver("direct.js")); 148 ASSERT_THAT(CreateResolver("direct.js"), IsOk());
144 149
145 ProxyInfo proxy_info; 150 ProxyInfo proxy_info;
146 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); 151 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings());
147 152
148 EXPECT_EQ(OK, result); 153 EXPECT_THAT(result, IsOk());
149 EXPECT_TRUE(proxy_info.is_direct()); 154 EXPECT_TRUE(proxy_info.is_direct());
150 155
151 EXPECT_EQ(0U, bindings()->alerts.size()); 156 EXPECT_EQ(0U, bindings()->alerts.size());
152 EXPECT_EQ(0U, bindings()->errors.size()); 157 EXPECT_EQ(0U, bindings()->errors.size());
153 } 158 }
154 159
155 TEST_F(ProxyResolverV8Test, ReturnEmptyString) { 160 TEST_F(ProxyResolverV8Test, ReturnEmptyString) {
156 ASSERT_EQ(OK, CreateResolver("return_empty_string.js")); 161 ASSERT_THAT(CreateResolver("return_empty_string.js"), IsOk());
157 162
158 ProxyInfo proxy_info; 163 ProxyInfo proxy_info;
159 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); 164 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings());
160 165
161 EXPECT_EQ(OK, result); 166 EXPECT_THAT(result, IsOk());
162 EXPECT_TRUE(proxy_info.is_direct()); 167 EXPECT_TRUE(proxy_info.is_direct());
163 168
164 EXPECT_EQ(0U, bindings()->alerts.size()); 169 EXPECT_EQ(0U, bindings()->alerts.size());
165 EXPECT_EQ(0U, bindings()->errors.size()); 170 EXPECT_EQ(0U, bindings()->errors.size());
166 } 171 }
167 172
168 TEST_F(ProxyResolverV8Test, Basic) { 173 TEST_F(ProxyResolverV8Test, Basic) {
169 ASSERT_EQ(OK, CreateResolver("passthrough.js")); 174 ASSERT_THAT(CreateResolver("passthrough.js"), IsOk());
170 175
171 // The "FindProxyForURL" of this PAC script simply concatenates all of the 176 // The "FindProxyForURL" of this PAC script simply concatenates all of the
172 // arguments into a pseudo-host. The purpose of this test is to verify that 177 // arguments into a pseudo-host. The purpose of this test is to verify that
173 // the correct arguments are being passed to FindProxyForURL(). 178 // the correct arguments are being passed to FindProxyForURL().
174 { 179 {
175 ProxyInfo proxy_info; 180 ProxyInfo proxy_info;
176 int result = resolver().GetProxyForURL(GURL("http://query.com/path"), 181 int result = resolver().GetProxyForURL(GURL("http://query.com/path"),
177 &proxy_info, bindings()); 182 &proxy_info, bindings());
178 EXPECT_EQ(OK, result); 183 EXPECT_THAT(result, IsOk());
179 EXPECT_EQ("http.query.com.path.query.com:80", 184 EXPECT_EQ("http.query.com.path.query.com:80",
180 proxy_info.proxy_server().ToURI()); 185 proxy_info.proxy_server().ToURI());
181 } 186 }
182 { 187 {
183 ProxyInfo proxy_info; 188 ProxyInfo proxy_info;
184 int result = resolver().GetProxyForURL(GURL("ftp://query.com:90/path"), 189 int result = resolver().GetProxyForURL(GURL("ftp://query.com:90/path"),
185 &proxy_info, bindings()); 190 &proxy_info, bindings());
186 EXPECT_EQ(OK, result); 191 EXPECT_THAT(result, IsOk());
187 // Note that FindProxyForURL(url, host) does not expect |host| to contain 192 // Note that FindProxyForURL(url, host) does not expect |host| to contain
188 // the port number. 193 // the port number.
189 EXPECT_EQ("ftp.query.com.90.path.query.com:80", 194 EXPECT_EQ("ftp.query.com.90.path.query.com:80",
190 proxy_info.proxy_server().ToURI()); 195 proxy_info.proxy_server().ToURI());
191 196
192 EXPECT_EQ(0U, bindings()->alerts.size()); 197 EXPECT_EQ(0U, bindings()->alerts.size());
193 EXPECT_EQ(0U, bindings()->errors.size()); 198 EXPECT_EQ(0U, bindings()->errors.size());
194 } 199 }
195 } 200 }
196 201
197 TEST_F(ProxyResolverV8Test, BadReturnType) { 202 TEST_F(ProxyResolverV8Test, BadReturnType) {
198 // These are the filenames of PAC scripts which each return a non-string 203 // These are the filenames of PAC scripts which each return a non-string
199 // types for FindProxyForURL(). They should all fail with 204 // types for FindProxyForURL(). They should all fail with
200 // ERR_PAC_SCRIPT_FAILED. 205 // ERR_PAC_SCRIPT_FAILED.
201 static const char* const filenames[] = { 206 static const char* const filenames[] = {
202 "return_undefined.js", 207 "return_undefined.js",
203 "return_integer.js", 208 "return_integer.js",
204 "return_function.js", 209 "return_function.js",
205 "return_object.js", 210 "return_object.js",
206 // TODO(eroman): Should 'null' be considered equivalent to "DIRECT" ? 211 // TODO(eroman): Should 'null' be considered equivalent to "DIRECT" ?
207 "return_null.js"}; 212 "return_null.js"};
208 213
209 for (size_t i = 0; i < arraysize(filenames); ++i) { 214 for (size_t i = 0; i < arraysize(filenames); ++i) {
210 ASSERT_EQ(OK, CreateResolver(filenames[i])); 215 ASSERT_THAT(CreateResolver(filenames[i]), IsOk());
211 216
212 MockJSBindings bindings; 217 MockJSBindings bindings;
213 ProxyInfo proxy_info; 218 ProxyInfo proxy_info;
214 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, &bindings); 219 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, &bindings);
215 220
216 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result); 221 EXPECT_THAT(result, IsError(ERR_PAC_SCRIPT_FAILED));
217 222
218 EXPECT_EQ(0U, bindings.alerts.size()); 223 EXPECT_EQ(0U, bindings.alerts.size());
219 ASSERT_EQ(1U, bindings.errors.size()); 224 ASSERT_EQ(1U, bindings.errors.size());
220 EXPECT_EQ("FindProxyForURL() did not return a string.", bindings.errors[0]); 225 EXPECT_EQ("FindProxyForURL() did not return a string.", bindings.errors[0]);
221 EXPECT_EQ(-1, bindings.errors_line_number[0]); 226 EXPECT_EQ(-1, bindings.errors_line_number[0]);
222 } 227 }
223 } 228 }
224 229
225 // Try using a PAC script which defines no "FindProxyForURL" function. 230 // Try using a PAC script which defines no "FindProxyForURL" function.
226 TEST_F(ProxyResolverV8Test, NoEntryPoint) { 231 TEST_F(ProxyResolverV8Test, NoEntryPoint) {
227 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, CreateResolver("no_entrypoint.js")); 232 EXPECT_THAT(CreateResolver("no_entrypoint.js"),
233 IsError(ERR_PAC_SCRIPT_FAILED));
228 234
229 ASSERT_EQ(1U, bindings()->errors.size()); 235 ASSERT_EQ(1U, bindings()->errors.size());
230 EXPECT_EQ("FindProxyForURL is undefined or not a function.", 236 EXPECT_EQ("FindProxyForURL is undefined or not a function.",
231 bindings()->errors[0]); 237 bindings()->errors[0]);
232 EXPECT_EQ(-1, bindings()->errors_line_number[0]); 238 EXPECT_EQ(-1, bindings()->errors_line_number[0]);
233 } 239 }
234 240
235 // Try loading a malformed PAC script. 241 // Try loading a malformed PAC script.
236 TEST_F(ProxyResolverV8Test, ParseError) { 242 TEST_F(ProxyResolverV8Test, ParseError) {
237 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, CreateResolver("missing_close_brace.js")); 243 EXPECT_THAT(CreateResolver("missing_close_brace.js"),
244 IsError(ERR_PAC_SCRIPT_FAILED));
238 245
239 EXPECT_EQ(0U, bindings()->alerts.size()); 246 EXPECT_EQ(0U, bindings()->alerts.size());
240 247
241 // We get one error during compilation. 248 // We get one error during compilation.
242 ASSERT_EQ(1U, bindings()->errors.size()); 249 ASSERT_EQ(1U, bindings()->errors.size());
243 250
244 EXPECT_EQ("Uncaught SyntaxError: Unexpected end of input", 251 EXPECT_EQ("Uncaught SyntaxError: Unexpected end of input",
245 bindings()->errors[0]); 252 bindings()->errors[0]);
246 EXPECT_EQ(5, bindings()->errors_line_number[0]); 253 EXPECT_EQ(5, bindings()->errors_line_number[0]);
247 } 254 }
248 255
249 // Run a PAC script several times, which has side-effects. 256 // Run a PAC script several times, which has side-effects.
250 TEST_F(ProxyResolverV8Test, SideEffects) { 257 TEST_F(ProxyResolverV8Test, SideEffects) {
251 ASSERT_EQ(OK, CreateResolver("side_effects.js")); 258 ASSERT_THAT(CreateResolver("side_effects.js"), IsOk());
252 259
253 // The PAC script increments a counter each time we invoke it. 260 // The PAC script increments a counter each time we invoke it.
254 for (int i = 0; i < 3; ++i) { 261 for (int i = 0; i < 3; ++i) {
255 ProxyInfo proxy_info; 262 ProxyInfo proxy_info;
256 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); 263 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings());
257 EXPECT_EQ(OK, result); 264 EXPECT_THAT(result, IsOk());
258 EXPECT_EQ(base::StringPrintf("sideffect_%d:80", i), 265 EXPECT_EQ(base::StringPrintf("sideffect_%d:80", i),
259 proxy_info.proxy_server().ToURI()); 266 proxy_info.proxy_server().ToURI());
260 } 267 }
261 268
262 // Reload the script -- the javascript environment should be reset, hence 269 // Reload the script -- the javascript environment should be reset, hence
263 // the counter starts over. 270 // the counter starts over.
264 ASSERT_EQ(OK, CreateResolver("side_effects.js")); 271 ASSERT_THAT(CreateResolver("side_effects.js"), IsOk());
265 272
266 for (int i = 0; i < 3; ++i) { 273 for (int i = 0; i < 3; ++i) {
267 ProxyInfo proxy_info; 274 ProxyInfo proxy_info;
268 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); 275 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings());
269 EXPECT_EQ(OK, result); 276 EXPECT_THAT(result, IsOk());
270 EXPECT_EQ(base::StringPrintf("sideffect_%d:80", i), 277 EXPECT_EQ(base::StringPrintf("sideffect_%d:80", i),
271 proxy_info.proxy_server().ToURI()); 278 proxy_info.proxy_server().ToURI());
272 } 279 }
273 } 280 }
274 281
275 // Execute a PAC script which throws an exception in FindProxyForURL. 282 // Execute a PAC script which throws an exception in FindProxyForURL.
276 TEST_F(ProxyResolverV8Test, UnhandledException) { 283 TEST_F(ProxyResolverV8Test, UnhandledException) {
277 ASSERT_EQ(OK, CreateResolver("unhandled_exception.js")); 284 ASSERT_THAT(CreateResolver("unhandled_exception.js"), IsOk());
278 285
279 ProxyInfo proxy_info; 286 ProxyInfo proxy_info;
280 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); 287 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings());
281 288
282 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result); 289 EXPECT_THAT(result, IsError(ERR_PAC_SCRIPT_FAILED));
283 290
284 EXPECT_EQ(0U, bindings()->alerts.size()); 291 EXPECT_EQ(0U, bindings()->alerts.size());
285 ASSERT_EQ(1U, bindings()->errors.size()); 292 ASSERT_EQ(1U, bindings()->errors.size());
286 EXPECT_EQ("Uncaught ReferenceError: undefined_variable is not defined", 293 EXPECT_EQ("Uncaught ReferenceError: undefined_variable is not defined",
287 bindings()->errors[0]); 294 bindings()->errors[0]);
288 EXPECT_EQ(3, bindings()->errors_line_number[0]); 295 EXPECT_EQ(3, bindings()->errors_line_number[0]);
289 } 296 }
290 297
291 // Execute a PAC script which throws an exception when first accessing 298 // Execute a PAC script which throws an exception when first accessing
292 // FindProxyForURL 299 // FindProxyForURL
293 TEST_F(ProxyResolverV8Test, ExceptionAccessingFindProxyForURLDuringInit) { 300 TEST_F(ProxyResolverV8Test, ExceptionAccessingFindProxyForURLDuringInit) {
294 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, 301 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED,
295 CreateResolver("exception_findproxyforurl_during_init.js")); 302 CreateResolver("exception_findproxyforurl_during_init.js"));
296 303
297 ASSERT_EQ(2U, bindings()->errors.size()); 304 ASSERT_EQ(2U, bindings()->errors.size());
298 EXPECT_EQ("Uncaught crash!", bindings()->errors[0]); 305 EXPECT_EQ("Uncaught crash!", bindings()->errors[0]);
299 EXPECT_EQ(9, bindings()->errors_line_number[0]); 306 EXPECT_EQ(9, bindings()->errors_line_number[0]);
300 EXPECT_EQ("Accessing FindProxyForURL threw an exception.", 307 EXPECT_EQ("Accessing FindProxyForURL threw an exception.",
301 bindings()->errors[1]); 308 bindings()->errors[1]);
302 EXPECT_EQ(-1, bindings()->errors_line_number[1]); 309 EXPECT_EQ(-1, bindings()->errors_line_number[1]);
303 } 310 }
304 311
305 // Execute a PAC script which throws an exception during the second access to 312 // Execute a PAC script which throws an exception during the second access to
306 // FindProxyForURL 313 // FindProxyForURL
307 TEST_F(ProxyResolverV8Test, ExceptionAccessingFindProxyForURLDuringResolve) { 314 TEST_F(ProxyResolverV8Test, ExceptionAccessingFindProxyForURLDuringResolve) {
308 ASSERT_EQ(OK, CreateResolver("exception_findproxyforurl_during_resolve.js")); 315 ASSERT_THAT(CreateResolver("exception_findproxyforurl_during_resolve.js"),
316 IsOk());
309 317
310 ProxyInfo proxy_info; 318 ProxyInfo proxy_info;
311 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); 319 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings());
312 320
313 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result); 321 EXPECT_THAT(result, IsError(ERR_PAC_SCRIPT_FAILED));
314 322
315 ASSERT_EQ(2U, bindings()->errors.size()); 323 ASSERT_EQ(2U, bindings()->errors.size());
316 EXPECT_EQ("Uncaught crash!", bindings()->errors[0]); 324 EXPECT_EQ("Uncaught crash!", bindings()->errors[0]);
317 EXPECT_EQ(17, bindings()->errors_line_number[0]); 325 EXPECT_EQ(17, bindings()->errors_line_number[0]);
318 EXPECT_EQ("Accessing FindProxyForURL threw an exception.", 326 EXPECT_EQ("Accessing FindProxyForURL threw an exception.",
319 bindings()->errors[1]); 327 bindings()->errors[1]);
320 EXPECT_EQ(-1, bindings()->errors_line_number[1]); 328 EXPECT_EQ(-1, bindings()->errors_line_number[1]);
321 } 329 }
322 330
323 TEST_F(ProxyResolverV8Test, ReturnUnicode) { 331 TEST_F(ProxyResolverV8Test, ReturnUnicode) {
324 ASSERT_EQ(OK, CreateResolver("return_unicode.js")); 332 ASSERT_THAT(CreateResolver("return_unicode.js"), IsOk());
325 333
326 ProxyInfo proxy_info; 334 ProxyInfo proxy_info;
327 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); 335 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings());
328 336
329 // The result from this resolve was unparseable, because it 337 // The result from this resolve was unparseable, because it
330 // wasn't ASCII. 338 // wasn't ASCII.
331 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result); 339 EXPECT_THAT(result, IsError(ERR_PAC_SCRIPT_FAILED));
332 } 340 }
333 341
334 // Test the PAC library functions that we expose in the JS environment. 342 // Test the PAC library functions that we expose in the JS environment.
335 TEST_F(ProxyResolverV8Test, JavascriptLibrary) { 343 TEST_F(ProxyResolverV8Test, JavascriptLibrary) {
336 ASSERT_EQ(OK, CreateResolver("pac_library_unittest.js")); 344 ASSERT_THAT(CreateResolver("pac_library_unittest.js"), IsOk());
337 345
338 ProxyInfo proxy_info; 346 ProxyInfo proxy_info;
339 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); 347 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings());
340 348
341 // If the javascript side of this unit-test fails, it will throw a javascript 349 // If the javascript side of this unit-test fails, it will throw a javascript
342 // exception. Otherwise it will return "PROXY success:80". 350 // exception. Otherwise it will return "PROXY success:80".
343 EXPECT_EQ(OK, result); 351 EXPECT_THAT(result, IsOk());
344 EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI()); 352 EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI());
345 353
346 EXPECT_EQ(0U, bindings()->alerts.size()); 354 EXPECT_EQ(0U, bindings()->alerts.size());
347 EXPECT_EQ(0U, bindings()->errors.size()); 355 EXPECT_EQ(0U, bindings()->errors.size());
348 } 356 }
349 357
350 // Test marshalling/un-marshalling of values between C++/V8. 358 // Test marshalling/un-marshalling of values between C++/V8.
351 TEST_F(ProxyResolverV8Test, V8Bindings) { 359 TEST_F(ProxyResolverV8Test, V8Bindings) {
352 ASSERT_EQ(OK, CreateResolver("bindings.js")); 360 ASSERT_THAT(CreateResolver("bindings.js"), IsOk());
353 bindings()->dns_resolve_result = "127.0.0.1"; 361 bindings()->dns_resolve_result = "127.0.0.1";
354 362
355 ProxyInfo proxy_info; 363 ProxyInfo proxy_info;
356 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); 364 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings());
357 365
358 EXPECT_EQ(OK, result); 366 EXPECT_THAT(result, IsOk());
359 EXPECT_TRUE(proxy_info.is_direct()); 367 EXPECT_TRUE(proxy_info.is_direct());
360 368
361 EXPECT_EQ(0U, bindings()->errors.size()); 369 EXPECT_EQ(0U, bindings()->errors.size());
362 370
363 // Alert was called 5 times. 371 // Alert was called 5 times.
364 ASSERT_EQ(5U, bindings()->alerts.size()); 372 ASSERT_EQ(5U, bindings()->alerts.size());
365 EXPECT_EQ("undefined", bindings()->alerts[0]); 373 EXPECT_EQ("undefined", bindings()->alerts[0]);
366 EXPECT_EQ("null", bindings()->alerts[1]); 374 EXPECT_EQ("null", bindings()->alerts[1]);
367 EXPECT_EQ("undefined", bindings()->alerts[2]); 375 EXPECT_EQ("undefined", bindings()->alerts[2]);
368 EXPECT_EQ("[object Object]", bindings()->alerts[3]); 376 EXPECT_EQ("[object Object]", bindings()->alerts[3]);
(...skipping 13 matching lines...) Expand all
382 390
383 // DnsResolveEx was called 2 times. 391 // DnsResolveEx was called 2 times.
384 ASSERT_EQ(2U, bindings()->dns_resolves_ex.size()); 392 ASSERT_EQ(2U, bindings()->dns_resolves_ex.size());
385 EXPECT_EQ("is_resolvable", bindings()->dns_resolves_ex[0]); 393 EXPECT_EQ("is_resolvable", bindings()->dns_resolves_ex[0]);
386 EXPECT_EQ("foobar", bindings()->dns_resolves_ex[1]); 394 EXPECT_EQ("foobar", bindings()->dns_resolves_ex[1]);
387 } 395 }
388 396
389 // Test calling a binding (myIpAddress()) from the script's global scope. 397 // Test calling a binding (myIpAddress()) from the script's global scope.
390 // http://crbug.com/40026 398 // http://crbug.com/40026
391 TEST_F(ProxyResolverV8Test, BindingCalledDuringInitialization) { 399 TEST_F(ProxyResolverV8Test, BindingCalledDuringInitialization) {
392 ASSERT_EQ(OK, CreateResolver("binding_from_global.js")); 400 ASSERT_THAT(CreateResolver("binding_from_global.js"), IsOk());
393 401
394 // myIpAddress() got called during initialization of the script. 402 // myIpAddress() got called during initialization of the script.
395 EXPECT_EQ(1, bindings()->my_ip_address_count); 403 EXPECT_EQ(1, bindings()->my_ip_address_count);
396 404
397 ProxyInfo proxy_info; 405 ProxyInfo proxy_info;
398 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); 406 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings());
399 407
400 EXPECT_EQ(OK, result); 408 EXPECT_THAT(result, IsOk());
401 EXPECT_FALSE(proxy_info.is_direct()); 409 EXPECT_FALSE(proxy_info.is_direct());
402 EXPECT_EQ("127.0.0.1:80", proxy_info.proxy_server().ToURI()); 410 EXPECT_EQ("127.0.0.1:80", proxy_info.proxy_server().ToURI());
403 411
404 // Check that no other bindings were called. 412 // Check that no other bindings were called.
405 EXPECT_EQ(0U, bindings()->errors.size()); 413 EXPECT_EQ(0U, bindings()->errors.size());
406 ASSERT_EQ(0U, bindings()->alerts.size()); 414 ASSERT_EQ(0U, bindings()->alerts.size());
407 ASSERT_EQ(0U, bindings()->dns_resolves.size()); 415 ASSERT_EQ(0U, bindings()->dns_resolves.size());
408 EXPECT_EQ(0, bindings()->my_ip_address_ex_count); 416 EXPECT_EQ(0, bindings()->my_ip_address_ex_count);
409 ASSERT_EQ(0U, bindings()->dns_resolves_ex.size()); 417 ASSERT_EQ(0U, bindings()->dns_resolves_ex.size());
410 } 418 }
411 419
412 // Try loading a PAC script that ends with a comment and has no terminal 420 // Try loading a PAC script that ends with a comment and has no terminal
413 // newline. This should not cause problems with the PAC utility functions 421 // newline. This should not cause problems with the PAC utility functions
414 // that we add to the script's environment. 422 // that we add to the script's environment.
415 // http://crbug.com/22864 423 // http://crbug.com/22864
416 TEST_F(ProxyResolverV8Test, EndsWithCommentNoNewline) { 424 TEST_F(ProxyResolverV8Test, EndsWithCommentNoNewline) {
417 ASSERT_EQ(OK, CreateResolver("ends_with_comment.js")); 425 ASSERT_THAT(CreateResolver("ends_with_comment.js"), IsOk());
418 426
419 ProxyInfo proxy_info; 427 ProxyInfo proxy_info;
420 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); 428 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings());
421 429
422 EXPECT_EQ(OK, result); 430 EXPECT_THAT(result, IsOk());
423 EXPECT_FALSE(proxy_info.is_direct()); 431 EXPECT_FALSE(proxy_info.is_direct());
424 EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI()); 432 EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI());
425 } 433 }
426 434
427 // Try loading a PAC script that ends with a statement and has no terminal 435 // Try loading a PAC script that ends with a statement and has no terminal
428 // newline. This should not cause problems with the PAC utility functions 436 // newline. This should not cause problems with the PAC utility functions
429 // that we add to the script's environment. 437 // that we add to the script's environment.
430 // http://crbug.com/22864 438 // http://crbug.com/22864
431 TEST_F(ProxyResolverV8Test, EndsWithStatementNoNewline) { 439 TEST_F(ProxyResolverV8Test, EndsWithStatementNoNewline) {
432 ASSERT_EQ(OK, CreateResolver("ends_with_statement_no_semicolon.js")); 440 ASSERT_THAT(CreateResolver("ends_with_statement_no_semicolon.js"), IsOk());
433 441
434 ProxyInfo proxy_info; 442 ProxyInfo proxy_info;
435 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); 443 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings());
436 444
437 EXPECT_EQ(OK, result); 445 EXPECT_THAT(result, IsOk());
438 EXPECT_FALSE(proxy_info.is_direct()); 446 EXPECT_FALSE(proxy_info.is_direct());
439 EXPECT_EQ("success:3", proxy_info.proxy_server().ToURI()); 447 EXPECT_EQ("success:3", proxy_info.proxy_server().ToURI());
440 } 448 }
441 449
442 // Test the return values from myIpAddress(), myIpAddressEx(), dnsResolve(), 450 // Test the return values from myIpAddress(), myIpAddressEx(), dnsResolve(),
443 // dnsResolveEx(), isResolvable(), isResolvableEx(), when the the binding 451 // dnsResolveEx(), isResolvable(), isResolvableEx(), when the the binding
444 // returns empty string (failure). This simulates the return values from 452 // returns empty string (failure). This simulates the return values from
445 // those functions when the underlying DNS resolution fails. 453 // those functions when the underlying DNS resolution fails.
446 TEST_F(ProxyResolverV8Test, DNSResolutionFailure) { 454 TEST_F(ProxyResolverV8Test, DNSResolutionFailure) {
447 ASSERT_EQ(OK, CreateResolver("dns_fail.js")); 455 ASSERT_THAT(CreateResolver("dns_fail.js"), IsOk());
448 456
449 ProxyInfo proxy_info; 457 ProxyInfo proxy_info;
450 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); 458 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings());
451 459
452 EXPECT_EQ(OK, result); 460 EXPECT_THAT(result, IsOk());
453 EXPECT_FALSE(proxy_info.is_direct()); 461 EXPECT_FALSE(proxy_info.is_direct());
454 EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI()); 462 EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI());
455 } 463 }
456 464
457 TEST_F(ProxyResolverV8Test, DNSResolutionOfInternationDomainName) { 465 TEST_F(ProxyResolverV8Test, DNSResolutionOfInternationDomainName) {
458 ASSERT_EQ(OK, CreateResolver("international_domain_names.js")); 466 ASSERT_THAT(CreateResolver("international_domain_names.js"), IsOk());
459 467
460 // Execute FindProxyForURL(). 468 // Execute FindProxyForURL().
461 ProxyInfo proxy_info; 469 ProxyInfo proxy_info;
462 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); 470 int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings());
463 471
464 EXPECT_EQ(OK, result); 472 EXPECT_THAT(result, IsOk());
465 EXPECT_TRUE(proxy_info.is_direct()); 473 EXPECT_TRUE(proxy_info.is_direct());
466 474
467 // Check that the international domain name was converted to punycode 475 // Check that the international domain name was converted to punycode
468 // before passing it onto the bindings layer. 476 // before passing it onto the bindings layer.
469 ASSERT_EQ(1u, bindings()->dns_resolves.size()); 477 ASSERT_EQ(1u, bindings()->dns_resolves.size());
470 EXPECT_EQ("xn--bcher-kva.ch", bindings()->dns_resolves[0]); 478 EXPECT_EQ("xn--bcher-kva.ch", bindings()->dns_resolves[0]);
471 479
472 ASSERT_EQ(1u, bindings()->dns_resolves_ex.size()); 480 ASSERT_EQ(1u, bindings()->dns_resolves_ex.size());
473 EXPECT_EQ("xn--bcher-kva.ch", bindings()->dns_resolves_ex[0]); 481 EXPECT_EQ("xn--bcher-kva.ch", bindings()->dns_resolves_ex[0]);
474 } 482 }
475 483
476 // Test that when resolving a URL which contains an IPv6 string literal, the 484 // Test that when resolving a URL which contains an IPv6 string literal, the
477 // brackets are removed from the host before passing it down to the PAC script. 485 // brackets are removed from the host before passing it down to the PAC script.
478 // If we don't do this, then subsequent calls to dnsResolveEx(host) will be 486 // If we don't do this, then subsequent calls to dnsResolveEx(host) will be
479 // doomed to fail since it won't correspond with a valid name. 487 // doomed to fail since it won't correspond with a valid name.
480 TEST_F(ProxyResolverV8Test, IPv6HostnamesNotBracketed) { 488 TEST_F(ProxyResolverV8Test, IPv6HostnamesNotBracketed) {
481 ASSERT_EQ(OK, CreateResolver("resolve_host.js")); 489 ASSERT_THAT(CreateResolver("resolve_host.js"), IsOk());
482 490
483 ProxyInfo proxy_info; 491 ProxyInfo proxy_info;
484 int result = resolver().GetProxyForURL( 492 int result = resolver().GetProxyForURL(
485 GURL("http://[abcd::efff]:99/watsupdawg"), &proxy_info, bindings()); 493 GURL("http://[abcd::efff]:99/watsupdawg"), &proxy_info, bindings());
486 494
487 EXPECT_EQ(OK, result); 495 EXPECT_THAT(result, IsOk());
488 EXPECT_TRUE(proxy_info.is_direct()); 496 EXPECT_TRUE(proxy_info.is_direct());
489 497
490 // We called dnsResolveEx() exactly once, by passing through the "host" 498 // We called dnsResolveEx() exactly once, by passing through the "host"
491 // argument to FindProxyForURL(). The brackets should have been stripped. 499 // argument to FindProxyForURL(). The brackets should have been stripped.
492 ASSERT_EQ(1U, bindings()->dns_resolves_ex.size()); 500 ASSERT_EQ(1U, bindings()->dns_resolves_ex.size());
493 EXPECT_EQ("abcd::efff", bindings()->dns_resolves_ex[0]); 501 EXPECT_EQ("abcd::efff", bindings()->dns_resolves_ex[0]);
494 } 502 }
495 503
496 // Test that terminating a script within DnsResolve() leads to eventual 504 // Test that terminating a script within DnsResolve() leads to eventual
497 // termination of the script. Also test that repeatedly calling terminate is 505 // termination of the script. Also test that repeatedly calling terminate is
498 // safe, and running the script again after termination still works. 506 // safe, and running the script again after termination still works.
499 TEST_F(ProxyResolverV8Test, Terminate) { 507 TEST_F(ProxyResolverV8Test, Terminate) {
500 ASSERT_EQ(OK, CreateResolver("terminate.js")); 508 ASSERT_THAT(CreateResolver("terminate.js"), IsOk());
501 509
502 // Terminate script execution upon reaching dnsResolve(). Note that 510 // Terminate script execution upon reaching dnsResolve(). Note that
503 // termination may not take effect right away (so the subsequent dnsResolve() 511 // termination may not take effect right away (so the subsequent dnsResolve()
504 // and alert() may be run). 512 // and alert() may be run).
505 bindings()->should_terminate = true; 513 bindings()->should_terminate = true;
506 514
507 ProxyInfo proxy_info; 515 ProxyInfo proxy_info;
508 int result = 516 int result =
509 resolver().GetProxyForURL(GURL("http://hang/"), &proxy_info, bindings()); 517 resolver().GetProxyForURL(GURL("http://hang/"), &proxy_info, bindings());
510 518
511 // The script execution was terminated. 519 // The script execution was terminated.
512 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result); 520 EXPECT_THAT(result, IsError(ERR_PAC_SCRIPT_FAILED));
513 521
514 EXPECT_EQ(1U, bindings()->dns_resolves.size()); 522 EXPECT_EQ(1U, bindings()->dns_resolves.size());
515 EXPECT_GE(2U, bindings()->dns_resolves_ex.size()); 523 EXPECT_GE(2U, bindings()->dns_resolves_ex.size());
516 EXPECT_GE(1U, bindings()->alerts.size()); 524 EXPECT_GE(1U, bindings()->alerts.size());
517 525
518 EXPECT_EQ(1U, bindings()->errors.size()); 526 EXPECT_EQ(1U, bindings()->errors.size());
519 527
520 // Termination shows up as an uncaught exception without any message. 528 // Termination shows up as an uncaught exception without any message.
521 EXPECT_EQ("", bindings()->errors[0]); 529 EXPECT_EQ("", bindings()->errors[0]);
522 530
523 bindings()->errors.clear(); 531 bindings()->errors.clear();
524 532
525 // Try running the script again, this time with a different input which won't 533 // Try running the script again, this time with a different input which won't
526 // cause a termination+hang. 534 // cause a termination+hang.
527 result = resolver().GetProxyForURL(GURL("http://kittens/"), &proxy_info, 535 result = resolver().GetProxyForURL(GURL("http://kittens/"), &proxy_info,
528 bindings()); 536 bindings());
529 537
530 EXPECT_EQ(OK, result); 538 EXPECT_THAT(result, IsOk());
531 EXPECT_EQ(0u, bindings()->errors.size()); 539 EXPECT_EQ(0u, bindings()->errors.size());
532 EXPECT_EQ("kittens:88", proxy_info.proxy_server().ToURI()); 540 EXPECT_EQ("kittens:88", proxy_info.proxy_server().ToURI());
533 } 541 }
534 542
535 } // namespace 543 } // namespace
536 } // namespace net 544 } // namespace net
OLDNEW
« no previous file with comments | « net/proxy/proxy_resolver_v8_tracing_wrapper_unittest.cc ('k') | net/proxy/proxy_script_decider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698