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

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

Issue 6993015: Add unit-tests for SingleRequestHostResolver. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Try to re-upload cuz patch not applying on commit bot... Created 9 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « net/base/single_request_host_resolver.cc ('k') | net/curvecp/test_client.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2011 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/base/single_request_host_resolver.h"
6
7 #include "net/base/mock_host_resolver.h"
8 #include "net/base/net_errors.h"
9 #include "net/base/test_completion_callback.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace net {
13
14 namespace {
15
16 // Helper class used by SingleRequestHostResolverTest.Cancel test.
17 // It checks that only one request is outstanding at a time, and that
18 // it is cancelled before the class is destroyed.
19 class HangingHostResolver : public HostResolver {
20 public:
21 HangingHostResolver() : outstanding_request_(NULL) {}
22
23 virtual ~HangingHostResolver() {
24 EXPECT_TRUE(!has_outstanding_request());
25 }
26
27 bool has_outstanding_request() const {
28 return outstanding_request_ != NULL;
29 }
30
31 virtual int Resolve(const RequestInfo& info,
32 AddressList* addresses,
33 CompletionCallback* callback,
34 RequestHandle* out_req,
35 const BoundNetLog& net_log) {
36 EXPECT_FALSE(has_outstanding_request());
37 outstanding_request_ = reinterpret_cast<RequestHandle>(0x1234);
38 *out_req = outstanding_request_;
39
40 // Never complete this request! Caller is expected to cancel it
41 // before destroying the resolver.
42 return ERR_IO_PENDING;
43 }
44
45 virtual void CancelRequest(RequestHandle req) {
46 EXPECT_TRUE(has_outstanding_request());
47 EXPECT_EQ(req, outstanding_request_);
48 outstanding_request_ = NULL;
49 }
50
51 virtual void AddObserver(Observer* observer) {
52 FAIL();
53 }
54
55 virtual void RemoveObserver(Observer* observer) {
56 FAIL();
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", "");
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, &addrlist, &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 EXPECT_EQ("199.188.1.166", NetAddressToString(addrlist.head()));
85 }
86
87 // Test that the Cancel() method cancels any outstanding request.
88 TEST(SingleRequestHostResolverTest, Cancel) {
89 HangingHostResolver resolver;
90
91 {
92 SingleRequestHostResolver single_request_resolver(&resolver);
93
94 // Resolve "watsup:90" using our SingleRequestHostResolver.
95 AddressList addrlist;
96 TestCompletionCallback callback;
97 HostResolver::RequestInfo request(HostPortPair("watsup", 90));
98 int rv = single_request_resolver.Resolve(
99 request, &addrlist, &callback, BoundNetLog());
100 EXPECT_EQ(ERR_IO_PENDING, rv);
101 EXPECT_TRUE(resolver.has_outstanding_request());
102 }
103
104 // Now that the SingleRequestHostResolver has been destroyed, the
105 // in-progress request should have been aborted.
106 EXPECT_FALSE(resolver.has_outstanding_request());
107 }
108
109 // Test that the Cancel() method is a no-op when there is no outstanding
110 // request.
111 TEST(SingleRequestHostResolverTest, CancelWhileNoPendingRequest) {
112 HangingHostResolver resolver;
113 SingleRequestHostResolver single_request_resolver(&resolver);
114 single_request_resolver.Cancel();
115
116 // To pass, HangingHostResolver should not have received a cancellation
117 // request (since there is nothing to cancel). If it does, it will crash.
118 }
119
120 } // namespace
121
122 } // namespace net
OLDNEW
« no previous file with comments | « net/base/single_request_host_resolver.cc ('k') | net/curvecp/test_client.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698