Revert of Re-enabling webrtc Telemetry tests. (patchset #1 id:1 of https://codereview...
[chromium-blink-merge.git] / net / proxy / mojo_proxy_resolver_factory_impl_unittest.cc
blob162e4b0a7ad76d4fefa3b3a87aeb1223a53e76eb
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"
15 namespace net {
16 namespace {
18 const char kScriptData[] = "FooBarBaz";
20 class FakeProxyResolver : public MockAsyncProxyResolver {
21 public:
22 explicit FakeProxyResolver(const base::Closure& on_destruction)
23 : on_destruction_(on_destruction) {}
25 ~FakeProxyResolver() override { on_destruction_.Run(); }
27 private:
28 const base::Closure on_destruction_;
31 enum Event {
32 NONE,
33 RESOLVER_CREATED,
34 CONNECTION_ERROR,
35 RESOLVER_DESTROYED,
38 class TestProxyResolverFactory : public MockAsyncProxyResolverFactory {
39 public:
40 explicit TestProxyResolverFactory(EventWaiter<Event>* waiter)
41 : MockAsyncProxyResolverFactory(true), waiter_(waiter) {}
43 int CreateProxyResolver(
44 const scoped_refptr<ProxyResolverScriptData>& pac_script,
45 scoped_ptr<ProxyResolver>* resolver,
46 const CompletionCallback& callback,
47 scoped_ptr<ProxyResolverFactory::Request>* request) override {
48 waiter_->NotifyEvent(RESOLVER_CREATED);
49 return MockAsyncProxyResolverFactory::CreateProxyResolver(
50 pac_script, resolver, callback, request);
53 private:
54 EventWaiter<Event>* waiter_;
57 } // namespace
59 class MojoProxyResolverFactoryImplTest
60 : public testing::Test,
61 public interfaces::ProxyResolverFactoryRequestClient {
62 public:
63 void SetUp() override {
64 new MojoProxyResolverFactoryImpl(
65 base::Bind(
66 &MojoProxyResolverFactoryImplTest::CreateFakeProxyResolverFactory,
67 base::Unretained(this)),
68 mojo::GetProxy(&factory_));
69 mock_factory_owner_.reset(new TestProxyResolverFactory(&waiter_));
70 mock_factory_ = mock_factory_owner_.get();
73 void OnConnectionError() { waiter_.NotifyEvent(CONNECTION_ERROR); }
75 scoped_ptr<ProxyResolverFactory> CreateFakeProxyResolverFactory(
76 HostResolver* host_resolver,
77 scoped_ptr<ProxyResolverErrorObserver> error_observer) {
78 EXPECT_TRUE(host_resolver);
79 DCHECK(mock_factory_owner_);
80 return mock_factory_owner_.Pass();
83 void OnFakeProxyInstanceDestroyed() {
84 instances_destroyed_++;
85 waiter_.NotifyEvent(RESOLVER_DESTROYED);
88 void ReportResult(int32_t error) override { create_callback_.Run(error); }
90 protected:
91 scoped_ptr<TestProxyResolverFactory> mock_factory_owner_;
92 TestProxyResolverFactory* mock_factory_;
93 interfaces::ProxyResolverFactoryPtr factory_;
95 int instances_destroyed_ = 0;
96 CompletionCallback create_callback_;
98 EventWaiter<Event> waiter_;
101 TEST_F(MojoProxyResolverFactoryImplTest, DisconnectHostResolver) {
102 interfaces::ProxyResolverPtr proxy_resolver;
103 interfaces::HostResolverPtr host_resolver;
104 mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request =
105 mojo::GetProxy(&host_resolver);
106 interfaces::ProxyResolverErrorObserverPtr error_observer;
107 mojo::GetProxy(&error_observer);
108 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr;
109 mojo::Binding<ProxyResolverFactoryRequestClient> client_binding(
110 this, mojo::GetProxy(&client_ptr));
111 factory_->CreateResolver(
112 mojo::String::From(kScriptData), mojo::GetProxy(&proxy_resolver),
113 host_resolver.Pass(), error_observer.Pass(), client_ptr.Pass());
114 proxy_resolver.set_connection_error_handler(
115 base::Bind(&MojoProxyResolverFactoryImplTest::OnConnectionError,
116 base::Unretained(this)));
117 waiter_.WaitForEvent(RESOLVER_CREATED);
118 EXPECT_EQ(0, instances_destroyed_);
119 ASSERT_EQ(1u, mock_factory_->pending_requests().size());
120 EXPECT_EQ(base::ASCIIToUTF16(kScriptData),
121 mock_factory_->pending_requests()[0]->script_data()->utf16());
122 TestCompletionCallback create_callback;
123 create_callback_ = create_callback.callback();
124 mock_factory_->pending_requests()[0]->CompleteNow(
125 OK, make_scoped_ptr(new FakeProxyResolver(base::Bind(
126 &MojoProxyResolverFactoryImplTest::OnFakeProxyInstanceDestroyed,
127 base::Unretained(this)))));
128 EXPECT_EQ(OK, create_callback.WaitForResult());
129 host_resolver_request = mojo::InterfaceRequest<interfaces::HostResolver>();
130 waiter_.WaitForEvent(CONNECTION_ERROR);
131 EXPECT_EQ(1, instances_destroyed_);
134 TEST_F(MojoProxyResolverFactoryImplTest, DisconnectProxyResolverClient) {
135 interfaces::ProxyResolverPtr proxy_resolver;
136 interfaces::HostResolverPtr host_resolver;
137 mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request =
138 mojo::GetProxy(&host_resolver);
139 mojo::Binding<interfaces::HostResolver> binding(nullptr, &host_resolver);
140 binding.set_connection_error_handler(
141 base::Bind(&MojoProxyResolverFactoryImplTest::OnConnectionError,
142 base::Unretained(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_connection_error_handler(
152 base::Bind(&MojoProxyResolverFactoryImplTest::OnConnectionError,
153 base::Unretained(this)));
154 waiter_.WaitForEvent(RESOLVER_CREATED);
155 EXPECT_EQ(0, instances_destroyed_);
156 ASSERT_EQ(1u, mock_factory_->pending_requests().size());
157 EXPECT_EQ(base::ASCIIToUTF16(kScriptData),
158 mock_factory_->pending_requests()[0]->script_data()->utf16());
159 TestCompletionCallback create_callback;
160 create_callback_ = create_callback.callback();
161 mock_factory_->pending_requests()[0]->CompleteNow(
162 OK, make_scoped_ptr(new FakeProxyResolver(base::Bind(
163 &MojoProxyResolverFactoryImplTest::OnFakeProxyInstanceDestroyed,
164 base::Unretained(this)))));
165 EXPECT_EQ(OK, create_callback.WaitForResult());
166 proxy_resolver.reset();
167 waiter_.WaitForEvent(CONNECTION_ERROR);
168 EXPECT_EQ(1, instances_destroyed_);
171 TEST_F(MojoProxyResolverFactoryImplTest, DisconnectBoth) {
172 interfaces::ProxyResolverPtr proxy_resolver;
173 interfaces::HostResolverPtr host_resolver;
174 mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request =
175 mojo::GetProxy(&host_resolver);
176 interfaces::ProxyResolverErrorObserverPtr error_observer;
177 mojo::GetProxy(&error_observer);
178 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr;
179 mojo::Binding<ProxyResolverFactoryRequestClient> client_binding(
180 this, mojo::GetProxy(&client_ptr));
181 factory_->CreateResolver(
182 mojo::String::From(kScriptData), mojo::GetProxy(&proxy_resolver),
183 host_resolver.Pass(), error_observer.Pass(), client_ptr.Pass());
184 proxy_resolver.set_connection_error_handler(
185 base::Bind(&MojoProxyResolverFactoryImplTest::OnConnectionError,
186 base::Unretained(this)));
187 waiter_.WaitForEvent(RESOLVER_CREATED);
188 EXPECT_EQ(0, instances_destroyed_);
189 ASSERT_EQ(1u, mock_factory_->pending_requests().size());
190 EXPECT_EQ(base::ASCIIToUTF16(kScriptData),
191 mock_factory_->pending_requests()[0]->script_data()->utf16());
192 TestCompletionCallback create_callback;
193 create_callback_ = create_callback.callback();
194 mock_factory_->pending_requests()[0]->CompleteNow(
195 OK, make_scoped_ptr(new FakeProxyResolver(base::Bind(
196 &MojoProxyResolverFactoryImplTest::OnFakeProxyInstanceDestroyed,
197 base::Unretained(this)))));
198 EXPECT_EQ(OK, create_callback.WaitForResult());
199 proxy_resolver.reset();
200 host_resolver_request = mojo::InterfaceRequest<interfaces::HostResolver>();
201 waiter_.WaitForEvent(RESOLVER_DESTROYED);
202 EXPECT_EQ(1, instances_destroyed_);
205 TEST_F(MojoProxyResolverFactoryImplTest, Error) {
206 interfaces::ProxyResolverPtr proxy_resolver;
207 interfaces::HostResolverPtr host_resolver;
208 mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request =
209 mojo::GetProxy(&host_resolver);
210 interfaces::ProxyResolverErrorObserverPtr error_observer;
211 mojo::GetProxy(&error_observer);
212 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr;
213 mojo::Binding<ProxyResolverFactoryRequestClient> client_binding(
214 this, mojo::GetProxy(&client_ptr));
215 factory_->CreateResolver(
216 mojo::String::From(kScriptData), mojo::GetProxy(&proxy_resolver),
217 host_resolver.Pass(), error_observer.Pass(), client_ptr.Pass());
218 proxy_resolver.set_connection_error_handler(
219 base::Bind(&MojoProxyResolverFactoryImplTest::OnConnectionError,
220 base::Unretained(this)));
221 waiter_.WaitForEvent(RESOLVER_CREATED);
222 EXPECT_EQ(0, instances_destroyed_);
223 ASSERT_EQ(1u, mock_factory_->pending_requests().size());
224 EXPECT_EQ(base::ASCIIToUTF16(kScriptData),
225 mock_factory_->pending_requests()[0]->script_data()->utf16());
226 TestCompletionCallback create_callback;
227 create_callback_ = create_callback.callback();
228 mock_factory_->pending_requests()[0]->CompleteNow(ERR_PAC_SCRIPT_FAILED,
229 nullptr);
230 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, create_callback.WaitForResult());
233 TEST_F(MojoProxyResolverFactoryImplTest,
234 DisconnectHostResolverDuringResolveCreation) {
235 interfaces::ProxyResolverPtr proxy_resolver;
236 interfaces::HostResolverPtr host_resolver;
237 mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request =
238 mojo::GetProxy(&host_resolver);
239 interfaces::ProxyResolverErrorObserverPtr error_observer;
240 mojo::GetProxy(&error_observer);
241 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr;
242 mojo::Binding<ProxyResolverFactoryRequestClient> client_binding(
243 this, mojo::GetProxy(&client_ptr));
244 factory_->CreateResolver(
245 mojo::String::From(kScriptData), mojo::GetProxy(&proxy_resolver),
246 host_resolver.Pass(), error_observer.Pass(), client_ptr.Pass());
247 proxy_resolver.set_connection_error_handler(
248 base::Bind(&MojoProxyResolverFactoryImplTest::OnConnectionError,
249 base::Unretained(this)));
250 waiter_.WaitForEvent(RESOLVER_CREATED);
251 EXPECT_EQ(0, instances_destroyed_);
252 ASSERT_EQ(1u, mock_factory_->pending_requests().size());
253 EXPECT_EQ(base::ASCIIToUTF16(kScriptData),
254 mock_factory_->pending_requests()[0]->script_data()->utf16());
255 host_resolver_request = mojo::InterfaceRequest<interfaces::HostResolver>();
256 TestCompletionCallback create_callback;
257 create_callback_ = create_callback.callback();
258 waiter_.WaitForEvent(CONNECTION_ERROR);
259 EXPECT_EQ(ERR_PAC_SCRIPT_TERMINATED, create_callback.WaitForResult());
262 TEST_F(MojoProxyResolverFactoryImplTest,
263 DisconnectClientDuringResolverCreation) {
264 interfaces::ProxyResolverPtr proxy_resolver;
265 interfaces::HostResolverPtr host_resolver;
266 mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request =
267 mojo::GetProxy(&host_resolver);
268 mojo::Binding<interfaces::HostResolver> binding(nullptr, &host_resolver);
269 binding.set_connection_error_handler(
270 base::Bind(&MojoProxyResolverFactoryImplTest::OnConnectionError,
271 base::Unretained(this)));
272 interfaces::ProxyResolverErrorObserverPtr error_observer;
273 mojo::GetProxy(&error_observer);
274 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr;
275 mojo::Binding<ProxyResolverFactoryRequestClient> client_binding(
276 this, mojo::GetProxy(&client_ptr));
277 factory_->CreateResolver(
278 mojo::String::From(kScriptData), mojo::GetProxy(&proxy_resolver),
279 host_resolver.Pass(), error_observer.Pass(), client_ptr.Pass());
280 proxy_resolver.set_connection_error_handler(
281 base::Bind(&MojoProxyResolverFactoryImplTest::OnConnectionError,
282 base::Unretained(this)));
283 waiter_.WaitForEvent(RESOLVER_CREATED);
284 EXPECT_EQ(0, instances_destroyed_);
285 ASSERT_EQ(1u, mock_factory_->pending_requests().size());
286 EXPECT_EQ(base::ASCIIToUTF16(kScriptData),
287 mock_factory_->pending_requests()[0]->script_data()->utf16());
288 client_binding.Close();
289 waiter_.WaitForEvent(CONNECTION_ERROR);
292 TEST_F(MojoProxyResolverFactoryImplTest,
293 DisconnectFactoryDuringResolverCreation) {
294 interfaces::ProxyResolverPtr proxy_resolver;
295 interfaces::HostResolverPtr host_resolver;
296 mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request =
297 mojo::GetProxy(&host_resolver);
298 mojo::Binding<interfaces::HostResolver> binding(nullptr, &host_resolver);
299 binding.set_connection_error_handler(
300 base::Bind(&MojoProxyResolverFactoryImplTest::OnConnectionError,
301 base::Unretained(this)));
302 interfaces::ProxyResolverErrorObserverPtr error_observer;
303 mojo::GetProxy(&error_observer);
304 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr;
305 mojo::Binding<ProxyResolverFactoryRequestClient> client_binding(
306 this, mojo::GetProxy(&client_ptr));
307 factory_->CreateResolver(
308 mojo::String::From(kScriptData), mojo::GetProxy(&proxy_resolver),
309 host_resolver.Pass(), error_observer.Pass(), client_ptr.Pass());
310 proxy_resolver.set_connection_error_handler(
311 base::Bind(&MojoProxyResolverFactoryImplTest::OnConnectionError,
312 base::Unretained(this)));
313 client_binding.set_connection_error_handler(
314 base::Bind(&MojoProxyResolverFactoryImplTest::OnConnectionError,
315 base::Unretained(this)));
316 waiter_.WaitForEvent(RESOLVER_CREATED);
317 EXPECT_EQ(0, instances_destroyed_);
318 ASSERT_EQ(1u, mock_factory_->pending_requests().size());
319 EXPECT_EQ(base::ASCIIToUTF16(kScriptData),
320 mock_factory_->pending_requests()[0]->script_data()->utf16());
321 factory_.reset();
322 waiter_.WaitForEvent(CONNECTION_ERROR);
325 } // namespace net