| 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_test_util.h" | 5 #include "net/dns/dns_test_util.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/big_endian.h" | 9 #include "base/big_endian.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 const DnsTransactionFactory::CallbackType& callback) | 43 const DnsTransactionFactory::CallbackType& callback) |
| 44 : result_(MockDnsClientRule::FAIL), | 44 : result_(MockDnsClientRule::FAIL), |
| 45 hostname_(hostname), | 45 hostname_(hostname), |
| 46 qtype_(qtype), | 46 qtype_(qtype), |
| 47 callback_(callback), | 47 callback_(callback), |
| 48 started_(false), | 48 started_(false), |
| 49 delayed_(false) { | 49 delayed_(false) { |
| 50 // Find the relevant rule which matches |qtype| and prefix of |hostname|. | 50 // Find the relevant rule which matches |qtype| and prefix of |hostname|. |
| 51 for (size_t i = 0; i < rules.size(); ++i) { | 51 for (size_t i = 0; i < rules.size(); ++i) { |
| 52 const std::string& prefix = rules[i].prefix; | 52 const std::string& prefix = rules[i].prefix; |
| 53 if ((rules[i].qtype == qtype) && | 53 if ((rules[i].qtype == qtype) && (hostname.size() >= prefix.size()) && |
| 54 (hostname.size() >= prefix.size()) && | |
| 55 (hostname.compare(0, prefix.size(), prefix) == 0)) { | 54 (hostname.compare(0, prefix.size(), prefix) == 0)) { |
| 56 result_ = rules[i].result; | 55 result_ = rules[i].result; |
| 57 delayed_ = rules[i].delay; | 56 delayed_ = rules[i].delay; |
| 58 break; | 57 break; |
| 59 } | 58 } |
| 60 } | 59 } |
| 61 } | 60 } |
| 62 | 61 |
| 63 virtual const std::string& GetHostname() const OVERRIDE { | 62 virtual const std::string& GetHostname() const OVERRIDE { return hostname_; } |
| 64 return hostname_; | |
| 65 } | |
| 66 | 63 |
| 67 virtual uint16 GetType() const OVERRIDE { | 64 virtual uint16 GetType() const OVERRIDE { return qtype_; } |
| 68 return qtype_; | |
| 69 } | |
| 70 | 65 |
| 71 virtual void Start() OVERRIDE { | 66 virtual void Start() OVERRIDE { |
| 72 EXPECT_FALSE(started_); | 67 EXPECT_FALSE(started_); |
| 73 started_ = true; | 68 started_ = true; |
| 74 if (delayed_) | 69 if (delayed_) |
| 75 return; | 70 return; |
| 76 // Using WeakPtr to cleanly cancel when transaction is destroyed. | 71 // Using WeakPtr to cleanly cancel when transaction is destroyed. |
| 77 base::MessageLoop::current()->PostTask( | 72 base::MessageLoop::current()->PostTask( |
| 78 FROM_HERE, base::Bind(&MockTransaction::Finish, AsWeakPtr())); | 73 FROM_HERE, base::Bind(&MockTransaction::Finish, AsWeakPtr())); |
| 79 } | 74 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 103 reinterpret_cast<dns_protocol::Header*>(buffer); | 98 reinterpret_cast<dns_protocol::Header*>(buffer); |
| 104 header->flags |= dns_protocol::kFlagResponse; | 99 header->flags |= dns_protocol::kFlagResponse; |
| 105 | 100 |
| 106 if (MockDnsClientRule::OK == result_) { | 101 if (MockDnsClientRule::OK == result_) { |
| 107 const uint16 kPointerToQueryName = | 102 const uint16 kPointerToQueryName = |
| 108 static_cast<uint16>(0xc000 | sizeof(*header)); | 103 static_cast<uint16>(0xc000 | sizeof(*header)); |
| 109 | 104 |
| 110 const uint32 kTTL = 86400; // One day. | 105 const uint32 kTTL = 86400; // One day. |
| 111 | 106 |
| 112 // Size of RDATA which is a IPv4 or IPv6 address. | 107 // Size of RDATA which is a IPv4 or IPv6 address. |
| 113 size_t rdata_size = qtype_ == net::dns_protocol::kTypeA ? | 108 size_t rdata_size = qtype_ == net::dns_protocol::kTypeA |
| 114 net::kIPv4AddressSize : net::kIPv6AddressSize; | 109 ? net::kIPv4AddressSize |
| 110 : net::kIPv6AddressSize; |
| 115 | 111 |
| 116 // 12 is the sum of sizes of the compressed name reference, TYPE, | 112 // 12 is the sum of sizes of the compressed name reference, TYPE, |
| 117 // CLASS, TTL and RDLENGTH. | 113 // CLASS, TTL and RDLENGTH. |
| 118 size_t answer_size = 12 + rdata_size; | 114 size_t answer_size = 12 + rdata_size; |
| 119 | 115 |
| 120 // Write answer with loopback IP address. | 116 // Write answer with loopback IP address. |
| 121 header->ancount = base::HostToNet16(1); | 117 header->ancount = base::HostToNet16(1); |
| 122 base::BigEndianWriter writer(buffer + nbytes, answer_size); | 118 base::BigEndianWriter writer(buffer + nbytes, answer_size); |
| 123 writer.WriteU16(kPointerToQueryName); | 119 writer.WriteU16(kPointerToQueryName); |
| 124 writer.WriteU16(qtype_); | 120 writer.WriteU16(qtype_); |
| 125 writer.WriteU16(net::dns_protocol::kClassIN); | 121 writer.WriteU16(net::dns_protocol::kClassIN); |
| 126 writer.WriteU32(kTTL); | 122 writer.WriteU32(kTTL); |
| 127 writer.WriteU16(rdata_size); | 123 writer.WriteU16(rdata_size); |
| 128 if (qtype_ == net::dns_protocol::kTypeA) { | 124 if (qtype_ == net::dns_protocol::kTypeA) { |
| 129 char kIPv4Loopback[] = { 0x7f, 0, 0, 1 }; | 125 char kIPv4Loopback[] = {0x7f, 0, 0, 1}; |
| 130 writer.WriteBytes(kIPv4Loopback, sizeof(kIPv4Loopback)); | 126 writer.WriteBytes(kIPv4Loopback, sizeof(kIPv4Loopback)); |
| 131 } else { | 127 } else { |
| 132 char kIPv6Loopback[] = { 0, 0, 0, 0, 0, 0, 0, 0, | 128 char kIPv6Loopback[] = {0, 0, 0, 0, 0, 0, 0, 0, |
| 133 0, 0, 0, 0, 0, 0, 0, 1 }; | 129 0, 0, 0, 0, 0, 0, 0, 1}; |
| 134 writer.WriteBytes(kIPv6Loopback, sizeof(kIPv6Loopback)); | 130 writer.WriteBytes(kIPv6Loopback, sizeof(kIPv6Loopback)); |
| 135 } | 131 } |
| 136 nbytes += answer_size; | 132 nbytes += answer_size; |
| 137 } | 133 } |
| 138 EXPECT_TRUE(response.InitParse(nbytes, query)); | 134 EXPECT_TRUE(response.InitParse(nbytes, query)); |
| 139 callback_.Run(this, OK, &response); | 135 callback_.Run(this, OK, &response); |
| 140 } break; | 136 } break; |
| 141 case MockDnsClientRule::FAIL: | 137 case MockDnsClientRule::FAIL: |
| 142 callback_.Run(this, ERR_NAME_NOT_RESOLVED, NULL); | 138 callback_.Run(this, ERR_NAME_NOT_RESOLVED, NULL); |
| 143 break; | 139 break; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 new MockTransaction(rules_, hostname, qtype, callback); | 173 new MockTransaction(rules_, hostname, qtype, callback); |
| 178 if (transaction->delayed()) | 174 if (transaction->delayed()) |
| 179 delayed_transactions_.push_back(transaction->AsWeakPtr()); | 175 delayed_transactions_.push_back(transaction->AsWeakPtr()); |
| 180 return scoped_ptr<DnsTransaction>(transaction); | 176 return scoped_ptr<DnsTransaction>(transaction); |
| 181 } | 177 } |
| 182 | 178 |
| 183 void CompleteDelayedTransactions() { | 179 void CompleteDelayedTransactions() { |
| 184 DelayedTransactionList old_delayed_transactions; | 180 DelayedTransactionList old_delayed_transactions; |
| 185 old_delayed_transactions.swap(delayed_transactions_); | 181 old_delayed_transactions.swap(delayed_transactions_); |
| 186 for (DelayedTransactionList::iterator it = old_delayed_transactions.begin(); | 182 for (DelayedTransactionList::iterator it = old_delayed_transactions.begin(); |
| 187 it != old_delayed_transactions.end(); ++it) { | 183 it != old_delayed_transactions.end(); |
| 184 ++it) { |
| 188 if (it->get()) | 185 if (it->get()) |
| 189 (*it)->FinishDelayedTransaction(); | 186 (*it)->FinishDelayedTransaction(); |
| 190 } | 187 } |
| 191 } | 188 } |
| 192 | 189 |
| 193 private: | 190 private: |
| 194 typedef std::vector<base::WeakPtr<MockTransaction> > DelayedTransactionList; | 191 typedef std::vector<base::WeakPtr<MockTransaction> > DelayedTransactionList; |
| 195 | 192 |
| 196 MockDnsClientRuleList rules_; | 193 MockDnsClientRuleList rules_; |
| 197 DelayedTransactionList delayed_transactions_; | 194 DelayedTransactionList delayed_transactions_; |
| 198 }; | 195 }; |
| 199 | 196 |
| 200 MockDnsClient::MockDnsClient(const DnsConfig& config, | 197 MockDnsClient::MockDnsClient(const DnsConfig& config, |
| 201 const MockDnsClientRuleList& rules) | 198 const MockDnsClientRuleList& rules) |
| 202 : config_(config), | 199 : config_(config), |
| 203 factory_(new MockTransactionFactory(rules)), | 200 factory_(new MockTransactionFactory(rules)), |
| 204 address_sorter_(new MockAddressSorter()) { | 201 address_sorter_(new MockAddressSorter()) { |
| 205 } | 202 } |
| 206 | 203 |
| 207 MockDnsClient::~MockDnsClient() {} | 204 MockDnsClient::~MockDnsClient() { |
| 205 } |
| 208 | 206 |
| 209 void MockDnsClient::SetConfig(const DnsConfig& config) { | 207 void MockDnsClient::SetConfig(const DnsConfig& config) { |
| 210 config_ = config; | 208 config_ = config; |
| 211 } | 209 } |
| 212 | 210 |
| 213 const DnsConfig* MockDnsClient::GetConfig() const { | 211 const DnsConfig* MockDnsClient::GetConfig() const { |
| 214 return config_.IsValid() ? &config_ : NULL; | 212 return config_.IsValid() ? &config_ : NULL; |
| 215 } | 213 } |
| 216 | 214 |
| 217 DnsTransactionFactory* MockDnsClient::GetTransactionFactory() { | 215 DnsTransactionFactory* MockDnsClient::GetTransactionFactory() { |
| 218 return config_.IsValid() ? factory_.get() : NULL; | 216 return config_.IsValid() ? factory_.get() : NULL; |
| 219 } | 217 } |
| 220 | 218 |
| 221 AddressSorter* MockDnsClient::GetAddressSorter() { | 219 AddressSorter* MockDnsClient::GetAddressSorter() { |
| 222 return address_sorter_.get(); | 220 return address_sorter_.get(); |
| 223 } | 221 } |
| 224 | 222 |
| 225 void MockDnsClient::CompleteDelayedTransactions() { | 223 void MockDnsClient::CompleteDelayedTransactions() { |
| 226 factory_->CompleteDelayedTransactions(); | 224 factory_->CompleteDelayedTransactions(); |
| 227 } | 225 } |
| 228 | 226 |
| 229 } // namespace net | 227 } // namespace net |
| OLD | NEW |