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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 scoped_ptr<DnsResponse> response( | 93 scoped_ptr<DnsResponse> response( |
94 new DnsResponse(query_->io_buffer()->data(), | 94 new DnsResponse(query_->io_buffer()->data(), |
95 query_->io_buffer()->size(), | 95 query_->io_buffer()->size(), |
96 0)); | 96 0)); |
97 dns_protocol::Header* header = | 97 dns_protocol::Header* header = |
98 reinterpret_cast<dns_protocol::Header*>(response->io_buffer()->data()); | 98 reinterpret_cast<dns_protocol::Header*>(response->io_buffer()->data()); |
99 header->flags |= base::HostToNet16(dns_protocol::kFlagResponse | rcode); | 99 header->flags |= base::HostToNet16(dns_protocol::kFlagResponse | rcode); |
100 AddResponse(response.Pass(), mode); | 100 AddResponse(response.Pass(), mode); |
101 } | 101 } |
102 | 102 |
| 103 // Add error response. |
| 104 void AddReadError(int error, IoMode mode) { |
| 105 reads_.push_back(MockRead(mode, error)); |
| 106 } |
| 107 |
103 // Build, if needed, and return the SocketDataProvider. No new responses | 108 // Build, if needed, and return the SocketDataProvider. No new responses |
104 // should be added afterwards. | 109 // should be added afterwards. |
105 SocketDataProvider* GetProvider() { | 110 SocketDataProvider* GetProvider() { |
106 if (provider_.get()) | 111 if (provider_.get()) |
107 return provider_.get(); | 112 return provider_.get(); |
108 // Terminate the reads with ERR_IO_PENDING to prevent overrun and default to | 113 // Terminate the reads with ERR_IO_PENDING to prevent overrun and default to |
109 // timeout. | 114 // timeout. |
110 reads_.push_back(MockRead(ASYNC, ERR_IO_PENDING)); | 115 reads_.push_back(MockRead(ASYNC, ERR_IO_PENDING)); |
111 provider_.reset(new DelayedSocketData(1, &reads_[0], reads_.size(), | 116 provider_.reset(new DelayedSocketData(1, &reads_[0], reads_.size(), |
112 &writes_[0], writes_.size())); | 117 &writes_[0], writes_.size())); |
(...skipping 782 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
895 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_DNS_SERVER_FAILED); | 900 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_DNS_SERVER_FAILED); |
896 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); | 901 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
897 } | 902 } |
898 | 903 |
899 TEST_F(DnsTransactionTest, TCPMalformed) { | 904 TEST_F(DnsTransactionTest, TCPMalformed) { |
900 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, | 905 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, |
901 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); | 906 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); |
902 scoped_ptr<DnsSocketData> data( | 907 scoped_ptr<DnsSocketData> data( |
903 new DnsSocketData(0 /* id */, kT0HostName, kT0Qtype, ASYNC, true)); | 908 new DnsSocketData(0 /* id */, kT0HostName, kT0Qtype, ASYNC, true)); |
904 // Valid response but length too short. | 909 // Valid response but length too short. |
| 910 // This must be truncated in the question section. The DnsResponse doesn't |
| 911 // examine the answer section until asked to parse it, so truncating it in |
| 912 // the answer section would result in the DnsTransaction itself succeeding. |
905 data->AddResponseWithLength( | 913 data->AddResponseWithLength( |
906 make_scoped_ptr( | 914 make_scoped_ptr( |
907 new DnsResponse(reinterpret_cast<const char*>(kT0ResponseDatagram), | 915 new DnsResponse(reinterpret_cast<const char*>(kT0ResponseDatagram), |
908 arraysize(kT0ResponseDatagram), 0)), | 916 arraysize(kT0ResponseDatagram), 0)), |
909 ASYNC, | 917 ASYNC, |
910 static_cast<uint16>(kT0QuerySize - 1)); | 918 static_cast<uint16>(kT0QuerySize - 1)); |
911 AddSocketData(data.Pass()); | 919 AddSocketData(data.Pass()); |
912 | 920 |
913 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_DNS_MALFORMED_RESPONSE); | 921 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_DNS_MALFORMED_RESPONSE); |
914 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); | 922 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
915 } | 923 } |
916 | 924 |
917 TEST_F(DnsTransactionTest, TCPTimeout) { | 925 TEST_F(DnsTransactionTest, TCPTimeout) { |
918 config_.timeout = TestTimeouts::tiny_timeout(); | 926 config_.timeout = TestTimeouts::tiny_timeout(); |
919 ConfigureFactory(); | 927 ConfigureFactory(); |
920 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, | 928 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, |
921 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); | 929 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); |
922 AddSocketData(make_scoped_ptr( | 930 AddSocketData(make_scoped_ptr( |
923 new DnsSocketData(1 /* id */, kT0HostName, kT0Qtype, ASYNC, true))); | 931 new DnsSocketData(1 /* id */, kT0HostName, kT0Qtype, ASYNC, true))); |
924 | 932 |
925 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_DNS_TIMED_OUT); | 933 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_DNS_TIMED_OUT); |
926 EXPECT_TRUE(helper0.RunUntilDone(transaction_factory_.get())); | 934 EXPECT_TRUE(helper0.RunUntilDone(transaction_factory_.get())); |
927 } | 935 } |
928 | 936 |
| 937 TEST_F(DnsTransactionTest, TCPReadReturnsZeroAsync) { |
| 938 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, |
| 939 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); |
| 940 scoped_ptr<DnsSocketData> data( |
| 941 new DnsSocketData(0 /* id */, kT0HostName, kT0Qtype, ASYNC, true)); |
| 942 // Return all but the last byte of the response. |
| 943 data->AddResponseWithLength( |
| 944 make_scoped_ptr( |
| 945 new DnsResponse(reinterpret_cast<const char*>(kT0ResponseDatagram), |
| 946 arraysize(kT0ResponseDatagram) - 1, 0)), |
| 947 ASYNC, |
| 948 static_cast<uint16>(arraysize(kT0ResponseDatagram))); |
| 949 // Then return a 0-length read. |
| 950 data->AddReadError(0, ASYNC); |
| 951 AddSocketData(data.Pass()); |
| 952 |
| 953 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_CONNECTION_CLOSED); |
| 954 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
| 955 } |
| 956 |
| 957 TEST_F(DnsTransactionTest, TCPReadReturnsZeroSynchronous) { |
| 958 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, |
| 959 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); |
| 960 scoped_ptr<DnsSocketData> data( |
| 961 new DnsSocketData(0 /* id */, kT0HostName, kT0Qtype, ASYNC, true)); |
| 962 // Return all but the last byte of the response. |
| 963 data->AddResponseWithLength( |
| 964 make_scoped_ptr( |
| 965 new DnsResponse(reinterpret_cast<const char*>(kT0ResponseDatagram), |
| 966 arraysize(kT0ResponseDatagram) - 1, 0)), |
| 967 SYNCHRONOUS, |
| 968 static_cast<uint16>(arraysize(kT0ResponseDatagram))); |
| 969 // Then return a 0-length read. |
| 970 data->AddReadError(0, SYNCHRONOUS); |
| 971 AddSocketData(data.Pass()); |
| 972 |
| 973 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_CONNECTION_CLOSED); |
| 974 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
| 975 } |
| 976 |
| 977 TEST_F(DnsTransactionTest, TCPConnectionClosedAsync) { |
| 978 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, |
| 979 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); |
| 980 scoped_ptr<DnsSocketData> data( |
| 981 new DnsSocketData(0 /* id */, kT0HostName, kT0Qtype, ASYNC, true)); |
| 982 data->AddReadError(ERR_CONNECTION_CLOSED, ASYNC); |
| 983 AddSocketData(data.Pass()); |
| 984 |
| 985 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_CONNECTION_CLOSED); |
| 986 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
| 987 } |
| 988 |
| 989 TEST_F(DnsTransactionTest, TCPConnectionClosedSynchronous) { |
| 990 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, |
| 991 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); |
| 992 scoped_ptr<DnsSocketData> data( |
| 993 new DnsSocketData(0 /* id */, kT0HostName, kT0Qtype, ASYNC, true)); |
| 994 data->AddReadError(ERR_CONNECTION_CLOSED, SYNCHRONOUS); |
| 995 AddSocketData(data.Pass()); |
| 996 |
| 997 TransactionHelper helper0(kT0HostName, kT0Qtype, ERR_CONNECTION_CLOSED); |
| 998 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
| 999 } |
| 1000 |
929 TEST_F(DnsTransactionTest, InvalidQuery) { | 1001 TEST_F(DnsTransactionTest, InvalidQuery) { |
930 config_.timeout = TestTimeouts::tiny_timeout(); | 1002 config_.timeout = TestTimeouts::tiny_timeout(); |
931 ConfigureFactory(); | 1003 ConfigureFactory(); |
932 | 1004 |
933 TransactionHelper helper0(".", dns_protocol::kTypeA, ERR_INVALID_ARGUMENT); | 1005 TransactionHelper helper0(".", dns_protocol::kTypeA, ERR_INVALID_ARGUMENT); |
934 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); | 1006 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); |
935 } | 1007 } |
936 | 1008 |
937 } // namespace | 1009 } // namespace |
938 | 1010 |
939 } // namespace net | 1011 } // namespace net |
OLD | NEW |