| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/single_request_host_resolver.h" | 5 #include "net/dns/single_request_host_resolver.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "net/base/address_list.h" | 8 #include "net/base/address_list.h" |
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 #include "net/base/test_completion_callback.h" | 10 #include "net/base/test_completion_callback.h" |
| 11 #include "net/dns/mock_host_resolver.h" | 11 #include "net/dns/mock_host_resolver.h" |
| 12 #include "net/log/net_log.h" | 12 #include "net/log/net_log.h" |
| 13 #include "net/test/gtest_util.h" | 13 #include "net/test/gtest_util.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 using net::test::IsError; | 17 using net::test::IsError; |
| 18 using net::test::IsOk; | 18 using net::test::IsOk; |
| 19 | 19 |
| 20 namespace net { | 20 namespace net { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 // Helper class used by SingleRequestHostResolverTest.Cancel test. | 24 // Helper class used by SingleRequestHostResolverTest.Cancel test. |
| 25 // It checks that only one request is outstanding at a time, and that | 25 // It checks that only one request is outstanding at a time, and that |
| 26 // it is cancelled before the class is destroyed. | 26 // it is cancelled before the class is destroyed. |
| 27 class HangingHostResolver : public HostResolver { | 27 class HangingHostResolver : public HostResolver { |
| 28 private: |
| 29 class RequestImpl; |
| 30 |
| 28 public: | 31 public: |
| 29 HangingHostResolver() : outstanding_request_(NULL) {} | 32 HangingHostResolver() : outstanding_request_(NULL) {} |
| 30 | 33 |
| 31 ~HangingHostResolver() override { EXPECT_TRUE(!has_outstanding_request()); } | 34 ~HangingHostResolver() override { EXPECT_TRUE(!has_outstanding_request()); } |
| 32 | 35 |
| 33 bool has_outstanding_request() const { | 36 bool has_outstanding_request() const { |
| 34 return outstanding_request_ != NULL; | 37 return outstanding_request_ != NULL; |
| 35 } | 38 } |
| 36 | 39 |
| 37 int Resolve(const RequestInfo& info, | 40 int Resolve(const RequestInfo& info, |
| 38 RequestPriority priority, | 41 RequestPriority priority, |
| 39 AddressList* addresses, | 42 AddressList* addresses, |
| 40 const CompletionCallback& callback, | 43 const CompletionCallback& callback, |
| 41 RequestHandle* out_req, | 44 std::unique_ptr<Request>* request, |
| 42 const BoundNetLog& net_log) override { | 45 const BoundNetLog& net_log) override { |
| 43 EXPECT_FALSE(has_outstanding_request()); | 46 EXPECT_FALSE(has_outstanding_request()); |
| 44 outstanding_request_ = reinterpret_cast<RequestHandle>(0x1234); | 47 outstanding_request_ = new RequestImpl(this); |
| 45 *out_req = outstanding_request_; | 48 request->reset(outstanding_request_); |
| 46 | 49 |
| 47 // Never complete this request! Caller is expected to cancel it | 50 // Never complete this request! Caller is expected to cancel it |
| 48 // before destroying the resolver. | 51 // before destroying the resolver. |
| 49 return ERR_IO_PENDING; | 52 return ERR_IO_PENDING; |
| 50 } | 53 } |
| 51 | 54 |
| 52 int ResolveFromCache(const RequestInfo& info, | 55 int ResolveFromCache(const RequestInfo& info, |
| 53 AddressList* addresses, | 56 AddressList* addresses, |
| 54 const BoundNetLog& net_log) override { | 57 const BoundNetLog& net_log) override { |
| 55 NOTIMPLEMENTED(); | 58 NOTIMPLEMENTED(); |
| 56 return ERR_UNEXPECTED; | 59 return ERR_UNEXPECTED; |
| 57 } | 60 } |
| 58 | 61 |
| 59 void CancelRequest(RequestHandle req) override { | 62 void RemoveRequest(RequestImpl* request) { |
| 60 EXPECT_TRUE(has_outstanding_request()); | 63 EXPECT_TRUE(has_outstanding_request()); |
| 61 EXPECT_EQ(req, outstanding_request_); | 64 EXPECT_EQ(outstanding_request_, request); |
| 62 outstanding_request_ = NULL; | 65 outstanding_request_ = nullptr; |
| 63 } | 66 } |
| 64 | 67 |
| 65 private: | 68 private: |
| 66 RequestHandle outstanding_request_; | 69 class RequestImpl : public HostResolver::Request { |
| 70 public: |
| 71 explicit RequestImpl(HangingHostResolver* resolver) : resolver_(resolver) {} |
| 72 |
| 73 ~RequestImpl() override { |
| 74 DCHECK(resolver_); |
| 75 resolver_->RemoveRequest(this); |
| 76 } |
| 77 |
| 78 void ChangeRequestPriority(RequestPriority priority) override {} |
| 79 |
| 80 private: |
| 81 HangingHostResolver* resolver_; |
| 82 }; |
| 83 |
| 84 HostResolver::Request* outstanding_request_; |
| 67 | 85 |
| 68 DISALLOW_COPY_AND_ASSIGN(HangingHostResolver); | 86 DISALLOW_COPY_AND_ASSIGN(HangingHostResolver); |
| 69 }; | 87 }; |
| 70 | 88 |
| 71 // Test that a regular end-to-end lookup returns the expected result. | 89 // Test that a regular end-to-end lookup returns the expected result. |
| 72 TEST(SingleRequestHostResolverTest, NormalResolve) { | 90 TEST(SingleRequestHostResolverTest, NormalResolve) { |
| 73 // Create a host resolver dependency that returns address "199.188.1.166" | 91 // Create a host resolver dependency that returns address "199.188.1.166" |
| 74 // for resolutions of "watsup". | 92 // for resolutions of "watsup". |
| 75 MockHostResolver resolver; | 93 MockHostResolver resolver; |
| 76 resolver.rules()->AddIPLiteralRule("watsup", "199.188.1.166", std::string()); | 94 resolver.rules()->AddIPLiteralRule("watsup", "199.188.1.166", std::string()); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 SingleRequestHostResolver single_request_resolver(&resolver); | 141 SingleRequestHostResolver single_request_resolver(&resolver); |
| 124 single_request_resolver.Cancel(); | 142 single_request_resolver.Cancel(); |
| 125 | 143 |
| 126 // To pass, HangingHostResolver should not have received a cancellation | 144 // To pass, HangingHostResolver should not have received a cancellation |
| 127 // request (since there is nothing to cancel). If it does, it will crash. | 145 // request (since there is nothing to cancel). If it does, it will crash. |
| 128 } | 146 } |
| 129 | 147 |
| 130 } // namespace | 148 } // namespace |
| 131 | 149 |
| 132 } // namespace net | 150 } // namespace net |
| OLD | NEW |