Fix broken path in extensions/common/PRESUBMIT.py
[chromium-blink-merge.git] / net / proxy / mojo_proxy_resolver_factory_impl_unittest.cc
blobc5fb69b954ea2e4a2230a3a220cb89e3b0a50912
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/test/event_waiter.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
13 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h"
15 namespace net {
16 namespace {
18 const char kScriptData[] = "FooBarBaz";
20 class FakeProxyResolver : public MockAsyncProxyResolverExpectsBytes {
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 mojo::ErrorHandler,
62 public interfaces::ProxyResolverFactoryRequestClient {
63 public:
64 void SetUp() override {
65 new MojoProxyResolverFactoryImpl(
66 base::Bind(
67 &MojoProxyResolverFactoryImplTest::CreateFakeProxyResolverFactory,
68 base::Unretained(this)),
69 mojo::GetProxy(&factory_));
70 mock_factory_owner_.reset(new TestProxyResolverFactory(&waiter_));
71 mock_factory_ = mock_factory_owner_.get();
74 void OnConnectionError() override { waiter_.NotifyEvent(CONNECTION_ERROR); }
76 scoped_ptr<ProxyResolverFactory> CreateFakeProxyResolverFactory(
77 HostResolver* host_resolver,
78 const ProxyResolver::LoadStateChangedCallback& callback) {
79 EXPECT_TRUE(host_resolver);
80 EXPECT_FALSE(callback.is_null());
81 DCHECK(mock_factory_owner_);
82 return mock_factory_owner_.Pass();
85 void OnFakeProxyInstanceDestroyed() {
86 instances_destroyed_++;
87 waiter_.NotifyEvent(RESOLVER_DESTROYED);
90 void ReportResult(int32_t error) override { create_callback_.Run(error); }
92 protected:
93 scoped_ptr<TestProxyResolverFactory> mock_factory_owner_;
94 TestProxyResolverFactory* mock_factory_;
95 interfaces::ProxyResolverFactoryPtr factory_;
97 int instances_destroyed_ = 0;
98 CompletionCallback create_callback_;
100 EventWaiter<Event> waiter_;
103 TEST_F(MojoProxyResolverFactoryImplTest, DisconnectHostResolver) {
104 interfaces::ProxyResolverPtr proxy_resolver;
105 interfaces::HostResolverPtr host_resolver;
106 mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request =
107 mojo::GetProxy(&host_resolver);
108 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr;
109 mojo::Binding<ProxyResolverFactoryRequestClient> client_binding(
110 this, mojo::GetProxy(&client_ptr));
111 factory_->CreateResolver(mojo::String::From(kScriptData),
112 mojo::GetProxy(&proxy_resolver),
113 host_resolver.Pass(), client_ptr.Pass());
114 proxy_resolver.set_error_handler(this);
115 waiter_.WaitForEvent(RESOLVER_CREATED);
116 EXPECT_EQ(0, instances_destroyed_);
117 ASSERT_EQ(1u, mock_factory_->pending_requests().size());
118 EXPECT_EQ(base::ASCIIToUTF16(kScriptData),
119 mock_factory_->pending_requests()[0]->script_data()->utf16());
120 TestCompletionCallback create_callback;
121 create_callback_ = create_callback.callback();
122 mock_factory_->pending_requests()[0]->CompleteNow(
123 OK, make_scoped_ptr(new FakeProxyResolver(base::Bind(
124 &MojoProxyResolverFactoryImplTest::OnFakeProxyInstanceDestroyed,
125 base::Unretained(this)))));
126 EXPECT_EQ(OK, create_callback.WaitForResult());
127 host_resolver_request = mojo::InterfaceRequest<interfaces::HostResolver>();
128 waiter_.WaitForEvent(CONNECTION_ERROR);
129 EXPECT_EQ(1, instances_destroyed_);
132 TEST_F(MojoProxyResolverFactoryImplTest, DisconnectProxyResolverClient) {
133 interfaces::ProxyResolverPtr proxy_resolver;
134 interfaces::HostResolverPtr host_resolver;
135 mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request =
136 mojo::GetProxy(&host_resolver);
137 mojo::Binding<interfaces::HostResolver> binding(nullptr, &host_resolver);
138 binding.set_error_handler(this);
139 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr;
140 mojo::Binding<ProxyResolverFactoryRequestClient> client_binding(
141 this, mojo::GetProxy(&client_ptr));
142 factory_->CreateResolver(mojo::String::From(kScriptData),
143 mojo::GetProxy(&proxy_resolver),
144 host_resolver.Pass(), client_ptr.Pass());
145 proxy_resolver.set_error_handler(this);
146 waiter_.WaitForEvent(RESOLVER_CREATED);
147 EXPECT_EQ(0, instances_destroyed_);
148 ASSERT_EQ(1u, mock_factory_->pending_requests().size());
149 EXPECT_EQ(base::ASCIIToUTF16(kScriptData),
150 mock_factory_->pending_requests()[0]->script_data()->utf16());
151 TestCompletionCallback create_callback;
152 create_callback_ = create_callback.callback();
153 mock_factory_->pending_requests()[0]->CompleteNow(
154 OK, make_scoped_ptr(new FakeProxyResolver(base::Bind(
155 &MojoProxyResolverFactoryImplTest::OnFakeProxyInstanceDestroyed,
156 base::Unretained(this)))));
157 EXPECT_EQ(OK, create_callback.WaitForResult());
158 proxy_resolver.reset();
159 waiter_.WaitForEvent(CONNECTION_ERROR);
160 EXPECT_EQ(1, instances_destroyed_);
163 TEST_F(MojoProxyResolverFactoryImplTest, DisconnectBoth) {
164 interfaces::ProxyResolverPtr proxy_resolver;
165 interfaces::HostResolverPtr host_resolver;
166 mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request =
167 mojo::GetProxy(&host_resolver);
168 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr;
169 mojo::Binding<ProxyResolverFactoryRequestClient> client_binding(
170 this, mojo::GetProxy(&client_ptr));
171 factory_->CreateResolver(mojo::String::From(kScriptData),
172 mojo::GetProxy(&proxy_resolver),
173 host_resolver.Pass(), client_ptr.Pass());
174 proxy_resolver.set_error_handler(this);
175 waiter_.WaitForEvent(RESOLVER_CREATED);
176 EXPECT_EQ(0, instances_destroyed_);
177 ASSERT_EQ(1u, mock_factory_->pending_requests().size());
178 EXPECT_EQ(base::ASCIIToUTF16(kScriptData),
179 mock_factory_->pending_requests()[0]->script_data()->utf16());
180 TestCompletionCallback create_callback;
181 create_callback_ = create_callback.callback();
182 mock_factory_->pending_requests()[0]->CompleteNow(
183 OK, make_scoped_ptr(new FakeProxyResolver(base::Bind(
184 &MojoProxyResolverFactoryImplTest::OnFakeProxyInstanceDestroyed,
185 base::Unretained(this)))));
186 EXPECT_EQ(OK, create_callback.WaitForResult());
187 proxy_resolver.reset();
188 host_resolver_request = mojo::InterfaceRequest<interfaces::HostResolver>();
189 waiter_.WaitForEvent(RESOLVER_DESTROYED);
190 EXPECT_EQ(1, instances_destroyed_);
193 TEST_F(MojoProxyResolverFactoryImplTest, Error) {
194 interfaces::ProxyResolverPtr proxy_resolver;
195 interfaces::HostResolverPtr host_resolver;
196 mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request =
197 mojo::GetProxy(&host_resolver);
198 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr;
199 mojo::Binding<ProxyResolverFactoryRequestClient> client_binding(
200 this, mojo::GetProxy(&client_ptr));
201 factory_->CreateResolver(mojo::String::From(kScriptData),
202 mojo::GetProxy(&proxy_resolver),
203 host_resolver.Pass(), client_ptr.Pass());
204 proxy_resolver.set_error_handler(this);
205 waiter_.WaitForEvent(RESOLVER_CREATED);
206 EXPECT_EQ(0, instances_destroyed_);
207 ASSERT_EQ(1u, mock_factory_->pending_requests().size());
208 EXPECT_EQ(base::ASCIIToUTF16(kScriptData),
209 mock_factory_->pending_requests()[0]->script_data()->utf16());
210 TestCompletionCallback create_callback;
211 create_callback_ = create_callback.callback();
212 mock_factory_->pending_requests()[0]->CompleteNow(ERR_PAC_SCRIPT_FAILED,
213 nullptr);
214 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, create_callback.WaitForResult());
217 TEST_F(MojoProxyResolverFactoryImplTest,
218 DisconnectHostResolverDuringResolveCreation) {
219 interfaces::ProxyResolverPtr proxy_resolver;
220 interfaces::HostResolverPtr host_resolver;
221 mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request =
222 mojo::GetProxy(&host_resolver);
223 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr;
224 mojo::Binding<ProxyResolverFactoryRequestClient> client_binding(
225 this, mojo::GetProxy(&client_ptr));
226 factory_->CreateResolver(mojo::String::From(kScriptData),
227 mojo::GetProxy(&proxy_resolver),
228 host_resolver.Pass(), client_ptr.Pass());
229 proxy_resolver.set_error_handler(this);
230 waiter_.WaitForEvent(RESOLVER_CREATED);
231 EXPECT_EQ(0, instances_destroyed_);
232 ASSERT_EQ(1u, mock_factory_->pending_requests().size());
233 EXPECT_EQ(base::ASCIIToUTF16(kScriptData),
234 mock_factory_->pending_requests()[0]->script_data()->utf16());
235 host_resolver_request = mojo::InterfaceRequest<interfaces::HostResolver>();
236 TestCompletionCallback create_callback;
237 create_callback_ = create_callback.callback();
238 waiter_.WaitForEvent(CONNECTION_ERROR);
239 EXPECT_EQ(ERR_PAC_SCRIPT_TERMINATED, create_callback.WaitForResult());
242 TEST_F(MojoProxyResolverFactoryImplTest,
243 DisconnectClientDuringResolverCreation) {
244 interfaces::ProxyResolverPtr proxy_resolver;
245 interfaces::HostResolverPtr host_resolver;
246 mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request =
247 mojo::GetProxy(&host_resolver);
248 mojo::Binding<interfaces::HostResolver> binding(nullptr, &host_resolver);
249 binding.set_error_handler(this);
250 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr;
251 mojo::Binding<ProxyResolverFactoryRequestClient> client_binding(
252 this, mojo::GetProxy(&client_ptr));
253 factory_->CreateResolver(mojo::String::From(kScriptData),
254 mojo::GetProxy(&proxy_resolver),
255 host_resolver.Pass(), client_ptr.Pass());
256 proxy_resolver.set_error_handler(this);
257 waiter_.WaitForEvent(RESOLVER_CREATED);
258 EXPECT_EQ(0, instances_destroyed_);
259 ASSERT_EQ(1u, mock_factory_->pending_requests().size());
260 EXPECT_EQ(base::ASCIIToUTF16(kScriptData),
261 mock_factory_->pending_requests()[0]->script_data()->utf16());
262 client_binding.Close();
263 waiter_.WaitForEvent(CONNECTION_ERROR);
266 TEST_F(MojoProxyResolverFactoryImplTest,
267 DisconnectFactoryDuringResolverCreation) {
268 interfaces::ProxyResolverPtr proxy_resolver;
269 interfaces::HostResolverPtr host_resolver;
270 mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request =
271 mojo::GetProxy(&host_resolver);
272 mojo::Binding<interfaces::HostResolver> binding(nullptr, &host_resolver);
273 binding.set_error_handler(this);
274 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr;
275 mojo::Binding<ProxyResolverFactoryRequestClient> client_binding(
276 this, mojo::GetProxy(&client_ptr));
277 factory_->CreateResolver(mojo::String::From(kScriptData),
278 mojo::GetProxy(&proxy_resolver),
279 host_resolver.Pass(), client_ptr.Pass());
280 proxy_resolver.set_error_handler(this);
281 client_binding.set_error_handler(this);
282 waiter_.WaitForEvent(RESOLVER_CREATED);
283 EXPECT_EQ(0, instances_destroyed_);
284 ASSERT_EQ(1u, mock_factory_->pending_requests().size());
285 EXPECT_EQ(base::ASCIIToUTF16(kScriptData),
286 mock_factory_->pending_requests()[0]->script_data()->utf16());
287 factory_.reset();
288 waiter_.WaitForEvent(CONNECTION_ERROR);
291 } // namespace net