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

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

Issue 2567623003: Make ERR_ICANN_NAME_COLLISION work for async DNS resolver. (Closed)
Patch Set: address Matt's comments Created 4 years 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
« no previous file with comments | « net/dns/dns_test_util.h ('k') | net/dns/host_resolver_impl.cc » ('j') | 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_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
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(), rdata_size);
128 char kIPv4Loopback[] = { 0x7f, 0, 0, 1 };
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; 134 nbytes += answer_size;
136 } 135 }
137 EXPECT_TRUE(response.InitParse(nbytes, query)); 136 EXPECT_TRUE(response.InitParse(nbytes, query));
138 callback_.Run(this, OK, &response); 137 callback_.Run(this, OK, &response);
139 } break; 138 } break;
140 case MockDnsClientRule::FAIL: 139 case MockDnsClientRule::FAIL:
141 callback_.Run(this, ERR_NAME_NOT_RESOLVED, NULL); 140 callback_.Run(this, ERR_NAME_NOT_RESOLVED, NULL);
142 break; 141 break;
143 case MockDnsClientRule::TIMEOUT: 142 case MockDnsClientRule::TIMEOUT:
144 callback_.Run(this, ERR_DNS_TIMED_OUT, NULL); 143 callback_.Run(this, ERR_DNS_TIMED_OUT, NULL);
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 224
226 std::unique_ptr<const base::Value> MockDnsClient::GetPersistentData() const { 225 std::unique_ptr<const base::Value> MockDnsClient::GetPersistentData() const {
227 return std::unique_ptr<const base::Value>(); 226 return std::unique_ptr<const base::Value>();
228 } 227 }
229 228
230 void MockDnsClient::CompleteDelayedTransactions() { 229 void MockDnsClient::CompleteDelayedTransactions() {
231 factory_->CompleteDelayedTransactions(); 230 factory_->CompleteDelayedTransactions();
232 } 231 }
233 232
234 } // namespace net 233 } // namespace net
OLDNEW
« no previous file with comments | « net/dns/dns_test_util.h ('k') | net/dns/host_resolver_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698