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

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

Issue 2415063002: Revert of Minor improvements to MockLogDnsTraffic (Closed)
Patch Set: 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
22 // Mocks DNS requests and responses for a Certificate Transparency (CT) log. 74 // Mocks DNS requests and responses for a Certificate Transparency (CT) log.
23 // This is implemented using mock sockets. Call the CreateDnsClient() method to 75 // This is implemented using mock sockets. Call the CreateDnsClient() method to
24 // get a net::DnsClient wired up to these mock sockets. 76 // get a net::DnsClient wired up to these mock sockets.
25 // 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.
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(...));
44 class MockLogDnsTraffic { 78 class MockLogDnsTraffic {
45 public: 79 public:
46 MockLogDnsTraffic(); 80 MockLogDnsTraffic();
47 ~MockLogDnsTraffic(); 81 ~MockLogDnsTraffic();
48 82
49 // Expect a CT DNS request for the domain |qname|. 83 // Expect a CT DNS request for the domain |qname|.
50 // Such a request will receive a DNS response indicating that the error 84 // Such a request will receive a DNS response indicating that the error
51 // specified by |rcode| occurred. See RFC1035, Section 4.1.1 for |rcode| 85 // specified by |rcode| occurred. See RFC1035, Section 4.1.1 for |rcode|
52 // values. 86 // values.
53 void ExpectRequestAndErrorResponse(base::StringPiece qname, uint8_t rcode); 87 void ExpectRequestAndErrorResponse(base::StringPiece qname, uint8_t rcode);
54
55 // Expect a CT DNS request for the domain |qname|. 88 // Expect a CT DNS request for the domain |qname|.
56 // Such a request will trigger a socket error of type |error|. 89 // Such a request will trigger a socket error of type |net_error|.
57 void ExpectRequestAndSocketError(base::StringPiece qname, net::Error error); 90 // |net_error| can be any net:Error value.
58 91 void ExpectRequestAndSocketError(base::StringPiece qname, int net_error);
59 // Expect a CT DNS request for the domain |qname|. 92 // Expect a CT DNS request for the domain |qname|.
60 // Such a request will timeout. 93 // Such a request will timeout.
61 // This will reduce the DNS timeout to minimize test duration. 94 // This will reduce the DNS timeout to minimize test duration.
62 void ExpectRequestAndTimeout(base::StringPiece qname); 95 void ExpectRequestAndTimeout(base::StringPiece qname);
63
64 // Expect a CT DNS request for the domain |qname|. 96 // Expect a CT DNS request for the domain |qname|.
65 // Such a request will receive a DNS TXT response containing |txt_strings|. 97 // Such a request will receive a DNS TXT response containing |txt_strings|.
66 void ExpectRequestAndResponse( 98 void ExpectRequestAndResponse(
67 base::StringPiece qname, 99 base::StringPiece qname,
68 const std::vector<base::StringPiece>& txt_strings); 100 const std::vector<base::StringPiece>& txt_strings);
69 // Expect a CT DNS request for the domain |qname|. 101 // Expect a CT DNS request for the domain |qname|.
70 // Such a request will receive a DNS response containing |leaf_index|. 102 // Such a request will receive a DNS response containing |leaf_index|.
71 // A description of such a request and response can be seen here: 103 // A description of such a request and response can be seen here:
72 // https://github.com/google/certificate-transparency-rfcs/blob/c8844de6bd0b5d 3d16bac79865e6edef533d760b/dns/draft-ct-over-dns.md#hash-query-hashquery 104 // https://github.com/google/certificate-transparency-rfcs/blob/c8844de6bd0b5d 3d16bac79865e6edef533d760b/dns/draft-ct-over-dns.md#hash-query-hashquery
73 void ExpectLeafIndexRequestAndResponse(base::StringPiece qname, 105 void ExpectLeafIndexRequestAndResponse(base::StringPiece qname,
(...skipping 17 matching lines...) Expand all
91 // Sets the DNS config to |config|. 123 // Sets the DNS config to |config|.
92 // Requires that net::NetworkChangeNotifier is initialized first. 124 // Requires that net::NetworkChangeNotifier is initialized first.
93 // The DNS config is propogated to NetworkChangeNotifier::DNSObservers 125 // The DNS config is propogated to NetworkChangeNotifier::DNSObservers
94 // asynchronously. 126 // asynchronously.
95 void SetDnsConfig(const net::DnsConfig& config); 127 void SetDnsConfig(const net::DnsConfig& config);
96 128
97 // Creates a DNS client that uses mock sockets. 129 // Creates a DNS client that uses mock sockets.
98 // It is this DNS client that the expectations will be tested against. 130 // It is this DNS client that the expectations will be tested against.
99 std::unique_ptr<net::DnsClient> CreateDnsClient(); 131 std::unique_ptr<net::DnsClient> CreateDnsClient();
100 132
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
108 // Sets whether mock reads should complete synchronously or asynchronously. 133 // Sets whether mock reads should complete synchronously or asynchronously.
109 // By default, they complete asynchronously.
110 void SetSocketReadMode(net::IoMode read_mode) { 134 void SetSocketReadMode(net::IoMode read_mode) {
111 socket_read_mode_ = read_mode; 135 socket_read_mode_ = read_mode;
112 } 136 }
113 137
138 private:
114 // Constructs MockSocketData from |args| and adds it to |socket_factory_|. 139 // Constructs MockSocketData from |args| and adds it to |socket_factory_|.
115 template <typename... Args> 140 template <typename... Args>
116 void EmplaceMockSocketData(Args&&... args); 141 void EmplaceMockSocketData(Args&&... args);
117 142
118 // Sets the timeout used for DNS queries. 143 // Sets the timeout used for DNS queries.
119 // Requires that net::NetworkChangeNotifier is initialized first. 144 // Requires that net::NetworkChangeNotifier is initialized first.
120 // The new timeout is propogated to NetworkChangeNotifier::DNSObservers 145 // The new timeout is propogated to NetworkChangeNotifier::DNSObservers
121 // asynchronously. 146 // asynchronously.
122 void SetDnsTimeout(const base::TimeDelta& timeout); 147 void SetDnsTimeout(const base::TimeDelta& timeout);
123 148
124 // One MockSocketData for each socket that is created. This corresponds to one 149 // One MockSocketData for each socket that is created. This corresponds to one
125 // for each DNS request sent. 150 // for each DNS request sent.
126 std::vector<std::unique_ptr<MockSocketData>> mock_socket_data_; 151 std::vector<std::unique_ptr<internal::MockSocketData>> mock_socket_data_;
127 // Provides as many mock sockets as there are entries in |mock_socket_data_|. 152 // Provides as many mock sockets as there are entries in |mock_socket_data_|.
128 net::MockClientSocketFactory socket_factory_; 153 net::MockClientSocketFactory socket_factory_;
129 // Controls whether mock socket reads are asynchronous. 154 // Controls whether mock socket reads are asynchronous.
130 net::IoMode socket_read_mode_; 155 net::IoMode socket_read_mode_;
131 156
132 DISALLOW_COPY_AND_ASSIGN(MockLogDnsTraffic); 157 DISALLOW_COPY_AND_ASSIGN(MockLogDnsTraffic);
133 }; 158 };
134 159
135 } // namespace certificate_transparency 160 } // namespace certificate_transparency
136 161
137 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_ 162 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698