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 23 matching lines...) Expand all Loading... |
34 callback.Run(true, list); | 34 callback.Run(true, list); |
35 } | 35 } |
36 }; | 36 }; |
37 | 37 |
38 // A DnsTransaction which uses MockDnsClientRuleList to determine the response. | 38 // A DnsTransaction which uses MockDnsClientRuleList to determine the response. |
39 class MockTransaction : public DnsTransaction, | 39 class MockTransaction : public DnsTransaction, |
40 public base::SupportsWeakPtr<MockTransaction> { | 40 public base::SupportsWeakPtr<MockTransaction> { |
41 public: | 41 public: |
42 MockTransaction(const MockDnsClientRuleList& rules, | 42 MockTransaction(const MockDnsClientRuleList& rules, |
43 const std::string& hostname, | 43 const std::string& hostname, |
44 uint16 qtype, | 44 uint16_t qtype, |
45 const DnsTransactionFactory::CallbackType& callback) | 45 const DnsTransactionFactory::CallbackType& callback) |
46 : result_(MockDnsClientRule::FAIL), | 46 : result_(MockDnsClientRule::FAIL), |
47 hostname_(hostname), | 47 hostname_(hostname), |
48 qtype_(qtype), | 48 qtype_(qtype), |
49 callback_(callback), | 49 callback_(callback), |
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 break; | 60 break; |
61 } | 61 } |
62 } | 62 } |
63 } | 63 } |
64 | 64 |
65 const std::string& GetHostname() const override { return hostname_; } | 65 const std::string& GetHostname() const override { return hostname_; } |
66 | 66 |
67 uint16 GetType() const override { return qtype_; } | 67 uint16_t GetType() const override { return qtype_; } |
68 | 68 |
69 void Start() override { | 69 void Start() override { |
70 EXPECT_FALSE(started_); | 70 EXPECT_FALSE(started_); |
71 started_ = true; | 71 started_ = true; |
72 if (delayed_) | 72 if (delayed_) |
73 return; | 73 return; |
74 // Using WeakPtr to cleanly cancel when transaction is destroyed. | 74 // Using WeakPtr to cleanly cancel when transaction is destroyed. |
75 base::ThreadTaskRunnerHandle::Get()->PostTask( | 75 base::ThreadTaskRunnerHandle::Get()->PostTask( |
76 FROM_HERE, base::Bind(&MockTransaction::Finish, AsWeakPtr())); | 76 FROM_HERE, base::Bind(&MockTransaction::Finish, AsWeakPtr())); |
77 } | 77 } |
(...skipping 17 matching lines...) Expand all Loading... |
95 | 95 |
96 DnsResponse response; | 96 DnsResponse response; |
97 char* buffer = response.io_buffer()->data(); | 97 char* buffer = response.io_buffer()->data(); |
98 int nbytes = query.io_buffer()->size(); | 98 int nbytes = query.io_buffer()->size(); |
99 memcpy(buffer, query.io_buffer()->data(), nbytes); | 99 memcpy(buffer, query.io_buffer()->data(), nbytes); |
100 dns_protocol::Header* header = | 100 dns_protocol::Header* header = |
101 reinterpret_cast<dns_protocol::Header*>(buffer); | 101 reinterpret_cast<dns_protocol::Header*>(buffer); |
102 header->flags |= dns_protocol::kFlagResponse; | 102 header->flags |= dns_protocol::kFlagResponse; |
103 | 103 |
104 if (MockDnsClientRule::OK == result_) { | 104 if (MockDnsClientRule::OK == result_) { |
105 const uint16 kPointerToQueryName = | 105 const uint16_t kPointerToQueryName = |
106 static_cast<uint16>(0xc000 | sizeof(*header)); | 106 static_cast<uint16_t>(0xc000 | sizeof(*header)); |
107 | 107 |
108 const uint32 kTTL = 86400; // One day. | 108 const uint32_t kTTL = 86400; // One day. |
109 | 109 |
110 // Size of RDATA which is a IPv4 or IPv6 address. | 110 // Size of RDATA which is a IPv4 or IPv6 address. |
111 size_t rdata_size = qtype_ == dns_protocol::kTypeA ? kIPv4AddressSize | 111 size_t rdata_size = qtype_ == dns_protocol::kTypeA ? kIPv4AddressSize |
112 : kIPv6AddressSize; | 112 : kIPv6AddressSize; |
113 | 113 |
114 // 12 is the sum of sizes of the compressed name reference, TYPE, | 114 // 12 is the sum of sizes of the compressed name reference, TYPE, |
115 // CLASS, TTL and RDLENGTH. | 115 // CLASS, TTL and RDLENGTH. |
116 size_t answer_size = 12 + rdata_size; | 116 size_t answer_size = 12 + rdata_size; |
117 | 117 |
118 // Write answer with loopback IP address. | 118 // Write answer with loopback IP address. |
119 header->ancount = base::HostToNet16(1); | 119 header->ancount = base::HostToNet16(1); |
120 base::BigEndianWriter writer(buffer + nbytes, answer_size); | 120 base::BigEndianWriter writer(buffer + nbytes, answer_size); |
121 writer.WriteU16(kPointerToQueryName); | 121 writer.WriteU16(kPointerToQueryName); |
122 writer.WriteU16(qtype_); | 122 writer.WriteU16(qtype_); |
123 writer.WriteU16(dns_protocol::kClassIN); | 123 writer.WriteU16(dns_protocol::kClassIN); |
124 writer.WriteU32(kTTL); | 124 writer.WriteU32(kTTL); |
125 writer.WriteU16(static_cast<uint16>(rdata_size)); | 125 writer.WriteU16(static_cast<uint16_t>(rdata_size)); |
126 if (qtype_ == dns_protocol::kTypeA) { | 126 if (qtype_ == dns_protocol::kTypeA) { |
127 char kIPv4Loopback[] = { 0x7f, 0, 0, 1 }; | 127 char kIPv4Loopback[] = { 0x7f, 0, 0, 1 }; |
128 writer.WriteBytes(kIPv4Loopback, sizeof(kIPv4Loopback)); | 128 writer.WriteBytes(kIPv4Loopback, sizeof(kIPv4Loopback)); |
129 } else { | 129 } else { |
130 char kIPv6Loopback[] = { 0, 0, 0, 0, 0, 0, 0, 0, | 130 char kIPv6Loopback[] = { 0, 0, 0, 0, 0, 0, 0, 0, |
131 0, 0, 0, 0, 0, 0, 0, 1 }; | 131 0, 0, 0, 0, 0, 0, 0, 1 }; |
132 writer.WriteBytes(kIPv6Loopback, sizeof(kIPv6Loopback)); | 132 writer.WriteBytes(kIPv6Loopback, sizeof(kIPv6Loopback)); |
133 } | 133 } |
134 nbytes += answer_size; | 134 nbytes += answer_size; |
135 } | 135 } |
136 EXPECT_TRUE(response.InitParse(nbytes, query)); | 136 EXPECT_TRUE(response.InitParse(nbytes, query)); |
137 callback_.Run(this, OK, &response); | 137 callback_.Run(this, OK, &response); |
138 } break; | 138 } break; |
139 case MockDnsClientRule::FAIL: | 139 case MockDnsClientRule::FAIL: |
140 callback_.Run(this, ERR_NAME_NOT_RESOLVED, NULL); | 140 callback_.Run(this, ERR_NAME_NOT_RESOLVED, NULL); |
141 break; | 141 break; |
142 case MockDnsClientRule::TIMEOUT: | 142 case MockDnsClientRule::TIMEOUT: |
143 callback_.Run(this, ERR_DNS_TIMED_OUT, NULL); | 143 callback_.Run(this, ERR_DNS_TIMED_OUT, NULL); |
144 break; | 144 break; |
145 default: | 145 default: |
146 NOTREACHED(); | 146 NOTREACHED(); |
147 break; | 147 break; |
148 } | 148 } |
149 } | 149 } |
150 | 150 |
151 MockDnsClientRule::Result result_; | 151 MockDnsClientRule::Result result_; |
152 const std::string hostname_; | 152 const std::string hostname_; |
153 const uint16 qtype_; | 153 const uint16_t qtype_; |
154 DnsTransactionFactory::CallbackType callback_; | 154 DnsTransactionFactory::CallbackType callback_; |
155 bool started_; | 155 bool started_; |
156 bool delayed_; | 156 bool delayed_; |
157 }; | 157 }; |
158 | 158 |
159 } // namespace | 159 } // namespace |
160 | 160 |
161 // A DnsTransactionFactory which creates MockTransaction. | 161 // A DnsTransactionFactory which creates MockTransaction. |
162 class MockTransactionFactory : public DnsTransactionFactory { | 162 class MockTransactionFactory : public DnsTransactionFactory { |
163 public: | 163 public: |
164 explicit MockTransactionFactory(const MockDnsClientRuleList& rules) | 164 explicit MockTransactionFactory(const MockDnsClientRuleList& rules) |
165 : rules_(rules) {} | 165 : rules_(rules) {} |
166 | 166 |
167 ~MockTransactionFactory() override {} | 167 ~MockTransactionFactory() override {} |
168 | 168 |
169 scoped_ptr<DnsTransaction> CreateTransaction( | 169 scoped_ptr<DnsTransaction> CreateTransaction( |
170 const std::string& hostname, | 170 const std::string& hostname, |
171 uint16 qtype, | 171 uint16_t qtype, |
172 const DnsTransactionFactory::CallbackType& callback, | 172 const DnsTransactionFactory::CallbackType& callback, |
173 const BoundNetLog&) override { | 173 const BoundNetLog&) override { |
174 MockTransaction* transaction = | 174 MockTransaction* transaction = |
175 new MockTransaction(rules_, hostname, qtype, callback); | 175 new MockTransaction(rules_, hostname, qtype, callback); |
176 if (transaction->delayed()) | 176 if (transaction->delayed()) |
177 delayed_transactions_.push_back(transaction->AsWeakPtr()); | 177 delayed_transactions_.push_back(transaction->AsWeakPtr()); |
178 return scoped_ptr<DnsTransaction>(transaction); | 178 return scoped_ptr<DnsTransaction>(transaction); |
179 } | 179 } |
180 | 180 |
181 void CompleteDelayedTransactions() { | 181 void CompleteDelayedTransactions() { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 | 218 |
219 AddressSorter* MockDnsClient::GetAddressSorter() { | 219 AddressSorter* MockDnsClient::GetAddressSorter() { |
220 return address_sorter_.get(); | 220 return address_sorter_.get(); |
221 } | 221 } |
222 | 222 |
223 void MockDnsClient::CompleteDelayedTransactions() { | 223 void MockDnsClient::CompleteDelayedTransactions() { |
224 factory_->CompleteDelayedTransactions(); | 224 factory_->CompleteDelayedTransactions(); |
225 } | 225 } |
226 | 226 |
227 } // namespace net | 227 } // namespace net |
OLD | NEW |