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

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

Issue 2348393002: Minor improvements to MockLogDnsTraffic (Closed)
Patch Set: Document use of MessageLoop with MockLogDnsTraffic Created 4 years, 3 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 | « no previous file | 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 #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 { 22 namespace internal {
23 23
24 // A container for all of the data we need to keep alive for a mock socket. 24 // A container for all of the data we need to keep alive for a mock socket.
Ryan Sleevi 2016/09/23 21:29:47 https://groups.google.com/a/chromium.org/d/topic/c
Rob Percival 2016/10/03 13:35:45 Done.
25 // This is useful because Mock{Read,Write}, SequencedSocketData and 25 // This is useful because Mock{Read,Write}, SequencedSocketData and
26 // MockClientSocketFactory all do not take ownership of or copy their arguments, 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 27 // so we have to manage the lifetime of those arguments ourselves. Wrapping all
Ryan Sleevi 2016/09/23 21:29:47 // so it is necessary to manage the lifetime of th
Rob Percival 2016/10/03 13:35:45 Done.
28 // of that up in a single class simplifies this. 28 // of that up in a single class simplifies this.
29 // This cannot be forward declared because MockLogDnsTraffic has a 29 // This cannot be forward declared because MockLogDnsTraffic has a
30 // vector<unique_ptr<MockSocketData>> member, which requires MockSocketData be 30 // vector<unique_ptr<MockSocketData>> member, which requires MockSocketData be
Ryan Sleevi 2016/09/23 21:29:47 Both vector and unique_ptr should allow you to for
Rob Percival 2016/10/03 13:35:45 Both vector and unique_ptr are templates, so they
Rob Percival 2016/10/03 13:50:19 Never mind, fixed.
31 // defined. 31 // defined.
32 class MockSocketData { 32 class MockSocketData {
33 public: 33 public:
34 // A socket that expects one write and one read operation. 34 // A socket that expects one write and one read operation.
35 MockSocketData(const std::vector<char>& write, const std::vector<char>& read); 35 MockSocketData(const std::vector<char>& write, const std::vector<char>& read);
Ryan Sleevi 2016/09/23 21:29:47 Isn't base::StringPiece the preferred data structu
Rob Percival 2016/10/03 13:35:45 One of the main purposes of this class is to manag
36 // A socket that expects one write and a read error. 36 // A socket that expects one write and a read error.
37 MockSocketData(const std::vector<char>& write, int net_error); 37 MockSocketData(const std::vector<char>& write, int net_error);
38 // A socket that expects one write and no response. 38 // A socket that expects one write and no response.
39 explicit MockSocketData(const std::vector<char>& write); 39 explicit MockSocketData(const std::vector<char>& write);
40 40
41 ~MockSocketData(); 41 ~MockSocketData();
42 42
43 void SetWriteMode(net::IoMode mode) { expected_write_.mode = mode; } 43 void SetWriteMode(net::IoMode mode) { expected_write_.mode = mode; }
44 void SetReadMode(net::IoMode mode) { expected_reads_[0].mode = mode; } 44 void SetReadMode(net::IoMode mode) { expected_reads_[0].mode = mode; }
45 45
46 void AddToFactory(net::MockClientSocketFactory* socket_factory); 46 void AddToFactory(net::MockClientSocketFactory* socket_factory);
47 47
48 private: 48 private:
49 // Prevents read overruns and makes a socket timeout the default behaviour. 49 // Prevents read overruns and makes a socket timeout the default behaviour.
50 static const net::MockRead no_more_data_; 50 static const net::MockRead no_more_data_;
Ryan Sleevi 2016/09/23 21:29:47 class-level private statics do not provide any adv
Rob Percival 2016/10/03 13:35:45 Done.
51 51
52 // This class only supports one write and one read, so just need to store one 52 // This class only supports one write and one read, so just need to store one
53 // payload each. 53 // payload each.
54 const std::vector<char> expected_write_payload_; 54 const std::vector<char> expected_write_payload_;
55 const std::vector<char> expected_read_payload_; 55 const std::vector<char> expected_read_payload_;
56 // Encapsulates the data that is expected to be written to a socket. 56 // Encapsulates the data that is expected to be written to a socket.
57 net::MockWrite expected_write_; 57 net::MockWrite expected_write_;
Ryan Sleevi 2016/09/23 21:29:47 Line breaks between 55-56, 57-58, 64-65 will all h
Rob Percival 2016/10/03 13:35:45 Ah I forget that there's so syntax highlighting in
58 // Encapsulates the data/error that should be returned when reading from a 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 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 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 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 62 // overrunning the MockRead array) and behaviour more like a real socket when
63 // an unexpected second socket read occurs. 63 // an unexpected second socket read occurs.
64 net::MockRead expected_reads_[2]; 64 net::MockRead expected_reads_[2];
65 // Holds pointers to |expected_write_| and |expected_reads_|. This is what is 65 // Holds pointers to |expected_write_| and |expected_reads_|. This is what is
66 // added to net::MockClientSocketFactory to prepare a mock socket. 66 // added to net::MockClientSocketFactory to prepare a mock socket.
67 net::SequencedSocketData socket_data_; 67 net::SequencedSocketData socket_data_;
68 68
69 DISALLOW_COPY_AND_ASSIGN(MockSocketData); 69 DISALLOW_COPY_AND_ASSIGN(MockSocketData);
70 }; 70 };
71 71
72 } // namespace internal 72 } // namespace internal
73 73
74 // Mocks DNS requests and responses for a Certificate Transparency (CT) log. 74 // Mocks DNS requests and responses for a Certificate Transparency (CT) log.
75 // This is implemented using mock sockets. Call the CreateDnsClient() method to 75 // This is implemented using mock sockets. Call the CreateDnsClient() method to
76 // get a net::DnsClient wired up to these mock sockets. 76 // get a net::DnsClient wired up to these mock sockets.
77 // The Expect*() methods must be called from within a GTest test case. 77 // The Expect*() methods must be called from within a GTest test case.
78 //
79 // Example Usage:
80 // // Create a mock NetworkChangeNotifier to propogate DNS config.
Ryan Sleevi 2016/09/23 21:29:47 spelling: propagate
Rob Percival 2016/10/03 13:35:45 Done.
81 // std::unique_ptr<net::NetworkChangeNotifier> net_change_notifier =
82 // net::NetworkChangeNotifier::CreateMock();
Ryan Sleevi 2016/09/23 21:29:47 pedantic nit: ( ) over = here since CreateMock ret
Rob Percival 2016/10/03 13:35:45 Done.
83 //
84 // // net::DnsClient requires an I/O message loop for async operations.
85 // base::MessageLoopForIO message_loop;
86 //
87 // MockLogDnsTraffic mock_dns;
Ryan Sleevi 2016/09/23 21:29:47 This is a fake, not a mock.
Rob Percival 2016/10/03 13:35:45 Based on most definitions I can find, it seems mor
88 // mock_dns.InitializeDnsConfig();
89 // // Use the Expect* methods to define expected DNS requests and responses.
Ryan Sleevi 2016/09/23 21:29:47 Naming wise, this feels uncomfortable with //net p
Rob Percival 2016/10/03 13:35:45 It actually does setup test expectations, if you l
90 // mock_dns.ExpectLeafIndexRequestAndResponse(
91 // "D4S6DSV2J743QJZEQMH4UYHEYK7KRQ5JIQOCPMFUHZVJNFGHXACA.hash.ct.test.",
92 // "123456");
93 //
94 // LogDnsClient log_client(mock_dns.CreateDnsClient(), ...);
95 // LogDnsClient::QueryLeafIndexCallback callback = base::Bind(...);
Ryan Sleevi 2016/09/23 21:29:47 As mentioned to Eran in a recent CL, I'm very much
Rob Percival 2016/10/03 13:35:45 Done.
96 // log_client.QueryLeafIndex("ct.test", ..., callback);
97 //
98 // // Pump the message loop. This will lead to |callback| being invoked with
99 // // the result of the leaf index query.
Ryan Sleevi 2016/09/23 21:29:47 You're making an API contract that RunUntilIdle is
Rob Percival 2016/10/03 13:35:45 Done.
100 // base::RunLoop().RunUntilIdle();
78 class MockLogDnsTraffic { 101 class MockLogDnsTraffic {
79 public: 102 public:
80 MockLogDnsTraffic(); 103 MockLogDnsTraffic();
81 ~MockLogDnsTraffic(); 104 ~MockLogDnsTraffic();
82 105
83 // Expect a CT DNS request for the domain |qname|. 106 // Expect a CT DNS request for the domain |qname|.
84 // Such a request will receive a DNS response indicating that the error 107 // 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| 108 // specified by |rcode| occurred. See RFC1035, Section 4.1.1 for |rcode|
86 // values. 109 // values.
87 void ExpectRequestAndErrorResponse(base::StringPiece qname, uint8_t rcode); 110 void ExpectRequestAndErrorResponse(base::StringPiece qname, uint8_t rcode);
88 // Expect a CT DNS request for the domain |qname|. 111 // Expect a CT DNS request for the domain |qname|.
Ryan Sleevi 2016/09/23 21:29:46 Linebreak between 110-111
Rob Percival 2016/10/03 13:35:45 Done.
89 // Such a request will trigger a socket error of type |net_error|. 112 // Such a request will trigger a socket error of type |net_error|.
90 // |net_error| can be any net:Error value. 113 // |net_error| can be any net:Error value.
91 void ExpectRequestAndSocketError(base::StringPiece qname, int net_error); 114 void ExpectRequestAndSocketError(base::StringPiece qname, int net_error);
92 // Expect a CT DNS request for the domain |qname|. 115 // Expect a CT DNS request for the domain |qname|.
Ryan Sleevi 2016/09/23 21:29:47 Linebreak
Rob Percival 2016/10/03 13:35:45 Done.
93 // Such a request will timeout. 116 // Such a request will timeout.
94 // This will reduce the DNS timeout to minimize test duration. 117 // This will reduce the DNS timeout to minimize test duration.
95 void ExpectRequestAndTimeout(base::StringPiece qname); 118 void ExpectRequestAndTimeout(base::StringPiece qname);
Ryan Sleevi 2016/09/23 21:29:47 Line break
Rob Percival 2016/10/03 13:35:45 Done.
96 // Expect a CT DNS request for the domain |qname|. 119 // Expect a CT DNS request for the domain |qname|.
97 // Such a request will receive a DNS response containing |leaf_index|. 120 // Such a request will receive a DNS response containing |leaf_index|.
98 // A description of such a request and response can be seen here: 121 // 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 122 // 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, 123 void ExpectLeafIndexRequestAndResponse(base::StringPiece qname,
101 base::StringPiece leaf_index); 124 base::StringPiece leaf_index);
102 // Expect a CT DNS request for the domain |qname|. 125 // Expect a CT DNS request for the domain |qname|.
Ryan Sleevi 2016/09/23 21:29:46 Line break
Rob Percival 2016/10/03 13:35:45 Done.
103 // Such a request will receive a DNS response containing the inclusion proof 126 // Such a request will receive a DNS response containing the inclusion proof
104 // nodes between |audit_path_start| and |audit_path_end|. 127 // nodes between |audit_path_start| and |audit_path_end|.
105 // A description of such a request and response can be seen here: 128 // 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 129 // https://github.com/google/certificate-transparency-rfcs/blob/c8844de6bd0b5d 3d16bac79865e6edef533d760b/dns/draft-ct-over-dns.md#tree-query-treequery
107 void ExpectAuditProofRequestAndResponse( 130 void ExpectAuditProofRequestAndResponse(
108 base::StringPiece qname, 131 base::StringPiece qname,
109 std::vector<std::string>::const_iterator audit_path_start, 132 std::vector<std::string>::const_iterator audit_path_start,
110 std::vector<std::string>::const_iterator audit_path_end); 133 std::vector<std::string>::const_iterator audit_path_end);
111 134
112 // Sets the initial DNS config appropriate for testing. 135 // Sets the initial DNS config appropriate for testing.
113 // Requires that net::NetworkChangeNotifier is initialized first. 136 // Requires that net::NetworkChangeNotifier is initialized first.
114 // The DNS config is propogated to NetworkChangeNotifier::DNSObservers 137 // The DNS config is propogated to NetworkChangeNotifier::DNSObservers
115 // asynchronously. 138 // asynchronously.
116 void InitializeDnsConfig(); 139 void InitializeDnsConfig();
117 140
118 // Sets the DNS config to |config|. 141 // Sets the DNS config to |config|.
119 // Requires that net::NetworkChangeNotifier is initialized first. 142 // Requires that net::NetworkChangeNotifier is initialized first.
120 // The DNS config is propogated to NetworkChangeNotifier::DNSObservers 143 // The DNS config is propogated to NetworkChangeNotifier::DNSObservers
121 // asynchronously. 144 // asynchronously.
122 void SetDnsConfig(const net::DnsConfig& config); 145 void SetDnsConfig(const net::DnsConfig& config);
123 146
124 // Creates a DNS client that uses mock sockets. 147 // Creates a DNS client that uses mock sockets.
125 // It is this DNS client that the expectations will be tested against. 148 // It is this DNS client that the expectations will be tested against.
126 std::unique_ptr<net::DnsClient> CreateDnsClient(); 149 std::unique_ptr<net::DnsClient> CreateDnsClient();
127 150
128 // Sets whether mock reads should complete synchronously or asynchronously. 151 // Sets whether mock reads should complete synchronously or asynchronously.
152 // By default, they complete asynchronously. The only reason to change this
153 // is to test that LogDnsClient handles both modes in the same way.
Ryan Sleevi 2016/09/23 21:29:47 DESIGN: I would expect that this would be private,
Rob Percival 2016/10/03 13:35:45 Done.
129 void SetSocketReadMode(net::IoMode read_mode) { 154 void SetSocketReadMode(net::IoMode read_mode) {
130 socket_read_mode_ = read_mode; 155 socket_read_mode_ = read_mode;
131 } 156 }
132 157
133 private: 158 private:
134 // Expect A CT DNS request for the domain |qname|. 159 // Expect A CT DNS request for the domain |qname|.
135 // Such a request will receive a DNS response containing |answer|. 160 // Such a request will receive a DNS response containing |answer|.
136 void ExpectRequestAndResponse(base::StringPiece qname, 161 void ExpectRequestAndResponse(base::StringPiece qname,
137 base::StringPiece answer); 162 base::StringPiece answer);
138 163
(...skipping 14 matching lines...) Expand all
153 net::MockClientSocketFactory socket_factory_; 178 net::MockClientSocketFactory socket_factory_;
154 // Controls whether mock socket reads are asynchronous. 179 // Controls whether mock socket reads are asynchronous.
155 net::IoMode socket_read_mode_; 180 net::IoMode socket_read_mode_;
156 181
157 DISALLOW_COPY_AND_ASSIGN(MockLogDnsTraffic); 182 DISALLOW_COPY_AND_ASSIGN(MockLogDnsTraffic);
158 }; 183 };
159 184
160 } // namespace certificate_transparency 185 } // namespace certificate_transparency
161 186
162 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_ 187 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698