| OLD | NEW |
| 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 "net/proxy/dhcp_proxy_script_adapter_fetcher_win.h" | 5 #include "net/proxy/dhcp_proxy_script_adapter_fetcher_win.h" |
| 6 | 6 |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
| 9 #include "base/test/test_timeouts.h" | 9 #include "base/test/test_timeouts.h" |
| 10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
| 11 #include "base/timer/elapsed_timer.h" | 11 #include "base/timer/elapsed_timer.h" |
| 12 #include "base/timer/timer.h" | 12 #include "base/timer/timer.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "net/base/test_completion_callback.h" | 14 #include "net/base/test_completion_callback.h" |
| 15 #include "net/proxy/mock_proxy_script_fetcher.h" | 15 #include "net/proxy/mock_proxy_script_fetcher.h" |
| 16 #include "net/proxy/proxy_script_fetcher_impl.h" | 16 #include "net/proxy/proxy_script_fetcher_impl.h" |
| 17 #include "net/test/embedded_test_server/embedded_test_server.h" | 17 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 18 #include "net/test/gtest_util.h" |
| 18 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 22 |
| 23 using net::test::IsError; |
| 24 using net::test::IsOk; |
| 25 |
| 21 namespace net { | 26 namespace net { |
| 22 | 27 |
| 23 namespace { | 28 namespace { |
| 24 | 29 |
| 25 const char kPacUrl[] = "http://pacserver/script.pac"; | 30 const char kPacUrl[] = "http://pacserver/script.pac"; |
| 26 | 31 |
| 27 // In net/proxy/dhcp_proxy_script_fetcher_win_unittest.cc there are a few | 32 // In net/proxy/dhcp_proxy_script_fetcher_win_unittest.cc there are a few |
| 28 // tests that exercise DhcpProxyScriptAdapterFetcher end-to-end along with | 33 // tests that exercise DhcpProxyScriptAdapterFetcher end-to-end along with |
| 29 // DhcpProxyScriptFetcherWin, i.e. it tests the end-to-end usage of Win32 | 34 // DhcpProxyScriptFetcherWin, i.e. it tests the end-to-end usage of Win32 |
| 30 // APIs and the network. In this file we test only by stubbing out | 35 // APIs and the network. In this file we test only by stubbing out |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 std::unique_ptr<MockDhcpProxyScriptAdapterFetcher> fetcher_; | 174 std::unique_ptr<MockDhcpProxyScriptAdapterFetcher> fetcher_; |
| 170 base::string16 pac_text_; | 175 base::string16 pac_text_; |
| 171 }; | 176 }; |
| 172 | 177 |
| 173 TEST(DhcpProxyScriptAdapterFetcher, NormalCaseURLNotInDhcp) { | 178 TEST(DhcpProxyScriptAdapterFetcher, NormalCaseURLNotInDhcp) { |
| 174 FetcherClient client; | 179 FetcherClient client; |
| 175 client.fetcher_->configured_url_ = ""; | 180 client.fetcher_->configured_url_ = ""; |
| 176 client.RunTest(); | 181 client.RunTest(); |
| 177 client.WaitForResult(ERR_PAC_NOT_IN_DHCP); | 182 client.WaitForResult(ERR_PAC_NOT_IN_DHCP); |
| 178 ASSERT_TRUE(client.fetcher_->DidFinish()); | 183 ASSERT_TRUE(client.fetcher_->DidFinish()); |
| 179 EXPECT_EQ(ERR_PAC_NOT_IN_DHCP, client.fetcher_->GetResult()); | 184 EXPECT_THAT(client.fetcher_->GetResult(), IsError(ERR_PAC_NOT_IN_DHCP)); |
| 180 EXPECT_EQ(base::string16(L""), client.fetcher_->GetPacScript()); | 185 EXPECT_EQ(base::string16(L""), client.fetcher_->GetPacScript()); |
| 181 } | 186 } |
| 182 | 187 |
| 183 TEST(DhcpProxyScriptAdapterFetcher, NormalCaseURLInDhcp) { | 188 TEST(DhcpProxyScriptAdapterFetcher, NormalCaseURLInDhcp) { |
| 184 FetcherClient client; | 189 FetcherClient client; |
| 185 client.RunTest(); | 190 client.RunTest(); |
| 186 client.WaitForResult(OK); | 191 client.WaitForResult(OK); |
| 187 ASSERT_TRUE(client.fetcher_->DidFinish()); | 192 ASSERT_TRUE(client.fetcher_->DidFinish()); |
| 188 EXPECT_EQ(OK, client.fetcher_->GetResult()); | 193 EXPECT_THAT(client.fetcher_->GetResult(), IsOk()); |
| 189 EXPECT_EQ(base::string16(L"bingo"), client.fetcher_->GetPacScript()); | 194 EXPECT_EQ(base::string16(L"bingo"), client.fetcher_->GetPacScript()); |
| 190 EXPECT_EQ(GURL(kPacUrl), client.fetcher_->GetPacURL()); | 195 EXPECT_EQ(GURL(kPacUrl), client.fetcher_->GetPacURL()); |
| 191 } | 196 } |
| 192 | 197 |
| 193 TEST(DhcpProxyScriptAdapterFetcher, TimeoutDuringDhcp) { | 198 TEST(DhcpProxyScriptAdapterFetcher, TimeoutDuringDhcp) { |
| 194 // Does a Fetch() with a long enough delay on accessing DHCP that the | 199 // Does a Fetch() with a long enough delay on accessing DHCP that the |
| 195 // fetcher should time out. This is to test a case manual testing found, | 200 // fetcher should time out. This is to test a case manual testing found, |
| 196 // where under certain circumstances (e.g. adapter enabled for DHCP and | 201 // where under certain circumstances (e.g. adapter enabled for DHCP and |
| 197 // needs to retrieve its configuration from DHCP, but no DHCP server | 202 // needs to retrieve its configuration from DHCP, but no DHCP server |
| 198 // present on the network) accessing DHCP can take on the order of tens | 203 // present on the network) accessing DHCP can take on the order of tens |
| 199 // of seconds. | 204 // of seconds. |
| 200 FetcherClient client; | 205 FetcherClient client; |
| 201 client.fetcher_->dhcp_delay_ = TestTimeouts::action_max_timeout(); | 206 client.fetcher_->dhcp_delay_ = TestTimeouts::action_max_timeout(); |
| 202 client.fetcher_->timeout_ = base::TimeDelta::FromMilliseconds(25); | 207 client.fetcher_->timeout_ = base::TimeDelta::FromMilliseconds(25); |
| 203 | 208 |
| 204 base::ElapsedTimer timer; | 209 base::ElapsedTimer timer; |
| 205 client.RunTest(); | 210 client.RunTest(); |
| 206 // An error different from this would be received if the timeout didn't | 211 // An error different from this would be received if the timeout didn't |
| 207 // kick in. | 212 // kick in. |
| 208 client.WaitForResult(ERR_TIMED_OUT); | 213 client.WaitForResult(ERR_TIMED_OUT); |
| 209 | 214 |
| 210 ASSERT_TRUE(client.fetcher_->DidFinish()); | 215 ASSERT_TRUE(client.fetcher_->DidFinish()); |
| 211 EXPECT_EQ(ERR_TIMED_OUT, client.fetcher_->GetResult()); | 216 EXPECT_THAT(client.fetcher_->GetResult(), IsError(ERR_TIMED_OUT)); |
| 212 EXPECT_EQ(base::string16(L""), client.fetcher_->GetPacScript()); | 217 EXPECT_EQ(base::string16(L""), client.fetcher_->GetPacScript()); |
| 213 EXPECT_EQ(GURL(), client.fetcher_->GetPacURL()); | 218 EXPECT_EQ(GURL(), client.fetcher_->GetPacURL()); |
| 214 client.FinishTestAllowCleanup(); | 219 client.FinishTestAllowCleanup(); |
| 215 } | 220 } |
| 216 | 221 |
| 217 TEST(DhcpProxyScriptAdapterFetcher, CancelWhileDhcp) { | 222 TEST(DhcpProxyScriptAdapterFetcher, CancelWhileDhcp) { |
| 218 FetcherClient client; | 223 FetcherClient client; |
| 219 client.RunTest(); | 224 client.RunTest(); |
| 220 client.fetcher_->Cancel(); | 225 client.fetcher_->Cancel(); |
| 221 base::RunLoop().RunUntilIdle(); | 226 base::RunLoop().RunUntilIdle(); |
| 222 ASSERT_FALSE(client.fetcher_->DidFinish()); | 227 ASSERT_FALSE(client.fetcher_->DidFinish()); |
| 223 ASSERT_TRUE(client.fetcher_->WasCancelled()); | 228 ASSERT_TRUE(client.fetcher_->WasCancelled()); |
| 224 EXPECT_EQ(ERR_ABORTED, client.fetcher_->GetResult()); | 229 EXPECT_THAT(client.fetcher_->GetResult(), IsError(ERR_ABORTED)); |
| 225 EXPECT_EQ(base::string16(L""), client.fetcher_->GetPacScript()); | 230 EXPECT_EQ(base::string16(L""), client.fetcher_->GetPacScript()); |
| 226 EXPECT_EQ(GURL(), client.fetcher_->GetPacURL()); | 231 EXPECT_EQ(GURL(), client.fetcher_->GetPacURL()); |
| 227 client.FinishTestAllowCleanup(); | 232 client.FinishTestAllowCleanup(); |
| 228 } | 233 } |
| 229 | 234 |
| 230 TEST(DhcpProxyScriptAdapterFetcher, CancelWhileFetcher) { | 235 TEST(DhcpProxyScriptAdapterFetcher, CancelWhileFetcher) { |
| 231 FetcherClient client; | 236 FetcherClient client; |
| 232 // This causes the mock fetcher not to pretend the | 237 // This causes the mock fetcher not to pretend the |
| 233 // fetcher finishes after a timeout. | 238 // fetcher finishes after a timeout. |
| 234 client.fetcher_->fetcher_delay_ms_ = -1; | 239 client.fetcher_->fetcher_delay_ms_ = -1; |
| 235 client.RunTest(); | 240 client.RunTest(); |
| 236 int max_loops = 4; | 241 int max_loops = 4; |
| 237 while (!client.fetcher_->IsWaitingForFetcher() && max_loops--) { | 242 while (!client.fetcher_->IsWaitingForFetcher() && max_loops--) { |
| 238 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); | 243 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); |
| 239 base::RunLoop().RunUntilIdle(); | 244 base::RunLoop().RunUntilIdle(); |
| 240 } | 245 } |
| 241 client.fetcher_->Cancel(); | 246 client.fetcher_->Cancel(); |
| 242 base::RunLoop().RunUntilIdle(); | 247 base::RunLoop().RunUntilIdle(); |
| 243 ASSERT_FALSE(client.fetcher_->DidFinish()); | 248 ASSERT_FALSE(client.fetcher_->DidFinish()); |
| 244 ASSERT_TRUE(client.fetcher_->WasCancelled()); | 249 ASSERT_TRUE(client.fetcher_->WasCancelled()); |
| 245 EXPECT_EQ(ERR_ABORTED, client.fetcher_->GetResult()); | 250 EXPECT_THAT(client.fetcher_->GetResult(), IsError(ERR_ABORTED)); |
| 246 EXPECT_EQ(base::string16(L""), client.fetcher_->GetPacScript()); | 251 EXPECT_EQ(base::string16(L""), client.fetcher_->GetPacScript()); |
| 247 // GetPacURL() still returns the URL fetched in this case. | 252 // GetPacURL() still returns the URL fetched in this case. |
| 248 EXPECT_EQ(GURL(kPacUrl), client.fetcher_->GetPacURL()); | 253 EXPECT_EQ(GURL(kPacUrl), client.fetcher_->GetPacURL()); |
| 249 client.FinishTestAllowCleanup(); | 254 client.FinishTestAllowCleanup(); |
| 250 } | 255 } |
| 251 | 256 |
| 252 TEST(DhcpProxyScriptAdapterFetcher, CancelAtCompletion) { | 257 TEST(DhcpProxyScriptAdapterFetcher, CancelAtCompletion) { |
| 253 FetcherClient client; | 258 FetcherClient client; |
| 254 client.RunTest(); | 259 client.RunTest(); |
| 255 client.WaitForResult(OK); | 260 client.WaitForResult(OK); |
| 256 client.fetcher_->Cancel(); | 261 client.fetcher_->Cancel(); |
| 257 // Canceling after you're done should have no effect, so these | 262 // Canceling after you're done should have no effect, so these |
| 258 // are identical expectations to the NormalCaseURLInDhcp test. | 263 // are identical expectations to the NormalCaseURLInDhcp test. |
| 259 ASSERT_TRUE(client.fetcher_->DidFinish()); | 264 ASSERT_TRUE(client.fetcher_->DidFinish()); |
| 260 EXPECT_EQ(OK, client.fetcher_->GetResult()); | 265 EXPECT_THAT(client.fetcher_->GetResult(), IsOk()); |
| 261 EXPECT_EQ(base::string16(L"bingo"), client.fetcher_->GetPacScript()); | 266 EXPECT_EQ(base::string16(L"bingo"), client.fetcher_->GetPacScript()); |
| 262 EXPECT_EQ(GURL(kPacUrl), client.fetcher_->GetPacURL()); | 267 EXPECT_EQ(GURL(kPacUrl), client.fetcher_->GetPacURL()); |
| 263 client.FinishTestAllowCleanup(); | 268 client.FinishTestAllowCleanup(); |
| 264 } | 269 } |
| 265 | 270 |
| 266 // Does a real fetch on a mock DHCP configuration. | 271 // Does a real fetch on a mock DHCP configuration. |
| 267 class MockDhcpRealFetchProxyScriptAdapterFetcher | 272 class MockDhcpRealFetchProxyScriptAdapterFetcher |
| 268 : public MockDhcpProxyScriptAdapterFetcher { | 273 : public MockDhcpProxyScriptAdapterFetcher { |
| 269 public: | 274 public: |
| 270 explicit MockDhcpRealFetchProxyScriptAdapterFetcher( | 275 explicit MockDhcpRealFetchProxyScriptAdapterFetcher( |
| (...skipping 26 matching lines...) Expand all Loading... |
| 297 scoped_refptr<base::TaskRunner> runner = | 302 scoped_refptr<base::TaskRunner> runner = |
| 298 client.worker_pool_->GetTaskRunnerWithShutdownBehavior( | 303 client.worker_pool_->GetTaskRunnerWithShutdownBehavior( |
| 299 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | 304 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); |
| 300 client.fetcher_.reset( | 305 client.fetcher_.reset( |
| 301 new MockDhcpRealFetchProxyScriptAdapterFetcher( | 306 new MockDhcpRealFetchProxyScriptAdapterFetcher( |
| 302 &url_request_context, runner)); | 307 &url_request_context, runner)); |
| 303 client.fetcher_->configured_url_ = configured_url.spec(); | 308 client.fetcher_->configured_url_ = configured_url.spec(); |
| 304 client.RunTest(); | 309 client.RunTest(); |
| 305 client.WaitForResult(OK); | 310 client.WaitForResult(OK); |
| 306 ASSERT_TRUE(client.fetcher_->DidFinish()); | 311 ASSERT_TRUE(client.fetcher_->DidFinish()); |
| 307 EXPECT_EQ(OK, client.fetcher_->GetResult()); | 312 EXPECT_THAT(client.fetcher_->GetResult(), IsOk()); |
| 308 EXPECT_EQ(base::string16(L"-downloadable.pac-\n"), | 313 EXPECT_EQ(base::string16(L"-downloadable.pac-\n"), |
| 309 client.fetcher_->GetPacScript()); | 314 client.fetcher_->GetPacScript()); |
| 310 EXPECT_EQ(configured_url, | 315 EXPECT_EQ(configured_url, |
| 311 client.fetcher_->GetPacURL()); | 316 client.fetcher_->GetPacURL()); |
| 312 } | 317 } |
| 313 | 318 |
| 314 #define BASE_URL "http://corpserver/proxy.pac" | 319 #define BASE_URL "http://corpserver/proxy.pac" |
| 315 | 320 |
| 316 TEST(DhcpProxyScriptAdapterFetcher, SanitizeDhcpApiString) { | 321 TEST(DhcpProxyScriptAdapterFetcher, SanitizeDhcpApiString) { |
| 317 const size_t kBaseUrlLen = strlen(BASE_URL); | 322 const size_t kBaseUrlLen = strlen(BASE_URL); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 330 EXPECT_EQ(BASE_URL, | 335 EXPECT_EQ(BASE_URL, |
| 331 DhcpProxyScriptAdapterFetcher::SanitizeDhcpApiString( | 336 DhcpProxyScriptAdapterFetcher::SanitizeDhcpApiString( |
| 332 BASE_URL "\0foo\0blat", kBaseUrlLen + 9)); | 337 BASE_URL "\0foo\0blat", kBaseUrlLen + 9)); |
| 333 } | 338 } |
| 334 | 339 |
| 335 #undef BASE_URL | 340 #undef BASE_URL |
| 336 | 341 |
| 337 } // namespace | 342 } // namespace |
| 338 | 343 |
| 339 } // namespace net | 344 } // namespace net |
| OLD | NEW |