Updating trunk VERSION from 849.0 to 850.0
[chromium-blink-merge.git] / net / dns / dns_transaction_unittest.cc
bloba99f55b35b0eba8e5406522646e99a722a8067cc
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.
5 #include "net/dns/dns_transaction.h"
7 #include <deque>
8 #include <vector>
10 #include "base/bind.h"
11 #include "net/dns/dns_test_util.h"
12 #include "net/socket/socket_test_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace net {
17 namespace {
19 static const base::TimeDelta kTimeoutsMs[] = {
20 base::TimeDelta::FromMilliseconds(20),
21 base::TimeDelta::FromMilliseconds(20),
22 base::TimeDelta::FromMilliseconds(20),
25 } // namespace
27 class TestDelegate : public DnsTransaction::Delegate {
28 public:
29 TestDelegate() : result_(ERR_UNEXPECTED), transaction_(NULL) {}
30 virtual ~TestDelegate() {}
31 virtual void OnTransactionComplete(
32 int result,
33 const DnsTransaction* transaction,
34 const IPAddressList& ip_addresses) {
35 result_ = result;
36 transaction_ = transaction;
37 ip_addresses_ = ip_addresses;
38 MessageLoop::current()->Quit();
40 int result() const { return result_; }
41 const DnsTransaction* transaction() const { return transaction_; }
42 const IPAddressList& ip_addresses() const {
43 return ip_addresses_;
46 private:
47 int result_;
48 const DnsTransaction* transaction_;
49 IPAddressList ip_addresses_;
51 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
55 TEST(DnsTransactionTest, NormalQueryResponseTest) {
56 MockWrite writes0[] = {
57 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram),
58 arraysize(kT0QueryDatagram))
61 MockRead reads0[] = {
62 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram),
63 arraysize(kT0ResponseDatagram))
66 StaticSocketDataProvider data(reads0, arraysize(reads0),
67 writes0, arraysize(writes0));
68 MockClientSocketFactory factory;
69 factory.AddSocketDataProvider(&data);
71 TestPrng test_prng(std::deque<int>(1, 0));
72 RandIntCallback rand_int_cb =
73 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng));
74 std::string t0_dns_name(kT0DnsName, arraysize(kT0DnsName));
76 IPEndPoint dns_server;
77 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server);
78 ASSERT_TRUE(rv);
80 DnsTransaction t(dns_server, t0_dns_name, kT1Qtype, rand_int_cb, &factory,
81 BoundNetLog(), NULL);
83 TestDelegate delegate;
84 t.SetDelegate(&delegate);
86 IPAddressList expected_ip_addresses;
87 rv = ConvertStringsToIPAddressList(kT0IpAddresses,
88 arraysize(kT0IpAddresses),
89 &expected_ip_addresses);
90 ASSERT_TRUE(rv);
92 int rv0 = t.Start();
93 EXPECT_EQ(ERR_IO_PENDING, rv0);
95 MessageLoop::current()->Run();
97 EXPECT_TRUE(DnsTransaction::Key(t0_dns_name, kT0Qtype) == t.key());
98 EXPECT_EQ(OK, delegate.result());
99 EXPECT_EQ(&t, delegate.transaction());
100 EXPECT_TRUE(expected_ip_addresses == delegate.ip_addresses());
102 EXPECT_TRUE(data.at_read_eof());
103 EXPECT_TRUE(data.at_write_eof());
106 TEST(DnsTransactionTest, MismatchedQueryResponseTest) {
107 MockWrite writes0[] = {
108 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram),
109 arraysize(kT0QueryDatagram))
112 MockRead reads1[] = {
113 MockRead(true, reinterpret_cast<const char*>(kT1ResponseDatagram),
114 arraysize(kT1ResponseDatagram))
117 StaticSocketDataProvider data(reads1, arraysize(reads1),
118 writes0, arraysize(writes0));
119 MockClientSocketFactory factory;
120 factory.AddSocketDataProvider(&data);
122 TestPrng test_prng(std::deque<int>(1, 0));
123 RandIntCallback rand_int_cb =
124 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng));
125 std::string t0_dns_name(kT0DnsName, arraysize(kT0DnsName));
127 IPEndPoint dns_server;
128 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server);
129 ASSERT_TRUE(rv);
131 DnsTransaction t(dns_server, t0_dns_name, kT1Qtype, rand_int_cb, &factory,
132 BoundNetLog(), NULL);
134 TestDelegate delegate;
135 t.SetDelegate(&delegate);
137 int rv0 = t.Start();
138 EXPECT_EQ(ERR_IO_PENDING, rv0);
140 MessageLoop::current()->Run();
142 EXPECT_TRUE(DnsTransaction::Key(t0_dns_name, kT0Qtype) == t.key());
143 EXPECT_EQ(ERR_DNS_MALFORMED_RESPONSE, delegate.result());
144 EXPECT_EQ(0u, delegate.ip_addresses().size());
145 EXPECT_EQ(&t, delegate.transaction());
146 EXPECT_TRUE(data.at_read_eof());
147 EXPECT_TRUE(data.at_write_eof());
150 // Test that after the first timeout we do a fresh connection and if we get
151 // a response on the new connection, we return it.
152 TEST(DnsTransactionTest, FirstTimeoutTest) {
153 MockWrite writes0[] = {
154 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram),
155 arraysize(kT0QueryDatagram))
158 MockRead reads0[] = {
159 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram),
160 arraysize(kT0ResponseDatagram))
163 scoped_refptr<DelayedSocketData> socket0_data(
164 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0)));
165 scoped_refptr<DelayedSocketData> socket1_data(
166 new DelayedSocketData(0, reads0, arraysize(reads0),
167 writes0, arraysize(writes0)));
168 MockClientSocketFactory factory;
169 factory.AddSocketDataProvider(socket0_data.get());
170 factory.AddSocketDataProvider(socket1_data.get());
172 TestPrng test_prng(std::deque<int>(2, 0));
173 RandIntCallback rand_int_cb =
174 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng));
175 std::string t0_dns_name(kT0DnsName, arraysize(kT0DnsName));
177 IPEndPoint dns_server;
178 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server);
179 ASSERT_TRUE(rv);
181 DnsTransaction t(dns_server, t0_dns_name, kT1Qtype, rand_int_cb, &factory,
182 BoundNetLog(), NULL);
184 TestDelegate delegate;
185 t.SetDelegate(&delegate);
187 t.set_timeouts_ms(
188 std::vector<base::TimeDelta>(kTimeoutsMs,
189 kTimeoutsMs + arraysize(kTimeoutsMs)));
191 IPAddressList expected_ip_addresses;
192 rv = ConvertStringsToIPAddressList(kT0IpAddresses,
193 arraysize(kT0IpAddresses),
194 &expected_ip_addresses);
195 ASSERT_TRUE(rv);
197 int rv0 = t.Start();
198 EXPECT_EQ(ERR_IO_PENDING, rv0);
201 MessageLoop::current()->Run();
203 EXPECT_TRUE(DnsTransaction::Key(t0_dns_name, kT0Qtype) == t.key());
204 EXPECT_EQ(OK, delegate.result());
205 EXPECT_EQ(&t, delegate.transaction());
206 EXPECT_TRUE(expected_ip_addresses == delegate.ip_addresses());
208 EXPECT_TRUE(socket0_data->at_read_eof());
209 EXPECT_TRUE(socket0_data->at_write_eof());
210 EXPECT_TRUE(socket1_data->at_read_eof());
211 EXPECT_TRUE(socket1_data->at_write_eof());
212 EXPECT_EQ(2u, factory.udp_client_sockets().size());
215 // Test that after the first timeout we do a fresh connection, and after
216 // the second timeout we do another fresh connection, and if we get a
217 // response on the second connection, we return it.
218 TEST(DnsTransactionTest, SecondTimeoutTest) {
219 MockWrite writes0[] = {
220 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram),
221 arraysize(kT0QueryDatagram))
224 MockRead reads0[] = {
225 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram),
226 arraysize(kT0ResponseDatagram))
229 scoped_refptr<DelayedSocketData> socket0_data(
230 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0)));
231 scoped_refptr<DelayedSocketData> socket1_data(
232 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0)));
233 scoped_refptr<DelayedSocketData> socket2_data(
234 new DelayedSocketData(0, reads0, arraysize(reads0),
235 writes0, arraysize(writes0)));
236 MockClientSocketFactory factory;
237 factory.AddSocketDataProvider(socket0_data.get());
238 factory.AddSocketDataProvider(socket1_data.get());
239 factory.AddSocketDataProvider(socket2_data.get());
241 TestPrng test_prng(std::deque<int>(3, 0));
242 RandIntCallback rand_int_cb =
243 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng));
244 std::string t0_dns_name(kT0DnsName, arraysize(kT0DnsName));
246 IPEndPoint dns_server;
247 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server);
248 ASSERT_TRUE(rv);
250 DnsTransaction t(dns_server, t0_dns_name, kT1Qtype, rand_int_cb, &factory,
251 BoundNetLog(), NULL);
253 TestDelegate delegate;
254 t.SetDelegate(&delegate);
256 t.set_timeouts_ms(
257 std::vector<base::TimeDelta>(kTimeoutsMs,
258 kTimeoutsMs + arraysize(kTimeoutsMs)));
260 IPAddressList expected_ip_addresses;
261 rv = ConvertStringsToIPAddressList(kT0IpAddresses,
262 arraysize(kT0IpAddresses),
263 &expected_ip_addresses);
264 ASSERT_TRUE(rv);
266 int rv0 = t.Start();
267 EXPECT_EQ(ERR_IO_PENDING, rv0);
269 MessageLoop::current()->Run();
271 EXPECT_TRUE(DnsTransaction::Key(t0_dns_name, kT1Qtype) == t.key());
272 EXPECT_EQ(OK, delegate.result());
273 EXPECT_EQ(&t, delegate.transaction());
274 EXPECT_TRUE(expected_ip_addresses == delegate.ip_addresses());
276 EXPECT_TRUE(socket0_data->at_read_eof());
277 EXPECT_TRUE(socket0_data->at_write_eof());
278 EXPECT_TRUE(socket1_data->at_read_eof());
279 EXPECT_TRUE(socket1_data->at_write_eof());
280 EXPECT_TRUE(socket2_data->at_read_eof());
281 EXPECT_TRUE(socket2_data->at_write_eof());
282 EXPECT_EQ(3u, factory.udp_client_sockets().size());
285 // Test that after the first timeout we do a fresh connection, and after
286 // the second timeout we do another fresh connection and after the third
287 // timeout we give up and return a timeout error.
288 TEST(DnsTransactionTest, ThirdTimeoutTest) {
289 MockWrite writes0[] = {
290 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram),
291 arraysize(kT0QueryDatagram))
294 scoped_refptr<DelayedSocketData> socket0_data(
295 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0)));
296 scoped_refptr<DelayedSocketData> socket1_data(
297 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0)));
298 scoped_refptr<DelayedSocketData> socket2_data(
299 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0)));
300 MockClientSocketFactory factory;
301 factory.AddSocketDataProvider(socket0_data.get());
302 factory.AddSocketDataProvider(socket1_data.get());
303 factory.AddSocketDataProvider(socket2_data.get());
305 TestPrng test_prng(std::deque<int>(3, 0));
306 RandIntCallback rand_int_cb =
307 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng));
308 std::string t0_dns_name(kT0DnsName, arraysize(kT0DnsName));
310 IPEndPoint dns_server;
311 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server);
312 ASSERT_TRUE(rv);
314 DnsTransaction t(dns_server, t0_dns_name, kT1Qtype, rand_int_cb, &factory,
315 BoundNetLog(), NULL);
317 TestDelegate delegate;
318 t.SetDelegate(&delegate);
320 t.set_timeouts_ms(
321 std::vector<base::TimeDelta>(kTimeoutsMs,
322 kTimeoutsMs + arraysize(kTimeoutsMs)));
324 int rv0 = t.Start();
325 EXPECT_EQ(ERR_IO_PENDING, rv0);
327 MessageLoop::current()->Run();
329 EXPECT_TRUE(DnsTransaction::Key(t0_dns_name, kT0Qtype) == t.key());
330 EXPECT_EQ(ERR_DNS_TIMED_OUT, delegate.result());
331 EXPECT_EQ(&t, delegate.transaction());
333 EXPECT_TRUE(socket0_data->at_read_eof());
334 EXPECT_TRUE(socket0_data->at_write_eof());
335 EXPECT_TRUE(socket1_data->at_read_eof());
336 EXPECT_TRUE(socket1_data->at_write_eof());
337 EXPECT_TRUE(socket2_data->at_read_eof());
338 EXPECT_TRUE(socket2_data->at_write_eof());
339 EXPECT_EQ(3u, factory.udp_client_sockets().size());
342 } // namespace net