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

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

Issue 10878090: Keep pool of pre-connected DNS sockets (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 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
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/dns_session.h"
6
7 #include <list>
8
9 #include "base/bind.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/rand_util.h"
12 #include "base/stl_util.h"
13 #include "net/base/net_log.h"
14 #include "net/dns/dns_protocol.h"
15 #include "net/dns/dns_socket_pool.h"
16 #include "net/socket/socket_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace net {
20
21 namespace {
22
23 class TestClientSocketFactory : public ClientSocketFactory {
24 public:
25 virtual ~TestClientSocketFactory();
26
27 virtual DatagramClientSocket* CreateDatagramClientSocket(
28 DatagramSocket::BindType bind_type,
29 const RandIntCallback& rand_int_cb,
30 net::NetLog* net_log,
31 const net::NetLog::Source& source) OVERRIDE;
32
33 virtual StreamSocket* CreateTransportClientSocket(
34 const AddressList& addresses,
35 NetLog*, const NetLog::Source&) {
szym 2012/09/26 11:16:12 nit: OVERRIDE
Deprecated (see juliatuttle) 2012/09/26 20:44:50 Done.
36 NOTIMPLEMENTED();
37 return NULL;
38 }
39
40 virtual SSLClientSocket* CreateSSLClientSocket(
41 ClientSocketHandle* transport_socket,
42 const HostPortPair& host_and_port,
43 const SSLConfig& ssl_config,
44 const SSLClientSocketContext& context) {
szym 2012/09/26 11:16:12 nit: OVERRIDE
Deprecated (see juliatuttle) 2012/09/26 20:44:50 Done.
45 NOTIMPLEMENTED();
46 return NULL;
47 }
48
49 virtual void ClearSSLSessionCache() { NOTIMPLEMENTED(); }
szym 2012/09/26 11:16:12 nit: OVERRIDE
Deprecated (see juliatuttle) 2012/09/26 20:44:50 Done.
50
51 private:
52 std::list<SocketDataProvider*> data_providers_;
53 };
54
55 struct PoolEvent {
56 enum { ALLOCATE, FREE } action;
57 unsigned server_index;
58 };
59
60 class DnsSessionTest : public testing::Test {
61 public:
62 void OnSocketAllocated(unsigned server_index);
63 void OnSocketFreed(unsigned server_index);
64
65 protected:
66 void Initialize(unsigned num_servers);
67 scoped_ptr<DnsSession::SocketLease> Allocate(unsigned server_index);
68 bool DidAllocate(unsigned server_index);
69 bool DidFree(unsigned server_index);
70 bool NoMoreEvents();
71
72 DnsConfig config_;
73 scoped_ptr<TestClientSocketFactory> test_client_socket_factory_;
74 scoped_refptr<DnsSession> session_;
75 NetLog::Source source_;
76
77 private:
78 bool ExpectEvent(const PoolEvent& event);
79 std::list<PoolEvent> events_;
80 };
81
82 class NullDnsSocketPool : public DnsSocketPool {
szym 2012/09/26 11:16:12 I suggest Mock rather than Null.
Deprecated (see juliatuttle) 2012/09/26 20:44:50 Done.
83 public:
84 NullDnsSocketPool(ClientSocketFactory* factory, DnsSessionTest* test)
85 : DnsSocketPool(factory), test_(test) { }
86
87 virtual ~NullDnsSocketPool() { }
88
89 virtual void Initialize(
90 NetLog* net_log,
91 const std::vector<IPEndPoint>* nameservers) {
92 InitializeInternal(net_log, nameservers);
93 }
94
95 virtual scoped_ptr<DatagramClientSocket> AllocateSocket(
96 unsigned server_index) OVERRIDE {
97 test_->OnSocketAllocated(server_index);
98 return CreateConnectedSocket(server_index).Pass();
szym 2012/09/26 11:16:12 No need for Pass().
Deprecated (see juliatuttle) 2012/09/26 20:44:50 Done.
99 }
100
101 virtual void FreeSocket(
102 unsigned server_index,
103 scoped_ptr<DatagramClientSocket> socket) OVERRIDE {
104 test_->OnSocketFreed(server_index);
105 }
106
107 private:
108 DnsSessionTest* test_;
109 };
110
111 void DnsSessionTest::Initialize(unsigned num_servers) {
112 CHECK(num_servers < 256u);
113 config_.nameservers.clear();
114 IPAddressNumber dns_ip;
115 bool rv = ParseIPLiteralToNumber("192.168.1.0", &dns_ip);
116 EXPECT_TRUE(rv);
117 for (unsigned char i = 0; i < num_servers; ++i) {
118 dns_ip[3] = i;
119 IPEndPoint dns_endpoint(dns_ip, dns_protocol::kDefaultPort);
120 config_.nameservers.push_back(dns_endpoint);
121 }
122
123 test_client_socket_factory_.reset(new TestClientSocketFactory());
124
125 DnsSocketPool* dns_socket_pool =
126 new NullDnsSocketPool(test_client_socket_factory_.get(), this);
127
128 session_ = new DnsSession(config_,
129 scoped_ptr<DnsSocketPool>(dns_socket_pool),
130 base::Bind(&base::RandInt),
131 NULL /* NetLog */);
132
133 events_.clear();
134 }
135
136 scoped_ptr<DnsSession::SocketLease> DnsSessionTest::Allocate(
137 unsigned server_index) {
138 return session_->AllocateSocket(server_index, source_);
139 }
140
141 bool DnsSessionTest::DidAllocate(unsigned server_index) {
142 PoolEvent expected_event = { PoolEvent::ALLOCATE, server_index };
143 return ExpectEvent(expected_event);
144 }
145
146 bool DnsSessionTest::DidFree(unsigned server_index) {
147 PoolEvent expected_event = { PoolEvent::FREE, server_index };
148 return ExpectEvent(expected_event);
149 }
150
151 bool DnsSessionTest::NoMoreEvents() {
152 return events_.empty();
153 }
154
155 void DnsSessionTest::OnSocketAllocated(unsigned server_index) {
156 PoolEvent event = { PoolEvent::ALLOCATE, server_index };
157 events_.push_back(event);
158 }
159
160 void DnsSessionTest::OnSocketFreed(unsigned server_index) {
161 PoolEvent event = { PoolEvent::FREE, server_index };
162 events_.push_back(event);
163 }
164
165 bool DnsSessionTest::ExpectEvent(const PoolEvent& expected) {
166 if (events_.empty()) {
167 return false;
168 }
169
170 const PoolEvent actual = events_.front();
171 if ((expected.action != actual.action)
172 || (expected.server_index != actual.server_index)) {
173 return false;
174 }
175 events_.pop_front();
176
177 return true;
178 }
179
180 DatagramClientSocket* TestClientSocketFactory::CreateDatagramClientSocket(
181 DatagramSocket::BindType bind_type,
182 const RandIntCallback& rand_int_cb,
183 net::NetLog* net_log,
184 const net::NetLog::Source& source) {
185 // We're not actually expecting to send or receive any data, so use the
186 // simplest SocketDataProvider with no data supplied.
187 SocketDataProvider* data_provider = new StaticSocketDataProvider();
188 data_providers_.push_back(data_provider);
189 MockUDPClientSocket* socket = new MockUDPClientSocket(data_provider, net_log);
190 data_provider->set_socket(socket);
191 return socket;
192 }
193
194 TestClientSocketFactory::~TestClientSocketFactory() {
195 STLDeleteElements(&data_providers_);
196 }
197
198 TEST_F(DnsSessionTest, AllocateFree) {
199 scoped_ptr<DnsSession::SocketLease> lease1, lease2;
200
201 Initialize(2);
202 EXPECT_TRUE(NoMoreEvents());
203
204 lease1 = Allocate(0);
205 EXPECT_TRUE(DidAllocate(0));
206 EXPECT_TRUE(NoMoreEvents());
207
208 lease2 = Allocate(1);
209 EXPECT_TRUE(DidAllocate(1));
210 EXPECT_TRUE(NoMoreEvents());
211
212 lease1.reset();
213 EXPECT_TRUE(DidFree(0));
214 EXPECT_TRUE(NoMoreEvents());
215
216 lease2.reset();
217 EXPECT_TRUE(DidFree(1));
218 EXPECT_TRUE(NoMoreEvents());
szym 2012/09/26 11:16:12 Since all that's tested here is whether DnsSession
Deprecated (see juliatuttle) 2012/09/26 20:44:50 I'm going to leave this, since it works and is not
219 }
220
221 } // namespace
222
223 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698