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 void AddObserver(Observer
* observer
) override
{}
22 void RemoveObserver(Observer
* observer
) override
{}
23 ConfigAvailability
GetLatestProxyConfig(net::ProxyConfig
* results
) override
{
24 *results
= net::ProxyConfig::CreateFromCustomPacURL(GURL("http://pac"));
29 class TestResolveProxyMsgHelper
: public ResolveProxyMsgHelper
{
31 TestResolveProxyMsgHelper(
32 net::ProxyService
* proxy_service
,
33 IPC::Listener
* listener
)
34 : ResolveProxyMsgHelper(proxy_service
),
35 listener_(listener
) {}
36 bool Send(IPC::Message
* message
) override
{
37 listener_
->OnMessageReceived(*message
);
43 ~TestResolveProxyMsgHelper() override
{}
45 IPC::Listener
* listener_
;
48 class ResolveProxyMsgHelperTest
: public testing::Test
, public IPC::Listener
{
50 struct PendingResult
{
51 PendingResult(bool result
,
52 const std::string
& proxy_list
)
53 : result(result
), proxy_list(proxy_list
) {
57 std::string proxy_list
;
60 ResolveProxyMsgHelperTest()
61 : service_(new net::ProxyService(
62 new MockProxyConfigService
,
64 new net::ForwardingProxyResolverFactory(&resolver_
)),
66 helper_(new TestResolveProxyMsgHelper(service_
.get(), this)),
67 io_thread_(BrowserThread::IO
, &message_loop_
) {
68 test_sink_
.AddFilter(this);
72 const PendingResult
* pending_result() const { return pending_result_
.get(); }
74 void clear_pending_result() {
75 pending_result_
.reset();
78 IPC::Message
* GenerateReply() {
80 std::string temp_string
;
81 ViewHostMsg_ResolveProxy
message(GURL(), &temp_bool
, &temp_string
);
82 return IPC::SyncMessage::GenerateReply(&message
);
85 net::MockAsyncProxyResolver resolver_
;
86 scoped_ptr
<net::ProxyService
> service_
;
87 scoped_refptr
<ResolveProxyMsgHelper
> helper_
;
88 scoped_ptr
<PendingResult
> pending_result_
;
91 bool OnMessageReceived(const IPC::Message
& msg
) override
{
92 TupleTypes
<ViewHostMsg_ResolveProxy::ReplyParam
>::ValueTuple reply_data
;
93 EXPECT_TRUE(ViewHostMsg_ResolveProxy::ReadReplyParam(&msg
, &reply_data
));
94 DCHECK(!pending_result_
.get());
95 pending_result_
.reset(
96 new PendingResult(get
<0>(reply_data
), get
<1>(reply_data
)));
97 test_sink_
.ClearMessages();
101 base::MessageLoopForIO message_loop_
;
102 BrowserThreadImpl io_thread_
;
103 IPC::TestSink test_sink_
;
106 // Issue three sequential requests -- each should succeed.
107 TEST_F(ResolveProxyMsgHelperTest
, Sequential
) {
108 GURL
url1("http://www.google1.com/");
109 GURL
url2("http://www.google2.com/");
110 GURL
url3("http://www.google3.com/");
112 // Messages are deleted by the sink.
113 IPC::Message
* msg1
= GenerateReply();
114 IPC::Message
* msg2
= GenerateReply();
115 IPC::Message
* msg3
= GenerateReply();
117 // Execute each request sequentially (so there are never 2 requests
118 // outstanding at the same time).
120 helper_
->OnResolveProxy(url1
, msg1
);
122 // Finish ProxyService's initialization.
123 resolver_
.pending_set_pac_script_request()->CompleteNow(net::OK
);
125 ASSERT_EQ(1u, resolver_
.pending_requests().size());
126 EXPECT_EQ(url1
, resolver_
.pending_requests()[0]->url());
127 resolver_
.pending_requests()[0]->results()->UseNamedProxy("result1:80");
128 resolver_
.pending_requests()[0]->CompleteNow(net::OK
);
131 EXPECT_EQ(true, pending_result()->result
);
132 EXPECT_EQ("PROXY result1:80", pending_result()->proxy_list
);
133 clear_pending_result();
135 helper_
->OnResolveProxy(url2
, msg2
);
137 ASSERT_EQ(1u, resolver_
.pending_requests().size());
138 EXPECT_EQ(url2
, resolver_
.pending_requests()[0]->url());
139 resolver_
.pending_requests()[0]->results()->UseNamedProxy("result2:80");
140 resolver_
.pending_requests()[0]->CompleteNow(net::OK
);
143 EXPECT_EQ(true, pending_result()->result
);
144 EXPECT_EQ("PROXY result2:80", pending_result()->proxy_list
);
145 clear_pending_result();
147 helper_
->OnResolveProxy(url3
, msg3
);
149 ASSERT_EQ(1u, resolver_
.pending_requests().size());
150 EXPECT_EQ(url3
, resolver_
.pending_requests()[0]->url());
151 resolver_
.pending_requests()[0]->results()->UseNamedProxy("result3:80");
152 resolver_
.pending_requests()[0]->CompleteNow(net::OK
);
155 EXPECT_EQ(true, pending_result()->result
);
156 EXPECT_EQ("PROXY result3:80", pending_result()->proxy_list
);
157 clear_pending_result();
160 // Issue a request while one is already in progress -- should be queued.
161 TEST_F(ResolveProxyMsgHelperTest
, QueueRequests
) {
162 GURL
url1("http://www.google1.com/");
163 GURL
url2("http://www.google2.com/");
164 GURL
url3("http://www.google3.com/");
166 IPC::Message
* msg1
= GenerateReply();
167 IPC::Message
* msg2
= GenerateReply();
168 IPC::Message
* msg3
= GenerateReply();
170 // Start three requests. Since the proxy resolver is async, all the
171 // requests will be pending.
173 helper_
->OnResolveProxy(url1
, msg1
);
175 // Finish ProxyService's initialization.
176 resolver_
.pending_set_pac_script_request()->CompleteNow(net::OK
);
178 helper_
->OnResolveProxy(url2
, msg2
);
179 helper_
->OnResolveProxy(url3
, msg3
);
181 // ResolveProxyHelper only keeps 1 request outstanding in ProxyService
183 ASSERT_EQ(1u, resolver_
.pending_requests().size());
184 EXPECT_EQ(url1
, resolver_
.pending_requests()[0]->url());
186 resolver_
.pending_requests()[0]->results()->UseNamedProxy("result1:80");
187 resolver_
.pending_requests()[0]->CompleteNow(net::OK
);
190 EXPECT_EQ(true, pending_result()->result
);
191 EXPECT_EQ("PROXY result1:80", pending_result()->proxy_list
);
192 clear_pending_result();
194 ASSERT_EQ(1u, resolver_
.pending_requests().size());
195 EXPECT_EQ(url2
, resolver_
.pending_requests()[0]->url());
197 resolver_
.pending_requests()[0]->results()->UseNamedProxy("result2:80");
198 resolver_
.pending_requests()[0]->CompleteNow(net::OK
);
201 EXPECT_EQ(true, pending_result()->result
);
202 EXPECT_EQ("PROXY result2:80", pending_result()->proxy_list
);
203 clear_pending_result();
205 ASSERT_EQ(1u, resolver_
.pending_requests().size());
206 EXPECT_EQ(url3
, resolver_
.pending_requests()[0]->url());
208 resolver_
.pending_requests()[0]->results()->UseNamedProxy("result3:80");
209 resolver_
.pending_requests()[0]->CompleteNow(net::OK
);
212 EXPECT_EQ(true, pending_result()->result
);
213 EXPECT_EQ("PROXY result3:80", pending_result()->proxy_list
);
214 clear_pending_result();
217 // Delete the helper while a request is in progress, and others are pending.
218 TEST_F(ResolveProxyMsgHelperTest
, CancelPendingRequests
) {
219 GURL
url1("http://www.google1.com/");
220 GURL
url2("http://www.google2.com/");
221 GURL
url3("http://www.google3.com/");
223 // They will be deleted by the request's cancellation.
224 IPC::Message
* msg1
= GenerateReply();
225 IPC::Message
* msg2
= GenerateReply();
226 IPC::Message
* msg3
= GenerateReply();
228 // Start three requests. Since the proxy resolver is async, all the
229 // requests will be pending.
231 helper_
->OnResolveProxy(url1
, msg1
);
233 // Finish ProxyService's initialization.
234 resolver_
.pending_set_pac_script_request()->CompleteNow(net::OK
);
236 helper_
->OnResolveProxy(url2
, msg2
);
237 helper_
->OnResolveProxy(url3
, msg3
);
239 // ResolveProxyHelper only keeps 1 request outstanding in ProxyService
241 ASSERT_EQ(1u, resolver_
.pending_requests().size());
242 EXPECT_EQ(url1
, resolver_
.pending_requests()[0]->url());
244 // Delete the underlying ResolveProxyMsgHelper -- this should cancel all
245 // the requests which are outstanding.
248 // The pending requests sent to the proxy resolver should have been cancelled.
250 EXPECT_EQ(0u, resolver_
.pending_requests().size());
252 EXPECT_TRUE(pending_result() == NULL
);
254 // It should also be the case that msg1, msg2, msg3 were deleted by the
255 // cancellation. (Else will show up as a leak in Valgrind).
258 } // namespace content