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

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

Issue 10855163: Revert 151586 - [net/dns] Resolve AF_UNSPEC on dual-stacked systems. Sort addresses according to RF… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 4 months 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 | Annotate | Revision Log
« no previous file with comments | « net/dns/dns_test_util.h ('k') | net/net.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:mergeinfo
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/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/sys_byteorder.h" 12 #include "base/sys_byteorder.h"
13 #include "net/base/big_endian.h" 13 #include "net/base/big_endian.h"
14 #include "net/base/dns_util.h" 14 #include "net/base/dns_util.h"
15 #include "net/base/io_buffer.h" 15 #include "net/base/io_buffer.h"
16 #include "net/base/net_errors.h" 16 #include "net/base/net_errors.h"
17 #include "net/dns/address_sorter.h"
18 #include "net/dns/dns_client.h" 17 #include "net/dns/dns_client.h"
19 #include "net/dns/dns_config_service.h" 18 #include "net/dns/dns_config_service.h"
20 #include "net/dns/dns_protocol.h" 19 #include "net/dns/dns_protocol.h"
21 #include "net/dns/dns_query.h" 20 #include "net/dns/dns_query.h"
22 #include "net/dns/dns_response.h" 21 #include "net/dns/dns_response.h"
23 #include "net/dns/dns_transaction.h" 22 #include "net/dns/dns_transaction.h"
24 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
25 24
26 namespace net { 25 namespace net {
27 namespace { 26 namespace {
28 27
29 // A DnsTransaction which uses MockDnsClientRuleList to determine the response. 28 // A DnsTransaction which responds with loopback to all queries starting with
29 // "ok", fails synchronously on all queries starting with "er", and NXDOMAIN to
30 // all others.
30 class MockTransaction : public DnsTransaction, 31 class MockTransaction : public DnsTransaction,
31 public base::SupportsWeakPtr<MockTransaction> { 32 public base::SupportsWeakPtr<MockTransaction> {
32 public: 33 public:
33 MockTransaction(const MockDnsClientRuleList& rules, 34 MockTransaction(const std::string& hostname,
34 const std::string& hostname,
35 uint16 qtype, 35 uint16 qtype,
36 const DnsTransactionFactory::CallbackType& callback) 36 const DnsTransactionFactory::CallbackType& callback)
37 : result_(MockDnsClientRule::FAIL_SYNC), 37 : hostname_(hostname),
38 hostname_(hostname),
39 qtype_(qtype), 38 qtype_(qtype),
40 callback_(callback), 39 callback_(callback),
41 started_(false) { 40 started_(false) {
42 // Find the relevant rule which matches |qtype| and prefix of |hostname|.
43 for (size_t i = 0; i < rules.size(); ++i) {
44 const std::string& prefix = rules[i].prefix;
45 if ((rules[i].qtype == qtype) &&
46 (hostname.size() >= prefix.size()) &&
47 (hostname.compare(0, prefix.size(), prefix) == 0)) {
48 result_ = rules[i].result;
49 break;
50 }
51 }
52 } 41 }
53 42
54 virtual const std::string& GetHostname() const OVERRIDE { 43 virtual const std::string& GetHostname() const OVERRIDE {
55 return hostname_; 44 return hostname_;
56 } 45 }
57 46
58 virtual uint16 GetType() const OVERRIDE { 47 virtual uint16 GetType() const OVERRIDE {
59 return qtype_; 48 return qtype_;
60 } 49 }
61 50
62 virtual int Start() OVERRIDE { 51 virtual int Start() OVERRIDE {
63 EXPECT_FALSE(started_); 52 EXPECT_FALSE(started_);
64 started_ = true; 53 started_ = true;
65 if (MockDnsClientRule::FAIL_SYNC == result_) 54 if (hostname_.substr(0, 2) == "er")
66 return ERR_NAME_NOT_RESOLVED; 55 return ERR_NAME_NOT_RESOLVED;
67 // Using WeakPtr to cleanly cancel when transaction is destroyed. 56 // Using WeakPtr to cleanly cancel when transaction is destroyed.
68 MessageLoop::current()->PostTask( 57 MessageLoop::current()->PostTask(
69 FROM_HERE, 58 FROM_HERE,
70 base::Bind(&MockTransaction::Finish, AsWeakPtr())); 59 base::Bind(&MockTransaction::Finish, AsWeakPtr()));
71 return ERR_IO_PENDING; 60 return ERR_IO_PENDING;
72 } 61 }
73 62
74 private: 63 private:
75 void Finish() { 64 void Finish() {
76 switch (result_) { 65 if (hostname_.substr(0, 2) == "ok") {
77 case MockDnsClientRule::EMPTY: 66 std::string qname;
78 case MockDnsClientRule::OK: { 67 DNSDomainFromDot(hostname_, &qname);
79 std::string qname; 68 DnsQuery query(0, qname, qtype_);
80 DNSDomainFromDot(hostname_, &qname);
81 DnsQuery query(0, qname, qtype_);
82 69
83 DnsResponse response; 70 DnsResponse response;
84 char* buffer = response.io_buffer()->data(); 71 char* buffer = response.io_buffer()->data();
85 int nbytes = query.io_buffer()->size(); 72 int nbytes = query.io_buffer()->size();
86 memcpy(buffer, query.io_buffer()->data(), nbytes); 73 memcpy(buffer, query.io_buffer()->data(), nbytes);
87 dns_protocol::Header* header =
88 reinterpret_cast<dns_protocol::Header*>(buffer);
89 header->flags |= dns_protocol::kFlagResponse;
90 74
91 if (MockDnsClientRule::OK == result_) { 75 const uint16 kPointerToQueryName =
92 const uint16 kPointerToQueryName = 76 static_cast<uint16>(0xc000 | sizeof(net::dns_protocol::Header));
93 static_cast<uint16>(0xc000 | sizeof(*header));
94 77
95 const uint32 kTTL = 86400; // One day. 78 const uint32 kTTL = 86400; // One day.
96 79
97 // Size of RDATA which is a IPv4 or IPv6 address. 80 // Size of RDATA which is a IPv4 or IPv6 address.
98 size_t rdata_size = qtype_ == net::dns_protocol::kTypeA ? 81 size_t rdata_size = qtype_ == net::dns_protocol::kTypeA ?
99 net::kIPv4AddressSize : net::kIPv6AddressSize; 82 net::kIPv4AddressSize : net::kIPv6AddressSize;
100 83
101 // 12 is the sum of sizes of the compressed name reference, TYPE, 84 // 12 is the sum of sizes of the compressed name reference, TYPE,
102 // CLASS, TTL and RDLENGTH. 85 // CLASS, TTL and RDLENGTH.
103 size_t answer_size = 12 + rdata_size; 86 size_t answer_size = 12 + rdata_size;
104 87
105 // Write answer with loopback IP address. 88 // Write answer with loopback IP address.
106 header->ancount = base::HostToNet16(1); 89 reinterpret_cast<dns_protocol::Header*>(buffer)->ancount =
107 BigEndianWriter writer(buffer + nbytes, answer_size); 90 base::HostToNet16(1);
108 writer.WriteU16(kPointerToQueryName); 91 BigEndianWriter writer(buffer + nbytes, answer_size);
109 writer.WriteU16(qtype_); 92 writer.WriteU16(kPointerToQueryName);
110 writer.WriteU16(net::dns_protocol::kClassIN); 93 writer.WriteU16(qtype_);
111 writer.WriteU32(kTTL); 94 writer.WriteU16(net::dns_protocol::kClassIN);
112 writer.WriteU16(rdata_size); 95 writer.WriteU32(kTTL);
113 if (qtype_ == net::dns_protocol::kTypeA) { 96 writer.WriteU16(rdata_size);
114 char kIPv4Loopback[] = { 0x7f, 0, 0, 1 }; 97 if (qtype_ == net::dns_protocol::kTypeA) {
115 writer.WriteBytes(kIPv4Loopback, sizeof(kIPv4Loopback)); 98 char kIPv4Loopback[] = { 0x7f, 0, 0, 1 };
116 } else { 99 writer.WriteBytes(kIPv4Loopback, sizeof(kIPv4Loopback));
117 char kIPv6Loopback[] = { 0, 0, 0, 0, 0, 0, 0, 0, 100 } else {
118 0, 0, 0, 0, 0, 0, 0, 1 }; 101 char kIPv6Loopback[] = { 0, 0, 0, 0, 0, 0, 0, 0,
119 writer.WriteBytes(kIPv6Loopback, sizeof(kIPv6Loopback)); 102 0, 0, 0, 0, 0, 0, 0, 1 };
120 } 103 writer.WriteBytes(kIPv6Loopback, sizeof(kIPv6Loopback));
121 nbytes += answer_size; 104 }
122 } 105
123 EXPECT_TRUE(response.InitParse(nbytes, query)); 106 EXPECT_TRUE(response.InitParse(nbytes + answer_size, query));
124 callback_.Run(this, OK, &response); 107 callback_.Run(this, OK, &response);
125 } break; 108 } else {
126 case MockDnsClientRule::FAIL_ASYNC: 109 callback_.Run(this, ERR_NAME_NOT_RESOLVED, NULL);
127 callback_.Run(this, ERR_NAME_NOT_RESOLVED, NULL);
128 break;
129 default:
130 NOTREACHED();
131 break;
132 } 110 }
133 } 111 }
134 112
135 MockDnsClientRule::Result result_;
136 const std::string hostname_; 113 const std::string hostname_;
137 const uint16 qtype_; 114 const uint16 qtype_;
138 DnsTransactionFactory::CallbackType callback_; 115 DnsTransactionFactory::CallbackType callback_;
139 bool started_; 116 bool started_;
140 }; 117 };
141 118
142 119
143 // A DnsTransactionFactory which creates MockTransaction. 120 // A DnsTransactionFactory which creates MockTransaction.
144 class MockTransactionFactory : public DnsTransactionFactory { 121 class MockTransactionFactory : public DnsTransactionFactory {
145 public: 122 public:
146 explicit MockTransactionFactory(const MockDnsClientRuleList& rules) 123 MockTransactionFactory() {}
147 : rules_(rules) {}
148 virtual ~MockTransactionFactory() {} 124 virtual ~MockTransactionFactory() {}
149 125
150 virtual scoped_ptr<DnsTransaction> CreateTransaction( 126 virtual scoped_ptr<DnsTransaction> CreateTransaction(
151 const std::string& hostname, 127 const std::string& hostname,
152 uint16 qtype, 128 uint16 qtype,
153 const DnsTransactionFactory::CallbackType& callback, 129 const DnsTransactionFactory::CallbackType& callback,
154 const BoundNetLog&) OVERRIDE { 130 const BoundNetLog&) OVERRIDE {
155 return scoped_ptr<DnsTransaction>( 131 return scoped_ptr<DnsTransaction>(
156 new MockTransaction(rules_, hostname, qtype, callback)); 132 new MockTransaction(hostname, qtype, callback));
157 }
158
159 private:
160 MockDnsClientRuleList rules_;
161 };
162
163 class MockAddressSorter : public AddressSorter {
164 public:
165 virtual ~MockAddressSorter() {}
166 virtual void Sort(const AddressList& list,
167 const CallbackType& callback) const OVERRIDE {
168 // Do nothing.
169 callback.Run(true, list);
170 } 133 }
171 }; 134 };
172 135
173 // MockDnsClient provides MockTransactionFactory. 136 // MockDnsClient provides MockTransactionFactory.
174 class MockDnsClient : public DnsClient { 137 class MockDnsClient : public DnsClient {
175 public: 138 public:
176 MockDnsClient(const DnsConfig& config, 139 explicit MockDnsClient(const DnsConfig& config) : config_(config) {}
177 const MockDnsClientRuleList& rules)
178 : config_(config), factory_(rules) {}
179 virtual ~MockDnsClient() {} 140 virtual ~MockDnsClient() {}
180 141
181 virtual void SetConfig(const DnsConfig& config) OVERRIDE { 142 virtual void SetConfig(const DnsConfig& config) OVERRIDE {
182 config_ = config; 143 config_ = config;
183 } 144 }
184 145
185 virtual const DnsConfig* GetConfig() const OVERRIDE { 146 virtual const DnsConfig* GetConfig() const OVERRIDE {
186 return config_.IsValid() ? &config_ : NULL; 147 return config_.IsValid() ? &config_ : NULL;
187 } 148 }
188 149
189 virtual DnsTransactionFactory* GetTransactionFactory() OVERRIDE { 150 virtual DnsTransactionFactory* GetTransactionFactory() OVERRIDE {
190 return config_.IsValid() ? &factory_ : NULL; 151 return config_.IsValid() ? &factory_ : NULL;
191 } 152 }
192 153
193 virtual AddressSorter* GetAddressSorter() OVERRIDE {
194 return &address_sorter_;
195 }
196
197 private: 154 private:
198 DnsConfig config_; 155 DnsConfig config_;
199 MockTransactionFactory factory_; 156 MockTransactionFactory factory_;
200 MockAddressSorter address_sorter_;
201 }; 157 };
202 158
203 } // namespace 159 } // namespace
204 160
205 // static 161 // static
206 scoped_ptr<DnsClient> CreateMockDnsClient(const DnsConfig& config, 162 scoped_ptr<DnsClient> CreateMockDnsClient(const DnsConfig& config) {
207 const MockDnsClientRuleList& rules) { 163 return scoped_ptr<DnsClient>(new MockDnsClient(config));
208 return scoped_ptr<DnsClient>(new MockDnsClient(config, rules));
209 } 164 }
210 165
211 MockDnsConfigService::~MockDnsConfigService() { 166 MockDnsConfigService::~MockDnsConfigService() {
212 } 167 }
213 168
214 void MockDnsConfigService::OnDNSChanged(unsigned detail) { 169 void MockDnsConfigService::OnDNSChanged(unsigned detail) {
215 } 170 }
216 171
217 void MockDnsConfigService::ChangeConfig(const DnsConfig& config) { 172 void MockDnsConfigService::ChangeConfig(const DnsConfig& config) {
218 DnsConfigService::OnConfigRead(config); 173 DnsConfigService::OnConfigRead(config);
219 } 174 }
220 175
221 void MockDnsConfigService::ChangeHosts(const DnsHosts& hosts) { 176 void MockDnsConfigService::ChangeHosts(const DnsHosts& hosts) {
222 DnsConfigService::OnHostsRead(hosts); 177 DnsConfigService::OnHostsRead(hosts);
223 } 178 }
224 179
225 } // namespace net 180 } // namespace net
OLDNEW
« no previous file with comments | « net/dns/dns_test_util.h ('k') | net/net.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698