| 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 | |
| 24 // Helper class used by SingleRequestHostResolverTest.Cancel test. | 23 // Helper class used by SingleRequestHostResolverTest.Cancel test. |
| 25 // It checks that only one request is outstanding at a time, and that | 24 // It checks that only one request is outstanding at a time, and that |
| 26 // it is cancelled before the class is destroyed. | 25 // it is cancelled before the class is destroyed. |
| 27 class HangingHostResolver : public HostResolver { | 26 class HangingHostResolver : public HostResolver { |
| 28 public: | 27 public: |
| 29 HangingHostResolver() : outstanding_request_(NULL) {} | 28 HangingHostResolver() : outstanding_request_(NULL) {} |
| 30 | 29 |
| 31 ~HangingHostResolver() override { EXPECT_TRUE(!has_outstanding_request()); } | 30 ~HangingHostResolver() override { EXPECT_TRUE(!has_outstanding_request()); } |
| 32 | 31 |
| 33 bool has_outstanding_request() const { | 32 bool has_outstanding_request() const { |
| 34 return outstanding_request_ != NULL; | 33 return outstanding_request_ != NULL; |
| 35 } | 34 } |
| 36 | 35 |
| 37 int Resolve(const RequestInfo& info, | 36 int Resolve(const RequestInfo& info, |
| 38 RequestPriority priority, | 37 RequestPriority priority, |
| 39 AddressList* addresses, | 38 AddressList* addresses, |
| 40 const CompletionCallback& callback, | 39 const CompletionCallback& callback, |
| 41 RequestHandle* out_req, | 40 std::unique_ptr<Request>* out_req, |
| 42 const BoundNetLog& net_log) override { | 41 const BoundNetLog& net_log) override { |
| 43 EXPECT_FALSE(has_outstanding_request()); | 42 EXPECT_FALSE(has_outstanding_request()); |
| 44 outstanding_request_ = reinterpret_cast<RequestHandle>(0x1234); | 43 outstanding_request_ = new RequestImpl(this); |
| 45 *out_req = outstanding_request_; | 44 (*out_req).reset(outstanding_request_); |
| 46 | 45 |
| 47 // Never complete this request! Caller is expected to cancel it | 46 // Never complete this request! Caller is expected to cancel it |
| 48 // before destroying the resolver. | 47 // before destroying the resolver. |
| 49 return ERR_IO_PENDING; | 48 return ERR_IO_PENDING; |
| 50 } | 49 } |
| 51 | 50 |
| 52 int ResolveFromCache(const RequestInfo& info, | 51 int ResolveFromCache(const RequestInfo& info, |
| 53 AddressList* addresses, | 52 AddressList* addresses, |
| 54 const BoundNetLog& net_log) override { | 53 const BoundNetLog& net_log) override { |
| 55 NOTIMPLEMENTED(); | 54 NOTIMPLEMENTED(); |
| 56 return ERR_UNEXPECTED; | 55 return ERR_UNEXPECTED; |
| 57 } | 56 } |
| 58 | 57 |
| 59 void CancelRequest(RequestHandle req) override { | 58 private: |
| 60 EXPECT_TRUE(has_outstanding_request()); | 59 class RequestImpl : public HostResolver::Request { |
| 61 EXPECT_EQ(req, outstanding_request_); | 60 public: |
| 62 outstanding_request_ = NULL; | 61 RequestImpl(HangingHostResolver* resolver) : resolver_(resolver) {} |
| 63 } | |
| 64 | 62 |
| 65 private: | 63 ~RequestImpl() override { |
| 66 RequestHandle outstanding_request_; | 64 if (resolver_) { |
| 65 EXPECT_TRUE(resolver_->has_outstanding_request()); |
| 66 EXPECT_EQ(resolver_->outstanding_request_, this); |
| 67 resolver_->outstanding_request_ = nullptr; |
| 68 } |
| 69 } |
| 70 |
| 71 void ChangeRequestPriority(RequestPriority priority) override {} |
| 72 |
| 73 void RemoveResolver() { resolver_ = nullptr; } |
| 74 |
| 75 private: |
| 76 HangingHostResolver* resolver_; |
| 77 |
| 78 DISALLOW_COPY_AND_ASSIGN(RequestImpl); |
| 79 }; |
| 80 |
| 81 friend RequestImpl; |
| 82 |
| 83 RequestImpl* outstanding_request_; |
| 67 | 84 |
| 68 DISALLOW_COPY_AND_ASSIGN(HangingHostResolver); | 85 DISALLOW_COPY_AND_ASSIGN(HangingHostResolver); |
| 69 }; | 86 }; |
| 70 | 87 |
| 71 // Test that a regular end-to-end lookup returns the expected result. | 88 // Test that a regular end-to-end lookup returns the expected result. |
| 72 TEST(SingleRequestHostResolverTest, NormalResolve) { | 89 TEST(SingleRequestHostResolverTest, NormalResolve) { |
| 73 // Create a host resolver dependency that returns address "199.188.1.166" | 90 // Create a host resolver dependency that returns address "199.188.1.166" |
| 74 // for resolutions of "watsup". | 91 // for resolutions of "watsup". |
| 75 MockHostResolver resolver; | 92 MockHostResolver resolver; |
| 76 resolver.rules()->AddIPLiteralRule("watsup", "199.188.1.166", std::string()); | 93 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); | 140 SingleRequestHostResolver single_request_resolver(&resolver); |
| 124 single_request_resolver.Cancel(); | 141 single_request_resolver.Cancel(); |
| 125 | 142 |
| 126 // To pass, HangingHostResolver should not have received a cancellation | 143 // To pass, HangingHostResolver should not have received a cancellation |
| 127 // request (since there is nothing to cancel). If it does, it will crash. | 144 // request (since there is nothing to cancel). If it does, it will crash. |
| 128 } | 145 } |
| 129 | 146 |
| 130 } // namespace | 147 } // namespace |
| 131 | 148 |
| 132 } // namespace net | 149 } // namespace net |
| OLD | NEW |