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

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

Issue 2348393002: Minor improvements to MockLogDnsTraffic (Closed)
Patch Set: Changes DCHECK to CHECK 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 // // net::DnsClient requires an I/O message loop for async operations.
29 // base::MessageLoopForIO message_loop;
30 //
31 // // Create a mock NetworkChangeNotifier to propagate DNS config.
32 // std::unique_ptr<net::NetworkChangeNotifier> net_change_notifier(
33 // net::NetworkChangeNotifier::CreateMock());
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 TXT response containing |txt_strings|. 65 // Such a request will receive a DNS TXT response containing |txt_strings|.
98 void ExpectRequestAndResponse( 66 void ExpectRequestAndResponse(
99 base::StringPiece qname, 67 base::StringPiece qname,
100 const std::vector<base::StringPiece>& txt_strings); 68 const std::vector<base::StringPiece>& txt_strings);
101 // Expect a CT DNS request for the domain |qname|. 69 // Expect a CT DNS request for the domain |qname|.
102 // Such a request will receive a DNS response containing |leaf_index|. 70 // Such a request will receive a DNS response containing |leaf_index|.
103 // A description of such a request and response can be seen here: 71 // A description of such a request and response can be seen here:
104 // https://github.com/google/certificate-transparency-rfcs/blob/c8844de6bd0b5d 3d16bac79865e6edef533d760b/dns/draft-ct-over-dns.md#hash-query-hashquery 72 // https://github.com/google/certificate-transparency-rfcs/blob/c8844de6bd0b5d 3d16bac79865e6edef533d760b/dns/draft-ct-over-dns.md#hash-query-hashquery
105 void ExpectLeafIndexRequestAndResponse(base::StringPiece qname, 73 void ExpectLeafIndexRequestAndResponse(base::StringPiece qname,
(...skipping 17 matching lines...) Expand all
123 // Sets the DNS config to |config|. 91 // Sets the DNS config to |config|.
124 // Requires that net::NetworkChangeNotifier is initialized first. 92 // Requires that net::NetworkChangeNotifier is initialized first.
125 // The DNS config is propogated to NetworkChangeNotifier::DNSObservers 93 // The DNS config is propogated to NetworkChangeNotifier::DNSObservers
126 // asynchronously. 94 // asynchronously.
127 void SetDnsConfig(const net::DnsConfig& config); 95 void SetDnsConfig(const net::DnsConfig& config);
128 96
129 // Creates a DNS client that uses mock sockets. 97 // Creates a DNS client that uses mock sockets.
130 // It is this DNS client that the expectations will be tested against. 98 // It is this DNS client that the expectations will be tested against.
131 std::unique_ptr<net::DnsClient> CreateDnsClient(); 99 std::unique_ptr<net::DnsClient> CreateDnsClient();
132 100
101 private:
102 // Allows tests to change socket read mode. Only the LogDnsClient tests should
103 // need to do so, to ensure consistent behaviour regardless of mode.
104 friend class LogDnsClientTest;
105
106 class MockSocketData;
107
133 // Sets whether mock reads should complete synchronously or asynchronously. 108 // Sets whether mock reads should complete synchronously or asynchronously.
109 // By default, they complete asynchronously.
134 void SetSocketReadMode(net::IoMode read_mode) { 110 void SetSocketReadMode(net::IoMode read_mode) {
135 socket_read_mode_ = read_mode; 111 socket_read_mode_ = read_mode;
136 } 112 }
137 113
138 private:
139 // Constructs MockSocketData from |args| and adds it to |socket_factory_|. 114 // Constructs MockSocketData from |args| and adds it to |socket_factory_|.
140 template <typename... Args> 115 template <typename... Args>
141 void EmplaceMockSocketData(Args&&... args); 116 void EmplaceMockSocketData(Args&&... args);
142 117
143 // Sets the timeout used for DNS queries. 118 // Sets the timeout used for DNS queries.
144 // Requires that net::NetworkChangeNotifier is initialized first. 119 // Requires that net::NetworkChangeNotifier is initialized first.
145 // The new timeout is propogated to NetworkChangeNotifier::DNSObservers 120 // The new timeout is propogated to NetworkChangeNotifier::DNSObservers
146 // asynchronously. 121 // asynchronously.
147 void SetDnsTimeout(const base::TimeDelta& timeout); 122 void SetDnsTimeout(const base::TimeDelta& timeout);
148 123
149 // One MockSocketData for each socket that is created. This corresponds to one 124 // One MockSocketData for each socket that is created. This corresponds to one
150 // for each DNS request sent. 125 // for each DNS request sent.
151 std::vector<std::unique_ptr<internal::MockSocketData>> mock_socket_data_; 126 std::vector<std::unique_ptr<MockSocketData>> mock_socket_data_;
152 // Provides as many mock sockets as there are entries in |mock_socket_data_|. 127 // Provides as many mock sockets as there are entries in |mock_socket_data_|.
153 net::MockClientSocketFactory socket_factory_; 128 net::MockClientSocketFactory socket_factory_;
154 // Controls whether mock socket reads are asynchronous. 129 // Controls whether mock socket reads are asynchronous.
155 net::IoMode socket_read_mode_; 130 net::IoMode socket_read_mode_;
156 131
157 DISALLOW_COPY_AND_ASSIGN(MockLogDnsTraffic); 132 DISALLOW_COPY_AND_ASSIGN(MockLogDnsTraffic);
158 }; 133 };
159 134
160 } // namespace certificate_transparency 135 } // namespace certificate_transparency
161 136
162 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_ 137 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698