| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <deque> | 7 #include <deque> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/time.h" |
| 12 #include "net/dns/dns_protocol.h" |
| 13 #include "net/dns/dns_query.h" |
| 14 #include "net/dns/dns_response.h" |
| 15 #include "net/dns/dns_session.h" |
| 11 #include "net/dns/dns_test_util.h" | 16 #include "net/dns/dns_test_util.h" |
| 12 #include "net/socket/socket_test_util.h" | 17 #include "net/socket/socket_test_util.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 19 |
| 15 namespace net { | 20 namespace net { |
| 16 | 21 |
| 17 namespace { | 22 namespace { |
| 18 | 23 |
| 19 static const base::TimeDelta kTimeoutsMs[] = { | 24 // A mock for RandIntCallback that always returns 0. |
| 20 base::TimeDelta::FromMilliseconds(20), | 25 int ReturnZero(int min, int max) { |
| 21 base::TimeDelta::FromMilliseconds(20), | 26 return 0; |
| 22 base::TimeDelta::FromMilliseconds(20), | 27 } |
| 28 |
| 29 class DnsTransactionTest : public testing::Test { |
| 30 protected: |
| 31 virtual void SetUp() OVERRIDE { |
| 32 DnsConfig config; |
| 33 |
| 34 IPEndPoint dns_server; |
| 35 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); |
| 36 ASSERT_TRUE(rv); |
| 37 config.nameservers.push_back(dns_server); |
| 38 config.attempts = 3; |
| 39 config.timeout = base::TimeDelta::FromMilliseconds(20); |
| 40 |
| 41 qname_ = std::string(kT0DnsName, arraysize(kT0DnsName)); |
| 42 |
| 43 session_ = new DnsSession(config, |
| 44 new MockClientSocketFactory(), |
| 45 base::Bind(&ReturnZero), |
| 46 NULL /* NetLog */); |
| 47 |
| 48 callback_ = base::Bind(&DnsTransactionTest::OnTransactionComplete, |
| 49 base::Unretained(this)); |
| 50 } |
| 51 |
| 52 void StartTransaction() { |
| 53 transaction_.reset(new DnsTransaction(session_.get(), |
| 54 qname_, |
| 55 kT0Qtype, |
| 56 callback_, |
| 57 BoundNetLog())); |
| 58 |
| 59 int rv0 = transaction_->Start(); |
| 60 EXPECT_EQ(ERR_IO_PENDING, rv0); |
| 61 } |
| 62 |
| 63 void OnTransactionComplete(DnsTransaction* transaction, int rv) { |
| 64 EXPECT_EQ(transaction_.get(), transaction); |
| 65 EXPECT_EQ(qname_, transaction->query()->qname().as_string()); |
| 66 EXPECT_EQ(kT0Qtype, transaction->query()->qtype()); |
| 67 rv_ = rv; |
| 68 MessageLoop::current()->Quit(); |
| 69 } |
| 70 |
| 71 MockClientSocketFactory& factory() { |
| 72 return *static_cast<MockClientSocketFactory*>(session_->socket_factory()); |
| 73 } |
| 74 |
| 75 int rv() const { return rv_; } |
| 76 |
| 77 private: |
| 78 std::string qname_; |
| 79 scoped_refptr<DnsSession> session_; |
| 80 scoped_ptr<DnsTransaction> transaction_; |
| 81 DnsTransaction::ResultCallback callback_; |
| 82 |
| 83 int rv_; |
| 23 }; | 84 }; |
| 24 | 85 |
| 25 } // namespace | 86 TEST_F(DnsTransactionTest, NormalQueryResponseTest) { |
| 26 | |
| 27 class TestDelegate : public DnsTransaction::Delegate { | |
| 28 public: | |
| 29 TestDelegate() : result_(ERR_UNEXPECTED), transaction_(NULL) {} | |
| 30 virtual ~TestDelegate() {} | |
| 31 virtual void OnTransactionComplete( | |
| 32 int result, | |
| 33 const DnsTransaction* transaction, | |
| 34 const IPAddressList& ip_addresses) { | |
| 35 result_ = result; | |
| 36 transaction_ = transaction; | |
| 37 ip_addresses_ = ip_addresses; | |
| 38 MessageLoop::current()->Quit(); | |
| 39 } | |
| 40 int result() const { return result_; } | |
| 41 const DnsTransaction* transaction() const { return transaction_; } | |
| 42 const IPAddressList& ip_addresses() const { | |
| 43 return ip_addresses_; | |
| 44 } | |
| 45 | |
| 46 private: | |
| 47 int result_; | |
| 48 const DnsTransaction* transaction_; | |
| 49 IPAddressList ip_addresses_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | |
| 52 }; | |
| 53 | |
| 54 | |
| 55 TEST(DnsTransactionTest, NormalQueryResponseTest) { | |
| 56 MockWrite writes0[] = { | 87 MockWrite writes0[] = { |
| 57 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), | 88 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), |
| 58 arraysize(kT0QueryDatagram)) | 89 arraysize(kT0QueryDatagram)) |
| 59 }; | 90 }; |
| 60 | 91 |
| 61 MockRead reads0[] = { | 92 MockRead reads0[] = { |
| 62 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram), | 93 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram), |
| 63 arraysize(kT0ResponseDatagram)) | 94 arraysize(kT0ResponseDatagram)) |
| 64 }; | 95 }; |
| 65 | 96 |
| 66 StaticSocketDataProvider data(reads0, arraysize(reads0), | 97 StaticSocketDataProvider data(reads0, arraysize(reads0), |
| 67 writes0, arraysize(writes0)); | 98 writes0, arraysize(writes0)); |
| 68 MockClientSocketFactory factory; | 99 factory().AddSocketDataProvider(&data); |
| 69 factory.AddSocketDataProvider(&data); | |
| 70 | 100 |
| 71 TestPrng test_prng(std::deque<int>(1, 0)); | 101 StartTransaction(); |
| 72 RandIntCallback rand_int_cb = | |
| 73 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); | |
| 74 std::string t0_dns_name(kT0DnsName, arraysize(kT0DnsName)); | |
| 75 | |
| 76 IPEndPoint dns_server; | |
| 77 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); | |
| 78 ASSERT_TRUE(rv); | |
| 79 | |
| 80 DnsTransaction t(dns_server, t0_dns_name, kT1Qtype, rand_int_cb, &factory, | |
| 81 BoundNetLog(), NULL); | |
| 82 | |
| 83 TestDelegate delegate; | |
| 84 t.SetDelegate(&delegate); | |
| 85 | |
| 86 IPAddressList expected_ip_addresses; | |
| 87 rv = ConvertStringsToIPAddressList(kT0IpAddresses, | |
| 88 arraysize(kT0IpAddresses), | |
| 89 &expected_ip_addresses); | |
| 90 ASSERT_TRUE(rv); | |
| 91 | |
| 92 int rv0 = t.Start(); | |
| 93 EXPECT_EQ(ERR_IO_PENDING, rv0); | |
| 94 | |
| 95 MessageLoop::current()->Run(); | 102 MessageLoop::current()->Run(); |
| 96 | 103 |
| 97 EXPECT_TRUE(DnsTransaction::Key(t0_dns_name, kT0Qtype) == t.key()); | 104 EXPECT_EQ(OK, rv()); |
| 98 EXPECT_EQ(OK, delegate.result()); | 105 // TODO(szym): test fields of |transaction_->response()| |
| 99 EXPECT_EQ(&t, delegate.transaction()); | |
| 100 EXPECT_TRUE(expected_ip_addresses == delegate.ip_addresses()); | |
| 101 | 106 |
| 102 EXPECT_TRUE(data.at_read_eof()); | 107 EXPECT_TRUE(data.at_read_eof()); |
| 103 EXPECT_TRUE(data.at_write_eof()); | 108 EXPECT_TRUE(data.at_write_eof()); |
| 104 } | 109 } |
| 105 | 110 |
| 106 TEST(DnsTransactionTest, MismatchedQueryResponseTest) { | 111 TEST_F(DnsTransactionTest, MismatchedQueryResponseTest) { |
| 107 MockWrite writes0[] = { | 112 MockWrite writes0[] = { |
| 108 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), | 113 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), |
| 109 arraysize(kT0QueryDatagram)) | 114 arraysize(kT0QueryDatagram)) |
| 110 }; | 115 }; |
| 111 | 116 |
| 112 MockRead reads1[] = { | 117 MockRead reads1[] = { |
| 113 MockRead(true, reinterpret_cast<const char*>(kT1ResponseDatagram), | 118 MockRead(true, reinterpret_cast<const char*>(kT1ResponseDatagram), |
| 114 arraysize(kT1ResponseDatagram)) | 119 arraysize(kT1ResponseDatagram)) |
| 115 }; | 120 }; |
| 116 | 121 |
| 117 StaticSocketDataProvider data(reads1, arraysize(reads1), | 122 StaticSocketDataProvider data(reads1, arraysize(reads1), |
| 118 writes0, arraysize(writes0)); | 123 writes0, arraysize(writes0)); |
| 119 MockClientSocketFactory factory; | 124 factory().AddSocketDataProvider(&data); |
| 120 factory.AddSocketDataProvider(&data); | |
| 121 | 125 |
| 122 TestPrng test_prng(std::deque<int>(1, 0)); | 126 StartTransaction(); |
| 123 RandIntCallback rand_int_cb = | |
| 124 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); | |
| 125 std::string t0_dns_name(kT0DnsName, arraysize(kT0DnsName)); | |
| 126 | |
| 127 IPEndPoint dns_server; | |
| 128 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); | |
| 129 ASSERT_TRUE(rv); | |
| 130 | |
| 131 DnsTransaction t(dns_server, t0_dns_name, kT1Qtype, rand_int_cb, &factory, | |
| 132 BoundNetLog(), NULL); | |
| 133 | |
| 134 TestDelegate delegate; | |
| 135 t.SetDelegate(&delegate); | |
| 136 | |
| 137 int rv0 = t.Start(); | |
| 138 EXPECT_EQ(ERR_IO_PENDING, rv0); | |
| 139 | |
| 140 MessageLoop::current()->Run(); | 127 MessageLoop::current()->Run(); |
| 141 | 128 |
| 142 EXPECT_TRUE(DnsTransaction::Key(t0_dns_name, kT0Qtype) == t.key()); | 129 EXPECT_EQ(ERR_DNS_MALFORMED_RESPONSE, rv()); |
| 143 EXPECT_EQ(ERR_DNS_MALFORMED_RESPONSE, delegate.result()); | 130 |
| 144 EXPECT_EQ(0u, delegate.ip_addresses().size()); | |
| 145 EXPECT_EQ(&t, delegate.transaction()); | |
| 146 EXPECT_TRUE(data.at_read_eof()); | 131 EXPECT_TRUE(data.at_read_eof()); |
| 147 EXPECT_TRUE(data.at_write_eof()); | 132 EXPECT_TRUE(data.at_write_eof()); |
| 148 } | 133 } |
| 149 | 134 |
| 150 // Test that after the first timeout we do a fresh connection and if we get | 135 // Test that after the first timeout we do a fresh connection and if we get |
| 151 // a response on the new connection, we return it. | 136 // a response on the new connection, we return it. |
| 152 TEST(DnsTransactionTest, FirstTimeoutTest) { | 137 TEST_F(DnsTransactionTest, FirstTimeoutTest) { |
| 153 MockWrite writes0[] = { | 138 MockWrite writes0[] = { |
| 154 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), | 139 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), |
| 155 arraysize(kT0QueryDatagram)) | 140 arraysize(kT0QueryDatagram)) |
| 156 }; | 141 }; |
| 157 | 142 |
| 158 MockRead reads0[] = { | 143 MockRead reads0[] = { |
| 159 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram), | 144 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram), |
| 160 arraysize(kT0ResponseDatagram)) | 145 arraysize(kT0ResponseDatagram)) |
| 161 }; | 146 }; |
| 162 | 147 |
| 163 scoped_refptr<DelayedSocketData> socket0_data( | 148 scoped_refptr<DelayedSocketData> socket0_data( |
| 164 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 149 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
| 165 scoped_refptr<DelayedSocketData> socket1_data( | 150 scoped_refptr<DelayedSocketData> socket1_data( |
| 166 new DelayedSocketData(0, reads0, arraysize(reads0), | 151 new DelayedSocketData(0, reads0, arraysize(reads0), |
| 167 writes0, arraysize(writes0))); | 152 writes0, arraysize(writes0))); |
| 168 MockClientSocketFactory factory; | |
| 169 factory.AddSocketDataProvider(socket0_data.get()); | |
| 170 factory.AddSocketDataProvider(socket1_data.get()); | |
| 171 | 153 |
| 172 TestPrng test_prng(std::deque<int>(2, 0)); | 154 factory().AddSocketDataProvider(socket0_data.get()); |
| 173 RandIntCallback rand_int_cb = | 155 factory().AddSocketDataProvider(socket1_data.get()); |
| 174 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); | |
| 175 std::string t0_dns_name(kT0DnsName, arraysize(kT0DnsName)); | |
| 176 | 156 |
| 177 IPEndPoint dns_server; | 157 StartTransaction(); |
| 178 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); | |
| 179 ASSERT_TRUE(rv); | |
| 180 | |
| 181 DnsTransaction t(dns_server, t0_dns_name, kT1Qtype, rand_int_cb, &factory, | |
| 182 BoundNetLog(), NULL); | |
| 183 | |
| 184 TestDelegate delegate; | |
| 185 t.SetDelegate(&delegate); | |
| 186 | |
| 187 t.set_timeouts_ms( | |
| 188 std::vector<base::TimeDelta>(kTimeoutsMs, | |
| 189 kTimeoutsMs + arraysize(kTimeoutsMs))); | |
| 190 | |
| 191 IPAddressList expected_ip_addresses; | |
| 192 rv = ConvertStringsToIPAddressList(kT0IpAddresses, | |
| 193 arraysize(kT0IpAddresses), | |
| 194 &expected_ip_addresses); | |
| 195 ASSERT_TRUE(rv); | |
| 196 | |
| 197 int rv0 = t.Start(); | |
| 198 EXPECT_EQ(ERR_IO_PENDING, rv0); | |
| 199 | |
| 200 | 158 |
| 201 MessageLoop::current()->Run(); | 159 MessageLoop::current()->Run(); |
| 202 | 160 |
| 203 EXPECT_TRUE(DnsTransaction::Key(t0_dns_name, kT0Qtype) == t.key()); | 161 EXPECT_EQ(OK, rv()); |
| 204 EXPECT_EQ(OK, delegate.result()); | |
| 205 EXPECT_EQ(&t, delegate.transaction()); | |
| 206 EXPECT_TRUE(expected_ip_addresses == delegate.ip_addresses()); | |
| 207 | 162 |
| 208 EXPECT_TRUE(socket0_data->at_read_eof()); | 163 EXPECT_TRUE(socket0_data->at_read_eof()); |
| 209 EXPECT_TRUE(socket0_data->at_write_eof()); | 164 EXPECT_TRUE(socket0_data->at_write_eof()); |
| 210 EXPECT_TRUE(socket1_data->at_read_eof()); | 165 EXPECT_TRUE(socket1_data->at_read_eof()); |
| 211 EXPECT_TRUE(socket1_data->at_write_eof()); | 166 EXPECT_TRUE(socket1_data->at_write_eof()); |
| 212 EXPECT_EQ(2u, factory.udp_client_sockets().size()); | 167 EXPECT_EQ(2u, factory().udp_client_sockets().size()); |
| 213 } | 168 } |
| 214 | 169 |
| 215 // Test that after the first timeout we do a fresh connection, and after | 170 // Test that after the first timeout we do a fresh connection, and after |
| 216 // the second timeout we do another fresh connection, and if we get a | 171 // the second timeout we do another fresh connection, and if we get a |
| 217 // response on the second connection, we return it. | 172 // response on the second connection, we return it. |
| 218 TEST(DnsTransactionTest, SecondTimeoutTest) { | 173 TEST_F(DnsTransactionTest, SecondTimeoutTest) { |
| 219 MockWrite writes0[] = { | 174 MockWrite writes0[] = { |
| 220 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), | 175 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), |
| 221 arraysize(kT0QueryDatagram)) | 176 arraysize(kT0QueryDatagram)) |
| 222 }; | 177 }; |
| 223 | 178 |
| 224 MockRead reads0[] = { | 179 MockRead reads0[] = { |
| 225 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram), | 180 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram), |
| 226 arraysize(kT0ResponseDatagram)) | 181 arraysize(kT0ResponseDatagram)) |
| 227 }; | 182 }; |
| 228 | 183 |
| 229 scoped_refptr<DelayedSocketData> socket0_data( | 184 scoped_refptr<DelayedSocketData> socket0_data( |
| 230 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 185 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
| 231 scoped_refptr<DelayedSocketData> socket1_data( | 186 scoped_refptr<DelayedSocketData> socket1_data( |
| 232 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 187 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
| 233 scoped_refptr<DelayedSocketData> socket2_data( | 188 scoped_refptr<DelayedSocketData> socket2_data( |
| 234 new DelayedSocketData(0, reads0, arraysize(reads0), | 189 new DelayedSocketData(0, reads0, arraysize(reads0), |
| 235 writes0, arraysize(writes0))); | 190 writes0, arraysize(writes0))); |
| 236 MockClientSocketFactory factory; | |
| 237 factory.AddSocketDataProvider(socket0_data.get()); | |
| 238 factory.AddSocketDataProvider(socket1_data.get()); | |
| 239 factory.AddSocketDataProvider(socket2_data.get()); | |
| 240 | 191 |
| 241 TestPrng test_prng(std::deque<int>(3, 0)); | 192 factory().AddSocketDataProvider(socket0_data.get()); |
| 242 RandIntCallback rand_int_cb = | 193 factory().AddSocketDataProvider(socket1_data.get()); |
| 243 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); | 194 factory().AddSocketDataProvider(socket2_data.get()); |
| 244 std::string t0_dns_name(kT0DnsName, arraysize(kT0DnsName)); | |
| 245 | 195 |
| 246 IPEndPoint dns_server; | 196 StartTransaction(); |
| 247 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); | |
| 248 ASSERT_TRUE(rv); | |
| 249 | |
| 250 DnsTransaction t(dns_server, t0_dns_name, kT1Qtype, rand_int_cb, &factory, | |
| 251 BoundNetLog(), NULL); | |
| 252 | |
| 253 TestDelegate delegate; | |
| 254 t.SetDelegate(&delegate); | |
| 255 | |
| 256 t.set_timeouts_ms( | |
| 257 std::vector<base::TimeDelta>(kTimeoutsMs, | |
| 258 kTimeoutsMs + arraysize(kTimeoutsMs))); | |
| 259 | |
| 260 IPAddressList expected_ip_addresses; | |
| 261 rv = ConvertStringsToIPAddressList(kT0IpAddresses, | |
| 262 arraysize(kT0IpAddresses), | |
| 263 &expected_ip_addresses); | |
| 264 ASSERT_TRUE(rv); | |
| 265 | |
| 266 int rv0 = t.Start(); | |
| 267 EXPECT_EQ(ERR_IO_PENDING, rv0); | |
| 268 | 197 |
| 269 MessageLoop::current()->Run(); | 198 MessageLoop::current()->Run(); |
| 270 | 199 |
| 271 EXPECT_TRUE(DnsTransaction::Key(t0_dns_name, kT1Qtype) == t.key()); | 200 EXPECT_EQ(OK, rv()); |
| 272 EXPECT_EQ(OK, delegate.result()); | |
| 273 EXPECT_EQ(&t, delegate.transaction()); | |
| 274 EXPECT_TRUE(expected_ip_addresses == delegate.ip_addresses()); | |
| 275 | 201 |
| 276 EXPECT_TRUE(socket0_data->at_read_eof()); | 202 EXPECT_TRUE(socket0_data->at_read_eof()); |
| 277 EXPECT_TRUE(socket0_data->at_write_eof()); | 203 EXPECT_TRUE(socket0_data->at_write_eof()); |
| 278 EXPECT_TRUE(socket1_data->at_read_eof()); | 204 EXPECT_TRUE(socket1_data->at_read_eof()); |
| 279 EXPECT_TRUE(socket1_data->at_write_eof()); | 205 EXPECT_TRUE(socket1_data->at_write_eof()); |
| 280 EXPECT_TRUE(socket2_data->at_read_eof()); | 206 EXPECT_TRUE(socket2_data->at_read_eof()); |
| 281 EXPECT_TRUE(socket2_data->at_write_eof()); | 207 EXPECT_TRUE(socket2_data->at_write_eof()); |
| 282 EXPECT_EQ(3u, factory.udp_client_sockets().size()); | 208 EXPECT_EQ(3u, factory().udp_client_sockets().size()); |
| 283 } | 209 } |
| 284 | 210 |
| 285 // Test that after the first timeout we do a fresh connection, and after | 211 // Test that after the first timeout we do a fresh connection, and after |
| 286 // the second timeout we do another fresh connection and after the third | 212 // the second timeout we do another fresh connection and after the third |
| 287 // timeout we give up and return a timeout error. | 213 // timeout we give up and return a timeout error. |
| 288 TEST(DnsTransactionTest, ThirdTimeoutTest) { | 214 TEST_F(DnsTransactionTest, ThirdTimeoutTest) { |
| 289 MockWrite writes0[] = { | 215 MockWrite writes0[] = { |
| 290 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), | 216 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), |
| 291 arraysize(kT0QueryDatagram)) | 217 arraysize(kT0QueryDatagram)) |
| 292 }; | 218 }; |
| 293 | 219 |
| 294 scoped_refptr<DelayedSocketData> socket0_data( | 220 scoped_refptr<DelayedSocketData> socket0_data( |
| 295 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 221 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
| 296 scoped_refptr<DelayedSocketData> socket1_data( | 222 scoped_refptr<DelayedSocketData> socket1_data( |
| 297 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 223 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
| 298 scoped_refptr<DelayedSocketData> socket2_data( | 224 scoped_refptr<DelayedSocketData> socket2_data( |
| 299 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); | 225 new DelayedSocketData(2, NULL, 0, writes0, arraysize(writes0))); |
| 300 MockClientSocketFactory factory; | |
| 301 factory.AddSocketDataProvider(socket0_data.get()); | |
| 302 factory.AddSocketDataProvider(socket1_data.get()); | |
| 303 factory.AddSocketDataProvider(socket2_data.get()); | |
| 304 | 226 |
| 305 TestPrng test_prng(std::deque<int>(3, 0)); | 227 factory().AddSocketDataProvider(socket0_data.get()); |
| 306 RandIntCallback rand_int_cb = | 228 factory().AddSocketDataProvider(socket1_data.get()); |
| 307 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); | 229 factory().AddSocketDataProvider(socket2_data.get()); |
| 308 std::string t0_dns_name(kT0DnsName, arraysize(kT0DnsName)); | |
| 309 | 230 |
| 310 IPEndPoint dns_server; | 231 StartTransaction(); |
| 311 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); | |
| 312 ASSERT_TRUE(rv); | |
| 313 | |
| 314 DnsTransaction t(dns_server, t0_dns_name, kT1Qtype, rand_int_cb, &factory, | |
| 315 BoundNetLog(), NULL); | |
| 316 | |
| 317 TestDelegate delegate; | |
| 318 t.SetDelegate(&delegate); | |
| 319 | |
| 320 t.set_timeouts_ms( | |
| 321 std::vector<base::TimeDelta>(kTimeoutsMs, | |
| 322 kTimeoutsMs + arraysize(kTimeoutsMs))); | |
| 323 | |
| 324 int rv0 = t.Start(); | |
| 325 EXPECT_EQ(ERR_IO_PENDING, rv0); | |
| 326 | 232 |
| 327 MessageLoop::current()->Run(); | 233 MessageLoop::current()->Run(); |
| 328 | 234 |
| 329 EXPECT_TRUE(DnsTransaction::Key(t0_dns_name, kT0Qtype) == t.key()); | 235 EXPECT_EQ(ERR_DNS_TIMED_OUT, rv()); |
| 330 EXPECT_EQ(ERR_DNS_TIMED_OUT, delegate.result()); | |
| 331 EXPECT_EQ(&t, delegate.transaction()); | |
| 332 | 236 |
| 333 EXPECT_TRUE(socket0_data->at_read_eof()); | 237 EXPECT_TRUE(socket0_data->at_read_eof()); |
| 334 EXPECT_TRUE(socket0_data->at_write_eof()); | 238 EXPECT_TRUE(socket0_data->at_write_eof()); |
| 335 EXPECT_TRUE(socket1_data->at_read_eof()); | 239 EXPECT_TRUE(socket1_data->at_read_eof()); |
| 336 EXPECT_TRUE(socket1_data->at_write_eof()); | 240 EXPECT_TRUE(socket1_data->at_write_eof()); |
| 337 EXPECT_TRUE(socket2_data->at_read_eof()); | 241 EXPECT_TRUE(socket2_data->at_read_eof()); |
| 338 EXPECT_TRUE(socket2_data->at_write_eof()); | 242 EXPECT_TRUE(socket2_data->at_write_eof()); |
| 339 EXPECT_EQ(3u, factory.udp_client_sockets().size()); | 243 EXPECT_EQ(3u, factory().udp_client_sockets().size()); |
| 340 } | 244 } |
| 341 | 245 |
| 246 } // namespace |
| 247 |
| 342 } // namespace net | 248 } // namespace net |
| OLD | NEW |