Revert of suppress unaddressable error in ManifestBrowserTest.CORSManifest (patchset...
[chromium-blink-merge.git] / net / proxy / dhcp_proxy_script_adapter_fetcher_win_unittest.cc
blob4a45e675248228b5f0211ee6aba97ce489ae5bef
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
3 // found in the LICENSE file.
5 #include "net/proxy/dhcp_proxy_script_adapter_fetcher_win.h"
7 #include "base/synchronization/waitable_event.h"
8 #include "base/test/test_timeouts.h"
9 #include "base/threading/sequenced_worker_pool.h"
10 #include "base/timer/elapsed_timer.h"
11 #include "base/timer/timer.h"
12 #include "net/base/net_errors.h"
13 #include "net/base/test_completion_callback.h"
14 #include "net/proxy/mock_proxy_script_fetcher.h"
15 #include "net/proxy/proxy_script_fetcher_impl.h"
16 #include "net/test/spawned_test_server/spawned_test_server.h"
17 #include "net/url_request/url_request_test_util.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 namespace net {
22 namespace {
24 const char kPacUrl[] = "http://pacserver/script.pac";
26 // In net/proxy/dhcp_proxy_script_fetcher_win_unittest.cc there are a few
27 // tests that exercise DhcpProxyScriptAdapterFetcher end-to-end along with
28 // DhcpProxyScriptFetcherWin, i.e. it tests the end-to-end usage of Win32
29 // APIs and the network. In this file we test only by stubbing out
30 // functionality.
32 // Version of DhcpProxyScriptAdapterFetcher that mocks out dependencies
33 // to allow unit testing.
34 class MockDhcpProxyScriptAdapterFetcher
35 : public DhcpProxyScriptAdapterFetcher {
36 public:
37 explicit MockDhcpProxyScriptAdapterFetcher(
38 URLRequestContext* context,
39 scoped_refptr<base::TaskRunner> task_runner)
40 : DhcpProxyScriptAdapterFetcher(context, task_runner),
41 dhcp_delay_(base::TimeDelta::FromMilliseconds(1)),
42 timeout_(TestTimeouts::action_timeout()),
43 configured_url_(kPacUrl),
44 fetcher_delay_ms_(1),
45 fetcher_result_(OK),
46 pac_script_("bingo") {
49 void Cancel() override {
50 DhcpProxyScriptAdapterFetcher::Cancel();
51 fetcher_ = NULL;
54 ProxyScriptFetcher* ImplCreateScriptFetcher() override {
55 // We don't maintain ownership of the fetcher, it is transferred to
56 // the caller.
57 fetcher_ = new MockProxyScriptFetcher();
58 if (fetcher_delay_ms_ != -1) {
59 fetcher_timer_.Start(FROM_HERE,
60 base::TimeDelta::FromMilliseconds(fetcher_delay_ms_),
61 this, &MockDhcpProxyScriptAdapterFetcher::OnFetcherTimer);
63 return fetcher_;
66 class DelayingDhcpQuery : public DhcpQuery {
67 public:
68 explicit DelayingDhcpQuery()
69 : DhcpQuery(),
70 test_finished_event_(true, false) {
73 std::string ImplGetPacURLFromDhcp(
74 const std::string& adapter_name) override {
75 base::ElapsedTimer timer;
76 test_finished_event_.TimedWait(dhcp_delay_);
77 return configured_url_;
80 base::WaitableEvent test_finished_event_;
81 base::TimeDelta dhcp_delay_;
82 std::string configured_url_;
85 DhcpQuery* ImplCreateDhcpQuery() override {
86 dhcp_query_ = new DelayingDhcpQuery();
87 dhcp_query_->dhcp_delay_ = dhcp_delay_;
88 dhcp_query_->configured_url_ = configured_url_;
89 return dhcp_query_.get();
92 // Use a shorter timeout so tests can finish more quickly.
93 base::TimeDelta ImplGetTimeout() const override { return timeout_; }
95 void OnFetcherTimer() {
96 // Note that there is an assumption by this mock implementation that
97 // DhcpProxyScriptAdapterFetcher::Fetch will call ImplCreateScriptFetcher
98 // and call Fetch on the fetcher before the message loop is re-entered.
99 // This holds true today, but if you hit this DCHECK the problem can
100 // possibly be resolved by having a separate subclass of
101 // MockProxyScriptFetcher that adds the delay internally (instead of
102 // the simple approach currently used in ImplCreateScriptFetcher above).
103 DCHECK(fetcher_ && fetcher_->has_pending_request());
104 fetcher_->NotifyFetchCompletion(fetcher_result_, pac_script_);
105 fetcher_ = NULL;
108 bool IsWaitingForFetcher() const {
109 return state() == STATE_WAIT_URL;
112 bool WasCancelled() const {
113 return state() == STATE_CANCEL;
116 void FinishTest() {
117 DCHECK(dhcp_query_.get());
118 dhcp_query_->test_finished_event_.Signal();
121 base::TimeDelta dhcp_delay_;
122 base::TimeDelta timeout_;
123 std::string configured_url_;
124 int fetcher_delay_ms_;
125 int fetcher_result_;
126 std::string pac_script_;
127 MockProxyScriptFetcher* fetcher_;
128 base::OneShotTimer<MockDhcpProxyScriptAdapterFetcher> fetcher_timer_;
129 scoped_refptr<DelayingDhcpQuery> dhcp_query_;
132 class FetcherClient {
133 public:
134 FetcherClient()
135 : url_request_context_(new TestURLRequestContext()),
136 worker_pool_(
137 new base::SequencedWorkerPool(4, "DhcpAdapterFetcherTest")),
138 fetcher_(new MockDhcpProxyScriptAdapterFetcher(
139 url_request_context_.get(),
140 worker_pool_->GetTaskRunnerWithShutdownBehavior(
141 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN))) {
144 ~FetcherClient() {
145 worker_pool_->Shutdown();
148 void WaitForResult(int expected_error) {
149 EXPECT_EQ(expected_error, callback_.WaitForResult());
152 void RunTest() {
153 fetcher_->Fetch("adapter name", callback_.callback());
156 void FinishTestAllowCleanup() {
157 fetcher_->FinishTest();
158 base::MessageLoop::current()->RunUntilIdle();
161 TestCompletionCallback callback_;
162 scoped_ptr<URLRequestContext> url_request_context_;
163 scoped_refptr<base::SequencedWorkerPool> worker_pool_;
164 scoped_ptr<MockDhcpProxyScriptAdapterFetcher> fetcher_;
165 base::string16 pac_text_;
168 TEST(DhcpProxyScriptAdapterFetcher, NormalCaseURLNotInDhcp) {
169 FetcherClient client;
170 client.fetcher_->configured_url_ = "";
171 client.RunTest();
172 client.WaitForResult(ERR_PAC_NOT_IN_DHCP);
173 ASSERT_TRUE(client.fetcher_->DidFinish());
174 EXPECT_EQ(ERR_PAC_NOT_IN_DHCP, client.fetcher_->GetResult());
175 EXPECT_EQ(base::string16(L""), client.fetcher_->GetPacScript());
178 TEST(DhcpProxyScriptAdapterFetcher, NormalCaseURLInDhcp) {
179 FetcherClient client;
180 client.RunTest();
181 client.WaitForResult(OK);
182 ASSERT_TRUE(client.fetcher_->DidFinish());
183 EXPECT_EQ(OK, client.fetcher_->GetResult());
184 EXPECT_EQ(base::string16(L"bingo"), client.fetcher_->GetPacScript());
185 EXPECT_EQ(GURL(kPacUrl), client.fetcher_->GetPacURL());
188 TEST(DhcpProxyScriptAdapterFetcher, TimeoutDuringDhcp) {
189 // Does a Fetch() with a long enough delay on accessing DHCP that the
190 // fetcher should time out. This is to test a case manual testing found,
191 // where under certain circumstances (e.g. adapter enabled for DHCP and
192 // needs to retrieve its configuration from DHCP, but no DHCP server
193 // present on the network) accessing DHCP can take on the order of tens
194 // of seconds.
195 FetcherClient client;
196 client.fetcher_->dhcp_delay_ = TestTimeouts::action_max_timeout();
197 client.fetcher_->timeout_ = base::TimeDelta::FromMilliseconds(25);
199 base::ElapsedTimer timer;
200 client.RunTest();
201 // An error different from this would be received if the timeout didn't
202 // kick in.
203 client.WaitForResult(ERR_TIMED_OUT);
205 ASSERT_TRUE(client.fetcher_->DidFinish());
206 EXPECT_EQ(ERR_TIMED_OUT, client.fetcher_->GetResult());
207 EXPECT_EQ(base::string16(L""), client.fetcher_->GetPacScript());
208 EXPECT_EQ(GURL(), client.fetcher_->GetPacURL());
209 client.FinishTestAllowCleanup();
212 TEST(DhcpProxyScriptAdapterFetcher, CancelWhileDhcp) {
213 FetcherClient client;
214 client.RunTest();
215 client.fetcher_->Cancel();
216 base::MessageLoop::current()->RunUntilIdle();
217 ASSERT_FALSE(client.fetcher_->DidFinish());
218 ASSERT_TRUE(client.fetcher_->WasCancelled());
219 EXPECT_EQ(ERR_ABORTED, client.fetcher_->GetResult());
220 EXPECT_EQ(base::string16(L""), client.fetcher_->GetPacScript());
221 EXPECT_EQ(GURL(), client.fetcher_->GetPacURL());
222 client.FinishTestAllowCleanup();
225 TEST(DhcpProxyScriptAdapterFetcher, CancelWhileFetcher) {
226 FetcherClient client;
227 // This causes the mock fetcher not to pretend the
228 // fetcher finishes after a timeout.
229 client.fetcher_->fetcher_delay_ms_ = -1;
230 client.RunTest();
231 int max_loops = 4;
232 while (!client.fetcher_->IsWaitingForFetcher() && max_loops--) {
233 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
234 base::MessageLoop::current()->RunUntilIdle();
236 client.fetcher_->Cancel();
237 base::MessageLoop::current()->RunUntilIdle();
238 ASSERT_FALSE(client.fetcher_->DidFinish());
239 ASSERT_TRUE(client.fetcher_->WasCancelled());
240 EXPECT_EQ(ERR_ABORTED, client.fetcher_->GetResult());
241 EXPECT_EQ(base::string16(L""), client.fetcher_->GetPacScript());
242 // GetPacURL() still returns the URL fetched in this case.
243 EXPECT_EQ(GURL(kPacUrl), client.fetcher_->GetPacURL());
244 client.FinishTestAllowCleanup();
247 TEST(DhcpProxyScriptAdapterFetcher, CancelAtCompletion) {
248 FetcherClient client;
249 client.RunTest();
250 client.WaitForResult(OK);
251 client.fetcher_->Cancel();
252 // Canceling after you're done should have no effect, so these
253 // are identical expectations to the NormalCaseURLInDhcp test.
254 ASSERT_TRUE(client.fetcher_->DidFinish());
255 EXPECT_EQ(OK, client.fetcher_->GetResult());
256 EXPECT_EQ(base::string16(L"bingo"), client.fetcher_->GetPacScript());
257 EXPECT_EQ(GURL(kPacUrl), client.fetcher_->GetPacURL());
258 client.FinishTestAllowCleanup();
261 // Does a real fetch on a mock DHCP configuration.
262 class MockDhcpRealFetchProxyScriptAdapterFetcher
263 : public MockDhcpProxyScriptAdapterFetcher {
264 public:
265 explicit MockDhcpRealFetchProxyScriptAdapterFetcher(
266 URLRequestContext* context,
267 scoped_refptr<base::TaskRunner> task_runner)
268 : MockDhcpProxyScriptAdapterFetcher(context, task_runner),
269 url_request_context_(context) {
272 // Returns a real proxy script fetcher.
273 ProxyScriptFetcher* ImplCreateScriptFetcher() override {
274 ProxyScriptFetcher* fetcher =
275 new ProxyScriptFetcherImpl(url_request_context_);
276 return fetcher;
279 URLRequestContext* url_request_context_;
282 TEST(DhcpProxyScriptAdapterFetcher, MockDhcpRealFetch) {
283 SpawnedTestServer test_server(
284 SpawnedTestServer::TYPE_HTTP,
285 SpawnedTestServer::kLocalhost,
286 base::FilePath(
287 FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest")));
288 ASSERT_TRUE(test_server.Start());
290 GURL configured_url = test_server.GetURL("files/downloadable.pac");
292 FetcherClient client;
293 TestURLRequestContext url_request_context;
294 scoped_refptr<base::TaskRunner> runner =
295 client.worker_pool_->GetTaskRunnerWithShutdownBehavior(
296 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
297 client.fetcher_.reset(
298 new MockDhcpRealFetchProxyScriptAdapterFetcher(
299 &url_request_context, runner));
300 client.fetcher_->configured_url_ = configured_url.spec();
301 client.RunTest();
302 client.WaitForResult(OK);
303 ASSERT_TRUE(client.fetcher_->DidFinish());
304 EXPECT_EQ(OK, client.fetcher_->GetResult());
305 EXPECT_EQ(base::string16(L"-downloadable.pac-\n"),
306 client.fetcher_->GetPacScript());
307 EXPECT_EQ(configured_url,
308 client.fetcher_->GetPacURL());
311 #define BASE_URL "http://corpserver/proxy.pac"
313 TEST(DhcpProxyScriptAdapterFetcher, SanitizeDhcpApiString) {
314 const size_t kBaseUrlLen = strlen(BASE_URL);
316 // Default case.
317 EXPECT_EQ(BASE_URL,
318 DhcpProxyScriptAdapterFetcher::SanitizeDhcpApiString(
319 BASE_URL, kBaseUrlLen));
321 // Trailing \n and no null-termination.
322 EXPECT_EQ(BASE_URL,
323 DhcpProxyScriptAdapterFetcher::SanitizeDhcpApiString(
324 BASE_URL "\nblablabla", kBaseUrlLen + 1));
326 // Embedded NULLs.
327 EXPECT_EQ(BASE_URL,
328 DhcpProxyScriptAdapterFetcher::SanitizeDhcpApiString(
329 BASE_URL "\0foo\0blat", kBaseUrlLen + 9));
332 #undef BASE_URL
334 } // namespace
336 } // namespace net