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 3 // found in the LICENSE file. 4 5 #include "net/dns/single_request_host_resolver.h" 6 7 #include "net/base/address_list.h" 8 #include "net/base/net_errors.h" 9 #include "net/base/net_log.h" 10 #include "net/base/test_completion_callback.h" 11 #include "net/dns/mock_host_resolver.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 namespace net { 15 16 namespace { 17 18 // Helper class used by SingleRequestHostResolverTest.Cancel test. 19 // It checks that only one request is outstanding at a time, and that 20 // it is cancelled before the class is destroyed. 21 class HangingHostResolver : public HostResolver { 22 public: 23 HangingHostResolver() : outstanding_request_(NULL) {} 24 25 virtual ~HangingHostResolver() { 26 EXPECT_TRUE(!has_outstanding_request()); 27 } 28 29 bool has_outstanding_request() const { 30 return outstanding_request_ != NULL; 31 } 32 33 virtual int Resolve(const RequestInfo& info, 34 AddressList* addresses, 35 const CompletionCallback& callback, 36 RequestHandle* out_req, 37 const BoundNetLog& net_log) OVERRIDE { 38 EXPECT_FALSE(has_outstanding_request()); 39 outstanding_request_ = reinterpret_cast<RequestHandle>(0x1234); 40 *out_req = outstanding_request_; 41 42 // Never complete this request! Caller is expected to cancel it 43 // before destroying the resolver. 44 return ERR_IO_PENDING; 45 } 46 47 virtual int ResolveFromCache(const RequestInfo& info, 48 AddressList* addresses, 49 const BoundNetLog& net_log) OVERRIDE { 50 NOTIMPLEMENTED(); 51 return ERR_UNEXPECTED; 52 } 53 54 virtual void CancelRequest(RequestHandle req) OVERRIDE { 55 EXPECT_TRUE(has_outstanding_request()); 56 EXPECT_EQ(req, outstanding_request_); 57 outstanding_request_ = NULL; 58 } 59 60 private: 61 RequestHandle outstanding_request_; 62 63 DISALLOW_COPY_AND_ASSIGN(HangingHostResolver); 64 }; 65 66 // Test that a regular end-to-end lookup returns the expected result. 67 TEST(SingleRequestHostResolverTest, NormalResolve) { 68 // Create a host resolver dependency that returns address "199.188.1.166" 69 // for resolutions of "watsup". 70 MockHostResolver resolver; 71 resolver.rules()->AddIPLiteralRule("watsup", "199.188.1.166", std::string()); 72 73 SingleRequestHostResolver single_request_resolver(&resolver); 74 75 // Resolve "watsup:90" using our SingleRequestHostResolver. 76 AddressList addrlist; 77 TestCompletionCallback callback; 78 HostResolver::RequestInfo request(HostPortPair("watsup", 90)); 79 int rv = single_request_resolver.Resolve( 80 request, &addrlist, callback.callback(), BoundNetLog()); 81 EXPECT_EQ(ERR_IO_PENDING, rv); 82 EXPECT_EQ(OK, callback.WaitForResult()); 83 84 // Verify that the result is what we specified in the MockHostResolver. 85 ASSERT_FALSE(addrlist.empty()); 86 EXPECT_EQ("199.188.1.166", addrlist.front().ToStringWithoutPort()); 87 } 88 89 // Test that the Cancel() method cancels any outstanding request. 90 TEST(SingleRequestHostResolverTest, Cancel) { 91 HangingHostResolver resolver; 92 93 { 94 SingleRequestHostResolver single_request_resolver(&resolver); 95 96 // Resolve "watsup:90" using our SingleRequestHostResolver. 97 AddressList addrlist; 98 TestCompletionCallback callback; 99 HostResolver::RequestInfo request(HostPortPair("watsup", 90)); 100 int rv = single_request_resolver.Resolve( 101 request, &addrlist, callback.callback(), BoundNetLog()); 102 EXPECT_EQ(ERR_IO_PENDING, rv); 103 EXPECT_TRUE(resolver.has_outstanding_request()); 104 } 105 106 // Now that the SingleRequestHostResolver has been destroyed, the 107 // in-progress request should have been aborted. 108 EXPECT_FALSE(resolver.has_outstanding_request()); 109 } 110 111 // Test that the Cancel() method is a no-op when there is no outstanding 112 // request. 113 TEST(SingleRequestHostResolverTest, CancelWhileNoPendingRequest) { 114 HangingHostResolver resolver; 115 SingleRequestHostResolver single_request_resolver(&resolver); 116 single_request_resolver.Cancel(); 117 118 // To pass, HangingHostResolver should not have received a cancellation 119 // request (since there is nothing to cancel). If it does, it will crash. 120 } 121 122 } // namespace 123 124 } // namespace net 125