| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 bool ok = file_util::ReadFileToString(path, &file_contents); | 108 bool ok = file_util::ReadFileToString(path, &file_contents); |
| 109 | 109 |
| 110 // If we can't load the file from disk, something is misconfigured. | 110 // If we can't load the file from disk, something is misconfigured. |
| 111 if (!ok) { | 111 if (!ok) { |
| 112 LOG(ERROR) << "Failed to read file: " << path.value(); | 112 LOG(ERROR) << "Failed to read file: " << path.value(); |
| 113 return ERR_UNEXPECTED; | 113 return ERR_UNEXPECTED; |
| 114 } | 114 } |
| 115 | 115 |
| 116 // Load the PAC script into the ProxyResolver. | 116 // Load the PAC script into the ProxyResolver. |
| 117 return SetPacScript(ProxyResolverScriptData::FromUTF8(file_contents), | 117 return SetPacScript(ProxyResolverScriptData::FromUTF8(file_contents), |
| 118 NULL); | 118 CompletionCallback()); |
| 119 } | 119 } |
| 120 }; | 120 }; |
| 121 | 121 |
| 122 // Doesn't really matter what these values are for many of the tests. | 122 // Doesn't really matter what these values are for many of the tests. |
| 123 const GURL kQueryUrl("http://www.google.com"); | 123 const GURL kQueryUrl("http://www.google.com"); |
| 124 const GURL kPacUrl; | 124 const GURL kPacUrl; |
| 125 | 125 |
| 126 | 126 |
| 127 TEST(ProxyResolverV8Test, Direct) { | 127 TEST(ProxyResolverV8Test, Direct) { |
| 128 ProxyResolverV8WithMockBindings resolver; | 128 ProxyResolverV8WithMockBindings resolver; |
| 129 int result = resolver.SetPacScriptFromDisk("direct.js"); | 129 int result = resolver.SetPacScriptFromDisk("direct.js"); |
| 130 EXPECT_EQ(OK, result); | 130 EXPECT_EQ(OK, result); |
| 131 | 131 |
| 132 ProxyInfo proxy_info; | 132 ProxyInfo proxy_info; |
| 133 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); | 133 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 134 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 134 result = resolver.GetProxyForURL( |
| 135 log.bound()); | 135 kQueryUrl, &proxy_info, CompletionCallback(), NULL, log.bound()); |
| 136 | 136 |
| 137 EXPECT_EQ(OK, result); | 137 EXPECT_EQ(OK, result); |
| 138 EXPECT_TRUE(proxy_info.is_direct()); | 138 EXPECT_TRUE(proxy_info.is_direct()); |
| 139 | 139 |
| 140 EXPECT_EQ(0U, resolver.mock_js_bindings()->alerts.size()); | 140 EXPECT_EQ(0U, resolver.mock_js_bindings()->alerts.size()); |
| 141 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size()); | 141 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size()); |
| 142 | 142 |
| 143 net::CapturingNetLog::EntryList entries; | 143 net::CapturingNetLog::EntryList entries; |
| 144 log.GetEntries(&entries); | 144 log.GetEntries(&entries); |
| 145 // No bindings were called, so no log entries. | 145 // No bindings were called, so no log entries. |
| 146 EXPECT_EQ(0u, entries.size()); | 146 EXPECT_EQ(0u, entries.size()); |
| 147 } | 147 } |
| 148 | 148 |
| 149 TEST(ProxyResolverV8Test, ReturnEmptyString) { | 149 TEST(ProxyResolverV8Test, ReturnEmptyString) { |
| 150 ProxyResolverV8WithMockBindings resolver; | 150 ProxyResolverV8WithMockBindings resolver; |
| 151 int result = resolver.SetPacScriptFromDisk("return_empty_string.js"); | 151 int result = resolver.SetPacScriptFromDisk("return_empty_string.js"); |
| 152 EXPECT_EQ(OK, result); | 152 EXPECT_EQ(OK, result); |
| 153 | 153 |
| 154 ProxyInfo proxy_info; | 154 ProxyInfo proxy_info; |
| 155 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 155 result = resolver.GetProxyForURL( |
| 156 BoundNetLog()); | 156 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 157 | 157 |
| 158 EXPECT_EQ(OK, result); | 158 EXPECT_EQ(OK, result); |
| 159 EXPECT_TRUE(proxy_info.is_direct()); | 159 EXPECT_TRUE(proxy_info.is_direct()); |
| 160 | 160 |
| 161 EXPECT_EQ(0U, resolver.mock_js_bindings()->alerts.size()); | 161 EXPECT_EQ(0U, resolver.mock_js_bindings()->alerts.size()); |
| 162 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size()); | 162 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size()); |
| 163 } | 163 } |
| 164 | 164 |
| 165 TEST(ProxyResolverV8Test, Basic) { | 165 TEST(ProxyResolverV8Test, Basic) { |
| 166 ProxyResolverV8WithMockBindings resolver; | 166 ProxyResolverV8WithMockBindings resolver; |
| 167 int result = resolver.SetPacScriptFromDisk("passthrough.js"); | 167 int result = resolver.SetPacScriptFromDisk("passthrough.js"); |
| 168 EXPECT_EQ(OK, result); | 168 EXPECT_EQ(OK, result); |
| 169 | 169 |
| 170 // The "FindProxyForURL" of this PAC script simply concatenates all of the | 170 // The "FindProxyForURL" of this PAC script simply concatenates all of the |
| 171 // arguments into a pseudo-host. The purpose of this test is to verify that | 171 // arguments into a pseudo-host. The purpose of this test is to verify that |
| 172 // the correct arguments are being passed to FindProxyForURL(). | 172 // the correct arguments are being passed to FindProxyForURL(). |
| 173 { | 173 { |
| 174 ProxyInfo proxy_info; | 174 ProxyInfo proxy_info; |
| 175 result = resolver.GetProxyForURL(GURL("http://query.com/path"), | 175 result = resolver.GetProxyForURL(GURL("http://query.com/path"), &proxy_info, |
| 176 &proxy_info, NULL, NULL, BoundNetLog()); | 176 CompletionCallback(), NULL, BoundNetLog()); |
| 177 EXPECT_EQ(OK, result); | 177 EXPECT_EQ(OK, result); |
| 178 EXPECT_EQ("http.query.com.path.query.com:80", | 178 EXPECT_EQ("http.query.com.path.query.com:80", |
| 179 proxy_info.proxy_server().ToURI()); | 179 proxy_info.proxy_server().ToURI()); |
| 180 } | 180 } |
| 181 { | 181 { |
| 182 ProxyInfo proxy_info; | 182 ProxyInfo proxy_info; |
| 183 int result = resolver.GetProxyForURL(GURL("ftp://query.com:90/path"), | 183 int result = resolver.GetProxyForURL( |
| 184 &proxy_info, NULL, NULL, | 184 GURL("ftp://query.com:90/path"), &proxy_info, CompletionCallback(), |
| 185 BoundNetLog()); | 185 NULL, BoundNetLog()); |
| 186 EXPECT_EQ(OK, result); | 186 EXPECT_EQ(OK, result); |
| 187 // Note that FindProxyForURL(url, host) does not expect |host| to contain | 187 // Note that FindProxyForURL(url, host) does not expect |host| to contain |
| 188 // the port number. | 188 // the port number. |
| 189 EXPECT_EQ("ftp.query.com.90.path.query.com:80", | 189 EXPECT_EQ("ftp.query.com.90.path.query.com:80", |
| 190 proxy_info.proxy_server().ToURI()); | 190 proxy_info.proxy_server().ToURI()); |
| 191 | 191 |
| 192 EXPECT_EQ(0U, resolver.mock_js_bindings()->alerts.size()); | 192 EXPECT_EQ(0U, resolver.mock_js_bindings()->alerts.size()); |
| 193 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size()); | 193 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size()); |
| 194 } | 194 } |
| 195 | 195 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 213 // TODO(eroman): Should 'null' be considered equivalent to "DIRECT" ? | 213 // TODO(eroman): Should 'null' be considered equivalent to "DIRECT" ? |
| 214 "return_null.js" | 214 "return_null.js" |
| 215 }; | 215 }; |
| 216 | 216 |
| 217 for (size_t i = 0; i < arraysize(filenames); ++i) { | 217 for (size_t i = 0; i < arraysize(filenames); ++i) { |
| 218 ProxyResolverV8WithMockBindings resolver; | 218 ProxyResolverV8WithMockBindings resolver; |
| 219 int result = resolver.SetPacScriptFromDisk(filenames[i]); | 219 int result = resolver.SetPacScriptFromDisk(filenames[i]); |
| 220 EXPECT_EQ(OK, result); | 220 EXPECT_EQ(OK, result); |
| 221 | 221 |
| 222 ProxyInfo proxy_info; | 222 ProxyInfo proxy_info; |
| 223 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 223 result = resolver.GetProxyForURL( |
| 224 BoundNetLog()); | 224 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 225 | 225 |
| 226 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result); | 226 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result); |
| 227 | 227 |
| 228 MockJSBindings* bindings = resolver.mock_js_bindings(); | 228 MockJSBindings* bindings = resolver.mock_js_bindings(); |
| 229 EXPECT_EQ(0U, bindings->alerts.size()); | 229 EXPECT_EQ(0U, bindings->alerts.size()); |
| 230 ASSERT_EQ(1U, bindings->errors.size()); | 230 ASSERT_EQ(1U, bindings->errors.size()); |
| 231 EXPECT_EQ("FindProxyForURL() did not return a string.", | 231 EXPECT_EQ("FindProxyForURL() did not return a string.", |
| 232 bindings->errors[0]); | 232 bindings->errors[0]); |
| 233 EXPECT_EQ(-1, bindings->errors_line_number[0]); | 233 EXPECT_EQ(-1, bindings->errors_line_number[0]); |
| 234 } | 234 } |
| 235 } | 235 } |
| 236 | 236 |
| 237 // Try using a PAC script which defines no "FindProxyForURL" function. | 237 // Try using a PAC script which defines no "FindProxyForURL" function. |
| 238 TEST(ProxyResolverV8Test, NoEntryPoint) { | 238 TEST(ProxyResolverV8Test, NoEntryPoint) { |
| 239 ProxyResolverV8WithMockBindings resolver; | 239 ProxyResolverV8WithMockBindings resolver; |
| 240 int result = resolver.SetPacScriptFromDisk("no_entrypoint.js"); | 240 int result = resolver.SetPacScriptFromDisk("no_entrypoint.js"); |
| 241 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result); | 241 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result); |
| 242 | 242 |
| 243 ProxyInfo proxy_info; | 243 ProxyInfo proxy_info; |
| 244 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 244 result = resolver.GetProxyForURL( |
| 245 BoundNetLog()); | 245 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 246 | 246 |
| 247 EXPECT_EQ(ERR_FAILED, result); | 247 EXPECT_EQ(ERR_FAILED, result); |
| 248 } | 248 } |
| 249 | 249 |
| 250 // Try loading a malformed PAC script. | 250 // Try loading a malformed PAC script. |
| 251 TEST(ProxyResolverV8Test, ParseError) { | 251 TEST(ProxyResolverV8Test, ParseError) { |
| 252 ProxyResolverV8WithMockBindings resolver; | 252 ProxyResolverV8WithMockBindings resolver; |
| 253 int result = resolver.SetPacScriptFromDisk("missing_close_brace.js"); | 253 int result = resolver.SetPacScriptFromDisk("missing_close_brace.js"); |
| 254 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result); | 254 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result); |
| 255 | 255 |
| 256 ProxyInfo proxy_info; | 256 ProxyInfo proxy_info; |
| 257 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 257 result = resolver.GetProxyForURL( |
| 258 BoundNetLog()); | 258 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 259 | 259 |
| 260 EXPECT_EQ(ERR_FAILED, result); | 260 EXPECT_EQ(ERR_FAILED, result); |
| 261 | 261 |
| 262 MockJSBindings* bindings = resolver.mock_js_bindings(); | 262 MockJSBindings* bindings = resolver.mock_js_bindings(); |
| 263 EXPECT_EQ(0U, bindings->alerts.size()); | 263 EXPECT_EQ(0U, bindings->alerts.size()); |
| 264 | 264 |
| 265 // We get one error during compilation. | 265 // We get one error during compilation. |
| 266 ASSERT_EQ(1U, bindings->errors.size()); | 266 ASSERT_EQ(1U, bindings->errors.size()); |
| 267 | 267 |
| 268 EXPECT_EQ("Uncaught SyntaxError: Unexpected end of input", | 268 EXPECT_EQ("Uncaught SyntaxError: Unexpected end of input", |
| 269 bindings->errors[0]); | 269 bindings->errors[0]); |
| 270 EXPECT_EQ(0, bindings->errors_line_number[0]); | 270 EXPECT_EQ(0, bindings->errors_line_number[0]); |
| 271 } | 271 } |
| 272 | 272 |
| 273 // Run a PAC script several times, which has side-effects. | 273 // Run a PAC script several times, which has side-effects. |
| 274 TEST(ProxyResolverV8Test, SideEffects) { | 274 TEST(ProxyResolverV8Test, SideEffects) { |
| 275 ProxyResolverV8WithMockBindings resolver; | 275 ProxyResolverV8WithMockBindings resolver; |
| 276 int result = resolver.SetPacScriptFromDisk("side_effects.js"); | 276 int result = resolver.SetPacScriptFromDisk("side_effects.js"); |
| 277 | 277 |
| 278 // The PAC script increments a counter each time we invoke it. | 278 // The PAC script increments a counter each time we invoke it. |
| 279 for (int i = 0; i < 3; ++i) { | 279 for (int i = 0; i < 3; ++i) { |
| 280 ProxyInfo proxy_info; | 280 ProxyInfo proxy_info; |
| 281 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 281 result = resolver.GetProxyForURL( |
| 282 BoundNetLog()); | 282 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 283 EXPECT_EQ(OK, result); | 283 EXPECT_EQ(OK, result); |
| 284 EXPECT_EQ(base::StringPrintf("sideffect_%d:80", i), | 284 EXPECT_EQ(base::StringPrintf("sideffect_%d:80", i), |
| 285 proxy_info.proxy_server().ToURI()); | 285 proxy_info.proxy_server().ToURI()); |
| 286 } | 286 } |
| 287 | 287 |
| 288 // Reload the script -- the javascript environment should be reset, hence | 288 // Reload the script -- the javascript environment should be reset, hence |
| 289 // the counter starts over. | 289 // the counter starts over. |
| 290 result = resolver.SetPacScriptFromDisk("side_effects.js"); | 290 result = resolver.SetPacScriptFromDisk("side_effects.js"); |
| 291 EXPECT_EQ(OK, result); | 291 EXPECT_EQ(OK, result); |
| 292 | 292 |
| 293 for (int i = 0; i < 3; ++i) { | 293 for (int i = 0; i < 3; ++i) { |
| 294 ProxyInfo proxy_info; | 294 ProxyInfo proxy_info; |
| 295 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 295 result = resolver.GetProxyForURL( |
| 296 BoundNetLog()); | 296 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 297 EXPECT_EQ(OK, result); | 297 EXPECT_EQ(OK, result); |
| 298 EXPECT_EQ(base::StringPrintf("sideffect_%d:80", i), | 298 EXPECT_EQ(base::StringPrintf("sideffect_%d:80", i), |
| 299 proxy_info.proxy_server().ToURI()); | 299 proxy_info.proxy_server().ToURI()); |
| 300 } | 300 } |
| 301 } | 301 } |
| 302 | 302 |
| 303 // Execute a PAC script which throws an exception in FindProxyForURL. | 303 // Execute a PAC script which throws an exception in FindProxyForURL. |
| 304 TEST(ProxyResolverV8Test, UnhandledException) { | 304 TEST(ProxyResolverV8Test, UnhandledException) { |
| 305 ProxyResolverV8WithMockBindings resolver; | 305 ProxyResolverV8WithMockBindings resolver; |
| 306 int result = resolver.SetPacScriptFromDisk("unhandled_exception.js"); | 306 int result = resolver.SetPacScriptFromDisk("unhandled_exception.js"); |
| 307 EXPECT_EQ(OK, result); | 307 EXPECT_EQ(OK, result); |
| 308 | 308 |
| 309 ProxyInfo proxy_info; | 309 ProxyInfo proxy_info; |
| 310 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 310 result = resolver.GetProxyForURL( |
| 311 BoundNetLog()); | 311 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 312 | 312 |
| 313 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result); | 313 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result); |
| 314 | 314 |
| 315 MockJSBindings* bindings = resolver.mock_js_bindings(); | 315 MockJSBindings* bindings = resolver.mock_js_bindings(); |
| 316 EXPECT_EQ(0U, bindings->alerts.size()); | 316 EXPECT_EQ(0U, bindings->alerts.size()); |
| 317 ASSERT_EQ(1U, bindings->errors.size()); | 317 ASSERT_EQ(1U, bindings->errors.size()); |
| 318 EXPECT_EQ("Uncaught ReferenceError: undefined_variable is not defined", | 318 EXPECT_EQ("Uncaught ReferenceError: undefined_variable is not defined", |
| 319 bindings->errors[0]); | 319 bindings->errors[0]); |
| 320 EXPECT_EQ(3, bindings->errors_line_number[0]); | 320 EXPECT_EQ(3, bindings->errors_line_number[0]); |
| 321 } | 321 } |
| 322 | 322 |
| 323 TEST(ProxyResolverV8Test, ReturnUnicode) { | 323 TEST(ProxyResolverV8Test, ReturnUnicode) { |
| 324 ProxyResolverV8WithMockBindings resolver; | 324 ProxyResolverV8WithMockBindings resolver; |
| 325 int result = resolver.SetPacScriptFromDisk("return_unicode.js"); | 325 int result = resolver.SetPacScriptFromDisk("return_unicode.js"); |
| 326 EXPECT_EQ(OK, result); | 326 EXPECT_EQ(OK, result); |
| 327 | 327 |
| 328 ProxyInfo proxy_info; | 328 ProxyInfo proxy_info; |
| 329 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 329 result = resolver.GetProxyForURL( |
| 330 BoundNetLog()); | 330 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 331 | 331 |
| 332 // The result from this resolve was unparseable, because it | 332 // The result from this resolve was unparseable, because it |
| 333 // wasn't ASCII. | 333 // wasn't ASCII. |
| 334 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result); | 334 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result); |
| 335 } | 335 } |
| 336 | 336 |
| 337 // Test the PAC library functions that we expose in the JS environmnet. | 337 // Test the PAC library functions that we expose in the JS environment. |
| 338 TEST(ProxyResolverV8Test, JavascriptLibrary) { | 338 TEST(ProxyResolverV8Test, JavascriptLibrary) { |
| 339 ProxyResolverV8WithMockBindings resolver; | 339 ProxyResolverV8WithMockBindings resolver; |
| 340 int result = resolver.SetPacScriptFromDisk("pac_library_unittest.js"); | 340 int result = resolver.SetPacScriptFromDisk("pac_library_unittest.js"); |
| 341 EXPECT_EQ(OK, result); | 341 EXPECT_EQ(OK, result); |
| 342 | 342 |
| 343 ProxyInfo proxy_info; | 343 ProxyInfo proxy_info; |
| 344 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 344 result = resolver.GetProxyForURL( |
| 345 BoundNetLog()); | 345 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 346 | 346 |
| 347 // If the javascript side of this unit-test fails, it will throw a javascript | 347 // If the javascript side of this unit-test fails, it will throw a javascript |
| 348 // exception. Otherwise it will return "PROXY success:80". | 348 // exception. Otherwise it will return "PROXY success:80". |
| 349 EXPECT_EQ(OK, result); | 349 EXPECT_EQ(OK, result); |
| 350 EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI()); | 350 EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI()); |
| 351 | 351 |
| 352 EXPECT_EQ(0U, resolver.mock_js_bindings()->alerts.size()); | 352 EXPECT_EQ(0U, resolver.mock_js_bindings()->alerts.size()); |
| 353 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size()); | 353 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size()); |
| 354 } | 354 } |
| 355 | 355 |
| 356 // Try resolving when SetPacScriptByData() has not been called. | 356 // Try resolving when SetPacScriptByData() has not been called. |
| 357 TEST(ProxyResolverV8Test, NoSetPacScript) { | 357 TEST(ProxyResolverV8Test, NoSetPacScript) { |
| 358 ProxyResolverV8WithMockBindings resolver; | 358 ProxyResolverV8WithMockBindings resolver; |
| 359 | 359 |
| 360 ProxyInfo proxy_info; | 360 ProxyInfo proxy_info; |
| 361 | 361 |
| 362 // Resolve should fail, as we are not yet initialized with a script. | 362 // Resolve should fail, as we are not yet initialized with a script. |
| 363 int result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 363 int result = resolver.GetProxyForURL( |
| 364 BoundNetLog()); | 364 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 365 EXPECT_EQ(ERR_FAILED, result); | 365 EXPECT_EQ(ERR_FAILED, result); |
| 366 | 366 |
| 367 // Initialize it. | 367 // Initialize it. |
| 368 result = resolver.SetPacScriptFromDisk("direct.js"); | 368 result = resolver.SetPacScriptFromDisk("direct.js"); |
| 369 EXPECT_EQ(OK, result); | 369 EXPECT_EQ(OK, result); |
| 370 | 370 |
| 371 // Resolve should now succeed. | 371 // Resolve should now succeed. |
| 372 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 372 result = resolver.GetProxyForURL( |
| 373 BoundNetLog()); | 373 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 374 EXPECT_EQ(OK, result); | 374 EXPECT_EQ(OK, result); |
| 375 | 375 |
| 376 // Clear it, by initializing with an empty string. | 376 // Clear it, by initializing with an empty string. |
| 377 resolver.SetPacScript( | 377 resolver.SetPacScript( |
| 378 ProxyResolverScriptData::FromUTF16(string16()), NULL); | 378 ProxyResolverScriptData::FromUTF16(string16()), CompletionCallback()); |
| 379 | 379 |
| 380 // Resolve should fail again now. | 380 // Resolve should fail again now. |
| 381 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 381 result = resolver.GetProxyForURL( |
| 382 BoundNetLog()); | 382 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 383 EXPECT_EQ(ERR_FAILED, result); | 383 EXPECT_EQ(ERR_FAILED, result); |
| 384 | 384 |
| 385 // Load a good script once more. | 385 // Load a good script once more. |
| 386 result = resolver.SetPacScriptFromDisk("direct.js"); | 386 result = resolver.SetPacScriptFromDisk("direct.js"); |
| 387 EXPECT_EQ(OK, result); | 387 EXPECT_EQ(OK, result); |
| 388 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 388 result = resolver.GetProxyForURL( |
| 389 BoundNetLog()); | 389 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 390 EXPECT_EQ(OK, result); | 390 EXPECT_EQ(OK, result); |
| 391 | 391 |
| 392 EXPECT_EQ(0U, resolver.mock_js_bindings()->alerts.size()); | 392 EXPECT_EQ(0U, resolver.mock_js_bindings()->alerts.size()); |
| 393 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size()); | 393 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size()); |
| 394 } | 394 } |
| 395 | 395 |
| 396 // Test marshalling/un-marshalling of values between C++/V8. | 396 // Test marshalling/un-marshalling of values between C++/V8. |
| 397 TEST(ProxyResolverV8Test, V8Bindings) { | 397 TEST(ProxyResolverV8Test, V8Bindings) { |
| 398 ProxyResolverV8WithMockBindings resolver; | 398 ProxyResolverV8WithMockBindings resolver; |
| 399 MockJSBindings* bindings = resolver.mock_js_bindings(); | 399 MockJSBindings* bindings = resolver.mock_js_bindings(); |
| 400 bindings->dns_resolve_result = "127.0.0.1"; | 400 bindings->dns_resolve_result = "127.0.0.1"; |
| 401 int result = resolver.SetPacScriptFromDisk("bindings.js"); | 401 int result = resolver.SetPacScriptFromDisk("bindings.js"); |
| 402 EXPECT_EQ(OK, result); | 402 EXPECT_EQ(OK, result); |
| 403 | 403 |
| 404 ProxyInfo proxy_info; | 404 ProxyInfo proxy_info; |
| 405 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 405 result = resolver.GetProxyForURL( |
| 406 BoundNetLog()); | 406 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 407 | 407 |
| 408 EXPECT_EQ(OK, result); | 408 EXPECT_EQ(OK, result); |
| 409 EXPECT_TRUE(proxy_info.is_direct()); | 409 EXPECT_TRUE(proxy_info.is_direct()); |
| 410 | 410 |
| 411 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size()); | 411 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size()); |
| 412 | 412 |
| 413 // Alert was called 5 times. | 413 // Alert was called 5 times. |
| 414 ASSERT_EQ(5U, bindings->alerts.size()); | 414 ASSERT_EQ(5U, bindings->alerts.size()); |
| 415 EXPECT_EQ("undefined", bindings->alerts[0]); | 415 EXPECT_EQ("undefined", bindings->alerts[0]); |
| 416 EXPECT_EQ("null", bindings->alerts[1]); | 416 EXPECT_EQ("null", bindings->alerts[1]); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 443 | 443 |
| 444 int result = resolver.SetPacScriptFromDisk("binding_from_global.js"); | 444 int result = resolver.SetPacScriptFromDisk("binding_from_global.js"); |
| 445 EXPECT_EQ(OK, result); | 445 EXPECT_EQ(OK, result); |
| 446 | 446 |
| 447 MockJSBindings* bindings = resolver.mock_js_bindings(); | 447 MockJSBindings* bindings = resolver.mock_js_bindings(); |
| 448 | 448 |
| 449 // myIpAddress() got called during initialization of the script. | 449 // myIpAddress() got called during initialization of the script. |
| 450 EXPECT_EQ(1, bindings->my_ip_address_count); | 450 EXPECT_EQ(1, bindings->my_ip_address_count); |
| 451 | 451 |
| 452 ProxyInfo proxy_info; | 452 ProxyInfo proxy_info; |
| 453 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 453 result = resolver.GetProxyForURL( |
| 454 BoundNetLog()); | 454 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 455 | 455 |
| 456 EXPECT_EQ(OK, result); | 456 EXPECT_EQ(OK, result); |
| 457 EXPECT_FALSE(proxy_info.is_direct()); | 457 EXPECT_FALSE(proxy_info.is_direct()); |
| 458 EXPECT_EQ("127.0.0.1:80", proxy_info.proxy_server().ToURI()); | 458 EXPECT_EQ("127.0.0.1:80", proxy_info.proxy_server().ToURI()); |
| 459 | 459 |
| 460 // Check that no other bindings were called. | 460 // Check that no other bindings were called. |
| 461 EXPECT_EQ(0U, bindings->errors.size()); | 461 EXPECT_EQ(0U, bindings->errors.size()); |
| 462 ASSERT_EQ(0U, bindings->alerts.size()); | 462 ASSERT_EQ(0U, bindings->alerts.size()); |
| 463 ASSERT_EQ(0U, bindings->dns_resolves.size()); | 463 ASSERT_EQ(0U, bindings->dns_resolves.size()); |
| 464 EXPECT_EQ(0, bindings->my_ip_address_ex_count); | 464 EXPECT_EQ(0, bindings->my_ip_address_ex_count); |
| 465 ASSERT_EQ(0U, bindings->dns_resolves_ex.size()); | 465 ASSERT_EQ(0U, bindings->dns_resolves_ex.size()); |
| 466 } | 466 } |
| 467 | 467 |
| 468 // Try loading a PAC script that ends with a comment and has no terminal | 468 // Try loading a PAC script that ends with a comment and has no terminal |
| 469 // newline. This should not cause problems with the PAC utility functions | 469 // newline. This should not cause problems with the PAC utility functions |
| 470 // that we add to the script's environment. | 470 // that we add to the script's environment. |
| 471 // http://crbug.com/22864 | 471 // http://crbug.com/22864 |
| 472 TEST(ProxyResolverV8Test, EndsWithCommentNoNewline) { | 472 TEST(ProxyResolverV8Test, EndsWithCommentNoNewline) { |
| 473 ProxyResolverV8WithMockBindings resolver; | 473 ProxyResolverV8WithMockBindings resolver; |
| 474 int result = resolver.SetPacScriptFromDisk("ends_with_comment.js"); | 474 int result = resolver.SetPacScriptFromDisk("ends_with_comment.js"); |
| 475 EXPECT_EQ(OK, result); | 475 EXPECT_EQ(OK, result); |
| 476 | 476 |
| 477 ProxyInfo proxy_info; | 477 ProxyInfo proxy_info; |
| 478 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); | 478 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 479 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 479 result = resolver.GetProxyForURL( |
| 480 log.bound()); | 480 kQueryUrl, &proxy_info, CompletionCallback(), NULL, log.bound()); |
| 481 | 481 |
| 482 EXPECT_EQ(OK, result); | 482 EXPECT_EQ(OK, result); |
| 483 EXPECT_FALSE(proxy_info.is_direct()); | 483 EXPECT_FALSE(proxy_info.is_direct()); |
| 484 EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI()); | 484 EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI()); |
| 485 } | 485 } |
| 486 | 486 |
| 487 // Try loading a PAC script that ends with a statement and has no terminal | 487 // Try loading a PAC script that ends with a statement and has no terminal |
| 488 // newline. This should not cause problems with the PAC utility functions | 488 // newline. This should not cause problems with the PAC utility functions |
| 489 // that we add to the script's environment. | 489 // that we add to the script's environment. |
| 490 // http://crbug.com/22864 | 490 // http://crbug.com/22864 |
| 491 TEST(ProxyResolverV8Test, EndsWithStatementNoNewline) { | 491 TEST(ProxyResolverV8Test, EndsWithStatementNoNewline) { |
| 492 ProxyResolverV8WithMockBindings resolver; | 492 ProxyResolverV8WithMockBindings resolver; |
| 493 int result = resolver.SetPacScriptFromDisk( | 493 int result = resolver.SetPacScriptFromDisk( |
| 494 "ends_with_statement_no_semicolon.js"); | 494 "ends_with_statement_no_semicolon.js"); |
| 495 EXPECT_EQ(OK, result); | 495 EXPECT_EQ(OK, result); |
| 496 | 496 |
| 497 ProxyInfo proxy_info; | 497 ProxyInfo proxy_info; |
| 498 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); | 498 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 499 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 499 result = resolver.GetProxyForURL( |
| 500 log.bound()); | 500 kQueryUrl, &proxy_info, CompletionCallback(), NULL, log.bound()); |
| 501 | 501 |
| 502 EXPECT_EQ(OK, result); | 502 EXPECT_EQ(OK, result); |
| 503 EXPECT_FALSE(proxy_info.is_direct()); | 503 EXPECT_FALSE(proxy_info.is_direct()); |
| 504 EXPECT_EQ("success:3", proxy_info.proxy_server().ToURI()); | 504 EXPECT_EQ("success:3", proxy_info.proxy_server().ToURI()); |
| 505 } | 505 } |
| 506 | 506 |
| 507 // Test the return values from myIpAddress(), myIpAddressEx(), dnsResolve(), | 507 // Test the return values from myIpAddress(), myIpAddressEx(), dnsResolve(), |
| 508 // dnsResolveEx(), isResolvable(), isResolvableEx(), when the the binding | 508 // dnsResolveEx(), isResolvable(), isResolvableEx(), when the the binding |
| 509 // returns empty string (failure). This simulates the return values from | 509 // returns empty string (failure). This simulates the return values from |
| 510 // those functions when the underlying DNS resolution fails. | 510 // those functions when the underlying DNS resolution fails. |
| 511 TEST(ProxyResolverV8Test, DNSResolutionFailure) { | 511 TEST(ProxyResolverV8Test, DNSResolutionFailure) { |
| 512 ProxyResolverV8WithMockBindings resolver; | 512 ProxyResolverV8WithMockBindings resolver; |
| 513 int result = resolver.SetPacScriptFromDisk("dns_fail.js"); | 513 int result = resolver.SetPacScriptFromDisk("dns_fail.js"); |
| 514 EXPECT_EQ(OK, result); | 514 EXPECT_EQ(OK, result); |
| 515 | 515 |
| 516 ProxyInfo proxy_info; | 516 ProxyInfo proxy_info; |
| 517 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 517 result = resolver.GetProxyForURL( |
| 518 BoundNetLog()); | 518 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 519 | 519 |
| 520 EXPECT_EQ(OK, result); | 520 EXPECT_EQ(OK, result); |
| 521 EXPECT_FALSE(proxy_info.is_direct()); | 521 EXPECT_FALSE(proxy_info.is_direct()); |
| 522 EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI()); | 522 EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI()); |
| 523 } | 523 } |
| 524 | 524 |
| 525 TEST(ProxyResolverV8Test, DNSResolutionOfInternationDomainName) { | 525 TEST(ProxyResolverV8Test, DNSResolutionOfInternationDomainName) { |
| 526 ProxyResolverV8WithMockBindings resolver; | 526 ProxyResolverV8WithMockBindings resolver; |
| 527 int result = resolver.SetPacScriptFromDisk("international_domain_names.js"); | 527 int result = resolver.SetPacScriptFromDisk("international_domain_names.js"); |
| 528 EXPECT_EQ(OK, result); | 528 EXPECT_EQ(OK, result); |
| 529 | 529 |
| 530 // Execute FindProxyForURL(). | 530 // Execute FindProxyForURL(). |
| 531 ProxyInfo proxy_info; | 531 ProxyInfo proxy_info; |
| 532 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, | 532 result = resolver.GetProxyForURL( |
| 533 BoundNetLog()); | 533 kQueryUrl, &proxy_info, CompletionCallback(), NULL, BoundNetLog()); |
| 534 | 534 |
| 535 EXPECT_EQ(OK, result); | 535 EXPECT_EQ(OK, result); |
| 536 EXPECT_TRUE(proxy_info.is_direct()); | 536 EXPECT_TRUE(proxy_info.is_direct()); |
| 537 | 537 |
| 538 // Check that the international domain name was converted to punycode | 538 // Check that the international domain name was converted to punycode |
| 539 // before passing it onto the bindings layer. | 539 // before passing it onto the bindings layer. |
| 540 MockJSBindings* bindings = resolver.mock_js_bindings(); | 540 MockJSBindings* bindings = resolver.mock_js_bindings(); |
| 541 | 541 |
| 542 ASSERT_EQ(1u, bindings->dns_resolves.size()); | 542 ASSERT_EQ(1u, bindings->dns_resolves.size()); |
| 543 EXPECT_EQ("xn--bcher-kva.ch", bindings->dns_resolves[0]); | 543 EXPECT_EQ("xn--bcher-kva.ch", bindings->dns_resolves[0]); |
| 544 | 544 |
| 545 ASSERT_EQ(1u, bindings->dns_resolves_ex.size()); | 545 ASSERT_EQ(1u, bindings->dns_resolves_ex.size()); |
| 546 EXPECT_EQ("xn--bcher-kva.ch", bindings->dns_resolves_ex[0]); | 546 EXPECT_EQ("xn--bcher-kva.ch", bindings->dns_resolves_ex[0]); |
| 547 } | 547 } |
| 548 | 548 |
| 549 } // namespace | 549 } // namespace |
| 550 } // namespace net | 550 } // namespace net |
| OLD | NEW |