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.
5 #include "net/dns/single_request_host_resolver.h"
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"
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
{
23 HangingHostResolver() : outstanding_request_(NULL
) {}
25 virtual ~HangingHostResolver() {
26 EXPECT_TRUE(!has_outstanding_request());
29 bool has_outstanding_request() const {
30 return outstanding_request_
!= NULL
;
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_
;
42 // Never complete this request! Caller is expected to cancel it
43 // before destroying the resolver.
44 return ERR_IO_PENDING
;
47 virtual int ResolveFromCache(const RequestInfo
& info
,
48 AddressList
* addresses
,
49 const BoundNetLog
& net_log
) OVERRIDE
{
51 return ERR_UNEXPECTED
;
54 virtual void CancelRequest(RequestHandle req
) OVERRIDE
{
55 EXPECT_TRUE(has_outstanding_request());
56 EXPECT_EQ(req
, outstanding_request_
);
57 outstanding_request_
= NULL
;
61 RequestHandle outstanding_request_
;
63 DISALLOW_COPY_AND_ASSIGN(HangingHostResolver
);
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());
73 SingleRequestHostResolver
single_request_resolver(&resolver
);
75 // Resolve "watsup:90" using our SingleRequestHostResolver.
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());
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());
89 // Test that the Cancel() method cancels any outstanding request.
90 TEST(SingleRequestHostResolverTest
, Cancel
) {
91 HangingHostResolver resolver
;
94 SingleRequestHostResolver
single_request_resolver(&resolver
);
96 // Resolve "watsup:90" using our SingleRequestHostResolver.
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());
106 // Now that the SingleRequestHostResolver has been destroyed, the
107 // in-progress request should have been aborted.
108 EXPECT_FALSE(resolver
.has_outstanding_request());
111 // Test that the Cancel() method is a no-op when there is no outstanding
113 TEST(SingleRequestHostResolverTest
, CancelWhileNoPendingRequest
) {
114 HangingHostResolver resolver
;
115 SingleRequestHostResolver
single_request_resolver(&resolver
);
116 single_request_resolver
.Cancel();
118 // To pass, HangingHostResolver should not have received a cancellation
119 // request (since there is nothing to cancel). If it does, it will crash.