Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(568)

Side by Side Diff: net/dns/dns_transaction_unittest.cc

Issue 15881008: DnsTransaction::RecordLostPacketsIfAny shouldn't crash if some attempts failed to connect and thus … (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address review comments. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/dns/dns_transaction.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 170
171 private: 171 private:
172 TestSocketFactory* factory_; 172 TestSocketFactory* factory_;
173 173
174 DISALLOW_COPY_AND_ASSIGN(TestUDPClientSocket); 174 DISALLOW_COPY_AND_ASSIGN(TestUDPClientSocket);
175 }; 175 };
176 176
177 // Creates TestUDPClientSockets and keeps endpoints reported via OnConnect. 177 // Creates TestUDPClientSockets and keeps endpoints reported via OnConnect.
178 class TestSocketFactory : public MockClientSocketFactory { 178 class TestSocketFactory : public MockClientSocketFactory {
179 public: 179 public:
180 TestSocketFactory() : create_failing_sockets_(false) {} 180 TestSocketFactory() : fail_next_socket_(false) {}
181 virtual ~TestSocketFactory() {} 181 virtual ~TestSocketFactory() {}
182 182
183 virtual DatagramClientSocket* CreateDatagramClientSocket( 183 virtual DatagramClientSocket* CreateDatagramClientSocket(
184 DatagramSocket::BindType bind_type, 184 DatagramSocket::BindType bind_type,
185 const RandIntCallback& rand_int_cb, 185 const RandIntCallback& rand_int_cb,
186 net::NetLog* net_log, 186 net::NetLog* net_log,
187 const net::NetLog::Source& source) OVERRIDE { 187 const net::NetLog::Source& source) OVERRIDE {
188 if (create_failing_sockets_) 188 if (fail_next_socket_) {
189 fail_next_socket_ = false;
189 return new FailingUDPClientSocket(&empty_data_, net_log); 190 return new FailingUDPClientSocket(&empty_data_, net_log);
191 }
190 SocketDataProvider* data_provider = mock_data().GetNext(); 192 SocketDataProvider* data_provider = mock_data().GetNext();
191 TestUDPClientSocket* socket = new TestUDPClientSocket(this, 193 TestUDPClientSocket* socket = new TestUDPClientSocket(this,
192 data_provider, 194 data_provider,
193 net_log); 195 net_log);
194 data_provider->set_socket(socket); 196 data_provider->set_socket(socket);
195 return socket; 197 return socket;
196 } 198 }
197 199
198 void OnConnect(const IPEndPoint& endpoint) { 200 void OnConnect(const IPEndPoint& endpoint) {
199 remote_endpoints_.push_back(endpoint); 201 remote_endpoints_.push_back(endpoint);
200 } 202 }
201 203
202 std::vector<IPEndPoint> remote_endpoints_; 204 std::vector<IPEndPoint> remote_endpoints_;
203 bool create_failing_sockets_; 205 bool fail_next_socket_;
204 206
205 private: 207 private:
206 StaticSocketDataProvider empty_data_; 208 StaticSocketDataProvider empty_data_;
207 209
208 DISALLOW_COPY_AND_ASSIGN(TestSocketFactory); 210 DISALLOW_COPY_AND_ASSIGN(TestSocketFactory);
209 }; 211 };
210 212
211 int TestUDPClientSocket::Connect(const IPEndPoint& endpoint) { 213 int TestUDPClientSocket::Connect(const IPEndPoint& endpoint) {
212 factory_->OnConnect(endpoint); 214 factory_->OnConnect(endpoint);
213 return MockUDPClientSocket::Connect(endpoint); 215 return MockUDPClientSocket::Connect(endpoint);
(...skipping 637 matching lines...) Expand 10 before | Expand all | Expand 10 after
851 AddAsyncQueryAndRcode("www.lab.ccs.neu.edu", dns_protocol::kTypeA, 853 AddAsyncQueryAndRcode("www.lab.ccs.neu.edu", dns_protocol::kTypeA,
852 dns_protocol::kRcodeNXDOMAIN); 854 dns_protocol::kRcodeNXDOMAIN);
853 AddSyncQueryAndResponse(2 /* id */, kT2HostName, kT2Qtype, 855 AddSyncQueryAndResponse(2 /* id */, kT2HostName, kT2Qtype,
854 kT2ResponseDatagram, arraysize(kT2ResponseDatagram)); 856 kT2ResponseDatagram, arraysize(kT2ResponseDatagram));
855 857
856 TransactionHelper helper0("www", kT2Qtype, kT2RecordCount); 858 TransactionHelper helper0("www", kT2Qtype, kT2RecordCount);
857 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); 859 EXPECT_TRUE(helper0.Run(transaction_factory_.get()));
858 } 860 }
859 861
860 TEST_F(DnsTransactionTest, ConnectFailure) { 862 TEST_F(DnsTransactionTest, ConnectFailure) {
861 socket_factory_->create_failing_sockets_ = true; 863 socket_factory_->fail_next_socket_ = true;
862 transaction_ids_.push_back(0); // Needed to make a DnsUDPAttempt. 864 transaction_ids_.push_back(0); // Needed to make a DnsUDPAttempt.
863 TransactionHelper helper0("www.chromium.org", dns_protocol::kTypeA, 865 TransactionHelper helper0("www.chromium.org", dns_protocol::kTypeA,
864 ERR_CONNECTION_REFUSED); 866 ERR_CONNECTION_REFUSED);
865 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); 867 EXPECT_TRUE(helper0.Run(transaction_factory_.get()));
866 } 868 }
867 869
870 TEST_F(DnsTransactionTest, ConnectFailureFollowedBySuccess) {
871 // Retry after server failure.
872 config_.attempts = 2;
873 ConfigureFactory();
874 // First server connection attempt fails.
875 transaction_ids_.push_back(0); // Needed to make a DnsUDPAttempt.
876 socket_factory_->fail_next_socket_ = true;
877 // Second DNS query succeeds.
878 AddAsyncQueryAndResponse(0 /* id */, kT0HostName, kT0Qtype,
879 kT0ResponseDatagram, arraysize(kT0ResponseDatagram));
880 TransactionHelper helper0(kT0HostName, kT0Qtype, kT0RecordCount);
881 EXPECT_TRUE(helper0.Run(transaction_factory_.get()));
882 }
883
868 TEST_F(DnsTransactionTest, TCPLookup) { 884 TEST_F(DnsTransactionTest, TCPLookup) {
869 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype, 885 AddAsyncQueryAndRcode(kT0HostName, kT0Qtype,
870 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC); 886 dns_protocol::kRcodeNOERROR | dns_protocol::kFlagTC);
871 AddQueryAndResponse(0 /* id */, kT0HostName, kT0Qtype, 887 AddQueryAndResponse(0 /* id */, kT0HostName, kT0Qtype,
872 kT0ResponseDatagram, arraysize(kT0ResponseDatagram), 888 kT0ResponseDatagram, arraysize(kT0ResponseDatagram),
873 ASYNC, true /* use_tcp */); 889 ASYNC, true /* use_tcp */);
874 890
875 TransactionHelper helper0(kT0HostName, kT0Qtype, kT0RecordCount); 891 TransactionHelper helper0(kT0HostName, kT0Qtype, kT0RecordCount);
876 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); 892 EXPECT_TRUE(helper0.Run(transaction_factory_.get()));
877 } 893 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
920 config_.timeout = TestTimeouts::tiny_timeout(); 936 config_.timeout = TestTimeouts::tiny_timeout();
921 ConfigureFactory(); 937 ConfigureFactory();
922 938
923 TransactionHelper helper0(".", dns_protocol::kTypeA, ERR_INVALID_ARGUMENT); 939 TransactionHelper helper0(".", dns_protocol::kTypeA, ERR_INVALID_ARGUMENT);
924 EXPECT_TRUE(helper0.Run(transaction_factory_.get())); 940 EXPECT_TRUE(helper0.Run(transaction_factory_.get()));
925 } 941 }
926 942
927 } // namespace 943 } // namespace
928 944
929 } // namespace net 945 } // namespace net
OLDNEW
« no previous file with comments | « net/dns/dns_transaction.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698