Landing Recent QUIC changes until 8/19/2015 17:00 UTC.
[chromium-blink-merge.git] / net / dns / single_request_host_resolver_unittest.cc
blob8bd285f1534149948a1da057a384e13fb42994aa
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/test_completion_callback.h"
10 #include "net/dns/mock_host_resolver.h"
11 #include "net/log/net_log.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace net {
16 namespace {
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) {}
25 ~HangingHostResolver() override { EXPECT_TRUE(!has_outstanding_request()); }
27 bool has_outstanding_request() const {
28 return outstanding_request_ != NULL;
31 int Resolve(const RequestInfo& info,
32 RequestPriority priority,
33 AddressList* addresses,
34 const CompletionCallback& callback,
35 RequestHandle* out_req,
36 const BoundNetLog& net_log) override {
37 EXPECT_FALSE(has_outstanding_request());
38 outstanding_request_ = reinterpret_cast<RequestHandle>(0x1234);
39 *out_req = outstanding_request_;
41 // Never complete this request! Caller is expected to cancel it
42 // before destroying the resolver.
43 return ERR_IO_PENDING;
46 int ResolveFromCache(const RequestInfo& info,
47 AddressList* addresses,
48 const BoundNetLog& net_log) override {
49 NOTIMPLEMENTED();
50 return ERR_UNEXPECTED;
53 void CancelRequest(RequestHandle req) override {
54 EXPECT_TRUE(has_outstanding_request());
55 EXPECT_EQ(req, outstanding_request_);
56 outstanding_request_ = NULL;
59 private:
60 RequestHandle outstanding_request_;
62 DISALLOW_COPY_AND_ASSIGN(HangingHostResolver);
65 // Test that a regular end-to-end lookup returns the expected result.
66 TEST(SingleRequestHostResolverTest, NormalResolve) {
67 // Create a host resolver dependency that returns address "199.188.1.166"
68 // for resolutions of "watsup".
69 MockHostResolver resolver;
70 resolver.rules()->AddIPLiteralRule("watsup", "199.188.1.166", std::string());
72 SingleRequestHostResolver single_request_resolver(&resolver);
74 // Resolve "watsup:90" using our SingleRequestHostResolver.
75 AddressList addrlist;
76 TestCompletionCallback callback;
77 HostResolver::RequestInfo request(HostPortPair("watsup", 90));
78 int rv = single_request_resolver.Resolve(
79 request, DEFAULT_PRIORITY, &addrlist, callback.callback(), BoundNetLog());
80 EXPECT_EQ(ERR_IO_PENDING, rv);
81 EXPECT_EQ(OK, callback.WaitForResult());
83 // Verify that the result is what we specified in the MockHostResolver.
84 ASSERT_FALSE(addrlist.empty());
85 EXPECT_EQ("199.188.1.166", addrlist.front().ToStringWithoutPort());
88 // Test that the Cancel() method cancels any outstanding request.
89 TEST(SingleRequestHostResolverTest, Cancel) {
90 HangingHostResolver resolver;
93 SingleRequestHostResolver single_request_resolver(&resolver);
95 // Resolve "watsup:90" using our SingleRequestHostResolver.
96 AddressList addrlist;
97 TestCompletionCallback callback;
98 HostResolver::RequestInfo request(HostPortPair("watsup", 90));
99 int rv = single_request_resolver.Resolve(request,
100 DEFAULT_PRIORITY,
101 &addrlist,
102 callback.callback(),
103 BoundNetLog());
104 EXPECT_EQ(ERR_IO_PENDING, rv);
105 EXPECT_TRUE(resolver.has_outstanding_request());
108 // Now that the SingleRequestHostResolver has been destroyed, the
109 // in-progress request should have been aborted.
110 EXPECT_FALSE(resolver.has_outstanding_request());
113 // Test that the Cancel() method is a no-op when there is no outstanding
114 // request.
115 TEST(SingleRequestHostResolverTest, CancelWhileNoPendingRequest) {
116 HangingHostResolver resolver;
117 SingleRequestHostResolver single_request_resolver(&resolver);
118 single_request_resolver.Cancel();
120 // To pass, HangingHostResolver should not have received a cancellation
121 // request (since there is nothing to cancel). If it does, it will crash.
124 } // namespace
126 } // namespace net