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