blob: 5e3dd0de58435ff2f1cef8468d99216182951034 [file] [log] [blame]
[email protected]f6b224d2011-03-15 17:16:551// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]41b2780f2009-01-31 01:34:202// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]97e6c4c2011-05-18 16:08:515#include "content/browser/resolve_proxy_msg_helper.h"
[email protected]41b2780f2009-01-31 01:34:206
[email protected]92145552011-10-31 16:28:037#include "content/browser/browser_thread_impl.h"
[email protected]2dd868f2011-08-29 23:03:578#include "content/common/view_messages.h"
[email protected]f6b224d2011-03-15 17:16:559#include "ipc/ipc_test_sink.h"
[email protected]41b2780f2009-01-31 01:34:2010#include "net/base/net_errors.h"
[email protected]c6efbc62009-08-06 12:52:1911#include "net/proxy/mock_proxy_resolver.h"
[email protected]e9e6b1c62009-03-02 22:51:0212#include "net/proxy/proxy_config_service.h"
[email protected]6104ea5d2011-04-27 21:37:1213#include "net/proxy/proxy_service.h"
[email protected]41b2780f2009-01-31 01:34:2014#include "testing/gtest/include/gtest/gtest.h"
15
[email protected]46488322012-10-30 03:22:2016namespace content {
[email protected]92145552011-10-31 16:28:0317
[email protected]41b2780f2009-01-31 01:34:2018// This ProxyConfigService always returns "http://pac" as the PAC url to use.
[email protected]c6efbc62009-08-06 12:52:1919class MockProxyConfigService : public net::ProxyConfigService {
[email protected]41b2780f2009-01-31 01:34:2020 public:
dchengc2282aa2014-10-21 12:07:5821 void AddObserver(Observer* observer) override {}
22 void RemoveObserver(Observer* observer) override {}
23 ConfigAvailability GetLatestProxyConfig(net::ProxyConfig* results) override {
[email protected]119655002010-07-23 06:02:4024 *results = net::ProxyConfig::CreateFromCustomPacURL(GURL("http://pac"));
[email protected]3a29593d2011-04-11 10:07:5225 return CONFIG_VALID;
[email protected]41b2780f2009-01-31 01:34:2026 }
27};
28
[email protected]ba780c12013-10-01 17:07:0629class TestResolveProxyMsgHelper : public ResolveProxyMsgHelper {
30 public:
31 TestResolveProxyMsgHelper(
32 net::ProxyService* proxy_service,
33 IPC::Listener* listener)
34 : ResolveProxyMsgHelper(proxy_service),
35 listener_(listener) {}
dchengc2282aa2014-10-21 12:07:5836 bool Send(IPC::Message* message) override {
[email protected]ba780c12013-10-01 17:07:0637 listener_->OnMessageReceived(*message);
38 delete message;
39 return true;
40 }
41
42 protected:
dchengc2282aa2014-10-21 12:07:5843 ~TestResolveProxyMsgHelper() override {}
[email protected]ba780c12013-10-01 17:07:0644
45 IPC::Listener* listener_;
46};
47
[email protected]d84effeb2012-06-25 17:03:1048class ResolveProxyMsgHelperTest : public testing::Test, public IPC::Listener {
[email protected]41b2780f2009-01-31 01:34:2049 public:
[email protected]c6efbc62009-08-06 12:52:1950 struct PendingResult {
[email protected]2dd868f2011-08-29 23:03:5751 PendingResult(bool result,
[email protected]c6efbc62009-08-06 12:52:1952 const std::string& proxy_list)
[email protected]2dd868f2011-08-29 23:03:5753 : result(result), proxy_list(proxy_list) {
[email protected]c6efbc62009-08-06 12:52:1954 }
[email protected]41b2780f2009-01-31 01:34:2055
[email protected]2dd868f2011-08-29 23:03:5756 bool result;
[email protected]c6efbc62009-08-06 12:52:1957 std::string proxy_list;
58 };
[email protected]41b2780f2009-01-31 01:34:2059
[email protected]f6b224d2011-03-15 17:16:5560 ResolveProxyMsgHelperTest()
sammce64b2362015-04-29 03:50:2361 : resolver_factory_(new net::MockAsyncProxyResolverFactory(false)),
csharrisonb7e3a082015-09-22 19:13:0462 service_(
63 new net::ProxyService(make_scoped_ptr(new MockProxyConfigService),
64 make_scoped_ptr(resolver_factory_),
65 NULL)),
[email protected]ba780c12013-10-01 17:07:0666 helper_(new TestResolveProxyMsgHelper(service_.get(), this)),
[email protected]f6b224d2011-03-15 17:16:5567 io_thread_(BrowserThread::IO, &message_loop_) {
68 test_sink_.AddFilter(this);
[email protected]c6efbc62009-08-06 12:52:1969 }
[email protected]a692c6f2009-08-06 12:11:0570
[email protected]f6b224d2011-03-15 17:16:5571 protected:
[email protected]c6efbc62009-08-06 12:52:1972 const PendingResult* pending_result() const { return pending_result_.get(); }
[email protected]a692c6f2009-08-06 12:11:0573
[email protected]c6efbc62009-08-06 12:52:1974 void clear_pending_result() {
75 pending_result_.reset();
[email protected]a692c6f2009-08-06 12:11:0576 }
77
[email protected]f6b224d2011-03-15 17:16:5578 IPC::Message* GenerateReply() {
[email protected]2dd868f2011-08-29 23:03:5779 bool temp_bool;
[email protected]f6b224d2011-03-15 17:16:5580 std::string temp_string;
[email protected]2dd868f2011-08-29 23:03:5781 ViewHostMsg_ResolveProxy message(GURL(), &temp_bool, &temp_string);
[email protected]f6b224d2011-03-15 17:16:5582 return IPC::SyncMessage::GenerateReply(&message);
83 }
84
sammce64b2362015-04-29 03:50:2385 net::MockAsyncProxyResolverFactory* resolver_factory_;
sammc5dd160c2015-04-02 02:43:1386 net::MockAsyncProxyResolver resolver_;
[email protected]6104ea5d2011-04-27 21:37:1287 scoped_ptr<net::ProxyService> service_;
[email protected]f6b224d2011-03-15 17:16:5588 scoped_refptr<ResolveProxyMsgHelper> helper_;
[email protected]c6efbc62009-08-06 12:52:1989 scoped_ptr<PendingResult> pending_result_;
[email protected]f6b224d2011-03-15 17:16:5590
91 private:
dchengc2282aa2014-10-21 12:07:5892 bool OnMessageReceived(const IPC::Message& msg) override {
brettwd5ca2bc2015-05-29 22:15:4793 base::TupleTypes<ViewHostMsg_ResolveProxy::ReplyParam>::ValueTuple
94 reply_data;
[email protected]2dd868f2011-08-29 23:03:5795 EXPECT_TRUE(ViewHostMsg_ResolveProxy::ReadReplyParam(&msg, &reply_data));
[email protected]f6b224d2011-03-15 17:16:5596 DCHECK(!pending_result_.get());
Avi Drissman95c2a1b72014-12-22 18:01:3297 pending_result_.reset(
brettwd5ca2bc2015-05-29 22:15:4798 new PendingResult(base::get<0>(reply_data), base::get<1>(reply_data)));
[email protected]f6b224d2011-03-15 17:16:5599 test_sink_.ClearMessages();
100 return true;
101 }
102
[email protected]1ef90012014-01-15 22:24:33103 base::MessageLoopForIO message_loop_;
[email protected]92145552011-10-31 16:28:03104 BrowserThreadImpl io_thread_;
[email protected]f6b224d2011-03-15 17:16:55105 IPC::TestSink test_sink_;
[email protected]a692c6f2009-08-06 12:11:05106};
107
[email protected]41b2780f2009-01-31 01:34:20108// Issue three sequential requests -- each should succeed.
[email protected]f6b224d2011-03-15 17:16:55109TEST_F(ResolveProxyMsgHelperTest, Sequential) {
[email protected]41b2780f2009-01-31 01:34:20110 GURL url1("http://www.google1.com/");
111 GURL url2("http://www.google2.com/");
112 GURL url3("http://www.google3.com/");
113
[email protected]f6b224d2011-03-15 17:16:55114 // Messages are deleted by the sink.
115 IPC::Message* msg1 = GenerateReply();
116 IPC::Message* msg2 = GenerateReply();
117 IPC::Message* msg3 = GenerateReply();
[email protected]41b2780f2009-01-31 01:34:20118
119 // Execute each request sequentially (so there are never 2 requests
120 // outstanding at the same time).
121
[email protected]f6b224d2011-03-15 17:16:55122 helper_->OnResolveProxy(url1, msg1);
[email protected]41b2780f2009-01-31 01:34:20123
[email protected]c6efbc62009-08-06 12:52:19124 // Finish ProxyService's initialization.
sammce64b2362015-04-29 03:50:23125 ASSERT_EQ(1u, resolver_factory_->pending_requests().size());
126 resolver_factory_->pending_requests()[0]->CompleteNowWithForwarder(
127 net::OK, &resolver_);
[email protected]41b2780f2009-01-31 01:34:20128
eroman9c8f4242016-02-29 21:16:54129 ASSERT_EQ(1u, resolver_.pending_requests().size());
130 EXPECT_EQ(url1, resolver_.pending_requests()[0]->url());
131 resolver_.pending_requests()[0]->results()->UseNamedProxy("result1:80");
132 resolver_.pending_requests()[0]->CompleteNow(net::OK);
[email protected]41b2780f2009-01-31 01:34:20133
[email protected]c6efbc62009-08-06 12:52:19134 // Check result.
[email protected]2dd868f2011-08-29 23:03:57135 EXPECT_EQ(true, pending_result()->result);
[email protected]f6b224d2011-03-15 17:16:55136 EXPECT_EQ("PROXY result1:80", pending_result()->proxy_list);
137 clear_pending_result();
[email protected]41b2780f2009-01-31 01:34:20138
[email protected]f6b224d2011-03-15 17:16:55139 helper_->OnResolveProxy(url2, msg2);
[email protected]41b2780f2009-01-31 01:34:20140
eroman9c8f4242016-02-29 21:16:54141 ASSERT_EQ(1u, resolver_.pending_requests().size());
142 EXPECT_EQ(url2, resolver_.pending_requests()[0]->url());
143 resolver_.pending_requests()[0]->results()->UseNamedProxy("result2:80");
144 resolver_.pending_requests()[0]->CompleteNow(net::OK);
[email protected]41b2780f2009-01-31 01:34:20145
[email protected]c6efbc62009-08-06 12:52:19146 // Check result.
[email protected]2dd868f2011-08-29 23:03:57147 EXPECT_EQ(true, pending_result()->result);
[email protected]f6b224d2011-03-15 17:16:55148 EXPECT_EQ("PROXY result2:80", pending_result()->proxy_list);
149 clear_pending_result();
[email protected]c6efbc62009-08-06 12:52:19150
[email protected]f6b224d2011-03-15 17:16:55151 helper_->OnResolveProxy(url3, msg3);
[email protected]c6efbc62009-08-06 12:52:19152
eroman9c8f4242016-02-29 21:16:54153 ASSERT_EQ(1u, resolver_.pending_requests().size());
154 EXPECT_EQ(url3, resolver_.pending_requests()[0]->url());
155 resolver_.pending_requests()[0]->results()->UseNamedProxy("result3:80");
156 resolver_.pending_requests()[0]->CompleteNow(net::OK);
[email protected]c6efbc62009-08-06 12:52:19157
158 // Check result.
[email protected]2dd868f2011-08-29 23:03:57159 EXPECT_EQ(true, pending_result()->result);
[email protected]f6b224d2011-03-15 17:16:55160 EXPECT_EQ("PROXY result3:80", pending_result()->proxy_list);
161 clear_pending_result();
[email protected]41b2780f2009-01-31 01:34:20162}
163
164// Issue a request while one is already in progress -- should be queued.
[email protected]f6b224d2011-03-15 17:16:55165TEST_F(ResolveProxyMsgHelperTest, QueueRequests) {
[email protected]41b2780f2009-01-31 01:34:20166 GURL url1("http://www.google1.com/");
167 GURL url2("http://www.google2.com/");
168 GURL url3("http://www.google3.com/");
169
[email protected]f6b224d2011-03-15 17:16:55170 IPC::Message* msg1 = GenerateReply();
171 IPC::Message* msg2 = GenerateReply();
172 IPC::Message* msg3 = GenerateReply();
[email protected]41b2780f2009-01-31 01:34:20173
[email protected]c6efbc62009-08-06 12:52:19174 // Start three requests. Since the proxy resolver is async, all the
175 // requests will be pending.
[email protected]41b2780f2009-01-31 01:34:20176
[email protected]f6b224d2011-03-15 17:16:55177 helper_->OnResolveProxy(url1, msg1);
[email protected]41b2780f2009-01-31 01:34:20178
[email protected]c6efbc62009-08-06 12:52:19179 // Finish ProxyService's initialization.
sammce64b2362015-04-29 03:50:23180 ASSERT_EQ(1u, resolver_factory_->pending_requests().size());
181 resolver_factory_->pending_requests()[0]->CompleteNowWithForwarder(
182 net::OK, &resolver_);
[email protected]41b2780f2009-01-31 01:34:20183
[email protected]f6b224d2011-03-15 17:16:55184 helper_->OnResolveProxy(url2, msg2);
185 helper_->OnResolveProxy(url3, msg3);
[email protected]41b2780f2009-01-31 01:34:20186
[email protected]c6efbc62009-08-06 12:52:19187 // ResolveProxyHelper only keeps 1 request outstanding in ProxyService
188 // at a time.
eroman9c8f4242016-02-29 21:16:54189 ASSERT_EQ(1u, resolver_.pending_requests().size());
190 EXPECT_EQ(url1, resolver_.pending_requests()[0]->url());
[email protected]41b2780f2009-01-31 01:34:20191
eroman9c8f4242016-02-29 21:16:54192 resolver_.pending_requests()[0]->results()->UseNamedProxy("result1:80");
193 resolver_.pending_requests()[0]->CompleteNow(net::OK);
[email protected]41b2780f2009-01-31 01:34:20194
[email protected]c6efbc62009-08-06 12:52:19195 // Check result.
[email protected]2dd868f2011-08-29 23:03:57196 EXPECT_EQ(true, pending_result()->result);
[email protected]f6b224d2011-03-15 17:16:55197 EXPECT_EQ("PROXY result1:80", pending_result()->proxy_list);
198 clear_pending_result();
[email protected]41b2780f2009-01-31 01:34:20199
eroman9c8f4242016-02-29 21:16:54200 ASSERT_EQ(1u, resolver_.pending_requests().size());
201 EXPECT_EQ(url2, resolver_.pending_requests()[0]->url());
[email protected]41b2780f2009-01-31 01:34:20202
eroman9c8f4242016-02-29 21:16:54203 resolver_.pending_requests()[0]->results()->UseNamedProxy("result2:80");
204 resolver_.pending_requests()[0]->CompleteNow(net::OK);
[email protected]41b2780f2009-01-31 01:34:20205
[email protected]c6efbc62009-08-06 12:52:19206 // Check result.
[email protected]2dd868f2011-08-29 23:03:57207 EXPECT_EQ(true, pending_result()->result);
[email protected]f6b224d2011-03-15 17:16:55208 EXPECT_EQ("PROXY result2:80", pending_result()->proxy_list);
209 clear_pending_result();
[email protected]c6efbc62009-08-06 12:52:19210
eroman9c8f4242016-02-29 21:16:54211 ASSERT_EQ(1u, resolver_.pending_requests().size());
212 EXPECT_EQ(url3, resolver_.pending_requests()[0]->url());
[email protected]c6efbc62009-08-06 12:52:19213
eroman9c8f4242016-02-29 21:16:54214 resolver_.pending_requests()[0]->results()->UseNamedProxy("result3:80");
215 resolver_.pending_requests()[0]->CompleteNow(net::OK);
[email protected]c6efbc62009-08-06 12:52:19216
217 // Check result.
[email protected]2dd868f2011-08-29 23:03:57218 EXPECT_EQ(true, pending_result()->result);
[email protected]f6b224d2011-03-15 17:16:55219 EXPECT_EQ("PROXY result3:80", pending_result()->proxy_list);
220 clear_pending_result();
[email protected]41b2780f2009-01-31 01:34:20221}
222
223// Delete the helper while a request is in progress, and others are pending.
[email protected]f6b224d2011-03-15 17:16:55224TEST_F(ResolveProxyMsgHelperTest, CancelPendingRequests) {
[email protected]41b2780f2009-01-31 01:34:20225 GURL url1("http://www.google1.com/");
226 GURL url2("http://www.google2.com/");
227 GURL url3("http://www.google3.com/");
228
[email protected]f6b224d2011-03-15 17:16:55229 // They will be deleted by the request's cancellation.
230 IPC::Message* msg1 = GenerateReply();
231 IPC::Message* msg2 = GenerateReply();
232 IPC::Message* msg3 = GenerateReply();
[email protected]41b2780f2009-01-31 01:34:20233
[email protected]c6efbc62009-08-06 12:52:19234 // Start three requests. Since the proxy resolver is async, all the
235 // requests will be pending.
[email protected]41b2780f2009-01-31 01:34:20236
[email protected]f6b224d2011-03-15 17:16:55237 helper_->OnResolveProxy(url1, msg1);
[email protected]41b2780f2009-01-31 01:34:20238
[email protected]c6efbc62009-08-06 12:52:19239 // Finish ProxyService's initialization.
sammce64b2362015-04-29 03:50:23240 ASSERT_EQ(1u, resolver_factory_->pending_requests().size());
241 resolver_factory_->pending_requests()[0]->CompleteNowWithForwarder(
242 net::OK, &resolver_);
[email protected]41b2780f2009-01-31 01:34:20243
[email protected]f6b224d2011-03-15 17:16:55244 helper_->OnResolveProxy(url2, msg2);
245 helper_->OnResolveProxy(url3, msg3);
[email protected]c6efbc62009-08-06 12:52:19246
247 // ResolveProxyHelper only keeps 1 request outstanding in ProxyService
248 // at a time.
eroman9c8f4242016-02-29 21:16:54249 ASSERT_EQ(1u, resolver_.pending_requests().size());
250 EXPECT_EQ(url1, resolver_.pending_requests()[0]->url());
[email protected]41b2780f2009-01-31 01:34:20251
252 // Delete the underlying ResolveProxyMsgHelper -- this should cancel all
253 // the requests which are outstanding.
[email protected]f6b224d2011-03-15 17:16:55254 helper_ = NULL;
[email protected]41b2780f2009-01-31 01:34:20255
[email protected]c6efbc62009-08-06 12:52:19256 // The pending requests sent to the proxy resolver should have been cancelled.
[email protected]41b2780f2009-01-31 01:34:20257
eroman9c8f4242016-02-29 21:16:54258 EXPECT_EQ(0u, resolver_.pending_requests().size());
[email protected]c6efbc62009-08-06 12:52:19259
[email protected]f6b224d2011-03-15 17:16:55260 EXPECT_TRUE(pending_result() == NULL);
[email protected]41b2780f2009-01-31 01:34:20261
262 // It should also be the case that msg1, msg2, msg3 were deleted by the
[email protected]64e95e12011-08-17 17:41:02263 // cancellation. (Else will show up as a leak in Valgrind).
[email protected]41b2780f2009-01-31 01:34:20264}
[email protected]46488322012-10-30 03:22:20265
266} // namespace content