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 detail { | |
Eran Messeri
2016/07/21 14:47:02
nit: Why not use 'internal' or 'internal_testing'
Rob Percival
2016/07/21 15:27:19
Done.
| |
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 class MockSocketData { | |
30 public: | |
31 // A socket that expects one write and one read operation. | |
32 MockSocketData(const std::vector<char>& write, const std::vector<char>& read); | |
33 // A socket that expects one write and a read error. | |
34 MockSocketData(const std::vector<char>& write, int net_error); | |
35 // A socket that expects one write and no response. | |
36 explicit MockSocketData(const std::vector<char>& write); | |
37 | |
38 ~MockSocketData(); | |
39 | |
40 void SetWriteMode(net::IoMode mode) { expected_write_.mode = mode; } | |
41 void SetReadMode(net::IoMode mode) { expected_reads_[0].mode = mode; } | |
42 | |
43 void AddToFactory(net::MockClientSocketFactory* socket_factory); | |
44 | |
45 private: | |
46 // Prevents read overruns and makes a socket timeout the default behaviour. | |
47 static const net::MockRead eof_; | |
48 | |
49 // This class only supports one write and one read, so just need to store one | |
50 // payload each. | |
51 const std::vector<char> expected_write_payload_; | |
52 const std::vector<char> expected_read_payload_; | |
53 // Encapsulates the data that is expected to be written to a socket. | |
54 net::MockWrite expected_write_; | |
55 // Encapsulates the data/error that should be returned when reading from a | |
56 // socket. The expected response is followed by |eof_|, to catch further, | |
57 // unexpected read attempts. | |
58 net::MockRead expected_reads_[2]; | |
Eran Messeri
2016/07/21 14:47:02
Why the 2 here, if we only support one read at mos
Rob Percival
2016/07/21 15:27:19
There is always a second "expected" read which is
Eran Messeri
2016/07/21 16:49:21
I noticed the eof_ later when reviewing the implem
Rob Percival
2016/07/21 17:32:21
Done.
| |
59 net::SequencedSocketData socket_data_; | |
60 | |
61 DISALLOW_COPY_AND_ASSIGN(MockSocketData); | |
62 }; | |
63 | |
64 } // namespace detail | |
65 | |
66 // Mocks DNS requests and responses for a Certificate Transparency (CT) log. | |
67 // This is implemented using mock sockets. Call the CreateDnsClient() method to | |
68 // get a net::DnsClient wired up to these mock sockets. | |
69 // The Expect*() methods must be called from within a GTest test case. | |
70 class MockLogDnsTraffic { | |
71 public: | |
72 MockLogDnsTraffic(); | |
73 ~MockLogDnsTraffic(); | |
74 | |
75 // Expect a CT DNS request for the domain |qname|. | |
76 // Such a request will receive a DNS response indicating that the error | |
77 // specified by |rcode| occurred. See RFC1035, Section 4.1.1 for |rcode| | |
78 // values. | |
79 void ExpectRequestAndErrorResponse(base::StringPiece qname, uint8_t rcode); | |
80 // Expect a CT DNS request for the domain |qname|. | |
81 // Such a request will trigger a socket error of type |net_error|. | |
82 // |net_error| can be any net:Error value. | |
83 void ExpectRequestAndSocketError(base::StringPiece qname, int net_error); | |
84 // Expect a CT DNS request for the domain |qname|. | |
85 // Such a request will timeout. | |
86 // This will reduce the DNS timeout to minimize test duration. | |
87 void ExpectRequestAndTimeout(base::StringPiece qname); | |
88 // Expect a CT DNS request for the domain |qname|. | |
89 // Such a request will receive a DNS response containing |leaf_index|. | |
90 // A description of such a request and response can be seen here: | |
91 // https://github.com/google/certificate-transparency-rfcs/blob/c8844de6bd0b5d 3d16bac79865e6edef533d760b/dns/draft-ct-over-dns.md#hash-query-hashquery | |
92 void ExpectLeafIndexRequestAndResponse(base::StringPiece qname, | |
93 base::StringPiece leaf_index); | |
94 // Expect a CT DNS request for the domain |qname|. | |
95 // Such a request will receive a DNS response containing the inclusion proof | |
96 // nodes between |audit_path_start| and |audit_path_end|. | |
97 // A description of such a request and response can be seen here: | |
98 // https://github.com/google/certificate-transparency-rfcs/blob/c8844de6bd0b5d 3d16bac79865e6edef533d760b/dns/draft-ct-over-dns.md#tree-query-treequery | |
99 void ExpectAuditProofRequestAndResponse( | |
100 base::StringPiece qname, | |
101 std::vector<std::string>::const_iterator audit_path_start, | |
102 std::vector<std::string>::const_iterator audit_path_end); | |
103 | |
104 // Creates a DNS client that uses mock sockets. | |
105 // It is this DNS client that the expectations will be tested against. | |
106 std::unique_ptr<net::DnsClient> CreateDnsClient(); | |
107 | |
108 // Sets whether mock reads should complete synchronously or asynchronously. | |
109 void SetSocketReadMode(net::IoMode read_mode) { | |
110 socket_read_mode_ = read_mode; | |
111 } | |
112 | |
113 private: | |
114 // Expect A CT DNS request for the domain |qname|. | |
115 // Such a request will receive a DNS response containing |answer|. | |
116 void ExpectRequestAndResponse(base::StringPiece qname, | |
117 base::StringPiece answer); | |
118 | |
119 // The DNS configuration to pass to the DNS client. | |
120 net::DnsConfig dns_config_; | |
121 // A list of mock sockets for the DNS client to use. | |
122 std::vector<std::unique_ptr<detail::MockSocketData>> mock_socket_data_; | |
123 // A factory for handing out the mock sockets. | |
124 net::MockClientSocketFactory socket_factory_; | |
125 // Controls whether mock socket reads are asynchronous. | |
126 net::IoMode socket_read_mode_; | |
127 | |
128 DISALLOW_COPY_AND_ASSIGN(MockLogDnsTraffic); | |
129 }; | |
130 | |
131 } // namespace certificate_transparency | |
132 | |
133 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_ | |
OLD | NEW |