Chromium Code Reviews| OLD | NEW |
|---|---|
| 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> |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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. | |
| 81 // std::unique_ptr<net::NetworkChangeNotifier> net_change_notifier = | |
| 82 // net::NetworkChangeNotifier::CreateMock(); | |
| 83 // | |
| 84 // MockLogDnsTraffic mock_dns; | |
| 85 // mock_dns.InitializeDnsConfig(); | |
| 86 // // Use the Expect* methods to define expected DNS requests and responses. | |
| 87 // mock_dns.ExpectLeafIndexRequestAndResponse( | |
| 88 // "D4S6DSV2J743QJZEQMH4UYHEYK7KRQ5JIQOCPMFUHZVJNFGHXACA.hash.ct.test.", | |
| 89 // "123456"); | |
| 90 // | |
| 91 // LogDnsClient log_client(mock_dns.CreateDnsClient(), ...); | |
| 92 // log_client.QueryLeafIndex("ct.test", ...); | |
|
Eran Messeri
2016/09/20 14:31:04
Which message loop should be pumped to guarantee t
Rob Percival
2016/09/20 16:33:11
I've added a few more lines to the example usage t
| |
| 78 class MockLogDnsTraffic { | 93 class MockLogDnsTraffic { |
| 79 public: | 94 public: |
| 80 MockLogDnsTraffic(); | 95 MockLogDnsTraffic(); |
| 81 ~MockLogDnsTraffic(); | 96 ~MockLogDnsTraffic(); |
| 82 | 97 |
| 83 // Expect a CT DNS request for the domain |qname|. | 98 // Expect a CT DNS request for the domain |qname|. |
| 84 // Such a request will receive a DNS response indicating that the error | 99 // 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| | 100 // specified by |rcode| occurred. See RFC1035, Section 4.1.1 for |rcode| |
| 86 // values. | 101 // values. |
| 87 void ExpectRequestAndErrorResponse(base::StringPiece qname, uint8_t rcode); | 102 void ExpectRequestAndErrorResponse(base::StringPiece qname, uint8_t rcode); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 // Requires that net::NetworkChangeNotifier is initialized first. | 134 // Requires that net::NetworkChangeNotifier is initialized first. |
| 120 // The DNS config is propogated to NetworkChangeNotifier::DNSObservers | 135 // The DNS config is propogated to NetworkChangeNotifier::DNSObservers |
| 121 // asynchronously. | 136 // asynchronously. |
| 122 void SetDnsConfig(const net::DnsConfig& config); | 137 void SetDnsConfig(const net::DnsConfig& config); |
| 123 | 138 |
| 124 // Creates a DNS client that uses mock sockets. | 139 // Creates a DNS client that uses mock sockets. |
| 125 // It is this DNS client that the expectations will be tested against. | 140 // It is this DNS client that the expectations will be tested against. |
| 126 std::unique_ptr<net::DnsClient> CreateDnsClient(); | 141 std::unique_ptr<net::DnsClient> CreateDnsClient(); |
| 127 | 142 |
| 128 // Sets whether mock reads should complete synchronously or asynchronously. | 143 // Sets whether mock reads should complete synchronously or asynchronously. |
| 144 // By default, they complete asynchronously. The only reason to change this | |
| 145 // is to test that LogDnsClient handles both modes in the same way. | |
| 129 void SetSocketReadMode(net::IoMode read_mode) { | 146 void SetSocketReadMode(net::IoMode read_mode) { |
| 130 socket_read_mode_ = read_mode; | 147 socket_read_mode_ = read_mode; |
| 131 } | 148 } |
| 132 | 149 |
| 133 private: | 150 private: |
| 134 // Expect A CT DNS request for the domain |qname|. | 151 // Expect A CT DNS request for the domain |qname|. |
| 135 // Such a request will receive a DNS response containing |answer|. | 152 // Such a request will receive a DNS response containing |answer|. |
| 136 void ExpectRequestAndResponse(base::StringPiece qname, | 153 void ExpectRequestAndResponse(base::StringPiece qname, |
| 137 base::StringPiece answer); | 154 base::StringPiece answer); |
| 138 | 155 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 153 net::MockClientSocketFactory socket_factory_; | 170 net::MockClientSocketFactory socket_factory_; |
| 154 // Controls whether mock socket reads are asynchronous. | 171 // Controls whether mock socket reads are asynchronous. |
| 155 net::IoMode socket_read_mode_; | 172 net::IoMode socket_read_mode_; |
| 156 | 173 |
| 157 DISALLOW_COPY_AND_ASSIGN(MockLogDnsTraffic); | 174 DISALLOW_COPY_AND_ASSIGN(MockLogDnsTraffic); |
| 158 }; | 175 }; |
| 159 | 176 |
| 160 } // namespace certificate_transparency | 177 } // namespace certificate_transparency |
| 161 | 178 |
| 162 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_ | 179 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_MOCK_LOG_DNS_TRAFFIC_H_ |
| OLD | NEW |