OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 | 134 |
135 completed_ = true; | 135 completed_ = true; |
136 | 136 |
137 if (cancel_in_callback_) { | 137 if (cancel_in_callback_) { |
138 Cancel(); | 138 Cancel(); |
139 return; | 139 return; |
140 } | 140 } |
141 | 141 |
142 if (expected_answer_count_ >= 0) { | 142 if (expected_answer_count_ >= 0) { |
143 EXPECT_EQ(OK, rv); | 143 EXPECT_EQ(OK, rv); |
144 EXPECT_EQ(expected_answer_count_, response->answer_count()); | 144 EXPECT_EQ(static_cast<unsigned>(expected_answer_count_), |
| 145 response->answer_count()); |
145 EXPECT_EQ(qtype_, response->qtype()); | 146 EXPECT_EQ(qtype_, response->qtype()); |
146 | 147 |
147 DnsRecordParser parser = response->Parser(); | 148 DnsRecordParser parser = response->Parser(); |
148 DnsResourceRecord record; | 149 DnsResourceRecord record; |
149 for (int i = 0; i < expected_answer_count_; ++i) { | 150 for (int i = 0; i < expected_answer_count_; ++i) { |
150 EXPECT_TRUE(parser.ParseRecord(&record)); | 151 EXPECT_TRUE(parser.ReadRecord(&record)); |
151 } | 152 } |
| 153 // Technically, there could be additional RRs, but not in our test data. |
152 EXPECT_TRUE(parser.AtEnd()); | 154 EXPECT_TRUE(parser.AtEnd()); |
153 } else { | 155 } else { |
154 EXPECT_EQ(expected_answer_count_, rv); | 156 EXPECT_EQ(expected_answer_count_, rv); |
155 EXPECT_EQ(NULL, response); | 157 EXPECT_EQ(NULL, response); |
156 } | 158 } |
157 | 159 |
158 if (quit_in_callback_) | 160 if (quit_in_callback_) |
159 MessageLoop::current()->Quit(); | 161 MessageLoop::current()->Quit(); |
160 } | 162 } |
161 | 163 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 } | 207 } |
206 for (unsigned i = 0; i < num_servers; ++i) { | 208 for (unsigned i = 0; i < num_servers; ++i) { |
207 dns_ip[3] = i; | 209 dns_ip[3] = i; |
208 config_.nameservers.push_back(IPEndPoint(dns_ip, | 210 config_.nameservers.push_back(IPEndPoint(dns_ip, |
209 dns_protocol::kDefaultPort)); | 211 dns_protocol::kDefaultPort)); |
210 } | 212 } |
211 } | 213 } |
212 | 214 |
213 // Called after fully configuring |config|. | 215 // Called after fully configuring |config|. |
214 void ConfigureFactory() { | 216 void ConfigureFactory() { |
215 socket_factory_ = new TestSocketFactory(); | 217 socket_factory_.reset(new TestSocketFactory()); |
216 session_ = new DnsSession( | 218 session_ = new DnsSession( |
217 config_, | 219 config_, |
218 socket_factory_, | 220 socket_factory_.get(), |
219 base::Bind(&DnsTransactionTest::GetNextId, base::Unretained(this)), | 221 base::Bind(&DnsTransactionTest::GetNextId, base::Unretained(this)), |
220 NULL /* NetLog */); | 222 NULL /* NetLog */); |
221 transaction_factory_ = DnsTransactionFactory::CreateFactory(session_.get()); | 223 transaction_factory_ = DnsTransactionFactory::CreateFactory(session_.get()); |
222 } | 224 } |
223 | 225 |
224 // Each socket used by a DnsTransaction expects only one write and zero or one | 226 // Each socket used by a DnsTransaction expects only one write and zero or one |
225 // reads. | 227 // reads. |
226 | 228 |
227 // Add expected query for |dotted_name| and |qtype| with |id| and response | 229 // Add expected query for |dotted_name| and |qtype| with |id| and response |
228 // taken verbatim from |data| of |data_length| bytes. The transaction id in | 230 // taken verbatim from |data| of |data_length| bytes. The transaction id in |
229 // |data| should equal |id|, unless testing mismatched response. | 231 // |data| should equal |id|, unless testing mismatched response. |
230 void AddResponse(const std::string& dotted_name, | 232 void AddResponse(const std::string& dotted_name, |
231 uint16 qtype, | 233 uint16 qtype, |
232 uint16 id, | 234 uint16 id, |
233 const char* data, | 235 const char* data, |
234 size_t data_length) { | 236 size_t data_length) { |
235 CHECK(socket_factory_); | 237 CHECK(socket_factory_.get()); |
236 DnsQuery* query = new DnsQuery(id, DomainFromDot(dotted_name), qtype); | 238 DnsQuery* query = new DnsQuery(id, DomainFromDot(dotted_name), qtype); |
237 queries_.push_back(query); | 239 queries_.push_back(query); |
238 | 240 |
239 // The response is only used to hold the IOBuffer. | 241 // The response is only used to hold the IOBuffer. |
240 DnsResponse* response = new DnsResponse(data, data_length, 0); | 242 DnsResponse* response = new DnsResponse(data, data_length, 0); |
241 responses_.push_back(response); | 243 responses_.push_back(response); |
242 | 244 |
243 writes_.push_back(MockWrite(true, | 245 writes_.push_back(MockWrite(true, |
244 query->io_buffer()->data(), | 246 query->io_buffer()->data(), |
245 query->io_buffer()->size())); | 247 query->io_buffer()->size())); |
246 reads_.push_back(MockRead(true, | 248 reads_.push_back(MockRead(true, |
247 response->io_buffer()->data(), | 249 response->io_buffer()->data(), |
248 data_length)); | 250 data_length)); |
249 | 251 |
250 transaction_ids_.push_back(id); | 252 transaction_ids_.push_back(id); |
251 } | 253 } |
252 | 254 |
253 // Add expected query of |dotted_name| and |qtype| and no response. | 255 // Add expected query of |dotted_name| and |qtype| and no response. |
254 void AddTimeout(const char* dotted_name, uint16 qtype) { | 256 void AddTimeout(const char* dotted_name, uint16 qtype) { |
255 CHECK(socket_factory_); | 257 CHECK(socket_factory_.get()); |
256 uint16 id = base::RandInt(0, kuint16max); | 258 uint16 id = base::RandInt(0, kuint16max); |
257 DnsQuery* query = new DnsQuery(id, DomainFromDot(dotted_name), qtype); | 259 DnsQuery* query = new DnsQuery(id, DomainFromDot(dotted_name), qtype); |
258 queries_.push_back(query); | 260 queries_.push_back(query); |
259 | 261 |
260 writes_.push_back(MockWrite(true, | 262 writes_.push_back(MockWrite(true, |
261 query->io_buffer()->data(), | 263 query->io_buffer()->data(), |
262 query->io_buffer()->size())); | 264 query->io_buffer()->size())); |
263 // Empty MockRead indicates no data. | 265 // Empty MockRead indicates no data. |
264 reads_.push_back(MockRead()); | 266 reads_.push_back(MockRead()); |
265 transaction_ids_.push_back(id); | 267 transaction_ids_.push_back(id); |
266 } | 268 } |
267 | 269 |
268 // Add expected query of |dotted_name| and |qtype| and response with no answer | 270 // Add expected query of |dotted_name| and |qtype| and response with no answer |
269 // and rcode set to |rcode|. | 271 // and rcode set to |rcode|. |
270 void AddRcode(const char* dotted_name, uint16 qtype, int rcode) { | 272 void AddRcode(const char* dotted_name, uint16 qtype, int rcode) { |
271 CHECK(socket_factory_); | 273 CHECK(socket_factory_.get()); |
272 CHECK_NE(dns_protocol::kRcodeNOERROR, rcode); | 274 CHECK_NE(dns_protocol::kRcodeNOERROR, rcode); |
273 uint16 id = base::RandInt(0, kuint16max); | 275 uint16 id = base::RandInt(0, kuint16max); |
274 DnsQuery* query = new DnsQuery(id, DomainFromDot(dotted_name), qtype); | 276 DnsQuery* query = new DnsQuery(id, DomainFromDot(dotted_name), qtype); |
275 queries_.push_back(query); | 277 queries_.push_back(query); |
276 | 278 |
277 DnsResponse* response = new DnsResponse(query->io_buffer()->data(), | 279 DnsResponse* response = new DnsResponse(query->io_buffer()->data(), |
278 query->io_buffer()->size(), | 280 query->io_buffer()->size(), |
279 0); | 281 0); |
280 dns_protocol::Header* header = | 282 dns_protocol::Header* header = |
281 reinterpret_cast<dns_protocol::Header*>(response->io_buffer()->data()); | 283 reinterpret_cast<dns_protocol::Header*>(response->io_buffer()->data()); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 ScopedVector<DnsResponse> responses_; | 355 ScopedVector<DnsResponse> responses_; |
354 | 356 |
355 // Holders for MockRead/MockWrites (SocketDataProvider does not own it). | 357 // Holders for MockRead/MockWrites (SocketDataProvider does not own it). |
356 std::vector<MockRead> reads_; | 358 std::vector<MockRead> reads_; |
357 std::vector<MockWrite> writes_; | 359 std::vector<MockWrite> writes_; |
358 | 360 |
359 // Holder for the socket data (MockClientSocketFactory does not own it). | 361 // Holder for the socket data (MockClientSocketFactory does not own it). |
360 ScopedVector<DelayedSocketData> socket_data_; | 362 ScopedVector<DelayedSocketData> socket_data_; |
361 | 363 |
362 std::deque<int> transaction_ids_; | 364 std::deque<int> transaction_ids_; |
363 // Owned by |session_|. | 365 scoped_ptr<TestSocketFactory> socket_factory_; |
364 TestSocketFactory* socket_factory_; | |
365 scoped_refptr<DnsSession> session_; | 366 scoped_refptr<DnsSession> session_; |
366 scoped_ptr<DnsTransactionFactory> transaction_factory_; | 367 scoped_ptr<DnsTransactionFactory> transaction_factory_; |
367 }; | 368 }; |
368 | 369 |
369 TEST_F(DnsTransactionTest, Lookup) { | 370 TEST_F(DnsTransactionTest, Lookup) { |
370 AddResponse(kT0HostName, | 371 AddResponse(kT0HostName, |
371 kT0Qtype, | 372 kT0Qtype, |
372 0 /* id */, | 373 0 /* id */, |
373 reinterpret_cast<const char*>(kT0ResponseDatagram), | 374 reinterpret_cast<const char*>(kT0ResponseDatagram), |
374 arraysize(kT0ResponseDatagram)); | 375 arraysize(kT0ResponseDatagram)); |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 TransactionHelper helper2("x", | 675 TransactionHelper helper2("x", |
675 dns_protocol::kTypeA, | 676 dns_protocol::kTypeA, |
676 ERR_NAME_NOT_RESOLVED); | 677 ERR_NAME_NOT_RESOLVED); |
677 EXPECT_TRUE(helper2.Run(transaction_factory_.get())); | 678 EXPECT_TRUE(helper2.Run(transaction_factory_.get())); |
678 } | 679 } |
679 | 680 |
680 } // namespace | 681 } // namespace |
681 | 682 |
682 } // namespace net | 683 } // namespace net |
683 | 684 |
OLD | NEW |