Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / dns / single_request_host_resolver_unittest.cc
blobcc20bf3fe443d3c614eb3ad9fbeecad7d6cc9d48
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"
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 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 RequestPriority priority,
35 AddressList* addresses,
36 const CompletionCallback& callback,
37 RequestHandle* out_req,
38 const BoundNetLog& net_log) OVERRIDE {
39 EXPECT_FALSE(has_outstanding_request());
40 outstanding_request_ = reinterpret_cast<RequestHandle>(0x1234);
41 *out_req = outstanding_request_;
43 // Never complete this request! Caller is expected to cancel it
44 // before destroying the resolver.
45 return ERR_IO_PENDING;
48 virtual int ResolveFromCache(const RequestInfo& info,
49 AddressList* addresses,
50 const BoundNetLog& net_log) OVERRIDE {
51 NOTIMPLEMENTED();
52 return ERR_UNEXPECTED;
55 virtual void CancelRequest(RequestHandle req) OVERRIDE {
56 EXPECT_TRUE(has_outstanding_request());
57 EXPECT_EQ(req, outstanding_request_);
58 outstanding_request_ = NULL;
61 private:
62 RequestHandle outstanding_request_;
64 DISALLOW_COPY_AND_ASSIGN(HangingHostResolver);
67 // Test that a regular end-to-end lookup returns the expected result.
68 TEST(SingleRequestHostResolverTest, NormalResolve) {
69 // Create a host resolver dependency that returns address "199.188.1.166"
70 // for resolutions of "watsup".
71 MockHostResolver resolver;
72 resolver.rules()->AddIPLiteralRule("watsup", "199.188.1.166", std::string());
74 SingleRequestHostResolver single_request_resolver(&resolver);
76 // Resolve "watsup:90" using our SingleRequestHostResolver.
77 AddressList addrlist;
78 TestCompletionCallback callback;
79 HostResolver::RequestInfo request(HostPortPair("watsup", 90));
80 int rv = single_request_resolver.Resolve(
81 request, DEFAULT_PRIORITY, &addrlist, callback.callback(), BoundNetLog());
82 EXPECT_EQ(ERR_IO_PENDING, rv);
83 EXPECT_EQ(OK, callback.WaitForResult());
85 // Verify that the result is what we specified in the MockHostResolver.
86 ASSERT_FALSE(addrlist.empty());
87 EXPECT_EQ("199.188.1.166", addrlist.front().ToStringWithoutPort());
90 // Test that the Cancel() method cancels any outstanding request.
91 TEST(SingleRequestHostResolverTest, Cancel) {
92 HangingHostResolver resolver;
95 SingleRequestHostResolver single_request_resolver(&resolver);
97 // Resolve "watsup:90" using our SingleRequestHostResolver.
98 AddressList addrlist;
99 TestCompletionCallback callback;
100 HostResolver::RequestInfo request(HostPortPair("watsup", 90));
101 int rv = single_request_resolver.Resolve(request,
102 DEFAULT_PRIORITY,
103 &addrlist,
104 callback.callback(),
105 BoundNetLog());
106 EXPECT_EQ(ERR_IO_PENDING, rv);
107 EXPECT_TRUE(resolver.has_outstanding_request());
110 // Now that the SingleRequestHostResolver has been destroyed, the
111 // in-progress request should have been aborted.
112 EXPECT_FALSE(resolver.has_outstanding_request());
115 // Test that the Cancel() method is a no-op when there is no outstanding
116 // request.
117 TEST(SingleRequestHostResolverTest, CancelWhileNoPendingRequest) {
118 HangingHostResolver resolver;
119 SingleRequestHostResolver single_request_resolver(&resolver);
120 single_request_resolver.Cancel();
122 // To pass, HangingHostResolver should not have received a cancellation
123 // request (since there is nothing to cancel). If it does, it will crash.
126 } // namespace
128 } // namespace net