OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_ |
| 6 #define COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <memory> |
| 11 #include <string> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/macros.h" |
| 15 #include "base/strings/string_piece.h" |
| 16 #include "net/dns/dns_client.h" |
| 17 #include "net/dns/dns_config_service.h" |
| 18 #include "net/socket/socket_test_util.h" |
| 19 |
| 20 namespace certificate_transparency { |
| 21 |
| 22 namespace internal { |
| 23 |
| 24 // A container for all of the data we need to keep alive for a mock socket. |
| 25 // This is useful because Mock{Read,Write}, SequencedSocketData and |
| 26 // MockClientSocketFactory all do not take ownership of or copy their arguments, |
| 27 // so we have to manage the lifetime of those arguments ourselves. Wrapping all |
| 28 // of that up in a single class simplifies this. |
| 29 // This cannot be forward declared because MockLogDnsTraffic has a |
| 30 // vector<unique_ptr<MockSocketData>> member, which requires MockSocketData be |
| 31 // defined. |
| 32 class MockSocketData { |
| 33 public: |
| 34 // A socket that expects one write and one read operation. |
| 35 MockSocketData(const std::vector<char>& write, const std::vector<char>& read); |
| 36 // A socket that expects one write and a read error. |
| 37 MockSocketData(const std::vector<char>& write, int net_error); |
| 38 // A socket that expects one write and no response. |
| 39 explicit MockSocketData(const std::vector<char>& write); |
| 40 |
| 41 ~MockSocketData(); |
| 42 |
| 43 void SetWriteMode(net::IoMode mode) { expected_write_.mode = mode; } |
| 44 void SetReadMode(net::IoMode mode) { expected_reads_[0].mode = mode; } |
| 45 |
| 46 void AddToFactory(net::MockClientSocketFactory* socket_factory); |
| 47 |
| 48 private: |
| 49 // Prevents read overruns and makes a socket timeout the default behaviour. |
| 50 static const net::MockRead no_more_data_; |
| 51 |
| 52 // This class only supports one write and one read, so just need to store one |
| 53 // payload each. |
| 54 const std::vector<char> expected_write_payload_; |
| 55 const std::vector<char> expected_read_payload_; |
| 56 // Encapsulates the data that is expected to be written to a socket. |
| 57 net::MockWrite expected_write_; |
| 58 // Encapsulates the data/error that should be returned when reading from a |
| 59 // socket. The second "expected" read is always |no_more_data_|, which |
| 60 // causes the socket read to hang until it times out. This results in better |
| 61 // test failure messages (rather than a CHECK-fail due to a socket read |
| 62 // overrunning the MockRead array) and behaviour more like a real socket when |
| 63 // an unexpected second socket read occurs. |
| 64 net::MockRead expected_reads_[2]; |
| 65 // Holds pointers to |expected_write_| and |expected_reads_|. This is what is |
| 66 // added to net::MockClientSocketFactory to prepare a mock socket. |
| 67 net::SequencedSocketData socket_data_; |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(MockSocketData); |
| 70 }; |
| 71 |
| 72 } // namespace internal |
| 73 |
| 74 // Mocks DNS requests and responses for a Certificate Transparency (CT) log. |
| 75 // This is implemented using mock sockets. Call the CreateDnsClient() method to |
| 76 // get a net::DnsClient wired up to these mock sockets. |
| 77 // The Expect*() methods must be called from within a GTest test case. |
| 78 class MockLogDnsTraffic { |
| 79 public: |
| 80 MockLogDnsTraffic(); |
| 81 ~MockLogDnsTraffic(); |
| 82 |
| 83 // Expect a CT DNS request for the domain |qname|. |
| 84 // Such a request will receive a DNS response indicating that the error |
| 85 // specified by |rcode| occurred. See RFC1035, Section 4.1.1 for |rcode| |
| 86 // values. |
| 87 void ExpectRequestAndErrorResponse(base::StringPiece qname, uint8_t rcode); |
| 88 // Expect a CT DNS request for the domain |qname|. |
| 89 // Such a request will trigger a socket error of type |net_error|. |
| 90 // |net_error| can be any net:Error value. |
| 91 void ExpectRequestAndSocketError(base::StringPiece qname, int net_error); |
| 92 // Expect a CT DNS request for the domain |qname|. |
| 93 // Such a request will timeout. |
| 94 // This will reduce the DNS timeout to minimize test duration. |
| 95 void ExpectRequestAndTimeout(base::StringPiece qname); |
| 96 // Expect a CT DNS request for the domain |qname|. |
| 97 // Such a request will receive a DNS response containing |leaf_index|. |
| 98 // A description of such a request and response can be seen here: |
| 99 // https://github.com/google/certificate-transparency-rfcs/blob/c8844de6bd0b5d
3d16bac79865e6edef533d760b/dns/draft-ct-over-dns.md#hash-query-hashquery |
| 100 void ExpectLeafIndexRequestAndResponse(base::StringPiece qname, |
| 101 base::StringPiece leaf_index); |
| 102 // Expect a CT DNS request for the domain |qname|. |
| 103 // Such a request will receive a DNS response containing the inclusion proof |
| 104 // nodes between |audit_path_start| and |audit_path_end|. |
| 105 // A description of such a request and response can be seen here: |
| 106 // https://github.com/google/certificate-transparency-rfcs/blob/c8844de6bd0b5d
3d16bac79865e6edef533d760b/dns/draft-ct-over-dns.md#tree-query-treequery |
| 107 void ExpectAuditProofRequestAndResponse( |
| 108 base::StringPiece qname, |
| 109 std::vector<std::string>::const_iterator audit_path_start, |
| 110 std::vector<std::string>::const_iterator audit_path_end); |
| 111 |
| 112 // Sets the initial DNS config appropriate for testing. |
| 113 // Requires that net::NetworkChangeNotifier is initialized first. |
| 114 // The DNS config is propogated to NetworkChangeNotifier::DNSObservers |
| 115 // asynchronously. |
| 116 void InitializeDnsConfig(); |
| 117 |
| 118 // Sets the DNS config to |config|. |
| 119 // Requires that net::NetworkChangeNotifier is initialized first. |
| 120 // The DNS config is propogated to NetworkChangeNotifier::DNSObservers |
| 121 // asynchronously. |
| 122 void SetDnsConfig(const net::DnsConfig& config); |
| 123 |
| 124 // Creates a DNS client that uses mock sockets. |
| 125 // It is this DNS client that the expectations will be tested against. |
| 126 std::unique_ptr<net::DnsClient> CreateDnsClient(); |
| 127 |
| 128 // Sets whether mock reads should complete synchronously or asynchronously. |
| 129 void SetSocketReadMode(net::IoMode read_mode) { |
| 130 socket_read_mode_ = read_mode; |
| 131 } |
| 132 |
| 133 private: |
| 134 // Expect A CT DNS request for the domain |qname|. |
| 135 // Such a request will receive a DNS response containing |answer|. |
| 136 void ExpectRequestAndResponse(base::StringPiece qname, |
| 137 base::StringPiece answer); |
| 138 |
| 139 // Constructs MockSocketData from |args| and adds it to |socket_factory_|. |
| 140 template <typename... Args> |
| 141 void EmplaceMockSocketData(Args&&... args); |
| 142 |
| 143 // Sets the timeout used for DNS queries. |
| 144 // Requires that net::NetworkChangeNotifier is initialized first. |
| 145 // The new timeout is propogated to NetworkChangeNotifier::DNSObservers |
| 146 // asynchronously. |
| 147 void SetDnsTimeout(const base::TimeDelta& timeout); |
| 148 |
| 149 // One MockSocketData for each socket that is created. This corresponds to one |
| 150 // for each DNS request sent. |
| 151 std::vector<std::unique_ptr<internal::MockSocketData>> mock_socket_data_; |
| 152 // Provides as many mock sockets as there are entries in |mock_socket_data_|. |
| 153 net::MockClientSocketFactory socket_factory_; |
| 154 // Controls whether mock socket reads are asynchronous. |
| 155 net::IoMode socket_read_mode_; |
| 156 |
| 157 DISALLOW_COPY_AND_ASSIGN(MockLogDnsTraffic); |
| 158 }; |
| 159 |
| 160 } // namespace certificate_transparency |
| 161 |
| 162 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_ |
OLD | NEW |