| 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 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 } | 251 } |
| 252 | 252 |
| 253 void AddAsyncResponse(const std::string& dotted_name, | 253 void AddAsyncResponse(const std::string& dotted_name, |
| 254 uint16 qtype, | 254 uint16 qtype, |
| 255 uint16 id, | 255 uint16 id, |
| 256 const char* data, | 256 const char* data, |
| 257 size_t data_length) { | 257 size_t data_length) { |
| 258 AddResponse(dotted_name, qtype, id, data, data_length, ASYNC); | 258 AddResponse(dotted_name, qtype, id, data, data_length, ASYNC); |
| 259 } | 259 } |
| 260 | 260 |
| 261 void AddSyncResponse(const std::string& dotted_name, |
| 262 uint16 qtype, |
| 263 uint16 id, |
| 264 const char* data, |
| 265 size_t data_length) { |
| 266 AddResponse(dotted_name, qtype, id, data, data_length, SYNCHRONOUS); |
| 267 } |
| 268 |
| 261 // Add expected query of |dotted_name| and |qtype| and no response. | 269 // Add expected query of |dotted_name| and |qtype| and no response. |
| 262 void AddTimeout(const char* dotted_name, uint16 qtype) { | 270 void AddTimeout(const char* dotted_name, uint16 qtype) { |
| 263 CHECK(socket_factory_.get()); | 271 CHECK(socket_factory_.get()); |
| 264 uint16 id = base::RandInt(0, kuint16max); | 272 uint16 id = base::RandInt(0, kuint16max); |
| 265 DnsQuery* query = new DnsQuery(id, DomainFromDot(dotted_name), qtype); | 273 DnsQuery* query = new DnsQuery(id, DomainFromDot(dotted_name), qtype); |
| 266 queries_.push_back(query); | 274 queries_.push_back(query); |
| 267 | 275 |
| 268 writes_.push_back(MockWrite(ASYNC, | 276 writes_.push_back(MockWrite(ASYNC, |
| 269 query->io_buffer()->data(), | 277 query->io_buffer()->data(), |
| 270 query->io_buffer()->size())); | 278 query->io_buffer()->size())); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 296 reads_.push_back(MockRead(mode, | 304 reads_.push_back(MockRead(mode, |
| 297 response->io_buffer()->data(), | 305 response->io_buffer()->data(), |
| 298 query->io_buffer()->size())); | 306 query->io_buffer()->size())); |
| 299 transaction_ids_.push_back(id); | 307 transaction_ids_.push_back(id); |
| 300 } | 308 } |
| 301 | 309 |
| 302 void AddAsyncRcode(const char* dotted_name, uint16 qtype, int rcode) { | 310 void AddAsyncRcode(const char* dotted_name, uint16 qtype, int rcode) { |
| 303 AddRcode(dotted_name, qtype, rcode, ASYNC); | 311 AddRcode(dotted_name, qtype, rcode, ASYNC); |
| 304 } | 312 } |
| 305 | 313 |
| 314 void AddSyncRcode(const char* dotted_name, uint16 qtype, int rcode) { |
| 315 AddRcode(dotted_name, qtype, rcode, SYNCHRONOUS); |
| 316 } |
| 317 |
| 306 // Call after all Add* calls to prepare data for |socket_factory_|. | 318 // Call after all Add* calls to prepare data for |socket_factory_|. |
| 307 // This separation is necessary because the |reads_| and |writes_| vectors | 319 // This separation is necessary because the |reads_| and |writes_| vectors |
| 308 // could reallocate their data during those calls. | 320 // could reallocate their data during those calls. |
| 309 void PrepareSockets() { | 321 void PrepareSockets() { |
| 310 CHECK_EQ(writes_.size(), reads_.size()); | 322 CHECK_EQ(writes_.size(), reads_.size()); |
| 311 for (size_t i = 0; i < writes_.size(); ++i) { | 323 for (size_t i = 0; i < writes_.size(); ++i) { |
| 312 DelayedSocketData* data; | 324 DelayedSocketData* data; |
| 313 if (reads_[i].data) { | 325 if (reads_[i].data) { |
| 314 data = new DelayedSocketData(1, &reads_[i], 1, &writes_[i], 1); | 326 data = new DelayedSocketData(1, &reads_[i], 1, &writes_[i], 1); |
| 315 } else { | 327 } else { |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 arraysize(kT0ResponseDatagram)); | 492 arraysize(kT0ResponseDatagram)); |
| 481 PrepareSockets(); | 493 PrepareSockets(); |
| 482 | 494 |
| 483 TransactionHelper helper0(kT0HostName, | 495 TransactionHelper helper0(kT0HostName, |
| 484 kT0Qtype, | 496 kT0Qtype, |
| 485 kT0RecordCount); | 497 kT0RecordCount); |
| 486 helper0.set_cancel_in_callback(); | 498 helper0.set_cancel_in_callback(); |
| 487 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); | 499 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
| 488 } | 500 } |
| 489 | 501 |
| 502 TEST_F(DnsTransactionTest, MalformedResponse) { |
| 503 config_.attempts = 3; |
| 504 ConfigureFactory(); |
| 505 AddAsyncResponse(kT0HostName, kT0Qtype, |
| 506 1 /* mismatched id */, |
| 507 reinterpret_cast<const char*>(kT0ResponseDatagram), |
| 508 arraysize(kT0ResponseDatagram)); |
| 509 AddSyncResponse(kT0HostName, kT0Qtype, |
| 510 2 /* mismatched id */, |
| 511 reinterpret_cast<const char*>(kT0ResponseDatagram), |
| 512 arraysize(kT0ResponseDatagram)); |
| 513 AddAsyncResponse(kT0HostName, kT0Qtype, |
| 514 0 /* matching id */, |
| 515 reinterpret_cast<const char*>(kT0ResponseDatagram), |
| 516 arraysize(kT0ResponseDatagram)); |
| 517 PrepareSockets(); |
| 518 |
| 519 TransactionHelper helper0(kT0HostName, |
| 520 kT0Qtype, |
| 521 kT0RecordCount); |
| 522 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
| 523 } |
| 524 |
| 490 TEST_F(DnsTransactionTest, ServerFail) { | 525 TEST_F(DnsTransactionTest, ServerFail) { |
| 491 AddAsyncRcode(kT0HostName, kT0Qtype, dns_protocol::kRcodeSERVFAIL); | 526 AddAsyncRcode(kT0HostName, kT0Qtype, dns_protocol::kRcodeSERVFAIL); |
| 492 PrepareSockets(); | 527 PrepareSockets(); |
| 493 | 528 |
| 494 TransactionHelper helper0(kT0HostName, | 529 TransactionHelper helper0(kT0HostName, |
| 495 kT0Qtype, | 530 kT0Qtype, |
| 496 ERR_DNS_SERVER_FAILED); | 531 ERR_DNS_SERVER_FAILED); |
| 497 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); | 532 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
| 498 } | 533 } |
| 499 | 534 |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 718 TransactionHelper helper0("x.y.z", dns_protocol::kTypeA, 0 /* answers */); | 753 TransactionHelper helper0("x.y.z", dns_protocol::kTypeA, 0 /* answers */); |
| 719 | 754 |
| 720 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); | 755 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
| 721 } | 756 } |
| 722 | 757 |
| 723 TEST_F(DnsTransactionTest, SyncFirstQuery) { | 758 TEST_F(DnsTransactionTest, SyncFirstQuery) { |
| 724 config_.search.push_back("lab.ccs.neu.edu"); | 759 config_.search.push_back("lab.ccs.neu.edu"); |
| 725 config_.search.push_back("ccs.neu.edu"); | 760 config_.search.push_back("ccs.neu.edu"); |
| 726 ConfigureFactory(); | 761 ConfigureFactory(); |
| 727 | 762 |
| 728 AddResponse(kT0HostName, | 763 AddSyncResponse(kT0HostName, |
| 729 kT0Qtype, | 764 kT0Qtype, |
| 730 0 /* id */, | 765 0 /* id */, |
| 731 reinterpret_cast<const char*>(kT0ResponseDatagram), | 766 reinterpret_cast<const char*>(kT0ResponseDatagram), |
| 732 arraysize(kT0ResponseDatagram), | 767 arraysize(kT0ResponseDatagram)); |
| 733 SYNCHRONOUS); | |
| 734 PrepareSockets(); | 768 PrepareSockets(); |
| 735 | 769 |
| 736 TransactionHelper helper0(kT0HostName, | 770 TransactionHelper helper0(kT0HostName, |
| 737 kT0Qtype, | 771 kT0Qtype, |
| 738 kT0RecordCount); | 772 kT0RecordCount); |
| 739 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); | 773 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
| 740 } | 774 } |
| 741 | 775 |
| 742 TEST_F(DnsTransactionTest, SyncFirstQueryWithSearch) { | 776 TEST_F(DnsTransactionTest, SyncFirstQueryWithSearch) { |
| 743 config_.search.push_back("lab.ccs.neu.edu"); | 777 config_.search.push_back("lab.ccs.neu.edu"); |
| 744 config_.search.push_back("ccs.neu.edu"); | 778 config_.search.push_back("ccs.neu.edu"); |
| 745 ConfigureFactory(); | 779 ConfigureFactory(); |
| 746 | 780 |
| 747 AddRcode("www.lab.ccs.neu.edu", | 781 AddSyncRcode("www.lab.ccs.neu.edu", |
| 748 kT2Qtype, | 782 kT2Qtype, |
| 749 dns_protocol::kRcodeNXDOMAIN, | 783 dns_protocol::kRcodeNXDOMAIN); |
| 750 SYNCHRONOUS); | 784 AddAsyncResponse(kT2HostName, // "www.ccs.neu.edu" |
| 751 AddResponse(kT2HostName, // "www.ccs.neu.edu" | 785 kT2Qtype, |
| 752 kT2Qtype, | 786 2 /* id */, |
| 753 2 /* id */, | 787 reinterpret_cast<const char*>(kT2ResponseDatagram), |
| 754 reinterpret_cast<const char*>(kT2ResponseDatagram), | 788 arraysize(kT2ResponseDatagram)); |
| 755 arraysize(kT2ResponseDatagram), | |
| 756 ASYNC); | |
| 757 PrepareSockets(); | 789 PrepareSockets(); |
| 758 | 790 |
| 759 TransactionHelper helper0("www", | 791 TransactionHelper helper0("www", |
| 760 kT2Qtype, | 792 kT2Qtype, |
| 761 kT2RecordCount); | 793 kT2RecordCount); |
| 762 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); | 794 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
| 763 } | 795 } |
| 764 | 796 |
| 765 TEST_F(DnsTransactionTest, SyncSearchQuery) { | 797 TEST_F(DnsTransactionTest, SyncSearchQuery) { |
| 766 config_.search.push_back("lab.ccs.neu.edu"); | 798 config_.search.push_back("lab.ccs.neu.edu"); |
| 767 config_.search.push_back("ccs.neu.edu"); | 799 config_.search.push_back("ccs.neu.edu"); |
| 768 ConfigureFactory(); | 800 ConfigureFactory(); |
| 769 | 801 |
| 770 AddRcode("www.lab.ccs.neu.edu", | 802 AddAsyncRcode("www.lab.ccs.neu.edu", |
| 771 dns_protocol::kTypeA, | 803 dns_protocol::kTypeA, |
| 772 dns_protocol::kRcodeNXDOMAIN, | 804 dns_protocol::kRcodeNXDOMAIN); |
| 773 ASYNC); | 805 AddSyncResponse(kT2HostName, |
| 774 AddResponse(kT2HostName, | 806 kT2Qtype, |
| 775 kT2Qtype, | 807 2 /* id */, |
| 776 2 /* id */, | 808 reinterpret_cast<const char*>(kT2ResponseDatagram), |
| 777 reinterpret_cast<const char*>(kT2ResponseDatagram), | 809 arraysize(kT2ResponseDatagram)); |
| 778 arraysize(kT2ResponseDatagram), | |
| 779 SYNCHRONOUS); | |
| 780 PrepareSockets(); | 810 PrepareSockets(); |
| 781 | 811 |
| 782 TransactionHelper helper0("www", | 812 TransactionHelper helper0("www", |
| 783 kT2Qtype, | 813 kT2Qtype, |
| 784 kT2RecordCount); | 814 kT2RecordCount); |
| 785 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); | 815 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
| 786 } | 816 } |
| 787 | 817 |
| 788 } // namespace | 818 } // namespace |
| 789 | 819 |
| 790 } // namespace net | 820 } // namespace net |
| OLD | NEW |