| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/dns/mojo_host_resolver_impl.h" | 5 #include "net/dns/mojo_host_resolver_impl.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "mojo/public/cpp/bindings/binding.h" | 13 #include "mojo/public/cpp/bindings/binding.h" |
| 14 #include "mojo/public/cpp/bindings/interface_request.h" | 14 #include "mojo/public/cpp/bindings/interface_request.h" |
| 15 #include "net/base/address_list.h" | 15 #include "net/base/address_list.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/dns/mock_host_resolver.h" | 17 #include "net/dns/mock_host_resolver.h" |
| 18 #include "net/dns/mojo_host_type_converters.h" | |
| 19 #include "net/log/net_log.h" | 18 #include "net/log/net_log.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 20 |
| 22 namespace net { | 21 namespace net { |
| 23 | 22 |
| 24 namespace { | 23 namespace { |
| 25 | 24 |
| 26 class TestRequestClient : public interfaces::HostResolverRequestClient { | 25 class TestRequestClient : public interfaces::HostResolverRequestClient { |
| 27 public: | 26 public: |
| 28 explicit TestRequestClient( | 27 explicit TestRequestClient( |
| 29 mojo::InterfaceRequest<interfaces::HostResolverRequestClient> req) | 28 mojo::InterfaceRequest<interfaces::HostResolverRequestClient> req) |
| 30 : done_(false), binding_(this, std::move(req)) { | 29 : done_(false), binding_(this, std::move(req)) { |
| 31 binding_.set_connection_error_handler(base::Bind( | 30 binding_.set_connection_error_handler(base::Bind( |
| 32 &TestRequestClient::OnConnectionError, base::Unretained(this))); | 31 &TestRequestClient::OnConnectionError, base::Unretained(this))); |
| 33 } | 32 } |
| 34 | 33 |
| 35 void WaitForResult(); | 34 void WaitForResult(); |
| 36 void WaitForConnectionError(); | 35 void WaitForConnectionError(); |
| 37 | 36 |
| 38 int32_t error_; | 37 int32_t error_; |
| 39 interfaces::AddressListPtr results_; | 38 AddressList results_; |
| 40 | 39 |
| 41 private: | 40 private: |
| 42 // Overridden from interfaces::HostResolverRequestClient. | 41 // Overridden from interfaces::HostResolverRequestClient. |
| 43 void ReportResult(int32_t error, interfaces::AddressListPtr results) override; | 42 void ReportResult(int32_t error, const AddressList& results) override; |
| 44 | 43 |
| 45 // Mojo error handler. | 44 // Mojo error handler. |
| 46 void OnConnectionError(); | 45 void OnConnectionError(); |
| 47 | 46 |
| 48 bool done_; | 47 bool done_; |
| 49 base::Closure run_loop_quit_closure_; | 48 base::Closure run_loop_quit_closure_; |
| 50 base::Closure connection_error_quit_closure_; | 49 base::Closure connection_error_quit_closure_; |
| 51 | 50 |
| 52 mojo::Binding<interfaces::HostResolverRequestClient> binding_; | 51 mojo::Binding<interfaces::HostResolverRequestClient> binding_; |
| 53 }; | 52 }; |
| 54 | 53 |
| 55 void TestRequestClient::WaitForResult() { | 54 void TestRequestClient::WaitForResult() { |
| 56 if (done_) | 55 if (done_) |
| 57 return; | 56 return; |
| 58 | 57 |
| 59 base::RunLoop run_loop; | 58 base::RunLoop run_loop; |
| 60 run_loop_quit_closure_ = run_loop.QuitClosure(); | 59 run_loop_quit_closure_ = run_loop.QuitClosure(); |
| 61 run_loop.Run(); | 60 run_loop.Run(); |
| 62 ASSERT_TRUE(done_); | 61 ASSERT_TRUE(done_); |
| 63 } | 62 } |
| 64 | 63 |
| 65 void TestRequestClient::WaitForConnectionError() { | 64 void TestRequestClient::WaitForConnectionError() { |
| 66 base::RunLoop run_loop; | 65 base::RunLoop run_loop; |
| 67 connection_error_quit_closure_ = run_loop.QuitClosure(); | 66 connection_error_quit_closure_ = run_loop.QuitClosure(); |
| 68 run_loop.Run(); | 67 run_loop.Run(); |
| 69 } | 68 } |
| 70 | 69 |
| 71 void TestRequestClient::ReportResult(int32_t error, | 70 void TestRequestClient::ReportResult(int32_t error, |
| 72 interfaces::AddressListPtr results) { | 71 const AddressList& results) { |
| 73 if (!run_loop_quit_closure_.is_null()) { | 72 if (!run_loop_quit_closure_.is_null()) { |
| 74 run_loop_quit_closure_.Run(); | 73 run_loop_quit_closure_.Run(); |
| 75 } | 74 } |
| 76 ASSERT_FALSE(done_); | 75 ASSERT_FALSE(done_); |
| 77 error_ = error; | 76 error_ = error; |
| 78 results_ = std::move(results); | 77 results_ = results; |
| 79 done_ = true; | 78 done_ = true; |
| 80 } | 79 } |
| 81 | 80 |
| 82 void TestRequestClient::OnConnectionError() { | 81 void TestRequestClient::OnConnectionError() { |
| 83 if (!connection_error_quit_closure_.is_null()) | 82 if (!connection_error_quit_closure_.is_null()) |
| 84 connection_error_quit_closure_.Run(); | 83 connection_error_quit_closure_.Run(); |
| 85 } | 84 } |
| 86 | 85 |
| 87 class CallbackMockHostResolver : public MockHostResolver { | 86 class CallbackMockHostResolver : public MockHostResolver { |
| 88 public: | 87 public: |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 protected: | 127 protected: |
| 129 void SetUp() override { | 128 void SetUp() override { |
| 130 mock_host_resolver_.rules()->AddRule("example.com", "1.2.3.4"); | 129 mock_host_resolver_.rules()->AddRule("example.com", "1.2.3.4"); |
| 131 mock_host_resolver_.rules()->AddRule("chromium.org", "8.8.8.8"); | 130 mock_host_resolver_.rules()->AddRule("chromium.org", "8.8.8.8"); |
| 132 mock_host_resolver_.rules()->AddSimulatedFailure("failure.fail"); | 131 mock_host_resolver_.rules()->AddSimulatedFailure("failure.fail"); |
| 133 | 132 |
| 134 resolver_service_.reset( | 133 resolver_service_.reset( |
| 135 new MojoHostResolverImpl(&mock_host_resolver_, BoundNetLog())); | 134 new MojoHostResolverImpl(&mock_host_resolver_, BoundNetLog())); |
| 136 } | 135 } |
| 137 | 136 |
| 138 interfaces::HostResolverRequestInfoPtr CreateRequest(const std::string& host, | 137 std::unique_ptr<HostResolver::RequestInfo> |
| 139 uint16_t port, | 138 CreateRequest(const std::string& host, uint16_t port, bool is_my_ip_address) { |
| 140 bool is_my_ip_address) { | 139 std::unique_ptr<HostResolver::RequestInfo> request = |
| 141 interfaces::HostResolverRequestInfoPtr request = | 140 base::MakeUnique<HostResolver::RequestInfo>(HostPortPair(host, port)); |
| 142 interfaces::HostResolverRequestInfo::New(); | 141 request->set_is_my_ip_address(is_my_ip_address); |
| 143 request->host = host; | 142 request->set_address_family(ADDRESS_FAMILY_IPV4); |
| 144 request->port = port; | |
| 145 request->address_family = interfaces::AddressFamily::IPV4; | |
| 146 request->is_my_ip_address = is_my_ip_address; | |
| 147 return request; | 143 return request; |
| 148 } | 144 } |
| 149 | 145 |
| 150 // Wait until the mock resolver has received |num| resolve requests. | 146 // Wait until the mock resolver has received |num| resolve requests. |
| 151 void WaitForRequests(size_t num) { | 147 void WaitForRequests(size_t num) { |
| 152 while (mock_host_resolver_.num_resolve() < num) { | 148 while (mock_host_resolver_.num_resolve() < num) { |
| 153 base::RunLoop run_loop; | 149 base::RunLoop run_loop; |
| 154 mock_host_resolver_.SetResolveCallback(run_loop.QuitClosure()); | 150 mock_host_resolver_.SetResolveCallback(run_loop.QuitClosure()); |
| 155 run_loop.Run(); | 151 run_loop.Run(); |
| 156 } | 152 } |
| 157 } | 153 } |
| 158 | 154 |
| 159 CallbackMockHostResolver mock_host_resolver_; | 155 CallbackMockHostResolver mock_host_resolver_; |
| 160 std::unique_ptr<MojoHostResolverImpl> resolver_service_; | 156 std::unique_ptr<MojoHostResolverImpl> resolver_service_; |
| 161 }; | 157 }; |
| 162 | 158 |
| 163 TEST_F(MojoHostResolverImplTest, Resolve) { | 159 TEST_F(MojoHostResolverImplTest, Resolve) { |
| 164 interfaces::HostResolverRequestClientPtr client_ptr; | 160 interfaces::HostResolverRequestClientPtr client_ptr; |
| 165 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 161 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
| 166 | 162 |
| 167 interfaces::HostResolverRequestInfoPtr request = | 163 resolver_service_->Resolve(CreateRequest("example.com", 80, false), |
| 168 CreateRequest("example.com", 80, false); | 164 std::move(client_ptr)); |
| 169 resolver_service_->Resolve(std::move(request), std::move(client_ptr)); | |
| 170 client.WaitForResult(); | 165 client.WaitForResult(); |
| 171 | 166 |
| 172 EXPECT_EQ(net::OK, client.error_); | 167 EXPECT_EQ(net::OK, client.error_); |
| 173 AddressList address_list = (*client.results_).To<AddressList>(); | 168 AddressList& address_list = client.results_; |
| 174 EXPECT_EQ(1U, address_list.size()); | 169 EXPECT_EQ(1U, address_list.size()); |
| 175 EXPECT_EQ("1.2.3.4:80", address_list[0].ToString()); | 170 EXPECT_EQ("1.2.3.4:80", address_list[0].ToString()); |
| 176 } | 171 } |
| 177 | 172 |
| 178 TEST_F(MojoHostResolverImplTest, ResolveSynchronous) { | 173 TEST_F(MojoHostResolverImplTest, ResolveSynchronous) { |
| 179 interfaces::HostResolverRequestClientPtr client_ptr; | 174 interfaces::HostResolverRequestClientPtr client_ptr; |
| 180 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 175 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
| 181 | 176 |
| 182 mock_host_resolver_.set_synchronous_mode(true); | 177 mock_host_resolver_.set_synchronous_mode(true); |
| 183 | 178 |
| 184 interfaces::HostResolverRequestInfoPtr request = | 179 resolver_service_->Resolve(CreateRequest("example.com", 80, false), |
| 185 CreateRequest("example.com", 80, false); | 180 std::move(client_ptr)); |
| 186 resolver_service_->Resolve(std::move(request), std::move(client_ptr)); | |
| 187 client.WaitForResult(); | 181 client.WaitForResult(); |
| 188 | 182 |
| 189 EXPECT_EQ(net::OK, client.error_); | 183 EXPECT_EQ(net::OK, client.error_); |
| 190 AddressList address_list = (*client.results_).To<AddressList>(); | 184 AddressList& address_list = client.results_; |
| 191 EXPECT_EQ(1U, address_list.size()); | 185 EXPECT_EQ(1U, address_list.size()); |
| 192 EXPECT_EQ("1.2.3.4:80", address_list[0].ToString()); | 186 EXPECT_EQ("1.2.3.4:80", address_list[0].ToString()); |
| 193 } | 187 } |
| 194 | 188 |
| 195 TEST_F(MojoHostResolverImplTest, ResolveMultiple) { | 189 TEST_F(MojoHostResolverImplTest, ResolveMultiple) { |
| 196 interfaces::HostResolverRequestClientPtr client1_ptr; | 190 interfaces::HostResolverRequestClientPtr client1_ptr; |
| 197 TestRequestClient client1(mojo::GetProxy(&client1_ptr)); | 191 TestRequestClient client1(mojo::GetProxy(&client1_ptr)); |
| 198 interfaces::HostResolverRequestClientPtr client2_ptr; | 192 interfaces::HostResolverRequestClientPtr client2_ptr; |
| 199 TestRequestClient client2(mojo::GetProxy(&client2_ptr)); | 193 TestRequestClient client2(mojo::GetProxy(&client2_ptr)); |
| 200 | 194 |
| 201 mock_host_resolver_.set_ondemand_mode(true); | 195 mock_host_resolver_.set_ondemand_mode(true); |
| 202 | 196 |
| 203 interfaces::HostResolverRequestInfoPtr request1 = | 197 resolver_service_->Resolve(CreateRequest("example.com", 80, false), |
| 204 CreateRequest("example.com", 80, false); | 198 std::move(client1_ptr)); |
| 205 resolver_service_->Resolve(std::move(request1), std::move(client1_ptr)); | 199 resolver_service_->Resolve(CreateRequest("chromium.org", 80, false), |
| 206 interfaces::HostResolverRequestInfoPtr request2 = | 200 std::move(client2_ptr)); |
| 207 CreateRequest("chromium.org", 80, false); | |
| 208 resolver_service_->Resolve(std::move(request2), std::move(client2_ptr)); | |
| 209 WaitForRequests(2); | 201 WaitForRequests(2); |
| 210 mock_host_resolver_.ResolveAllPending(); | 202 mock_host_resolver_.ResolveAllPending(); |
| 211 | 203 |
| 212 client1.WaitForResult(); | 204 client1.WaitForResult(); |
| 213 client2.WaitForResult(); | 205 client2.WaitForResult(); |
| 214 | 206 |
| 215 EXPECT_EQ(net::OK, client1.error_); | 207 EXPECT_EQ(net::OK, client1.error_); |
| 216 AddressList address_list = (*client1.results_).To<AddressList>(); | 208 AddressList& address_list1 = client1.results_; |
| 217 EXPECT_EQ(1U, address_list.size()); | 209 EXPECT_EQ(1U, address_list1.size()); |
| 218 EXPECT_EQ("1.2.3.4:80", address_list[0].ToString()); | 210 EXPECT_EQ("1.2.3.4:80", address_list1[0].ToString()); |
| 219 EXPECT_EQ(net::OK, client2.error_); | 211 EXPECT_EQ(net::OK, client2.error_); |
| 220 address_list = (*client2.results_).To<AddressList>(); | 212 AddressList& address_list2 = client2.results_; |
| 221 EXPECT_EQ(1U, address_list.size()); | 213 EXPECT_EQ(1U, address_list2.size()); |
| 222 EXPECT_EQ("8.8.8.8:80", address_list[0].ToString()); | 214 EXPECT_EQ("8.8.8.8:80", address_list2[0].ToString()); |
| 223 } | 215 } |
| 224 | 216 |
| 225 TEST_F(MojoHostResolverImplTest, ResolveDuplicate) { | 217 TEST_F(MojoHostResolverImplTest, ResolveDuplicate) { |
| 226 interfaces::HostResolverRequestClientPtr client1_ptr; | 218 interfaces::HostResolverRequestClientPtr client1_ptr; |
| 227 TestRequestClient client1(mojo::GetProxy(&client1_ptr)); | 219 TestRequestClient client1(mojo::GetProxy(&client1_ptr)); |
| 228 interfaces::HostResolverRequestClientPtr client2_ptr; | 220 interfaces::HostResolverRequestClientPtr client2_ptr; |
| 229 TestRequestClient client2(mojo::GetProxy(&client2_ptr)); | 221 TestRequestClient client2(mojo::GetProxy(&client2_ptr)); |
| 230 | 222 |
| 231 mock_host_resolver_.set_ondemand_mode(true); | 223 mock_host_resolver_.set_ondemand_mode(true); |
| 232 | 224 |
| 233 interfaces::HostResolverRequestInfoPtr request1 = | 225 resolver_service_->Resolve(CreateRequest("example.com", 80, false), |
| 234 CreateRequest("example.com", 80, false); | 226 std::move(client1_ptr)); |
| 235 resolver_service_->Resolve(std::move(request1), std::move(client1_ptr)); | 227 resolver_service_->Resolve(CreateRequest("example.com", 80, false), |
| 236 interfaces::HostResolverRequestInfoPtr request2 = | 228 std::move(client2_ptr)); |
| 237 CreateRequest("example.com", 80, false); | |
| 238 resolver_service_->Resolve(std::move(request2), std::move(client2_ptr)); | |
| 239 WaitForRequests(2); | 229 WaitForRequests(2); |
| 240 mock_host_resolver_.ResolveAllPending(); | 230 mock_host_resolver_.ResolveAllPending(); |
| 241 | 231 |
| 242 client1.WaitForResult(); | 232 client1.WaitForResult(); |
| 243 client2.WaitForResult(); | 233 client2.WaitForResult(); |
| 244 | 234 |
| 245 EXPECT_EQ(net::OK, client1.error_); | 235 EXPECT_EQ(net::OK, client1.error_); |
| 246 AddressList address_list = (*client1.results_).To<AddressList>(); | 236 AddressList& address_list1 = client1.results_; |
| 247 EXPECT_EQ(1U, address_list.size()); | 237 EXPECT_EQ(1U, address_list1.size()); |
| 248 EXPECT_EQ("1.2.3.4:80", address_list[0].ToString()); | 238 EXPECT_EQ("1.2.3.4:80", address_list1[0].ToString()); |
| 249 EXPECT_EQ(net::OK, client2.error_); | 239 EXPECT_EQ(net::OK, client2.error_); |
| 250 address_list = (*client2.results_).To<AddressList>(); | 240 AddressList& address_list2 = client2.results_; |
| 251 EXPECT_EQ(1U, address_list.size()); | 241 EXPECT_EQ(1U, address_list2.size()); |
| 252 EXPECT_EQ("1.2.3.4:80", address_list[0].ToString()); | 242 EXPECT_EQ("1.2.3.4:80", address_list2[0].ToString()); |
| 253 } | 243 } |
| 254 | 244 |
| 255 TEST_F(MojoHostResolverImplTest, ResolveFailure) { | 245 TEST_F(MojoHostResolverImplTest, ResolveFailure) { |
| 256 interfaces::HostResolverRequestClientPtr client_ptr; | 246 interfaces::HostResolverRequestClientPtr client_ptr; |
| 257 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 247 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
| 258 | 248 |
| 259 interfaces::HostResolverRequestInfoPtr request = | 249 resolver_service_->Resolve(CreateRequest("failure.fail", 80, false), |
| 260 CreateRequest("failure.fail", 80, false); | 250 std::move(client_ptr)); |
| 261 resolver_service_->Resolve(std::move(request), std::move(client_ptr)); | |
| 262 client.WaitForResult(); | 251 client.WaitForResult(); |
| 263 | 252 |
| 264 EXPECT_EQ(net::ERR_NAME_NOT_RESOLVED, client.error_); | 253 EXPECT_EQ(net::ERR_NAME_NOT_RESOLVED, client.error_); |
| 265 EXPECT_TRUE(client.results_.is_null()); | 254 EXPECT_TRUE(client.results_.empty()); |
| 266 } | 255 } |
| 267 | 256 |
| 268 TEST_F(MojoHostResolverImplTest, DestroyClient) { | 257 TEST_F(MojoHostResolverImplTest, DestroyClient) { |
| 269 interfaces::HostResolverRequestClientPtr client_ptr; | 258 interfaces::HostResolverRequestClientPtr client_ptr; |
| 270 std::unique_ptr<TestRequestClient> client( | 259 std::unique_ptr<TestRequestClient> client( |
| 271 new TestRequestClient(mojo::GetProxy(&client_ptr))); | 260 new TestRequestClient(mojo::GetProxy(&client_ptr))); |
| 272 | 261 |
| 273 mock_host_resolver_.set_ondemand_mode(true); | 262 mock_host_resolver_.set_ondemand_mode(true); |
| 274 | 263 |
| 275 interfaces::HostResolverRequestInfoPtr request = | 264 resolver_service_->Resolve(CreateRequest("example.com", 80, false), |
| 276 CreateRequest("example.com", 80, false); | 265 std::move(client_ptr)); |
| 277 resolver_service_->Resolve(std::move(request), std::move(client_ptr)); | |
| 278 WaitForRequests(1); | 266 WaitForRequests(1); |
| 279 | 267 |
| 280 client.reset(); | 268 client.reset(); |
| 281 base::RunLoop().RunUntilIdle(); | 269 base::RunLoop().RunUntilIdle(); |
| 282 | 270 |
| 283 mock_host_resolver_.ResolveAllPending(); | 271 mock_host_resolver_.ResolveAllPending(); |
| 284 base::RunLoop().RunUntilIdle(); | 272 base::RunLoop().RunUntilIdle(); |
| 285 } | 273 } |
| 286 | 274 |
| 287 } // namespace net | 275 } // namespace net |
| OLD | NEW |