| 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 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 } | 205 } |
| 206 for (unsigned i = 0; i < num_servers; ++i) { | 206 for (unsigned i = 0; i < num_servers; ++i) { |
| 207 dns_ip[3] = i; | 207 dns_ip[3] = i; |
| 208 config_.nameservers.push_back(IPEndPoint(dns_ip, | 208 config_.nameservers.push_back(IPEndPoint(dns_ip, |
| 209 dns_protocol::kDefaultPort)); | 209 dns_protocol::kDefaultPort)); |
| 210 } | 210 } |
| 211 } | 211 } |
| 212 | 212 |
| 213 // Called after fully configuring |config|. | 213 // Called after fully configuring |config|. |
| 214 void ConfigureFactory() { | 214 void ConfigureFactory() { |
| 215 socket_factory_ = new TestSocketFactory(); | 215 socket_factory_.reset(new TestSocketFactory()); |
| 216 session_ = new DnsSession( | 216 session_ = new DnsSession( |
| 217 config_, | 217 config_, |
| 218 socket_factory_, | 218 socket_factory_.get(), |
| 219 base::Bind(&DnsTransactionTest::GetNextId, base::Unretained(this)), | 219 base::Bind(&DnsTransactionTest::GetNextId, base::Unretained(this)), |
| 220 NULL /* NetLog */); | 220 NULL /* NetLog */); |
| 221 transaction_factory_ = DnsTransactionFactory::CreateFactory(session_.get()); | 221 transaction_factory_ = DnsTransactionFactory::CreateFactory(session_.get()); |
| 222 } | 222 } |
| 223 | 223 |
| 224 // Each socket used by a DnsTransaction expects only one write and zero or one | 224 // Each socket used by a DnsTransaction expects only one write and zero or one |
| 225 // reads. | 225 // reads. |
| 226 | 226 |
| 227 // Add expected query for |dotted_name| and |qtype| with |id| and response | 227 // Add expected query for |dotted_name| and |qtype| with |id| and response |
| 228 // taken verbatim from |data| of |data_length| bytes. The transaction id in | 228 // taken verbatim from |data| of |data_length| bytes. The transaction id in |
| 229 // |data| should equal |id|, unless testing mismatched response. | 229 // |data| should equal |id|, unless testing mismatched response. |
| 230 void AddResponse(const std::string& dotted_name, | 230 void AddResponse(const std::string& dotted_name, |
| 231 uint16 qtype, | 231 uint16 qtype, |
| 232 uint16 id, | 232 uint16 id, |
| 233 const char* data, | 233 const char* data, |
| 234 size_t data_length) { | 234 size_t data_length) { |
| 235 CHECK(socket_factory_); | 235 CHECK(socket_factory_.get()); |
| 236 DnsQuery* query = new DnsQuery(id, DomainFromDot(dotted_name), qtype); | 236 DnsQuery* query = new DnsQuery(id, DomainFromDot(dotted_name), qtype); |
| 237 queries_.push_back(query); | 237 queries_.push_back(query); |
| 238 | 238 |
| 239 // The response is only used to hold the IOBuffer. | 239 // The response is only used to hold the IOBuffer. |
| 240 DnsResponse* response = new DnsResponse(data, data_length, 0); | 240 DnsResponse* response = new DnsResponse(data, data_length, 0); |
| 241 responses_.push_back(response); | 241 responses_.push_back(response); |
| 242 | 242 |
| 243 writes_.push_back(MockWrite(true, | 243 writes_.push_back(MockWrite(true, |
| 244 query->io_buffer()->data(), | 244 query->io_buffer()->data(), |
| 245 query->io_buffer()->size())); | 245 query->io_buffer()->size())); |
| 246 reads_.push_back(MockRead(true, | 246 reads_.push_back(MockRead(true, |
| 247 response->io_buffer()->data(), | 247 response->io_buffer()->data(), |
| 248 data_length)); | 248 data_length)); |
| 249 | 249 |
| 250 transaction_ids_.push_back(id); | 250 transaction_ids_.push_back(id); |
| 251 } | 251 } |
| 252 | 252 |
| 253 // Add expected query of |dotted_name| and |qtype| and no response. | 253 // Add expected query of |dotted_name| and |qtype| and no response. |
| 254 void AddTimeout(const char* dotted_name, uint16 qtype) { | 254 void AddTimeout(const char* dotted_name, uint16 qtype) { |
| 255 CHECK(socket_factory_); | 255 CHECK(socket_factory_.get()); |
| 256 uint16 id = base::RandInt(0, kuint16max); | 256 uint16 id = base::RandInt(0, kuint16max); |
| 257 DnsQuery* query = new DnsQuery(id, DomainFromDot(dotted_name), qtype); | 257 DnsQuery* query = new DnsQuery(id, DomainFromDot(dotted_name), qtype); |
| 258 queries_.push_back(query); | 258 queries_.push_back(query); |
| 259 | 259 |
| 260 writes_.push_back(MockWrite(true, | 260 writes_.push_back(MockWrite(true, |
| 261 query->io_buffer()->data(), | 261 query->io_buffer()->data(), |
| 262 query->io_buffer()->size())); | 262 query->io_buffer()->size())); |
| 263 // Empty MockRead indicates no data. | 263 // Empty MockRead indicates no data. |
| 264 reads_.push_back(MockRead()); | 264 reads_.push_back(MockRead()); |
| 265 transaction_ids_.push_back(id); | 265 transaction_ids_.push_back(id); |
| 266 } | 266 } |
| 267 | 267 |
| 268 // Add expected query of |dotted_name| and |qtype| and response with no answer | 268 // Add expected query of |dotted_name| and |qtype| and response with no answer |
| 269 // and rcode set to |rcode|. | 269 // and rcode set to |rcode|. |
| 270 void AddRcode(const char* dotted_name, uint16 qtype, int rcode) { | 270 void AddRcode(const char* dotted_name, uint16 qtype, int rcode) { |
| 271 CHECK(socket_factory_); | 271 CHECK(socket_factory_.get()); |
| 272 CHECK_NE(dns_protocol::kRcodeNOERROR, rcode); | 272 CHECK_NE(dns_protocol::kRcodeNOERROR, rcode); |
| 273 uint16 id = base::RandInt(0, kuint16max); | 273 uint16 id = base::RandInt(0, kuint16max); |
| 274 DnsQuery* query = new DnsQuery(id, DomainFromDot(dotted_name), qtype); | 274 DnsQuery* query = new DnsQuery(id, DomainFromDot(dotted_name), qtype); |
| 275 queries_.push_back(query); | 275 queries_.push_back(query); |
| 276 | 276 |
| 277 DnsResponse* response = new DnsResponse(query->io_buffer()->data(), | 277 DnsResponse* response = new DnsResponse(query->io_buffer()->data(), |
| 278 query->io_buffer()->size(), | 278 query->io_buffer()->size(), |
| 279 0); | 279 0); |
| 280 dns_protocol::Header* header = | 280 dns_protocol::Header* header = |
| 281 reinterpret_cast<dns_protocol::Header*>(response->io_buffer()->data()); | 281 reinterpret_cast<dns_protocol::Header*>(response->io_buffer()->data()); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 ScopedVector<DnsResponse> responses_; | 353 ScopedVector<DnsResponse> responses_; |
| 354 | 354 |
| 355 // Holders for MockRead/MockWrites (SocketDataProvider does not own it). | 355 // Holders for MockRead/MockWrites (SocketDataProvider does not own it). |
| 356 std::vector<MockRead> reads_; | 356 std::vector<MockRead> reads_; |
| 357 std::vector<MockWrite> writes_; | 357 std::vector<MockWrite> writes_; |
| 358 | 358 |
| 359 // Holder for the socket data (MockClientSocketFactory does not own it). | 359 // Holder for the socket data (MockClientSocketFactory does not own it). |
| 360 ScopedVector<DelayedSocketData> socket_data_; | 360 ScopedVector<DelayedSocketData> socket_data_; |
| 361 | 361 |
| 362 std::deque<int> transaction_ids_; | 362 std::deque<int> transaction_ids_; |
| 363 // Owned by |session_|. | 363 scoped_ptr<TestSocketFactory> socket_factory_; |
| 364 TestSocketFactory* socket_factory_; | |
| 365 scoped_refptr<DnsSession> session_; | 364 scoped_refptr<DnsSession> session_; |
| 366 scoped_ptr<DnsTransactionFactory> transaction_factory_; | 365 scoped_ptr<DnsTransactionFactory> transaction_factory_; |
| 367 }; | 366 }; |
| 368 | 367 |
| 369 TEST_F(DnsTransactionTest, Lookup) { | 368 TEST_F(DnsTransactionTest, Lookup) { |
| 370 AddResponse(kT0HostName, | 369 AddResponse(kT0HostName, |
| 371 kT0Qtype, | 370 kT0Qtype, |
| 372 0 /* id */, | 371 0 /* id */, |
| 373 reinterpret_cast<const char*>(kT0ResponseDatagram), | 372 reinterpret_cast<const char*>(kT0ResponseDatagram), |
| 374 arraysize(kT0ResponseDatagram)); | 373 arraysize(kT0ResponseDatagram)); |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 TransactionHelper helper2("x", | 673 TransactionHelper helper2("x", |
| 675 dns_protocol::kTypeA, | 674 dns_protocol::kTypeA, |
| 676 ERR_NAME_NOT_RESOLVED); | 675 ERR_NAME_NOT_RESOLVED); |
| 677 EXPECT_TRUE(helper2.Run(transaction_factory_.get())); | 676 EXPECT_TRUE(helper2.Run(transaction_factory_.get())); |
| 678 } | 677 } |
| 679 | 678 |
| 680 } // namespace | 679 } // namespace |
| 681 | 680 |
| 682 } // namespace net | 681 } // namespace net |
| 683 | 682 |
| OLD | NEW |