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

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

Issue 2348393002: Minor improvements to MockLogDnsTraffic (Closed)
Patch Set: Forward declare MockSocketData 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
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 #ifndef COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_ 5 #ifndef COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_
6 #define COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_ 6 #define COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
11 #include <string> 11 #include <string>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/strings/string_piece.h" 15 #include "base/strings/string_piece.h"
16 #include "net/dns/dns_client.h" 16 #include "net/dns/dns_client.h"
17 #include "net/dns/dns_config_service.h" 17 #include "net/dns/dns_config_service.h"
18 #include "net/socket/socket_test_util.h" 18 #include "net/socket/socket_test_util.h"
19 19
20 namespace certificate_transparency { 20 namespace certificate_transparency {
21 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. 22 // Mocks DNS requests and responses for a Certificate Transparency (CT) log.
75 // This is implemented using mock sockets. Call the CreateDnsClient() method to 23 // This is implemented using mock sockets. Call the CreateDnsClient() method to
76 // get a net::DnsClient wired up to these mock sockets. 24 // get a net::DnsClient wired up to these mock sockets.
77 // The Expect*() methods must be called from within a GTest test case. 25 // The Expect*() methods must be called from within a GTest test case.
26 //
27 // Example Usage:
28 // // Create a mock NetworkChangeNotifier to propagate DNS config.
29 // std::unique_ptr<net::NetworkChangeNotifier> net_change_notifier(
30 // net::NetworkChangeNotifier::CreateMock());
31 //
32 // // net::DnsClient requires an I/O message loop for async operations.
33 // base::MessageLoopForIO message_loop;
34 //
35 // MockLogDnsTraffic mock_dns;
36 // mock_dns.InitializeDnsConfig();
37 // // Use the Expect* methods to define expected DNS requests and responses.
38 // mock_dns.ExpectLeafIndexRequestAndResponse(
39 // "D4S6DSV2J743QJZEQMH4UYHEYK7KRQ5JIQOCPMFUHZVJNFGHXACA.hash.ct.test.",
40 // "123456");
41 //
42 // LogDnsClient log_client(mock_dns.CreateDnsClient(), ...);
43 // log_client.QueryAuditProof("ct.test", ..., base::Bind(...));
78 class MockLogDnsTraffic { 44 class MockLogDnsTraffic {
79 public: 45 public:
80 MockLogDnsTraffic(); 46 MockLogDnsTraffic();
81 ~MockLogDnsTraffic(); 47 ~MockLogDnsTraffic();
82 48
83 // Expect a CT DNS request for the domain |qname|. 49 // Expect a CT DNS request for the domain |qname|.
84 // Such a request will receive a DNS response indicating that the error 50 // 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| 51 // specified by |rcode| occurred. See RFC1035, Section 4.1.1 for |rcode|
86 // values. 52 // values.
87 void ExpectRequestAndErrorResponse(base::StringPiece qname, uint8_t rcode); 53 void ExpectRequestAndErrorResponse(base::StringPiece qname, uint8_t rcode);
54
88 // Expect a CT DNS request for the domain |qname|. 55 // Expect a CT DNS request for the domain |qname|.
89 // Such a request will trigger a socket error of type |net_error|. 56 // Such a request will trigger a socket error of type |error|.
90 // |net_error| can be any net:Error value. 57 void ExpectRequestAndSocketError(base::StringPiece qname, net::Error error);
91 void ExpectRequestAndSocketError(base::StringPiece qname, int net_error); 58
92 // Expect a CT DNS request for the domain |qname|. 59 // Expect a CT DNS request for the domain |qname|.
93 // Such a request will timeout. 60 // Such a request will timeout.
94 // This will reduce the DNS timeout to minimize test duration. 61 // This will reduce the DNS timeout to minimize test duration.
95 void ExpectRequestAndTimeout(base::StringPiece qname); 62 void ExpectRequestAndTimeout(base::StringPiece qname);
63
96 // Expect a CT DNS request for the domain |qname|. 64 // Expect a CT DNS request for the domain |qname|.
97 // Such a request will receive a DNS response containing |leaf_index|. 65 // Such a request will receive a DNS response containing |leaf_index|.
98 // A description of such a request and response can be seen here: 66 // 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 67 // 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, 68 void ExpectLeafIndexRequestAndResponse(base::StringPiece qname,
101 base::StringPiece leaf_index); 69 base::StringPiece leaf_index);
102 // Expect a CT DNS request for the domain |qname|. 70 // Expect a CT DNS request for the domain |qname|.
103 // Such a request will receive a DNS response containing the inclusion proof 71 // Such a request will receive a DNS response containing the inclusion proof
104 // nodes between |audit_path_start| and |audit_path_end|. 72 // nodes between |audit_path_start| and |audit_path_end|.
105 // A description of such a request and response can be seen here: 73 // A description of such a request and response can be seen here:
(...skipping 12 matching lines...) Expand all
118 // Sets the DNS config to |config|. 86 // Sets the DNS config to |config|.
119 // Requires that net::NetworkChangeNotifier is initialized first. 87 // Requires that net::NetworkChangeNotifier is initialized first.
120 // The DNS config is propogated to NetworkChangeNotifier::DNSObservers 88 // The DNS config is propogated to NetworkChangeNotifier::DNSObservers
121 // asynchronously. 89 // asynchronously.
122 void SetDnsConfig(const net::DnsConfig& config); 90 void SetDnsConfig(const net::DnsConfig& config);
123 91
124 // Creates a DNS client that uses mock sockets. 92 // Creates a DNS client that uses mock sockets.
125 // It is this DNS client that the expectations will be tested against. 93 // It is this DNS client that the expectations will be tested against.
126 std::unique_ptr<net::DnsClient> CreateDnsClient(); 94 std::unique_ptr<net::DnsClient> CreateDnsClient();
127 95
96 private:
97 // Allows tests to change socket read mode. Only the LogDnsClient tests should
98 // need to do so, to ensure consistent behaviour regardless of mode.
99 friend class LogDnsClientTest;
100
101 // Forward declaration of a class that encapsulates socket simulation data.
Ryan Sleevi 2016/10/03 23:44:52 I would say this is an unnecessary comment, but I
Rob Percival 2016/10/05 15:42:00 Done.
102 class MockSocketData;
103
128 // Sets whether mock reads should complete synchronously or asynchronously. 104 // Sets whether mock reads should complete synchronously or asynchronously.
105 // By default, they complete asynchronously.
129 void SetSocketReadMode(net::IoMode read_mode) { 106 void SetSocketReadMode(net::IoMode read_mode) {
130 socket_read_mode_ = read_mode; 107 socket_read_mode_ = read_mode;
131 } 108 }
132 109
133 private:
134 // Expect A CT DNS request for the domain |qname|. 110 // Expect A CT DNS request for the domain |qname|.
135 // Such a request will receive a DNS response containing |answer|. 111 // Such a request will receive a DNS response containing |answer|.
136 void ExpectRequestAndResponse(base::StringPiece qname, 112 void ExpectRequestAndResponse(base::StringPiece qname,
137 base::StringPiece answer); 113 base::StringPiece answer);
138 114
139 // Constructs MockSocketData from |args| and adds it to |socket_factory_|. 115 // Constructs MockSocketData from |args| and adds it to |socket_factory_|.
140 template <typename... Args> 116 template <typename... Args>
141 void EmplaceMockSocketData(Args&&... args); 117 void EmplaceMockSocketData(Args&&... args);
142 118
143 // Sets the timeout used for DNS queries. 119 // Sets the timeout used for DNS queries.
144 // Requires that net::NetworkChangeNotifier is initialized first. 120 // Requires that net::NetworkChangeNotifier is initialized first.
145 // The new timeout is propogated to NetworkChangeNotifier::DNSObservers 121 // The new timeout is propogated to NetworkChangeNotifier::DNSObservers
146 // asynchronously. 122 // asynchronously.
147 void SetDnsTimeout(const base::TimeDelta& timeout); 123 void SetDnsTimeout(const base::TimeDelta& timeout);
148 124
149 // One MockSocketData for each socket that is created. This corresponds to one 125 // One MockSocketData for each socket that is created. This corresponds to one
150 // for each DNS request sent. 126 // for each DNS request sent.
151 std::vector<std::unique_ptr<internal::MockSocketData>> mock_socket_data_; 127 std::vector<std::unique_ptr<MockSocketData>> mock_socket_data_;
152 // Provides as many mock sockets as there are entries in |mock_socket_data_|. 128 // Provides as many mock sockets as there are entries in |mock_socket_data_|.
153 net::MockClientSocketFactory socket_factory_; 129 net::MockClientSocketFactory socket_factory_;
154 // Controls whether mock socket reads are asynchronous. 130 // Controls whether mock socket reads are asynchronous.
155 net::IoMode socket_read_mode_; 131 net::IoMode socket_read_mode_;
156 132
157 DISALLOW_COPY_AND_ASSIGN(MockLogDnsTraffic); 133 DISALLOW_COPY_AND_ASSIGN(MockLogDnsTraffic);
158 }; 134 };
159 135
160 } // namespace certificate_transparency 136 } // namespace certificate_transparency
161 137
162 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_ 138 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698