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