1 // Copyright 2015 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/mojo_proxy_resolver_factory_impl.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "net/base/test_completion_callback.h"
9 #include "net/proxy/mock_proxy_resolver.h"
10 #include "net/proxy/proxy_resolver_error_observer.h"
11 #include "net/test/event_waiter.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
14 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h"
19 const char kScriptData
[] = "FooBarBaz";
21 class FakeProxyResolver
: public MockAsyncProxyResolver
{
23 explicit FakeProxyResolver(const base::Closure
& on_destruction
)
24 : on_destruction_(on_destruction
) {}
26 ~FakeProxyResolver() override
{ on_destruction_
.Run(); }
29 const base::Closure on_destruction_
;
39 class TestProxyResolverFactory
: public MockAsyncProxyResolverFactory
{
41 explicit TestProxyResolverFactory(EventWaiter
<Event
>* waiter
)
42 : MockAsyncProxyResolverFactory(true), waiter_(waiter
) {}
44 int CreateProxyResolver(
45 const scoped_refptr
<ProxyResolverScriptData
>& pac_script
,
46 scoped_ptr
<ProxyResolver
>* resolver
,
47 const CompletionCallback
& callback
,
48 scoped_ptr
<ProxyResolverFactory::Request
>* request
) override
{
49 waiter_
->NotifyEvent(RESOLVER_CREATED
);
50 return MockAsyncProxyResolverFactory::CreateProxyResolver(
51 pac_script
, resolver
, callback
, request
);
55 EventWaiter
<Event
>* waiter_
;
60 class MojoProxyResolverFactoryImplTest
61 : public testing::Test
,
62 public mojo::ErrorHandler
,
63 public interfaces::ProxyResolverFactoryRequestClient
{
65 void SetUp() override
{
66 new MojoProxyResolverFactoryImpl(
68 &MojoProxyResolverFactoryImplTest::CreateFakeProxyResolverFactory
,
69 base::Unretained(this)),
70 mojo::GetProxy(&factory_
));
71 mock_factory_owner_
.reset(new TestProxyResolverFactory(&waiter_
));
72 mock_factory_
= mock_factory_owner_
.get();
75 void OnConnectionError() override
{ waiter_
.NotifyEvent(CONNECTION_ERROR
); }
77 scoped_ptr
<ProxyResolverFactory
> CreateFakeProxyResolverFactory(
78 HostResolver
* host_resolver
,
79 scoped_ptr
<ProxyResolverErrorObserver
> error_observer
,
80 const ProxyResolver::LoadStateChangedCallback
& callback
) {
81 EXPECT_TRUE(host_resolver
);
82 EXPECT_FALSE(callback
.is_null());
83 DCHECK(mock_factory_owner_
);
84 return mock_factory_owner_
.Pass();
87 void OnFakeProxyInstanceDestroyed() {
88 instances_destroyed_
++;
89 waiter_
.NotifyEvent(RESOLVER_DESTROYED
);
92 void ReportResult(int32_t error
) override
{ create_callback_
.Run(error
); }
95 scoped_ptr
<TestProxyResolverFactory
> mock_factory_owner_
;
96 TestProxyResolverFactory
* mock_factory_
;
97 interfaces::ProxyResolverFactoryPtr factory_
;
99 int instances_destroyed_
= 0;
100 CompletionCallback create_callback_
;
102 EventWaiter
<Event
> waiter_
;
105 TEST_F(MojoProxyResolverFactoryImplTest
, DisconnectHostResolver
) {
106 interfaces::ProxyResolverPtr proxy_resolver
;
107 interfaces::HostResolverPtr host_resolver
;
108 mojo::InterfaceRequest
<interfaces::HostResolver
> host_resolver_request
=
109 mojo::GetProxy(&host_resolver
);
110 interfaces::ProxyResolverErrorObserverPtr error_observer
;
111 mojo::GetProxy(&error_observer
);
112 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr
;
113 mojo::Binding
<ProxyResolverFactoryRequestClient
> client_binding(
114 this, mojo::GetProxy(&client_ptr
));
115 factory_
->CreateResolver(
116 mojo::String::From(kScriptData
), mojo::GetProxy(&proxy_resolver
),
117 host_resolver
.Pass(), error_observer
.Pass(), client_ptr
.Pass());
118 proxy_resolver
.set_error_handler(this);
119 waiter_
.WaitForEvent(RESOLVER_CREATED
);
120 EXPECT_EQ(0, instances_destroyed_
);
121 ASSERT_EQ(1u, mock_factory_
->pending_requests().size());
122 EXPECT_EQ(base::ASCIIToUTF16(kScriptData
),
123 mock_factory_
->pending_requests()[0]->script_data()->utf16());
124 TestCompletionCallback create_callback
;
125 create_callback_
= create_callback
.callback();
126 mock_factory_
->pending_requests()[0]->CompleteNow(
127 OK
, make_scoped_ptr(new FakeProxyResolver(base::Bind(
128 &MojoProxyResolverFactoryImplTest::OnFakeProxyInstanceDestroyed
,
129 base::Unretained(this)))));
130 EXPECT_EQ(OK
, create_callback
.WaitForResult());
131 host_resolver_request
= mojo::InterfaceRequest
<interfaces::HostResolver
>();
132 waiter_
.WaitForEvent(CONNECTION_ERROR
);
133 EXPECT_EQ(1, instances_destroyed_
);
136 TEST_F(MojoProxyResolverFactoryImplTest
, DisconnectProxyResolverClient
) {
137 interfaces::ProxyResolverPtr proxy_resolver
;
138 interfaces::HostResolverPtr host_resolver
;
139 mojo::InterfaceRequest
<interfaces::HostResolver
> host_resolver_request
=
140 mojo::GetProxy(&host_resolver
);
141 mojo::Binding
<interfaces::HostResolver
> binding(nullptr, &host_resolver
);
142 binding
.set_error_handler(this);
143 interfaces::ProxyResolverErrorObserverPtr error_observer
;
144 mojo::GetProxy(&error_observer
);
145 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr
;
146 mojo::Binding
<ProxyResolverFactoryRequestClient
> client_binding(
147 this, mojo::GetProxy(&client_ptr
));
148 factory_
->CreateResolver(
149 mojo::String::From(kScriptData
), mojo::GetProxy(&proxy_resolver
),
150 host_resolver
.Pass(), error_observer
.Pass(), client_ptr
.Pass());
151 proxy_resolver
.set_error_handler(this);
152 waiter_
.WaitForEvent(RESOLVER_CREATED
);
153 EXPECT_EQ(0, instances_destroyed_
);
154 ASSERT_EQ(1u, mock_factory_
->pending_requests().size());
155 EXPECT_EQ(base::ASCIIToUTF16(kScriptData
),
156 mock_factory_
->pending_requests()[0]->script_data()->utf16());
157 TestCompletionCallback create_callback
;
158 create_callback_
= create_callback
.callback();
159 mock_factory_
->pending_requests()[0]->CompleteNow(
160 OK
, make_scoped_ptr(new FakeProxyResolver(base::Bind(
161 &MojoProxyResolverFactoryImplTest::OnFakeProxyInstanceDestroyed
,
162 base::Unretained(this)))));
163 EXPECT_EQ(OK
, create_callback
.WaitForResult());
164 proxy_resolver
.reset();
165 waiter_
.WaitForEvent(CONNECTION_ERROR
);
166 EXPECT_EQ(1, instances_destroyed_
);
169 TEST_F(MojoProxyResolverFactoryImplTest
, DisconnectBoth
) {
170 interfaces::ProxyResolverPtr proxy_resolver
;
171 interfaces::HostResolverPtr host_resolver
;
172 mojo::InterfaceRequest
<interfaces::HostResolver
> host_resolver_request
=
173 mojo::GetProxy(&host_resolver
);
174 interfaces::ProxyResolverErrorObserverPtr error_observer
;
175 mojo::GetProxy(&error_observer
);
176 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr
;
177 mojo::Binding
<ProxyResolverFactoryRequestClient
> client_binding(
178 this, mojo::GetProxy(&client_ptr
));
179 factory_
->CreateResolver(
180 mojo::String::From(kScriptData
), mojo::GetProxy(&proxy_resolver
),
181 host_resolver
.Pass(), error_observer
.Pass(), client_ptr
.Pass());
182 proxy_resolver
.set_error_handler(this);
183 waiter_
.WaitForEvent(RESOLVER_CREATED
);
184 EXPECT_EQ(0, instances_destroyed_
);
185 ASSERT_EQ(1u, mock_factory_
->pending_requests().size());
186 EXPECT_EQ(base::ASCIIToUTF16(kScriptData
),
187 mock_factory_
->pending_requests()[0]->script_data()->utf16());
188 TestCompletionCallback create_callback
;
189 create_callback_
= create_callback
.callback();
190 mock_factory_
->pending_requests()[0]->CompleteNow(
191 OK
, make_scoped_ptr(new FakeProxyResolver(base::Bind(
192 &MojoProxyResolverFactoryImplTest::OnFakeProxyInstanceDestroyed
,
193 base::Unretained(this)))));
194 EXPECT_EQ(OK
, create_callback
.WaitForResult());
195 proxy_resolver
.reset();
196 host_resolver_request
= mojo::InterfaceRequest
<interfaces::HostResolver
>();
197 waiter_
.WaitForEvent(RESOLVER_DESTROYED
);
198 EXPECT_EQ(1, instances_destroyed_
);
201 TEST_F(MojoProxyResolverFactoryImplTest
, Error
) {
202 interfaces::ProxyResolverPtr proxy_resolver
;
203 interfaces::HostResolverPtr host_resolver
;
204 mojo::InterfaceRequest
<interfaces::HostResolver
> host_resolver_request
=
205 mojo::GetProxy(&host_resolver
);
206 interfaces::ProxyResolverErrorObserverPtr error_observer
;
207 mojo::GetProxy(&error_observer
);
208 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr
;
209 mojo::Binding
<ProxyResolverFactoryRequestClient
> client_binding(
210 this, mojo::GetProxy(&client_ptr
));
211 factory_
->CreateResolver(
212 mojo::String::From(kScriptData
), mojo::GetProxy(&proxy_resolver
),
213 host_resolver
.Pass(), error_observer
.Pass(), client_ptr
.Pass());
214 proxy_resolver
.set_error_handler(this);
215 waiter_
.WaitForEvent(RESOLVER_CREATED
);
216 EXPECT_EQ(0, instances_destroyed_
);
217 ASSERT_EQ(1u, mock_factory_
->pending_requests().size());
218 EXPECT_EQ(base::ASCIIToUTF16(kScriptData
),
219 mock_factory_
->pending_requests()[0]->script_data()->utf16());
220 TestCompletionCallback create_callback
;
221 create_callback_
= create_callback
.callback();
222 mock_factory_
->pending_requests()[0]->CompleteNow(ERR_PAC_SCRIPT_FAILED
,
224 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED
, create_callback
.WaitForResult());
227 TEST_F(MojoProxyResolverFactoryImplTest
,
228 DisconnectHostResolverDuringResolveCreation
) {
229 interfaces::ProxyResolverPtr proxy_resolver
;
230 interfaces::HostResolverPtr host_resolver
;
231 mojo::InterfaceRequest
<interfaces::HostResolver
> host_resolver_request
=
232 mojo::GetProxy(&host_resolver
);
233 interfaces::ProxyResolverErrorObserverPtr error_observer
;
234 mojo::GetProxy(&error_observer
);
235 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr
;
236 mojo::Binding
<ProxyResolverFactoryRequestClient
> client_binding(
237 this, mojo::GetProxy(&client_ptr
));
238 factory_
->CreateResolver(
239 mojo::String::From(kScriptData
), mojo::GetProxy(&proxy_resolver
),
240 host_resolver
.Pass(), error_observer
.Pass(), client_ptr
.Pass());
241 proxy_resolver
.set_error_handler(this);
242 waiter_
.WaitForEvent(RESOLVER_CREATED
);
243 EXPECT_EQ(0, instances_destroyed_
);
244 ASSERT_EQ(1u, mock_factory_
->pending_requests().size());
245 EXPECT_EQ(base::ASCIIToUTF16(kScriptData
),
246 mock_factory_
->pending_requests()[0]->script_data()->utf16());
247 host_resolver_request
= mojo::InterfaceRequest
<interfaces::HostResolver
>();
248 TestCompletionCallback create_callback
;
249 create_callback_
= create_callback
.callback();
250 waiter_
.WaitForEvent(CONNECTION_ERROR
);
251 EXPECT_EQ(ERR_PAC_SCRIPT_TERMINATED
, create_callback
.WaitForResult());
254 TEST_F(MojoProxyResolverFactoryImplTest
,
255 DisconnectClientDuringResolverCreation
) {
256 interfaces::ProxyResolverPtr proxy_resolver
;
257 interfaces::HostResolverPtr host_resolver
;
258 mojo::InterfaceRequest
<interfaces::HostResolver
> host_resolver_request
=
259 mojo::GetProxy(&host_resolver
);
260 mojo::Binding
<interfaces::HostResolver
> binding(nullptr, &host_resolver
);
261 binding
.set_error_handler(this);
262 interfaces::ProxyResolverErrorObserverPtr error_observer
;
263 mojo::GetProxy(&error_observer
);
264 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr
;
265 mojo::Binding
<ProxyResolverFactoryRequestClient
> client_binding(
266 this, mojo::GetProxy(&client_ptr
));
267 factory_
->CreateResolver(
268 mojo::String::From(kScriptData
), mojo::GetProxy(&proxy_resolver
),
269 host_resolver
.Pass(), error_observer
.Pass(), client_ptr
.Pass());
270 proxy_resolver
.set_error_handler(this);
271 waiter_
.WaitForEvent(RESOLVER_CREATED
);
272 EXPECT_EQ(0, instances_destroyed_
);
273 ASSERT_EQ(1u, mock_factory_
->pending_requests().size());
274 EXPECT_EQ(base::ASCIIToUTF16(kScriptData
),
275 mock_factory_
->pending_requests()[0]->script_data()->utf16());
276 client_binding
.Close();
277 waiter_
.WaitForEvent(CONNECTION_ERROR
);
280 TEST_F(MojoProxyResolverFactoryImplTest
,
281 DisconnectFactoryDuringResolverCreation
) {
282 interfaces::ProxyResolverPtr proxy_resolver
;
283 interfaces::HostResolverPtr host_resolver
;
284 mojo::InterfaceRequest
<interfaces::HostResolver
> host_resolver_request
=
285 mojo::GetProxy(&host_resolver
);
286 mojo::Binding
<interfaces::HostResolver
> binding(nullptr, &host_resolver
);
287 binding
.set_error_handler(this);
288 interfaces::ProxyResolverErrorObserverPtr error_observer
;
289 mojo::GetProxy(&error_observer
);
290 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr
;
291 mojo::Binding
<ProxyResolverFactoryRequestClient
> client_binding(
292 this, mojo::GetProxy(&client_ptr
));
293 factory_
->CreateResolver(
294 mojo::String::From(kScriptData
), mojo::GetProxy(&proxy_resolver
),
295 host_resolver
.Pass(), error_observer
.Pass(), client_ptr
.Pass());
296 proxy_resolver
.set_error_handler(this);
297 client_binding
.set_error_handler(this);
298 waiter_
.WaitForEvent(RESOLVER_CREATED
);
299 EXPECT_EQ(0, instances_destroyed_
);
300 ASSERT_EQ(1u, mock_factory_
->pending_requests().size());
301 EXPECT_EQ(base::ASCIIToUTF16(kScriptData
),
302 mock_factory_
->pending_requests()[0]->script_data()->utf16());
304 waiter_
.WaitForEvent(CONNECTION_ERROR
);