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
3 // found in the LICENSE file.
5 #include "content/browser/resolve_proxy_msg_helper.h"
7 #include "content/browser/browser_thread_impl.h"
8 #include "content/common/view_messages.h"
9 #include "ipc/ipc_test_sink.h"
10 #include "net/base/net_errors.h"
11 #include "net/proxy/mock_proxy_resolver.h"
12 #include "net/proxy/proxy_config_service.h"
13 #include "net/proxy/proxy_service.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 // This ProxyConfigService always returns "http://pac" as the PAC url to use.
19 class MockProxyConfigService
: public net::ProxyConfigService
{
21 virtual void AddObserver(Observer
* observer
) {}
22 virtual void RemoveObserver(Observer
* observer
) {}
23 virtual ConfigAvailability
GetLatestProxyConfig(net::ProxyConfig
* results
) {
24 *results
= net::ProxyConfig::CreateFromCustomPacURL(GURL("http://pac"));
29 class ResolveProxyMsgHelperTest
: public testing::Test
, public IPC::Listener
{
31 struct PendingResult
{
32 PendingResult(bool result
,
33 const std::string
& proxy_list
)
34 : result(result
), proxy_list(proxy_list
) {
38 std::string proxy_list
;
41 ResolveProxyMsgHelperTest()
42 : resolver_(new net::MockAsyncProxyResolver
),
43 service_(new net::ProxyService(
44 new MockProxyConfigService
, resolver_
, NULL
)),
45 helper_(new ResolveProxyMsgHelper(service_
.get())),
46 message_loop_(MessageLoop::TYPE_IO
),
47 io_thread_(BrowserThread::IO
, &message_loop_
) {
48 test_sink_
.AddFilter(this);
49 helper_
->OnFilterAdded(&test_sink_
);
53 const PendingResult
* pending_result() const { return pending_result_
.get(); }
55 void clear_pending_result() {
56 pending_result_
.reset();
59 IPC::Message
* GenerateReply() {
61 std::string temp_string
;
62 ViewHostMsg_ResolveProxy
message(GURL(), &temp_bool
, &temp_string
);
63 return IPC::SyncMessage::GenerateReply(&message
);
66 net::MockAsyncProxyResolver
* resolver_
;
67 scoped_ptr
<net::ProxyService
> service_
;
68 scoped_refptr
<ResolveProxyMsgHelper
> helper_
;
69 scoped_ptr
<PendingResult
> pending_result_
;
72 virtual bool OnMessageReceived(const IPC::Message
& msg
) {
73 TupleTypes
<ViewHostMsg_ResolveProxy::ReplyParam
>::ValueTuple reply_data
;
74 EXPECT_TRUE(ViewHostMsg_ResolveProxy::ReadReplyParam(&msg
, &reply_data
));
75 DCHECK(!pending_result_
.get());
76 pending_result_
.reset(new PendingResult(reply_data
.a
, reply_data
.b
));
77 test_sink_
.ClearMessages();
81 MessageLoop message_loop_
;
82 BrowserThreadImpl io_thread_
;
83 IPC::TestSink test_sink_
;
86 // Issue three sequential requests -- each should succeed.
87 TEST_F(ResolveProxyMsgHelperTest
, Sequential
) {
88 GURL
url1("http://www.google1.com/");
89 GURL
url2("http://www.google2.com/");
90 GURL
url3("http://www.google3.com/");
92 // Messages are deleted by the sink.
93 IPC::Message
* msg1
= GenerateReply();
94 IPC::Message
* msg2
= GenerateReply();
95 IPC::Message
* msg3
= GenerateReply();
97 // Execute each request sequentially (so there are never 2 requests
98 // outstanding at the same time).
100 helper_
->OnResolveProxy(url1
, msg1
);
102 // Finish ProxyService's initialization.
103 resolver_
->pending_set_pac_script_request()->CompleteNow(net::OK
);
105 ASSERT_EQ(1u, resolver_
->pending_requests().size());
106 EXPECT_EQ(url1
, resolver_
->pending_requests()[0]->url());
107 resolver_
->pending_requests()[0]->results()->UseNamedProxy("result1:80");
108 resolver_
->pending_requests()[0]->CompleteNow(net::OK
);
111 EXPECT_EQ(true, pending_result()->result
);
112 EXPECT_EQ("PROXY result1:80", pending_result()->proxy_list
);
113 clear_pending_result();
115 helper_
->OnResolveProxy(url2
, msg2
);
117 ASSERT_EQ(1u, resolver_
->pending_requests().size());
118 EXPECT_EQ(url2
, resolver_
->pending_requests()[0]->url());
119 resolver_
->pending_requests()[0]->results()->UseNamedProxy("result2:80");
120 resolver_
->pending_requests()[0]->CompleteNow(net::OK
);
123 EXPECT_EQ(true, pending_result()->result
);
124 EXPECT_EQ("PROXY result2:80", pending_result()->proxy_list
);
125 clear_pending_result();
127 helper_
->OnResolveProxy(url3
, msg3
);
129 ASSERT_EQ(1u, resolver_
->pending_requests().size());
130 EXPECT_EQ(url3
, resolver_
->pending_requests()[0]->url());
131 resolver_
->pending_requests()[0]->results()->UseNamedProxy("result3:80");
132 resolver_
->pending_requests()[0]->CompleteNow(net::OK
);
135 EXPECT_EQ(true, pending_result()->result
);
136 EXPECT_EQ("PROXY result3:80", pending_result()->proxy_list
);
137 clear_pending_result();
140 // Issue a request while one is already in progress -- should be queued.
141 TEST_F(ResolveProxyMsgHelperTest
, QueueRequests
) {
142 GURL
url1("http://www.google1.com/");
143 GURL
url2("http://www.google2.com/");
144 GURL
url3("http://www.google3.com/");
146 IPC::Message
* msg1
= GenerateReply();
147 IPC::Message
* msg2
= GenerateReply();
148 IPC::Message
* msg3
= GenerateReply();
150 // Start three requests. Since the proxy resolver is async, all the
151 // requests will be pending.
153 helper_
->OnResolveProxy(url1
, msg1
);
155 // Finish ProxyService's initialization.
156 resolver_
->pending_set_pac_script_request()->CompleteNow(net::OK
);
158 helper_
->OnResolveProxy(url2
, msg2
);
159 helper_
->OnResolveProxy(url3
, msg3
);
161 // ResolveProxyHelper only keeps 1 request outstanding in ProxyService
163 ASSERT_EQ(1u, resolver_
->pending_requests().size());
164 EXPECT_EQ(url1
, resolver_
->pending_requests()[0]->url());
166 resolver_
->pending_requests()[0]->results()->UseNamedProxy("result1:80");
167 resolver_
->pending_requests()[0]->CompleteNow(net::OK
);
170 EXPECT_EQ(true, pending_result()->result
);
171 EXPECT_EQ("PROXY result1:80", pending_result()->proxy_list
);
172 clear_pending_result();
174 ASSERT_EQ(1u, resolver_
->pending_requests().size());
175 EXPECT_EQ(url2
, resolver_
->pending_requests()[0]->url());
177 resolver_
->pending_requests()[0]->results()->UseNamedProxy("result2:80");
178 resolver_
->pending_requests()[0]->CompleteNow(net::OK
);
181 EXPECT_EQ(true, pending_result()->result
);
182 EXPECT_EQ("PROXY result2:80", pending_result()->proxy_list
);
183 clear_pending_result();
185 ASSERT_EQ(1u, resolver_
->pending_requests().size());
186 EXPECT_EQ(url3
, resolver_
->pending_requests()[0]->url());
188 resolver_
->pending_requests()[0]->results()->UseNamedProxy("result3:80");
189 resolver_
->pending_requests()[0]->CompleteNow(net::OK
);
192 EXPECT_EQ(true, pending_result()->result
);
193 EXPECT_EQ("PROXY result3:80", pending_result()->proxy_list
);
194 clear_pending_result();
197 // Delete the helper while a request is in progress, and others are pending.
198 TEST_F(ResolveProxyMsgHelperTest
, CancelPendingRequests
) {
199 GURL
url1("http://www.google1.com/");
200 GURL
url2("http://www.google2.com/");
201 GURL
url3("http://www.google3.com/");
203 // They will be deleted by the request's cancellation.
204 IPC::Message
* msg1
= GenerateReply();
205 IPC::Message
* msg2
= GenerateReply();
206 IPC::Message
* msg3
= GenerateReply();
208 // Start three requests. Since the proxy resolver is async, all the
209 // requests will be pending.
211 helper_
->OnResolveProxy(url1
, msg1
);
213 // Finish ProxyService's initialization.
214 resolver_
->pending_set_pac_script_request()->CompleteNow(net::OK
);
216 helper_
->OnResolveProxy(url2
, msg2
);
217 helper_
->OnResolveProxy(url3
, msg3
);
219 // ResolveProxyHelper only keeps 1 request outstanding in ProxyService
221 ASSERT_EQ(1u, resolver_
->pending_requests().size());
222 EXPECT_EQ(url1
, resolver_
->pending_requests()[0]->url());
224 // Delete the underlying ResolveProxyMsgHelper -- this should cancel all
225 // the requests which are outstanding.
228 // The pending requests sent to the proxy resolver should have been cancelled.
230 EXPECT_EQ(0u, resolver_
->pending_requests().size());
232 EXPECT_TRUE(pending_result() == NULL
);
234 // It should also be the case that msg1, msg2, msg3 were deleted by the
235 // cancellation. (Else will show up as a leak in Valgrind).
238 } // namespace content