OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/dns/dns_transaction.h" | 5 #include "net/dns/dns_transaction.h" |
6 | 6 |
7 #include <deque> | 7 #include <deque> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/test/test_timeouts.h" |
11 #include "base/time.h" | 12 #include "base/time.h" |
12 #include "net/dns/dns_protocol.h" | 13 #include "net/dns/dns_protocol.h" |
13 #include "net/dns/dns_query.h" | 14 #include "net/dns/dns_query.h" |
14 #include "net/dns/dns_response.h" | 15 #include "net/dns/dns_response.h" |
15 #include "net/dns/dns_session.h" | 16 #include "net/dns/dns_session.h" |
16 #include "net/dns/dns_test_util.h" | 17 #include "net/dns/dns_test_util.h" |
17 #include "net/socket/socket_test_util.h" | 18 #include "net/socket/socket_test_util.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 namespace net { | 21 namespace net { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 // A mock for RandIntCallback that always returns 0. | 25 // A mock for RandIntCallback that always returns 0. |
25 int ReturnZero(int min, int max) { | 26 int ReturnZero(int min, int max) { |
26 return 0; | 27 return 0; |
27 } | 28 } |
28 | 29 |
29 class DnsTransactionTest : public testing::Test { | 30 class DnsTransactionTest : public testing::Test { |
30 protected: | 31 protected: |
31 virtual void SetUp() OVERRIDE { | 32 virtual void SetUp() OVERRIDE { |
32 DnsConfig config; | 33 qname_ = std::string(kT0DnsName, arraysize(kT0DnsName)); |
| 34 // Use long timeout to prevent timing out on slow bots. |
| 35 ConfigureSession(base::TimeDelta::FromMilliseconds( |
| 36 TestTimeouts::action_timeout_ms())); |
| 37 callback_ = base::Bind(&DnsTransactionTest::OnTransactionComplete, |
| 38 base::Unretained(this)); |
| 39 } |
33 | 40 |
| 41 void ConfigureSession(const base::TimeDelta& timeout) { |
34 IPEndPoint dns_server; | 42 IPEndPoint dns_server; |
35 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); | 43 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); |
36 ASSERT_TRUE(rv); | 44 ASSERT_TRUE(rv); |
| 45 |
| 46 DnsConfig config; |
37 config.nameservers.push_back(dns_server); | 47 config.nameservers.push_back(dns_server); |
38 config.attempts = 3; | 48 config.attempts = 3; |
39 config.timeout = base::TimeDelta::FromMilliseconds(20); | 49 config.timeout = timeout; |
40 | |
41 qname_ = std::string(kT0DnsName, arraysize(kT0DnsName)); | |
42 | 50 |
43 session_ = new DnsSession(config, | 51 session_ = new DnsSession(config, |
44 new MockClientSocketFactory(), | 52 new MockClientSocketFactory(), |
45 base::Bind(&ReturnZero), | 53 base::Bind(&ReturnZero), |
46 NULL /* NetLog */); | 54 NULL /* NetLog */); |
47 | |
48 callback_ = base::Bind(&DnsTransactionTest::OnTransactionComplete, | |
49 base::Unretained(this)); | |
50 } | 55 } |
51 | 56 |
52 void StartTransaction() { | 57 void StartTransaction() { |
53 transaction_.reset(new DnsTransaction(session_.get(), | 58 transaction_.reset(new DnsTransaction(session_.get(), |
54 qname_, | 59 qname_, |
55 kT0Qtype, | 60 kT0Qtype, |
56 callback_, | 61 callback_, |
57 BoundNetLog())); | 62 BoundNetLog())); |
58 | 63 |
59 int rv0 = transaction_->Start(); | 64 int rv0 = transaction_->Start(); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram), | 149 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram), |
145 arraysize(kT0ResponseDatagram)) | 150 arraysize(kT0ResponseDatagram)) |
146 }; | 151 }; |
147 | 152 |
148 scoped_refptr<DelayedSocketData> socket0_data( | 153 scoped_refptr<DelayedSocketData> socket0_data( |
149 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 154 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
150 scoped_refptr<DelayedSocketData> socket1_data( | 155 scoped_refptr<DelayedSocketData> socket1_data( |
151 new DelayedSocketData(0, reads0, arraysize(reads0), | 156 new DelayedSocketData(0, reads0, arraysize(reads0), |
152 writes0, arraysize(writes0))); | 157 writes0, arraysize(writes0))); |
153 | 158 |
| 159 // Use short timeout to speed up the test. |
| 160 ConfigureSession(base::TimeDelta::FromMilliseconds( |
| 161 TestTimeouts::tiny_timeout_ms())); |
154 factory().AddSocketDataProvider(socket0_data.get()); | 162 factory().AddSocketDataProvider(socket0_data.get()); |
155 factory().AddSocketDataProvider(socket1_data.get()); | 163 factory().AddSocketDataProvider(socket1_data.get()); |
156 | 164 |
157 StartTransaction(); | 165 StartTransaction(); |
158 | 166 |
159 MessageLoop::current()->Run(); | 167 MessageLoop::current()->Run(); |
160 | 168 |
161 EXPECT_EQ(OK, rv()); | 169 EXPECT_EQ(OK, rv()); |
162 | 170 |
163 EXPECT_TRUE(socket0_data->at_read_eof()); | 171 EXPECT_TRUE(socket0_data->at_read_eof()); |
(...skipping 18 matching lines...) Expand all Loading... |
182 }; | 190 }; |
183 | 191 |
184 scoped_refptr<DelayedSocketData> socket0_data( | 192 scoped_refptr<DelayedSocketData> socket0_data( |
185 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 193 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
186 scoped_refptr<DelayedSocketData> socket1_data( | 194 scoped_refptr<DelayedSocketData> socket1_data( |
187 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 195 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
188 scoped_refptr<DelayedSocketData> socket2_data( | 196 scoped_refptr<DelayedSocketData> socket2_data( |
189 new DelayedSocketData(0, reads0, arraysize(reads0), | 197 new DelayedSocketData(0, reads0, arraysize(reads0), |
190 writes0, arraysize(writes0))); | 198 writes0, arraysize(writes0))); |
191 | 199 |
| 200 // Use short timeout to speed up the test. |
| 201 ConfigureSession(base::TimeDelta::FromMilliseconds( |
| 202 TestTimeouts::tiny_timeout_ms())); |
192 factory().AddSocketDataProvider(socket0_data.get()); | 203 factory().AddSocketDataProvider(socket0_data.get()); |
193 factory().AddSocketDataProvider(socket1_data.get()); | 204 factory().AddSocketDataProvider(socket1_data.get()); |
194 factory().AddSocketDataProvider(socket2_data.get()); | 205 factory().AddSocketDataProvider(socket2_data.get()); |
195 | 206 |
196 StartTransaction(); | 207 StartTransaction(); |
197 | 208 |
198 MessageLoop::current()->Run(); | 209 MessageLoop::current()->Run(); |
199 | 210 |
200 EXPECT_EQ(OK, rv()); | 211 EXPECT_EQ(OK, rv()); |
201 | 212 |
(...skipping 15 matching lines...) Expand all Loading... |
217 arraysize(kT0QueryDatagram)) | 228 arraysize(kT0QueryDatagram)) |
218 }; | 229 }; |
219 | 230 |
220 scoped_refptr<DelayedSocketData> socket0_data( | 231 scoped_refptr<DelayedSocketData> socket0_data( |
221 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 232 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
222 scoped_refptr<DelayedSocketData> socket1_data( | 233 scoped_refptr<DelayedSocketData> socket1_data( |
223 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 234 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
224 scoped_refptr<DelayedSocketData> socket2_data( | 235 scoped_refptr<DelayedSocketData> socket2_data( |
225 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 236 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
226 | 237 |
| 238 // Use short timeout to speed up the test. |
| 239 ConfigureSession(base::TimeDelta::FromMilliseconds( |
| 240 TestTimeouts::tiny_timeout_ms())); |
227 factory().AddSocketDataProvider(socket0_data.get()); | 241 factory().AddSocketDataProvider(socket0_data.get()); |
228 factory().AddSocketDataProvider(socket1_data.get()); | 242 factory().AddSocketDataProvider(socket1_data.get()); |
229 factory().AddSocketDataProvider(socket2_data.get()); | 243 factory().AddSocketDataProvider(socket2_data.get()); |
230 | 244 |
231 StartTransaction(); | 245 StartTransaction(); |
232 | 246 |
233 MessageLoop::current()->Run(); | 247 MessageLoop::current()->Run(); |
234 | 248 |
235 EXPECT_EQ(ERR_DNS_TIMED_OUT, rv()); | 249 EXPECT_EQ(ERR_DNS_TIMED_OUT, rv()); |
236 | 250 |
237 EXPECT_TRUE(socket0_data->at_read_eof()); | 251 EXPECT_TRUE(socket0_data->at_read_eof()); |
238 EXPECT_TRUE(socket0_data->at_write_eof()); | 252 EXPECT_TRUE(socket0_data->at_write_eof()); |
239 EXPECT_TRUE(socket1_data->at_read_eof()); | 253 EXPECT_TRUE(socket1_data->at_read_eof()); |
240 EXPECT_TRUE(socket1_data->at_write_eof()); | 254 EXPECT_TRUE(socket1_data->at_write_eof()); |
241 EXPECT_TRUE(socket2_data->at_read_eof()); | 255 EXPECT_TRUE(socket2_data->at_read_eof()); |
242 EXPECT_TRUE(socket2_data->at_write_eof()); | 256 EXPECT_TRUE(socket2_data->at_write_eof()); |
243 EXPECT_EQ(3u, factory().udp_client_sockets().size()); | 257 EXPECT_EQ(3u, factory().udp_client_sockets().size()); |
244 } | 258 } |
245 | 259 |
246 } // namespace | 260 } // namespace |
247 | 261 |
248 } // namespace net | 262 } // namespace net |
OLD | NEW |