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

Side by Side Diff: components/certificate_transparency/mock_log_dns_traffic.cc

Issue 2375693002: LogDnsClient now rejects responses unless they contain exactly one TXT RDATA string (Closed)
Patch Set: Adds missing #include <numeric> Created 4 years, 2 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
« no previous file with comments | « components/certificate_transparency/mock_log_dns_traffic.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "components/certificate_transparency/mock_log_dns_traffic.h" 5 #include "components/certificate_transparency/mock_log_dns_traffic.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <numeric> 8 #include <numeric>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/big_endian.h" 11 #include "base/big_endian.h"
12 #include "base/strings/string_number_conversions.h"
12 #include "base/sys_byteorder.h" 13 #include "base/sys_byteorder.h"
13 #include "base/test/test_timeouts.h" 14 #include "base/test/test_timeouts.h"
14 #include "net/dns/dns_client.h" 15 #include "net/dns/dns_client.h"
15 #include "net/dns/dns_protocol.h" 16 #include "net/dns/dns_protocol.h"
16 #include "net/dns/dns_util.h" 17 #include "net/dns/dns_util.h"
17 #include "net/socket/socket_test_util.h" 18 #include "net/socket/socket_test_util.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 20
20 namespace certificate_transparency { 21 namespace certificate_transparency {
21 22
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 EmplaceMockSocketData(CreateDnsTxtRequest(qname), net_error); 187 EmplaceMockSocketData(CreateDnsTxtRequest(qname), net_error);
187 } 188 }
188 189
189 void MockLogDnsTraffic::ExpectRequestAndTimeout(base::StringPiece qname) { 190 void MockLogDnsTraffic::ExpectRequestAndTimeout(base::StringPiece qname) {
190 EmplaceMockSocketData(CreateDnsTxtRequest(qname)); 191 EmplaceMockSocketData(CreateDnsTxtRequest(qname));
191 192
192 // Speed up timeout tests. 193 // Speed up timeout tests.
193 SetDnsTimeout(TestTimeouts::tiny_timeout()); 194 SetDnsTimeout(TestTimeouts::tiny_timeout());
194 } 195 }
195 196
197 void MockLogDnsTraffic::ExpectRequestAndResponse(
198 base::StringPiece qname,
199 const std::vector<base::StringPiece>& txt_strings) {
200 std::string answer;
201 for (base::StringPiece str : txt_strings) {
202 // The size of the string must precede it. The size must fit into 1 byte.
203 CHECK_LE(str.size(), 0xFFul);
Ryan Sleevi 2016/10/03 23:20:12 nit: The 0xFFul is weird to see here. C++11 will a
Rob Percival 2016/10/04 11:46:47 Would you recommend instead making all of the meth
Rob Percival 2016/10/05 09:27:06 Here's an implementation of that: https://coderevi
204 answer.insert(answer.end(), static_cast<char>(str.size()));
Ryan Sleevi 2016/10/03 23:20:12 Why not use the safe-numerics here, if you want th
Rob Percival 2016/10/04 11:46:47 It seemed like overkill for a scenario that should
Rob Percival 2016/10/04 15:11:05 Done.
205 str.AppendToString(&answer);
206 }
207
208 std::vector<char> request = CreateDnsTxtRequest(qname);
209 EmplaceMockSocketData(request, CreateDnsTxtResponse(request, answer));
210 }
211
196 void MockLogDnsTraffic::ExpectLeafIndexRequestAndResponse( 212 void MockLogDnsTraffic::ExpectLeafIndexRequestAndResponse(
197 base::StringPiece qname, 213 base::StringPiece qname,
198 base::StringPiece leaf_index) { 214 uint64_t leaf_index) {
199 // Prepend size to leaf_index to create the query answer (rdata) 215 ExpectRequestAndResponse(qname, { base::Uint64ToString(leaf_index) });
200 ASSERT_LE(leaf_index.size(), 0xFFul); // size must fit into a single byte
201 std::string answer = leaf_index.as_string();
202 answer.insert(answer.begin(), static_cast<char>(leaf_index.size()));
203
204 ExpectRequestAndResponse(qname, answer);
205 } 216 }
206 217
207 void MockLogDnsTraffic::ExpectAuditProofRequestAndResponse( 218 void MockLogDnsTraffic::ExpectAuditProofRequestAndResponse(
208 base::StringPiece qname, 219 base::StringPiece qname,
209 std::vector<std::string>::const_iterator audit_path_start, 220 std::vector<std::string>::const_iterator audit_path_start,
210 std::vector<std::string>::const_iterator audit_path_end) { 221 std::vector<std::string>::const_iterator audit_path_end) {
211 // Join nodes in the audit path into a single string. 222 // Join nodes in the audit path into a single string.
212 std::string proof = 223 std::string proof =
213 std::accumulate(audit_path_start, audit_path_end, std::string()); 224 std::accumulate(audit_path_start, audit_path_end, std::string());
214 225
215 // Prepend size to proof to create the query answer (rdata) 226 ExpectRequestAndResponse(qname, { proof });
216 ASSERT_LE(proof.size(), 0xFFul); // size must fit into a single byte
217 proof.insert(proof.begin(), static_cast<char>(proof.size()));
218
219 ExpectRequestAndResponse(qname, proof);
220 } 227 }
221 228
222 void MockLogDnsTraffic::InitializeDnsConfig() { 229 void MockLogDnsTraffic::InitializeDnsConfig() {
223 net::DnsConfig dns_config; 230 net::DnsConfig dns_config;
224 // Use an invalid nameserver address. This prevents the tests accidentally 231 // Use an invalid nameserver address. This prevents the tests accidentally
225 // sending real DNS queries. The mock sockets don't care that the address 232 // sending real DNS queries. The mock sockets don't care that the address
226 // is invalid. 233 // is invalid.
227 dns_config.nameservers.push_back(net::IPEndPoint()); 234 dns_config.nameservers.push_back(net::IPEndPoint());
228 // Don't attempt retransmissions - just fail. 235 // Don't attempt retransmissions - just fail.
229 dns_config.attempts = 1; 236 dns_config.attempts = 1;
230 // This ensures timeouts are long enough for memory tests. 237 // This ensures timeouts are long enough for memory tests.
231 dns_config.timeout = TestTimeouts::action_timeout(); 238 dns_config.timeout = TestTimeouts::action_timeout();
232 // Simplify testing - don't require random numbers for the source port. 239 // Simplify testing - don't require random numbers for the source port.
233 // This means our FakeRandInt function should only be called to get query 240 // This means our FakeRandInt function should only be called to get query
234 // IDs. 241 // IDs.
235 dns_config.randomize_ports = false; 242 dns_config.randomize_ports = false;
236 243
237 DnsChangeNotifier::SetInitialDnsConfig(dns_config); 244 DnsChangeNotifier::SetInitialDnsConfig(dns_config);
238 } 245 }
239 246
240 void MockLogDnsTraffic::SetDnsConfig(const net::DnsConfig& config) { 247 void MockLogDnsTraffic::SetDnsConfig(const net::DnsConfig& config) {
241 DnsChangeNotifier::SetDnsConfig(config); 248 DnsChangeNotifier::SetDnsConfig(config);
242 } 249 }
243 250
244 std::unique_ptr<net::DnsClient> MockLogDnsTraffic::CreateDnsClient() { 251 std::unique_ptr<net::DnsClient> MockLogDnsTraffic::CreateDnsClient() {
245 return net::DnsClient::CreateClientForTesting(nullptr, &socket_factory_, 252 return net::DnsClient::CreateClientForTesting(nullptr, &socket_factory_,
246 base::Bind(&FakeRandInt)); 253 base::Bind(&FakeRandInt));
247 } 254 }
248 255
249 void MockLogDnsTraffic::ExpectRequestAndResponse(base::StringPiece qname,
250 base::StringPiece answer) {
251 std::vector<char> request = CreateDnsTxtRequest(qname);
252 EmplaceMockSocketData(request, CreateDnsTxtResponse(request, answer));
253 }
254
255 template <typename... Args> 256 template <typename... Args>
256 void MockLogDnsTraffic::EmplaceMockSocketData(Args&&... args) { 257 void MockLogDnsTraffic::EmplaceMockSocketData(Args&&... args) {
257 mock_socket_data_.emplace_back( 258 mock_socket_data_.emplace_back(
258 new MockSocketData(std::forward<Args>(args)...)); 259 new MockSocketData(std::forward<Args>(args)...));
259 mock_socket_data_.back()->SetReadMode(socket_read_mode_); 260 mock_socket_data_.back()->SetReadMode(socket_read_mode_);
260 mock_socket_data_.back()->AddToFactory(&socket_factory_); 261 mock_socket_data_.back()->AddToFactory(&socket_factory_);
261 } 262 }
262 263
263 void MockLogDnsTraffic::SetDnsTimeout(const base::TimeDelta& timeout) { 264 void MockLogDnsTraffic::SetDnsTimeout(const base::TimeDelta& timeout) {
264 net::DnsConfig dns_config; 265 net::DnsConfig dns_config;
265 DnsChangeNotifier::GetDnsConfig(&dns_config); 266 DnsChangeNotifier::GetDnsConfig(&dns_config);
266 dns_config.timeout = timeout; 267 dns_config.timeout = timeout;
267 DnsChangeNotifier::SetDnsConfig(dns_config); 268 DnsChangeNotifier::SetDnsConfig(dns_config);
268 } 269 }
269 270
270 } // namespace certificate_transparency 271 } // namespace certificate_transparency
OLDNEW
« no previous file with comments | « components/certificate_transparency/mock_log_dns_traffic.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698