Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(371)

Side by Side Diff: net/dns/single_request_host_resolver_unittest.cc

Issue 992733002: Remove //net (except for Android test stuff) and sdch (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/dns/single_request_host_resolver.cc ('k') | net/docs/bug-triage.txt » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 ~HangingHostResolver() override { EXPECT_TRUE(!has_outstanding_request()); }
26
27 bool has_outstanding_request() const {
28 return outstanding_request_ != NULL;
29 }
30
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_;
40
41 // Never complete this request! Caller is expected to cancel it
42 // before destroying the resolver.
43 return ERR_IO_PENDING;
44 }
45
46 int ResolveFromCache(const RequestInfo& info,
47 AddressList* addresses,
48 const BoundNetLog& net_log) override {
49 NOTIMPLEMENTED();
50 return ERR_UNEXPECTED;
51 }
52
53 void CancelRequest(RequestHandle req) override {
54 EXPECT_TRUE(has_outstanding_request());
55 EXPECT_EQ(req, outstanding_request_);
56 outstanding_request_ = NULL;
57 }
58
59 private:
60 RequestHandle outstanding_request_;
61
62 DISALLOW_COPY_AND_ASSIGN(HangingHostResolver);
63 };
64
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());
71
72 SingleRequestHostResolver single_request_resolver(&resolver);
73
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());
82
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());
86 }
87
88 // Test that the Cancel() method cancels any outstanding request.
89 TEST(SingleRequestHostResolverTest, Cancel) {
90 HangingHostResolver resolver;
91
92 {
93 SingleRequestHostResolver single_request_resolver(&resolver);
94
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());
106 }
107
108 // Now that the SingleRequestHostResolver has been destroyed, the
109 // in-progress request should have been aborted.
110 EXPECT_FALSE(resolver.has_outstanding_request());
111 }
112
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();
119
120 // To pass, HangingHostResolver should not have received a cancellation
121 // request (since there is nothing to cancel). If it does, it will crash.
122 }
123
124 } // namespace
125
126 } // namespace net
OLDNEW
« no previous file with comments | « net/dns/single_request_host_resolver.cc ('k') | net/docs/bug-triage.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698