| OLD | NEW |
| 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/numerics/safe_conversions.h" |
| 13 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/sys_byteorder.h" | 14 #include "base/sys_byteorder.h" |
| 13 #include "base/test/test_timeouts.h" | 15 #include "base/test/test_timeouts.h" |
| 14 #include "net/dns/dns_client.h" | 16 #include "net/dns/dns_client.h" |
| 15 #include "net/dns/dns_protocol.h" | 17 #include "net/dns/dns_protocol.h" |
| 16 #include "net/dns/dns_util.h" | 18 #include "net/dns/dns_util.h" |
| 17 #include "net/socket/socket_test_util.h" | 19 #include "net/socket/socket_test_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 21 |
| 20 namespace certificate_transparency { | 22 namespace certificate_transparency { |
| 21 | 23 |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 EmplaceMockSocketData(CreateDnsTxtRequest(qname), net_error); | 188 EmplaceMockSocketData(CreateDnsTxtRequest(qname), net_error); |
| 187 } | 189 } |
| 188 | 190 |
| 189 void MockLogDnsTraffic::ExpectRequestAndTimeout(base::StringPiece qname) { | 191 void MockLogDnsTraffic::ExpectRequestAndTimeout(base::StringPiece qname) { |
| 190 EmplaceMockSocketData(CreateDnsTxtRequest(qname)); | 192 EmplaceMockSocketData(CreateDnsTxtRequest(qname)); |
| 191 | 193 |
| 192 // Speed up timeout tests. | 194 // Speed up timeout tests. |
| 193 SetDnsTimeout(TestTimeouts::tiny_timeout()); | 195 SetDnsTimeout(TestTimeouts::tiny_timeout()); |
| 194 } | 196 } |
| 195 | 197 |
| 198 void MockLogDnsTraffic::ExpectRequestAndResponse( |
| 199 base::StringPiece qname, |
| 200 const std::vector<base::StringPiece>& txt_strings) { |
| 201 std::string answer; |
| 202 for (base::StringPiece str : txt_strings) { |
| 203 // The size of the string must precede it. The size must fit into 1 byte. |
| 204 answer.insert(answer.end(), base::checked_cast<uint8_t>(str.size())); |
| 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 |
| OLD | NEW |