| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 | 90 |
| 91 // Adds pre-built DnsResponse. | 91 // Adds pre-built DnsResponse. |
| 92 void AddResponse(std::unique_ptr<DnsResponse> response, IoMode mode) { | 92 void AddResponse(std::unique_ptr<DnsResponse> response, IoMode mode) { |
| 93 uint16_t tcp_length = response->io_buffer()->size(); | 93 uint16_t tcp_length = response->io_buffer()->size(); |
| 94 AddResponseWithLength(std::move(response), mode, tcp_length); | 94 AddResponseWithLength(std::move(response), mode, tcp_length); |
| 95 } | 95 } |
| 96 | 96 |
| 97 // Adds pre-built response from |data| buffer. | 97 // Adds pre-built response from |data| buffer. |
| 98 void AddResponseData(const uint8_t* data, size_t length, IoMode mode) { | 98 void AddResponseData(const uint8_t* data, size_t length, IoMode mode) { |
| 99 CHECK(!provider_.get()); | 99 CHECK(!provider_.get()); |
| 100 AddResponse(base::WrapUnique(new DnsResponse( | 100 AddResponse(base::MakeUnique<DnsResponse>( |
| 101 reinterpret_cast<const char*>(data), length, 0)), | 101 reinterpret_cast<const char*>(data), length, 0), |
| 102 mode); | 102 mode); |
| 103 } | 103 } |
| 104 | 104 |
| 105 // Add no-answer (RCODE only) response matching the query. | 105 // Add no-answer (RCODE only) response matching the query. |
| 106 void AddRcode(int rcode, IoMode mode) { | 106 void AddRcode(int rcode, IoMode mode) { |
| 107 std::unique_ptr<DnsResponse> response(new DnsResponse( | 107 std::unique_ptr<DnsResponse> response(new DnsResponse( |
| 108 query_->io_buffer()->data(), query_->io_buffer()->size(), 0)); | 108 query_->io_buffer()->data(), query_->io_buffer()->size(), 0)); |
| 109 dns_protocol::Header* header = | 109 dns_protocol::Header* header = |
| 110 reinterpret_cast<dns_protocol::Header*>(response->io_buffer()->data()); | 110 reinterpret_cast<dns_protocol::Header*>(response->io_buffer()->data()); |
| 111 header->flags |= base::HostToNet16(dns_protocol::kFlagResponse | rcode); | 111 header->flags |= base::HostToNet16(dns_protocol::kFlagResponse | rcode); |
| (...skipping 791 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 903 TEST_F(DnsTransactionTest, TCPMalformed) { | 903 TEST_F(DnsTransactionTest, TCPMalformed) { |
| 904 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, | 904 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, |
| 905 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); | 905 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); |
| 906 std::unique_ptr<DnsSocketData> data( | 906 std::unique_ptr<DnsSocketData> data( |
| 907 new DnsSocketData(0 /* id */, kT0HostName, kT0Qtype, ASYNC, true)); | 907 new DnsSocketData(0 /* id */, kT0HostName, kT0Qtype, ASYNC, true)); |
| 908 // Valid response but length too short. | 908 // Valid response but length too short. |
| 909 // This must be truncated in the question section. The DnsResponse doesn't | 909 // This must be truncated in the question section. The DnsResponse doesn't |
| 910 // examine the answer section until asked to parse it, so truncating it in | 910 // examine the answer section until asked to parse it, so truncating it in |
| 911 // the answer section would result in the DnsTransaction itself succeeding. | 911 // the answer section would result in the DnsTransaction itself succeeding. |
| 912 data->AddResponseWithLength( | 912 data->AddResponseWithLength( |
| 913 base::WrapUnique( | 913 base::MakeUnique<DnsResponse>( |
| 914 new DnsResponse(reinterpret_cast<const char*>(kT0ResponseDatagram), | 914 reinterpret_cast<const char*>(kT0ResponseDatagram), |
| 915 arraysize(kT0ResponseDatagram), 0)), | 915 arraysize(kT0ResponseDatagram), 0), |
| 916 ASYNC, static_cast<uint16_t>(kT0QuerySize - 1)); | 916 ASYNC, static_cast<uint16_t>(kT0QuerySize - 1)); |
| 917 AddSocketData(std::move(data)); | 917 AddSocketData(std::move(data)); |
| 918 | 918 |
| 919 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_DNS_MALFORMED_RESPONSE); | 919 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_DNS_MALFORMED_RESPONSE); |
| 920 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); | 920 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
| 921 } | 921 } |
| 922 | 922 |
| 923 TEST_F(DnsTransactionTest, TCPTimeout) { | 923 TEST_F(DnsTransactionTest, TCPTimeout) { |
| 924 config_.timeout = TestTimeouts::tiny_timeout(); | 924 config_.timeout = TestTimeouts::tiny_timeout(); |
| 925 ConfigureFactory(); | 925 ConfigureFactory(); |
| 926 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, | 926 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, |
| 927 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); | 927 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); |
| 928 AddSocketData(base::WrapUnique( | 928 AddSocketData(base::MakeUnique<DnsSocketData>(1 /* id */, kT0HostName, |
| 929 new DnsSocketData(1 /* id */, kT0HostName, kT0Qtype, ASYNC, true))); | 929 kT0Qtype, ASYNC, true)); |
| 930 | 930 |
| 931 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_DNS_TIMED_OUT); | 931 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_DNS_TIMED_OUT); |
| 932 EXPECT_TRUE(helper0.RunUntilDone(transaction_factory_.get())); | 932 EXPECT_TRUE(helper0.RunUntilDone(transaction_factory_.get())); |
| 933 } | 933 } |
| 934 | 934 |
| 935 TEST_F(DnsTransactionTest, TCPReadReturnsZeroAsync) { | 935 TEST_F(DnsTransactionTest, TCPReadReturnsZeroAsync) { |
| 936 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, | 936 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, |
| 937 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); | 937 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); |
| 938 std::unique_ptr<DnsSocketData> data( | 938 std::unique_ptr<DnsSocketData> data( |
| 939 new DnsSocketData(0 /* id */, kT0HostName, kT0Qtype, ASYNC, true)); | 939 new DnsSocketData(0 /* id */, kT0HostName, kT0Qtype, ASYNC, true)); |
| 940 // Return all but the last byte of the response. | 940 // Return all but the last byte of the response. |
| 941 data->AddResponseWithLength( | 941 data->AddResponseWithLength( |
| 942 base::WrapUnique( | 942 base::MakeUnique<DnsResponse>( |
| 943 new DnsResponse(reinterpret_cast<const char*>(kT0ResponseDatagram), | 943 reinterpret_cast<const char*>(kT0ResponseDatagram), |
| 944 arraysize(kT0ResponseDatagram) - 1, 0)), | 944 arraysize(kT0ResponseDatagram) - 1, 0), |
| 945 ASYNC, static_cast<uint16_t>(arraysize(kT0ResponseDatagram))); | 945 ASYNC, static_cast<uint16_t>(arraysize(kT0ResponseDatagram))); |
| 946 // Then return a 0-length read. | 946 // Then return a 0-length read. |
| 947 data->AddReadError(0, ASYNC); | 947 data->AddReadError(0, ASYNC); |
| 948 AddSocketData(std::move(data)); | 948 AddSocketData(std::move(data)); |
| 949 | 949 |
| 950 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_CONNECTION_CLOSED); | 950 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_CONNECTION_CLOSED); |
| 951 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); | 951 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
| 952 } | 952 } |
| 953 | 953 |
| 954 TEST_F(DnsTransactionTest, TCPReadReturnsZeroSynchronous) { | 954 TEST_F(DnsTransactionTest, TCPReadReturnsZeroSynchronous) { |
| 955 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, | 955 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, |
| 956 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); | 956 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); |
| 957 std::unique_ptr<DnsSocketData> data( | 957 std::unique_ptr<DnsSocketData> data( |
| 958 new DnsSocketData(0 /* id */, kT0HostName, kT0Qtype, ASYNC, true)); | 958 new DnsSocketData(0 /* id */, kT0HostName, kT0Qtype, ASYNC, true)); |
| 959 // Return all but the last byte of the response. | 959 // Return all but the last byte of the response. |
| 960 data->AddResponseWithLength( | 960 data->AddResponseWithLength( |
| 961 base::WrapUnique( | 961 base::MakeUnique<DnsResponse>( |
| 962 new DnsResponse(reinterpret_cast<const char*>(kT0ResponseDatagram), | 962 reinterpret_cast<const char*>(kT0ResponseDatagram), |
| 963 arraysize(kT0ResponseDatagram) - 1, 0)), | 963 arraysize(kT0ResponseDatagram) - 1, 0), |
| 964 SYNCHRONOUS, static_cast<uint16_t>(arraysize(kT0ResponseDatagram))); | 964 SYNCHRONOUS, static_cast<uint16_t>(arraysize(kT0ResponseDatagram))); |
| 965 // Then return a 0-length read. | 965 // Then return a 0-length read. |
| 966 data->AddReadError(0, SYNCHRONOUS); | 966 data->AddReadError(0, SYNCHRONOUS); |
| 967 AddSocketData(std::move(data)); | 967 AddSocketData(std::move(data)); |
| 968 | 968 |
| 969 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_CONNECTION_CLOSED); | 969 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_CONNECTION_CLOSED); |
| 970 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); | 970 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
| 971 } | 971 } |
| 972 | 972 |
| 973 TEST_F(DnsTransactionTest, TCPConnectionClosedAsync) { | 973 TEST_F(DnsTransactionTest, TCPConnectionClosedAsync) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 998 config_.timeout = TestTimeouts::tiny_timeout(); | 998 config_.timeout = TestTimeouts::tiny_timeout(); |
| 999 ConfigureFactory(); | 999 ConfigureFactory(); |
| 1000 | 1000 |
| 1001 TransactionHelper helper0(".", dns_protocol::kTypeA, ERR_INVALID_ARGUMENT); | 1001 TransactionHelper helper0(".", dns_protocol::kTypeA, ERR_INVALID_ARGUMENT); |
| 1002 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); | 1002 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
| 1003 } | 1003 } |
| 1004 | 1004 |
| 1005 } // namespace | 1005 } // namespace |
| 1006 | 1006 |
| 1007 } // namespace net | 1007 } // namespace net |
| OLD | NEW |