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" |
| 12 #include "base/time.h" |
| 13 #include "net/dns/dns_protocol.h" |
| 14 #include "net/dns/dns_query.h" |
| 15 #include "net/dns/dns_response.h" |
| 16 #include "net/dns/dns_session.h" |
11 #include "net/dns/dns_test_util.h" | 17 #include "net/dns/dns_test_util.h" |
12 #include "net/socket/socket_test_util.h" | 18 #include "net/socket/socket_test_util.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
14 | 20 |
15 namespace net { | 21 namespace net { |
16 | 22 |
17 namespace { | 23 namespace { |
18 | 24 |
19 static const base::TimeDelta kTimeoutsMs[] = { | 25 // A mock for RandIntCallback that always returns 0. |
20 base::TimeDelta::FromMilliseconds(20), | 26 int ReturnZero(int min, int max) { |
21 base::TimeDelta::FromMilliseconds(20), | 27 return 0; |
22 base::TimeDelta::FromMilliseconds(20), | 28 } |
| 29 |
| 30 class DnsTransactionTest : public testing::Test { |
| 31 protected: |
| 32 virtual void SetUp() OVERRIDE { |
| 33 callback_ = base::Bind(&DnsTransactionTest::OnTransactionComplete, |
| 34 base::Unretained(this)); |
| 35 qname_ = std::string(kT0DnsName, arraysize(kT0DnsName)); |
| 36 // Use long timeout to prevent timing out on slow bots. |
| 37 ConfigureSession(base::TimeDelta::FromMilliseconds( |
| 38 TestTimeouts::action_timeout_ms())); |
| 39 } |
| 40 |
| 41 void ConfigureSession(const base::TimeDelta& timeout) { |
| 42 IPEndPoint dns_server; |
| 43 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); |
| 44 ASSERT_TRUE(rv); |
| 45 |
| 46 DnsConfig config; |
| 47 config.nameservers.push_back(dns_server); |
| 48 config.attempts = 3; |
| 49 config.timeout = timeout; |
| 50 |
| 51 session_ = new DnsSession(config, |
| 52 new MockClientSocketFactory(), |
| 53 base::Bind(&ReturnZero), |
| 54 NULL /* NetLog */); |
| 55 } |
| 56 |
| 57 void StartTransaction() { |
| 58 transaction_.reset(new DnsTransaction(session_.get(), |
| 59 qname_, |
| 60 kT0Qtype, |
| 61 callback_, |
| 62 BoundNetLog())); |
| 63 |
| 64 int rv0 = transaction_->Start(); |
| 65 EXPECT_EQ(ERR_IO_PENDING, rv0); |
| 66 } |
| 67 |
| 68 void OnTransactionComplete(DnsTransaction* transaction, int rv) { |
| 69 EXPECT_EQ(transaction_.get(), transaction); |
| 70 EXPECT_EQ(qname_, transaction->query()->qname().as_string()); |
| 71 EXPECT_EQ(kT0Qtype, transaction->query()->qtype()); |
| 72 rv_ = rv; |
| 73 MessageLoop::current()->Quit(); |
| 74 } |
| 75 |
| 76 MockClientSocketFactory& factory() { |
| 77 return *static_cast<MockClientSocketFactory*>(session_->socket_factory()); |
| 78 } |
| 79 |
| 80 int rv() const { return rv_; } |
| 81 |
| 82 private: |
| 83 DnsTransaction::ResultCallback callback_; |
| 84 std::string qname_; |
| 85 scoped_refptr<DnsSession> session_; |
| 86 scoped_ptr<DnsTransaction> transaction_; |
| 87 |
| 88 int rv_; |
23 }; | 89 }; |
24 | 90 |
25 } // namespace | 91 TEST_F(DnsTransactionTest, NormalQueryResponseTest) { |
26 | |
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(); | |
39 } | |
40 int result() const { return result_; } | |
41 const DnsTransaction* transaction() const { return transaction_; } | |
42 const IPAddressList& ip_addresses() const { | |
43 return ip_addresses_; | |
44 } | |
45 | |
46 private: | |
47 int result_; | |
48 const DnsTransaction* transaction_; | |
49 IPAddressList ip_addresses_; | |
50 | |
51 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | |
52 }; | |
53 | |
54 | |
55 TEST(DnsTransactionTest, NormalQueryResponseTest) { | |
56 MockWrite writes0[] = { | 92 MockWrite writes0[] = { |
57 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), | 93 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), |
58 arraysize(kT0QueryDatagram)) | 94 arraysize(kT0QueryDatagram)) |
59 }; | 95 }; |
60 | 96 |
61 MockRead reads0[] = { | 97 MockRead reads0[] = { |
62 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram), | 98 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram), |
63 arraysize(kT0ResponseDatagram)) | 99 arraysize(kT0ResponseDatagram)) |
64 }; | 100 }; |
65 | 101 |
66 StaticSocketDataProvider data(reads0, arraysize(reads0), | 102 StaticSocketDataProvider data(reads0, arraysize(reads0), |
67 writes0, arraysize(writes0)); | 103 writes0, arraysize(writes0)); |
68 MockClientSocketFactory factory; | 104 factory().AddSocketDataProvider(&data); |
69 factory.AddSocketDataProvider(&data); | |
70 | 105 |
71 TestPrng test_prng(std::deque<int>(1, 0)); | 106 StartTransaction(); |
72 RandIntCallback rand_int_cb = | |
73 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); | |
74 std::string t0_dns_name(kT0DnsName, arraysize(kT0DnsName)); | |
75 | |
76 IPEndPoint dns_server; | |
77 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); | |
78 ASSERT_TRUE(rv); | |
79 | |
80 DnsTransaction t(dns_server, t0_dns_name, kT1Qtype, rand_int_cb, &factory, | |
81 BoundNetLog(), NULL); | |
82 | |
83 TestDelegate delegate; | |
84 t.SetDelegate(&delegate); | |
85 | |
86 IPAddressList expected_ip_addresses; | |
87 rv = ConvertStringsToIPAddressList(kT0IpAddresses, | |
88 arraysize(kT0IpAddresses), | |
89 &expected_ip_addresses); | |
90 ASSERT_TRUE(rv); | |
91 | |
92 int rv0 = t.Start(); | |
93 EXPECT_EQ(ERR_IO_PENDING, rv0); | |
94 | |
95 MessageLoop::current()->Run(); | 107 MessageLoop::current()->Run(); |
96 | 108 |
97 EXPECT_TRUE(DnsTransaction::Key(t0_dns_name, kT0Qtype) == t.key()); | 109 EXPECT_EQ(OK, rv()); |
98 EXPECT_EQ(OK, delegate.result()); | 110 // TODO(szym): test fields of |transaction_->response()| |
99 EXPECT_EQ(&t, delegate.transaction()); | |
100 EXPECT_TRUE(expected_ip_addresses == delegate.ip_addresses()); | |
101 | 111 |
102 EXPECT_TRUE(data.at_read_eof()); | 112 EXPECT_TRUE(data.at_read_eof()); |
103 EXPECT_TRUE(data.at_write_eof()); | 113 EXPECT_TRUE(data.at_write_eof()); |
104 } | 114 } |
105 | 115 |
106 TEST(DnsTransactionTest, MismatchedQueryResponseTest) { | 116 TEST_F(DnsTransactionTest, MismatchedQueryResponseTest) { |
107 MockWrite writes0[] = { | 117 MockWrite writes0[] = { |
108 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), | 118 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), |
109 arraysize(kT0QueryDatagram)) | 119 arraysize(kT0QueryDatagram)) |
110 }; | 120 }; |
111 | 121 |
112 MockRead reads1[] = { | 122 MockRead reads1[] = { |
113 MockRead(true, reinterpret_cast<const char*>(kT1ResponseDatagram), | 123 MockRead(true, reinterpret_cast<const char*>(kT1ResponseDatagram), |
114 arraysize(kT1ResponseDatagram)) | 124 arraysize(kT1ResponseDatagram)) |
115 }; | 125 }; |
116 | 126 |
117 StaticSocketDataProvider data(reads1, arraysize(reads1), | 127 StaticSocketDataProvider data(reads1, arraysize(reads1), |
118 writes0, arraysize(writes0)); | 128 writes0, arraysize(writes0)); |
119 MockClientSocketFactory factory; | 129 factory().AddSocketDataProvider(&data); |
120 factory.AddSocketDataProvider(&data); | |
121 | 130 |
122 TestPrng test_prng(std::deque<int>(1, 0)); | 131 StartTransaction(); |
123 RandIntCallback rand_int_cb = | |
124 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); | |
125 std::string t0_dns_name(kT0DnsName, arraysize(kT0DnsName)); | |
126 | |
127 IPEndPoint dns_server; | |
128 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); | |
129 ASSERT_TRUE(rv); | |
130 | |
131 DnsTransaction t(dns_server, t0_dns_name, kT1Qtype, rand_int_cb, &factory, | |
132 BoundNetLog(), NULL); | |
133 | |
134 TestDelegate delegate; | |
135 t.SetDelegate(&delegate); | |
136 | |
137 int rv0 = t.Start(); | |
138 EXPECT_EQ(ERR_IO_PENDING, rv0); | |
139 | |
140 MessageLoop::current()->Run(); | 132 MessageLoop::current()->Run(); |
141 | 133 |
142 EXPECT_TRUE(DnsTransaction::Key(t0_dns_name, kT0Qtype) == t.key()); | 134 EXPECT_EQ(ERR_DNS_MALFORMED_RESPONSE, rv()); |
143 EXPECT_EQ(ERR_DNS_MALFORMED_RESPONSE, delegate.result()); | 135 |
144 EXPECT_EQ(0u, delegate.ip_addresses().size()); | |
145 EXPECT_EQ(&t, delegate.transaction()); | |
146 EXPECT_TRUE(data.at_read_eof()); | 136 EXPECT_TRUE(data.at_read_eof()); |
147 EXPECT_TRUE(data.at_write_eof()); | 137 EXPECT_TRUE(data.at_write_eof()); |
148 } | 138 } |
149 | 139 |
150 // Test that after the first timeout we do a fresh connection and if we get | 140 // 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. | 141 // a response on the new connection, we return it. |
152 TEST(DnsTransactionTest, FirstTimeoutTest) { | 142 TEST_F(DnsTransactionTest, FirstTimeoutTest) { |
153 MockWrite writes0[] = { | 143 MockWrite writes0[] = { |
154 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), | 144 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), |
155 arraysize(kT0QueryDatagram)) | 145 arraysize(kT0QueryDatagram)) |
156 }; | 146 }; |
157 | 147 |
158 MockRead reads0[] = { | 148 MockRead reads0[] = { |
159 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram), | 149 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram), |
160 arraysize(kT0ResponseDatagram)) | 150 arraysize(kT0ResponseDatagram)) |
161 }; | 151 }; |
162 | 152 |
163 scoped_refptr<DelayedSocketData> socket0_data( | 153 scoped_refptr<DelayedSocketData> socket0_data( |
164 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 154 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
165 scoped_refptr<DelayedSocketData> socket1_data( | 155 scoped_refptr<DelayedSocketData> socket1_data( |
166 new DelayedSocketData(0, reads0, arraysize(reads0), | 156 new DelayedSocketData(0, reads0, arraysize(reads0), |
167 writes0, arraysize(writes0))); | 157 writes0, arraysize(writes0))); |
168 MockClientSocketFactory factory; | |
169 factory.AddSocketDataProvider(socket0_data.get()); | |
170 factory.AddSocketDataProvider(socket1_data.get()); | |
171 | 158 |
172 TestPrng test_prng(std::deque<int>(2, 0)); | 159 // Use short timeout to speed up the test. |
173 RandIntCallback rand_int_cb = | 160 ConfigureSession(base::TimeDelta::FromMilliseconds( |
174 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); | 161 TestTimeouts::tiny_timeout_ms())); |
175 std::string t0_dns_name(kT0DnsName, arraysize(kT0DnsName)); | 162 factory().AddSocketDataProvider(socket0_data.get()); |
| 163 factory().AddSocketDataProvider(socket1_data.get()); |
176 | 164 |
177 IPEndPoint dns_server; | 165 StartTransaction(); |
178 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); | |
179 ASSERT_TRUE(rv); | |
180 | |
181 DnsTransaction t(dns_server, t0_dns_name, kT1Qtype, rand_int_cb, &factory, | |
182 BoundNetLog(), NULL); | |
183 | |
184 TestDelegate delegate; | |
185 t.SetDelegate(&delegate); | |
186 | |
187 t.set_timeouts_ms( | |
188 std::vector<base::TimeDelta>(kTimeoutsMs, | |
189 kTimeoutsMs + arraysize(kTimeoutsMs))); | |
190 | |
191 IPAddressList expected_ip_addresses; | |
192 rv = ConvertStringsToIPAddressList(kT0IpAddresses, | |
193 arraysize(kT0IpAddresses), | |
194 &expected_ip_addresses); | |
195 ASSERT_TRUE(rv); | |
196 | |
197 int rv0 = t.Start(); | |
198 EXPECT_EQ(ERR_IO_PENDING, rv0); | |
199 | |
200 | 166 |
201 MessageLoop::current()->Run(); | 167 MessageLoop::current()->Run(); |
202 | 168 |
203 EXPECT_TRUE(DnsTransaction::Key(t0_dns_name, kT0Qtype) == t.key()); | 169 EXPECT_EQ(OK, rv()); |
204 EXPECT_EQ(OK, delegate.result()); | |
205 EXPECT_EQ(&t, delegate.transaction()); | |
206 EXPECT_TRUE(expected_ip_addresses == delegate.ip_addresses()); | |
207 | 170 |
208 EXPECT_TRUE(socket0_data->at_read_eof()); | 171 EXPECT_TRUE(socket0_data->at_read_eof()); |
209 EXPECT_TRUE(socket0_data->at_write_eof()); | 172 EXPECT_TRUE(socket0_data->at_write_eof()); |
210 EXPECT_TRUE(socket1_data->at_read_eof()); | 173 EXPECT_TRUE(socket1_data->at_read_eof()); |
211 EXPECT_TRUE(socket1_data->at_write_eof()); | 174 EXPECT_TRUE(socket1_data->at_write_eof()); |
212 EXPECT_EQ(2u, factory.udp_client_sockets().size()); | 175 EXPECT_EQ(2u, factory().udp_client_sockets().size()); |
213 } | 176 } |
214 | 177 |
215 // Test that after the first timeout we do a fresh connection, and after | 178 // 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 | 179 // the second timeout we do another fresh connection, and if we get a |
217 // response on the second connection, we return it. | 180 // response on the second connection, we return it. |
218 TEST(DnsTransactionTest, SecondTimeoutTest) { | 181 TEST_F(DnsTransactionTest, SecondTimeoutTest) { |
219 MockWrite writes0[] = { | 182 MockWrite writes0[] = { |
220 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), | 183 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), |
221 arraysize(kT0QueryDatagram)) | 184 arraysize(kT0QueryDatagram)) |
222 }; | 185 }; |
223 | 186 |
224 MockRead reads0[] = { | 187 MockRead reads0[] = { |
225 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram), | 188 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram), |
226 arraysize(kT0ResponseDatagram)) | 189 arraysize(kT0ResponseDatagram)) |
227 }; | 190 }; |
228 | 191 |
229 scoped_refptr<DelayedSocketData> socket0_data( | 192 scoped_refptr<DelayedSocketData> socket0_data( |
230 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 193 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
231 scoped_refptr<DelayedSocketData> socket1_data( | 194 scoped_refptr<DelayedSocketData> socket1_data( |
232 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 195 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
233 scoped_refptr<DelayedSocketData> socket2_data( | 196 scoped_refptr<DelayedSocketData> socket2_data( |
234 new DelayedSocketData(0, reads0, arraysize(reads0), | 197 new DelayedSocketData(0, reads0, arraysize(reads0), |
235 writes0, arraysize(writes0))); | 198 writes0, arraysize(writes0))); |
236 MockClientSocketFactory factory; | |
237 factory.AddSocketDataProvider(socket0_data.get()); | |
238 factory.AddSocketDataProvider(socket1_data.get()); | |
239 factory.AddSocketDataProvider(socket2_data.get()); | |
240 | 199 |
241 TestPrng test_prng(std::deque<int>(3, 0)); | 200 // Use short timeout to speed up the test. |
242 RandIntCallback rand_int_cb = | 201 ConfigureSession(base::TimeDelta::FromMilliseconds( |
243 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); | 202 TestTimeouts::tiny_timeout_ms())); |
244 std::string t0_dns_name(kT0DnsName, arraysize(kT0DnsName)); | 203 factory().AddSocketDataProvider(socket0_data.get()); |
| 204 factory().AddSocketDataProvider(socket1_data.get()); |
| 205 factory().AddSocketDataProvider(socket2_data.get()); |
245 | 206 |
246 IPEndPoint dns_server; | 207 StartTransaction(); |
247 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); | |
248 ASSERT_TRUE(rv); | |
249 | |
250 DnsTransaction t(dns_server, t0_dns_name, kT1Qtype, rand_int_cb, &factory, | |
251 BoundNetLog(), NULL); | |
252 | |
253 TestDelegate delegate; | |
254 t.SetDelegate(&delegate); | |
255 | |
256 t.set_timeouts_ms( | |
257 std::vector<base::TimeDelta>(kTimeoutsMs, | |
258 kTimeoutsMs + arraysize(kTimeoutsMs))); | |
259 | |
260 IPAddressList expected_ip_addresses; | |
261 rv = ConvertStringsToIPAddressList(kT0IpAddresses, | |
262 arraysize(kT0IpAddresses), | |
263 &expected_ip_addresses); | |
264 ASSERT_TRUE(rv); | |
265 | |
266 int rv0 = t.Start(); | |
267 EXPECT_EQ(ERR_IO_PENDING, rv0); | |
268 | 208 |
269 MessageLoop::current()->Run(); | 209 MessageLoop::current()->Run(); |
270 | 210 |
271 EXPECT_TRUE(DnsTransaction::Key(t0_dns_name, kT1Qtype) == t.key()); | 211 EXPECT_EQ(OK, rv()); |
272 EXPECT_EQ(OK, delegate.result()); | |
273 EXPECT_EQ(&t, delegate.transaction()); | |
274 EXPECT_TRUE(expected_ip_addresses == delegate.ip_addresses()); | |
275 | 212 |
276 EXPECT_TRUE(socket0_data->at_read_eof()); | 213 EXPECT_TRUE(socket0_data->at_read_eof()); |
277 EXPECT_TRUE(socket0_data->at_write_eof()); | 214 EXPECT_TRUE(socket0_data->at_write_eof()); |
278 EXPECT_TRUE(socket1_data->at_read_eof()); | 215 EXPECT_TRUE(socket1_data->at_read_eof()); |
279 EXPECT_TRUE(socket1_data->at_write_eof()); | 216 EXPECT_TRUE(socket1_data->at_write_eof()); |
280 EXPECT_TRUE(socket2_data->at_read_eof()); | 217 EXPECT_TRUE(socket2_data->at_read_eof()); |
281 EXPECT_TRUE(socket2_data->at_write_eof()); | 218 EXPECT_TRUE(socket2_data->at_write_eof()); |
282 EXPECT_EQ(3u, factory.udp_client_sockets().size()); | 219 EXPECT_EQ(3u, factory().udp_client_sockets().size()); |
283 } | 220 } |
284 | 221 |
285 // Test that after the first timeout we do a fresh connection, and after | 222 // 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 | 223 // the second timeout we do another fresh connection and after the third |
287 // timeout we give up and return a timeout error. | 224 // timeout we give up and return a timeout error. |
288 TEST(DnsTransactionTest, ThirdTimeoutTest) { | 225 TEST_F(DnsTransactionTest, ThirdTimeoutTest) { |
289 MockWrite writes0[] = { | 226 MockWrite writes0[] = { |
290 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), | 227 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), |
291 arraysize(kT0QueryDatagram)) | 228 arraysize(kT0QueryDatagram)) |
292 }; | 229 }; |
293 | 230 |
294 scoped_refptr<DelayedSocketData> socket0_data( | 231 scoped_refptr<DelayedSocketData> socket0_data( |
295 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 232 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
296 scoped_refptr<DelayedSocketData> socket1_data( | 233 scoped_refptr<DelayedSocketData> socket1_data( |
297 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 234 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
298 scoped_refptr<DelayedSocketData> socket2_data( | 235 scoped_refptr<DelayedSocketData> socket2_data( |
299 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 236 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()); | |
304 | 237 |
305 TestPrng test_prng(std::deque<int>(3, 0)); | 238 // Use short timeout to speed up the test. |
306 RandIntCallback rand_int_cb = | 239 ConfigureSession(base::TimeDelta::FromMilliseconds( |
307 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); | 240 TestTimeouts::tiny_timeout_ms())); |
308 std::string t0_dns_name(kT0DnsName, arraysize(kT0DnsName)); | 241 factory().AddSocketDataProvider(socket0_data.get()); |
| 242 factory().AddSocketDataProvider(socket1_data.get()); |
| 243 factory().AddSocketDataProvider(socket2_data.get()); |
309 | 244 |
310 IPEndPoint dns_server; | 245 StartTransaction(); |
311 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); | |
312 ASSERT_TRUE(rv); | |
313 | |
314 DnsTransaction t(dns_server, t0_dns_name, kT1Qtype, rand_int_cb, &factory, | |
315 BoundNetLog(), NULL); | |
316 | |
317 TestDelegate delegate; | |
318 t.SetDelegate(&delegate); | |
319 | |
320 t.set_timeouts_ms( | |
321 std::vector<base::TimeDelta>(kTimeoutsMs, | |
322 kTimeoutsMs + arraysize(kTimeoutsMs))); | |
323 | |
324 int rv0 = t.Start(); | |
325 EXPECT_EQ(ERR_IO_PENDING, rv0); | |
326 | 246 |
327 MessageLoop::current()->Run(); | 247 MessageLoop::current()->Run(); |
328 | 248 |
329 EXPECT_TRUE(DnsTransaction::Key(t0_dns_name, kT0Qtype) == t.key()); | 249 EXPECT_EQ(ERR_DNS_TIMED_OUT, rv()); |
330 EXPECT_EQ(ERR_DNS_TIMED_OUT, delegate.result()); | |
331 EXPECT_EQ(&t, delegate.transaction()); | |
332 | 250 |
333 EXPECT_TRUE(socket0_data->at_read_eof()); | 251 EXPECT_TRUE(socket0_data->at_read_eof()); |
334 EXPECT_TRUE(socket0_data->at_write_eof()); | 252 EXPECT_TRUE(socket0_data->at_write_eof()); |
335 EXPECT_TRUE(socket1_data->at_read_eof()); | 253 EXPECT_TRUE(socket1_data->at_read_eof()); |
336 EXPECT_TRUE(socket1_data->at_write_eof()); | 254 EXPECT_TRUE(socket1_data->at_write_eof()); |
337 EXPECT_TRUE(socket2_data->at_read_eof()); | 255 EXPECT_TRUE(socket2_data->at_read_eof()); |
338 EXPECT_TRUE(socket2_data->at_write_eof()); | 256 EXPECT_TRUE(socket2_data->at_write_eof()); |
339 EXPECT_EQ(3u, factory.udp_client_sockets().size()); | 257 EXPECT_EQ(3u, factory().udp_client_sockets().size()); |
340 } | 258 } |
341 | 259 |
| 260 } // namespace |
| 261 |
342 } // namespace net | 262 } // namespace net |
OLD | NEW |