Chromium Code Reviews| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 started_(false), | 50 started_(false), |
| 51 delayed_(false) { | 51 delayed_(false) { |
| 52 // Find the relevant rule which matches |qtype| and prefix of |hostname|. | 52 // Find the relevant rule which matches |qtype| and prefix of |hostname|. |
| 53 for (size_t i = 0; i < rules.size(); ++i) { | 53 for (size_t i = 0; i < rules.size(); ++i) { |
| 54 const std::string& prefix = rules[i].prefix; | 54 const std::string& prefix = rules[i].prefix; |
| 55 if ((rules[i].qtype == qtype) && | 55 if ((rules[i].qtype == qtype) && |
| 56 (hostname.size() >= prefix.size()) && | 56 (hostname.size() >= prefix.size()) && |
| 57 (hostname.compare(0, prefix.size(), prefix) == 0)) { | 57 (hostname.compare(0, prefix.size(), prefix) == 0)) { |
| 58 result_ = rules[i].result; | 58 result_ = rules[i].result; |
| 59 delayed_ = rules[i].delay; | 59 delayed_ = rules[i].delay; |
| 60 | |
| 61 // Fill in an IP address for the result if one was not specified. | |
| 62 if (result_.ip.empty() && result_.type == MockDnsClientRule::OK) { | |
| 63 result_.ip = qtype_ == dns_protocol::kTypeA | |
| 64 ? IPAddress::IPv4Localhost() | |
| 65 : IPAddress::IPv6Localhost(); | |
| 66 } | |
| 60 break; | 67 break; |
| 61 } | 68 } |
| 62 } | 69 } |
| 63 } | 70 } |
| 64 | 71 |
| 65 const std::string& GetHostname() const override { return hostname_; } | 72 const std::string& GetHostname() const override { return hostname_; } |
| 66 | 73 |
| 67 uint16_t GetType() const override { return qtype_; } | 74 uint16_t GetType() const override { return qtype_; } |
| 68 | 75 |
| 69 void Start() override { | 76 void Start() override { |
| 70 EXPECT_FALSE(started_); | 77 EXPECT_FALSE(started_); |
| 71 started_ = true; | 78 started_ = true; |
| 72 if (delayed_) | 79 if (delayed_) |
| 73 return; | 80 return; |
| 74 // Using WeakPtr to cleanly cancel when transaction is destroyed. | 81 // Using WeakPtr to cleanly cancel when transaction is destroyed. |
| 75 base::ThreadTaskRunnerHandle::Get()->PostTask( | 82 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 76 FROM_HERE, base::Bind(&MockTransaction::Finish, AsWeakPtr())); | 83 FROM_HERE, base::Bind(&MockTransaction::Finish, AsWeakPtr())); |
| 77 } | 84 } |
| 78 | 85 |
| 79 void FinishDelayedTransaction() { | 86 void FinishDelayedTransaction() { |
| 80 EXPECT_TRUE(delayed_); | 87 EXPECT_TRUE(delayed_); |
| 81 delayed_ = false; | 88 delayed_ = false; |
| 82 Finish(); | 89 Finish(); |
| 83 } | 90 } |
| 84 | 91 |
| 85 bool delayed() const { return delayed_; } | 92 bool delayed() const { return delayed_; } |
| 86 | 93 |
| 87 private: | 94 private: |
| 88 void Finish() { | 95 void Finish() { |
| 89 switch (result_) { | 96 switch (result_.type) { |
| 90 case MockDnsClientRule::EMPTY: | 97 case MockDnsClientRule::EMPTY: |
| 91 case MockDnsClientRule::OK: { | 98 case MockDnsClientRule::OK: { |
| 92 std::string qname; | 99 std::string qname; |
| 93 DNSDomainFromDot(hostname_, &qname); | 100 DNSDomainFromDot(hostname_, &qname); |
| 94 DnsQuery query(0, qname, qtype_); | 101 DnsQuery query(0, qname, qtype_); |
| 95 | 102 |
| 96 DnsResponse response; | 103 DnsResponse response; |
| 97 char* buffer = response.io_buffer()->data(); | 104 char* buffer = response.io_buffer()->data(); |
| 98 int nbytes = query.io_buffer()->size(); | 105 int nbytes = query.io_buffer()->size(); |
| 99 memcpy(buffer, query.io_buffer()->data(), nbytes); | 106 memcpy(buffer, query.io_buffer()->data(), nbytes); |
| 100 dns_protocol::Header* header = | 107 dns_protocol::Header* header = |
| 101 reinterpret_cast<dns_protocol::Header*>(buffer); | 108 reinterpret_cast<dns_protocol::Header*>(buffer); |
| 102 header->flags |= dns_protocol::kFlagResponse; | 109 header->flags |= dns_protocol::kFlagResponse; |
| 103 | 110 |
| 104 if (MockDnsClientRule::OK == result_) { | 111 if (MockDnsClientRule::OK == result_.type) { |
| 105 const uint16_t kPointerToQueryName = | 112 const uint16_t kPointerToQueryName = |
| 106 static_cast<uint16_t>(0xc000 | sizeof(*header)); | 113 static_cast<uint16_t>(0xc000 | sizeof(*header)); |
| 107 | 114 |
| 108 const uint32_t kTTL = 86400; // One day. | 115 const uint32_t kTTL = 86400; // One day. |
| 109 | 116 |
| 110 // Size of RDATA which is a IPv4 or IPv6 address. | 117 // Size of RDATA which is a IPv4 or IPv6 address. |
| 111 size_t rdata_size = qtype_ == dns_protocol::kTypeA | 118 EXPECT_TRUE(result_.ip.IsValid()); |
| 112 ? IPAddress::kIPv4AddressSize | 119 size_t rdata_size = result_.ip.size(); |
| 113 : IPAddress::kIPv6AddressSize; | |
| 114 | 120 |
| 115 // 12 is the sum of sizes of the compressed name reference, TYPE, | 121 // 12 is the sum of sizes of the compressed name reference, TYPE, |
| 116 // CLASS, TTL and RDLENGTH. | 122 // CLASS, TTL and RDLENGTH. |
| 117 size_t answer_size = 12 + rdata_size; | 123 size_t answer_size = 12 + rdata_size; |
| 118 | 124 |
| 119 // Write answer with loopback IP address. | 125 // Write the answer using the expected IP address. |
| 120 header->ancount = base::HostToNet16(1); | 126 header->ancount = base::HostToNet16(1); |
| 121 base::BigEndianWriter writer(buffer + nbytes, answer_size); | 127 base::BigEndianWriter writer(buffer + nbytes, answer_size); |
| 122 writer.WriteU16(kPointerToQueryName); | 128 writer.WriteU16(kPointerToQueryName); |
| 123 writer.WriteU16(qtype_); | 129 writer.WriteU16(qtype_); |
| 124 writer.WriteU16(dns_protocol::kClassIN); | 130 writer.WriteU16(dns_protocol::kClassIN); |
| 125 writer.WriteU32(kTTL); | 131 writer.WriteU32(kTTL); |
| 126 writer.WriteU16(static_cast<uint16_t>(rdata_size)); | 132 writer.WriteU16(static_cast<uint16_t>(rdata_size)); |
| 127 if (qtype_ == dns_protocol::kTypeA) { | 133 writer.WriteBytes(result_.ip.bytes().data(), |
| 128 char kIPv4Loopback[] = { 0x7f, 0, 0, 1 }; | 134 result_.ip.bytes().size()); |
|
mmenke
2016/12/13 19:24:15
nit: result_.ip.bytes().size() -> size, or remove
eroman
2016/12/13 22:09:04
Done.
| |
| 129 writer.WriteBytes(kIPv4Loopback, sizeof(kIPv4Loopback)); | |
| 130 } else { | |
| 131 char kIPv6Loopback[] = { 0, 0, 0, 0, 0, 0, 0, 0, | |
| 132 0, 0, 0, 0, 0, 0, 0, 1 }; | |
| 133 writer.WriteBytes(kIPv6Loopback, sizeof(kIPv6Loopback)); | |
| 134 } | |
| 135 nbytes += answer_size; | 135 nbytes += answer_size; |
| 136 } | 136 } |
| 137 EXPECT_TRUE(response.InitParse(nbytes, query)); | 137 EXPECT_TRUE(response.InitParse(nbytes, query)); |
| 138 callback_.Run(this, OK, &response); | 138 callback_.Run(this, OK, &response); |
| 139 } break; | 139 } break; |
| 140 case MockDnsClientRule::FAIL: | 140 case MockDnsClientRule::FAIL: |
| 141 callback_.Run(this, ERR_NAME_NOT_RESOLVED, NULL); | 141 callback_.Run(this, ERR_NAME_NOT_RESOLVED, NULL); |
| 142 break; | 142 break; |
| 143 case MockDnsClientRule::TIMEOUT: | 143 case MockDnsClientRule::TIMEOUT: |
| 144 callback_.Run(this, ERR_DNS_TIMED_OUT, NULL); | 144 callback_.Run(this, ERR_DNS_TIMED_OUT, NULL); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 | 225 |
| 226 std::unique_ptr<const base::Value> MockDnsClient::GetPersistentData() const { | 226 std::unique_ptr<const base::Value> MockDnsClient::GetPersistentData() const { |
| 227 return std::unique_ptr<const base::Value>(); | 227 return std::unique_ptr<const base::Value>(); |
| 228 } | 228 } |
| 229 | 229 |
| 230 void MockDnsClient::CompleteDelayedTransactions() { | 230 void MockDnsClient::CompleteDelayedTransactions() { |
| 231 factory_->CompleteDelayedTransactions(); | 231 factory_->CompleteDelayedTransactions(); |
| 232 } | 232 } |
| 233 | 233 |
| 234 } // namespace net | 234 } // namespace net |
| OLD | NEW |