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