OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "net/dns/mojo_host_resolver_impl.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/run_loop.h" | |
11 #include "base/time/time.h" | |
12 #include "net/base/address_list.h" | |
13 #include "net/base/net_errors.h" | |
14 #include "net/base/net_util.h" | |
15 #include "net/dns/mock_host_resolver.h" | |
16 #include "net/dns/mojo_type_converters.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" | |
19 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h" | |
20 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" | |
21 | |
22 namespace net { | |
23 | |
24 namespace { | |
25 | |
26 class TestRequestClient : public interfaces::HostResolverRequestClient, | |
27 public mojo::ErrorHandler { | |
28 public: | |
29 explicit TestRequestClient( | |
30 mojo::InterfaceRequest<interfaces::HostResolverRequestClient> req) | |
31 : done_(false), binding_(this, req.Pass()) { | |
32 binding_.set_error_handler(this); | |
33 } | |
34 | |
35 void WaitForResult(); | |
36 void WaitForConnectionError(); | |
37 | |
38 int32_t error_; | |
39 interfaces::AddressListPtr results_; | |
40 | |
41 private: | |
42 // Overridden from interfaces::HostResolverRequestClient. | |
43 void ReportResult(int32_t error, interfaces::AddressListPtr results) override; | |
44 | |
45 // Overridden from mojo::ErrorHandler. | |
46 void OnConnectionError() override; | |
47 | |
48 bool done_; | |
49 base::Closure run_loop_quit_closure_; | |
50 base::Closure connection_error_quit_closure_; | |
51 | |
52 mojo::Binding<interfaces::HostResolverRequestClient> binding_; | |
53 }; | |
54 | |
55 void TestRequestClient::WaitForResult() { | |
56 if (done_) | |
57 return; | |
58 | |
59 base::RunLoop run_loop; | |
60 run_loop_quit_closure_ = run_loop.QuitClosure(); | |
61 run_loop.Run(); | |
62 ASSERT_TRUE(done_); | |
63 } | |
64 | |
65 void TestRequestClient::WaitForConnectionError() { | |
66 base::RunLoop run_loop; | |
67 connection_error_quit_closure_ = run_loop.QuitClosure(); | |
68 run_loop.Run(); | |
69 } | |
70 | |
71 void TestRequestClient::ReportResult(int32_t error, | |
72 interfaces::AddressListPtr results) { | |
73 if (!run_loop_quit_closure_.is_null()) { | |
74 run_loop_quit_closure_.Run(); | |
75 } | |
76 ASSERT_FALSE(done_); | |
77 error_ = error; | |
78 results_ = results.Pass(); | |
79 done_ = true; | |
80 } | |
81 | |
82 void TestRequestClient::OnConnectionError() { | |
83 if (!connection_error_quit_closure_.is_null()) | |
84 connection_error_quit_closure_.Run(); | |
85 } | |
86 | |
87 class CallbackMockHostResolver : public MockHostResolver { | |
88 public: | |
89 CallbackMockHostResolver() {} | |
90 ~CallbackMockHostResolver() override {} | |
91 | |
92 // Set a callback to run whenever Resolve is called. Callback is cleared after | |
93 // every run. | |
94 void SetResolveCallback(base::Closure callback) { | |
95 resolve_callback_ = callback; | |
96 } | |
97 | |
98 // Overridden from MockHostResolver. | |
99 int Resolve(const RequestInfo& info, | |
100 RequestPriority priority, | |
101 AddressList* addresses, | |
102 const CompletionCallback& callback, | |
103 RequestHandle* out_req, | |
104 const BoundNetLog& net_log) override; | |
105 | |
106 private: | |
107 base::Closure resolve_callback_; | |
108 }; | |
109 | |
110 int CallbackMockHostResolver::Resolve(const RequestInfo& info, | |
111 RequestPriority priority, | |
112 AddressList* addresses, | |
113 const CompletionCallback& callback, | |
114 RequestHandle* out_req, | |
115 const BoundNetLog& net_log) { | |
116 int result = MockHostResolver::Resolve(info, priority, addresses, callback, | |
117 out_req, net_log); | |
118 if (!resolve_callback_.is_null()) { | |
119 resolve_callback_.Run(); | |
120 resolve_callback_.Reset(); | |
121 } | |
122 return result; | |
123 } | |
124 | |
125 } // namespace | |
126 | |
127 class MojoHostResolverImplTest : public testing::Test { | |
128 protected: | |
129 void SetUp() override { | |
130 mock_host_resolver_.rules()->AddRule("example.com", "1.2.3.4"); | |
131 mock_host_resolver_.rules()->AddRule("chromium.org", "8.8.8.8"); | |
132 mock_host_resolver_.rules()->AddSimulatedFailure("failure.fail"); | |
133 | |
134 resolver_service_.reset(new MojoHostResolverImpl(&mock_host_resolver_)); | |
135 resolver_service_binding_.reset( | |
136 new mojo::Binding<interfaces::HostResolver>(resolver_service_.get())); | |
137 resolver_service_binding_->Bind(mojo::GetProxy(&resolver_service_ptr_)); | |
138 } | |
139 | |
140 interfaces::HostResolverRequestInfoPtr CreateRequest(const std::string& host, | |
141 uint16_t port, | |
142 bool is_my_ip_address) { | |
143 interfaces::HostResolverRequestInfoPtr request = | |
144 interfaces::HostResolverRequestInfo::New(); | |
145 request->host = host; | |
146 request->port = port; | |
147 request->address_family = interfaces::ADDRESS_FAMILY_IPV4; | |
148 request->is_my_ip_address = is_my_ip_address; | |
149 return request.Pass(); | |
150 } | |
151 | |
152 // Wait until the mock resolver has received |num| resolve requests. | |
153 void WaitForRequests(size_t num) { | |
154 while (mock_host_resolver_.num_resolve() < num) { | |
155 base::RunLoop run_loop; | |
156 mock_host_resolver_.SetResolveCallback(run_loop.QuitClosure()); | |
157 run_loop.Run(); | |
158 } | |
159 } | |
160 | |
161 CallbackMockHostResolver mock_host_resolver_; | |
162 scoped_ptr<MojoHostResolverImpl> resolver_service_; | |
163 | |
164 scoped_ptr<mojo::Binding<interfaces::HostResolver>> resolver_service_binding_; | |
jamesr
2015/02/13 00:59:54
this class would be much simpler if you had MojoHo
Anand Mistry (off Chromium)
2015/02/13 02:29:08
Using a scoped_ptr<> here is intentional. In one t
| |
165 interfaces::HostResolverPtr resolver_service_ptr_; | |
166 }; | |
167 | |
168 TEST_F(MojoHostResolverImplTest, Resolve) { | |
169 interfaces::HostResolverRequestClientPtr client_ptr; | |
170 TestRequestClient client(mojo::GetProxy(&client_ptr)); | |
171 | |
172 interfaces::HostResolverRequestInfoPtr request = | |
173 CreateRequest("example.com", 80, false); | |
174 resolver_service_ptr_->Resolve(request.Pass(), client_ptr.Pass()); | |
175 client.WaitForResult(); | |
176 | |
177 EXPECT_EQ(net::OK, client.error_); | |
178 AddressList address_list = (*client.results_).To<AddressList>(); | |
179 EXPECT_EQ(1U, address_list.size()); | |
180 EXPECT_EQ("1.2.3.4:80", address_list[0].ToString()); | |
181 } | |
182 | |
183 TEST_F(MojoHostResolverImplTest, ResolveSynchronous) { | |
184 interfaces::HostResolverRequestClientPtr client_ptr; | |
185 TestRequestClient client(mojo::GetProxy(&client_ptr)); | |
186 | |
187 mock_host_resolver_.set_synchronous_mode(true); | |
188 | |
189 interfaces::HostResolverRequestInfoPtr request = | |
190 CreateRequest("example.com", 80, false); | |
191 resolver_service_ptr_->Resolve(request.Pass(), client_ptr.Pass()); | |
192 client.WaitForResult(); | |
193 | |
194 EXPECT_EQ(net::OK, client.error_); | |
195 AddressList address_list = (*client.results_).To<AddressList>(); | |
196 EXPECT_EQ(1U, address_list.size()); | |
197 EXPECT_EQ("1.2.3.4:80", address_list[0].ToString()); | |
198 } | |
199 | |
200 TEST_F(MojoHostResolverImplTest, ResolveMultiple) { | |
201 interfaces::HostResolverRequestClientPtr client1_ptr; | |
202 TestRequestClient client1(mojo::GetProxy(&client1_ptr)); | |
203 interfaces::HostResolverRequestClientPtr client2_ptr; | |
204 TestRequestClient client2(mojo::GetProxy(&client2_ptr)); | |
205 | |
206 mock_host_resolver_.set_ondemand_mode(true); | |
207 | |
208 interfaces::HostResolverRequestInfoPtr request1 = | |
209 CreateRequest("example.com", 80, false); | |
210 resolver_service_ptr_->Resolve(request1.Pass(), client1_ptr.Pass()); | |
211 interfaces::HostResolverRequestInfoPtr request2 = | |
212 CreateRequest("chromium.org", 80, false); | |
213 resolver_service_ptr_->Resolve(request2.Pass(), client2_ptr.Pass()); | |
214 WaitForRequests(2); | |
215 mock_host_resolver_.ResolveAllPending(); | |
216 | |
217 client1.WaitForResult(); | |
218 client2.WaitForResult(); | |
219 | |
220 EXPECT_EQ(net::OK, client1.error_); | |
221 AddressList address_list = (*client1.results_).To<AddressList>(); | |
222 EXPECT_EQ(1U, address_list.size()); | |
223 EXPECT_EQ("1.2.3.4:80", address_list[0].ToString()); | |
224 EXPECT_EQ(net::OK, client2.error_); | |
225 address_list = (*client2.results_).To<AddressList>(); | |
226 EXPECT_EQ(1U, address_list.size()); | |
227 EXPECT_EQ("8.8.8.8:80", address_list[0].ToString()); | |
228 } | |
229 | |
230 TEST_F(MojoHostResolverImplTest, ResolveDuplicate) { | |
231 interfaces::HostResolverRequestClientPtr client1_ptr; | |
232 TestRequestClient client1(mojo::GetProxy(&client1_ptr)); | |
233 interfaces::HostResolverRequestClientPtr client2_ptr; | |
234 TestRequestClient client2(mojo::GetProxy(&client2_ptr)); | |
235 | |
236 mock_host_resolver_.set_ondemand_mode(true); | |
237 | |
238 interfaces::HostResolverRequestInfoPtr request1 = | |
239 CreateRequest("example.com", 80, false); | |
240 resolver_service_ptr_->Resolve(request1.Pass(), client1_ptr.Pass()); | |
241 interfaces::HostResolverRequestInfoPtr request2 = | |
242 CreateRequest("example.com", 80, false); | |
243 resolver_service_ptr_->Resolve(request2.Pass(), client2_ptr.Pass()); | |
244 WaitForRequests(2); | |
245 mock_host_resolver_.ResolveAllPending(); | |
246 | |
247 client1.WaitForResult(); | |
248 client2.WaitForResult(); | |
249 | |
250 EXPECT_EQ(net::OK, client1.error_); | |
251 AddressList address_list = (*client1.results_).To<AddressList>(); | |
252 EXPECT_EQ(1U, address_list.size()); | |
253 EXPECT_EQ("1.2.3.4:80", address_list[0].ToString()); | |
254 EXPECT_EQ(net::OK, client2.error_); | |
255 address_list = (*client2.results_).To<AddressList>(); | |
256 EXPECT_EQ(1U, address_list.size()); | |
257 EXPECT_EQ("1.2.3.4:80", address_list[0].ToString()); | |
258 } | |
259 | |
260 TEST_F(MojoHostResolverImplTest, ResolveFailure) { | |
261 interfaces::HostResolverRequestClientPtr client_ptr; | |
262 TestRequestClient client(mojo::GetProxy(&client_ptr)); | |
263 | |
264 interfaces::HostResolverRequestInfoPtr request = | |
265 CreateRequest("failure.fail", 80, false); | |
266 resolver_service_ptr_->Resolve(request.Pass(), client_ptr.Pass()); | |
267 client.WaitForResult(); | |
268 | |
269 EXPECT_EQ(net::ERR_NAME_NOT_RESOLVED, client.error_); | |
270 EXPECT_TRUE(client.results_.is_null()); | |
271 } | |
272 | |
273 TEST_F(MojoHostResolverImplTest, DestroyClient) { | |
274 interfaces::HostResolverRequestClientPtr client_ptr; | |
275 scoped_ptr<TestRequestClient> client( | |
276 new TestRequestClient(mojo::GetProxy(&client_ptr))); | |
277 | |
278 mock_host_resolver_.set_ondemand_mode(true); | |
279 | |
280 interfaces::HostResolverRequestInfoPtr request = | |
281 CreateRequest("example.com", 80, false); | |
282 resolver_service_ptr_->Resolve(request.Pass(), client_ptr.Pass()); | |
283 WaitForRequests(1); | |
284 | |
285 client.reset(); | |
286 base::RunLoop().RunUntilIdle(); | |
287 | |
288 mock_host_resolver_.ResolveAllPending(); | |
289 base::RunLoop().RunUntilIdle(); | |
290 } | |
291 | |
292 TEST_F(MojoHostResolverImplTest, DestroyService) { | |
293 interfaces::HostResolverRequestClientPtr client_ptr; | |
294 TestRequestClient client(mojo::GetProxy(&client_ptr)); | |
295 | |
296 mock_host_resolver_.set_ondemand_mode(true); | |
297 | |
298 interfaces::HostResolverRequestInfoPtr request = | |
299 CreateRequest("example.com", 80, false); | |
300 resolver_service_ptr_->Resolve(request.Pass(), client_ptr.Pass()); | |
301 WaitForRequests(1); | |
302 | |
303 resolver_service_binding_.reset(); | |
304 resolver_service_.reset(); | |
305 | |
306 client.WaitForConnectionError(); | |
307 } | |
308 | |
309 } // namespace net | |
OLD | NEW |