OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_CLIENT_H_ | |
6 #define NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_CLIENT_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "net/base/ip_endpoint.h" | |
13 #include "net/quic/quic_framer.h" | |
14 #include "net/quic/quic_packet_creator.h" | |
15 #include "net/quic/quic_protocol.h" | |
16 #include "net/tools/balsa/balsa_frame.h" | |
17 #include "net/tools/epoll_server/epoll_server.h" | |
18 #include "net/tools/quic/quic_client.h" | |
19 #include "net/tools/quic/test_tools/simple_client.h" | |
20 | |
21 namespace net { | |
22 | |
23 class ProofVerifier; | |
24 | |
25 namespace tools { | |
26 | |
27 class QuicPacketWriterWrapper; | |
28 | |
29 namespace test { | |
30 | |
31 class HTTPMessage; | |
32 class MockableQuicClient; | |
33 | |
34 // A quic client which allows mocking out writes. | |
35 class MockableQuicClient : public QuicClient { | |
36 public: | |
37 MockableQuicClient(IPEndPoint server_address, | |
38 const QuicServerId& server_id, | |
39 const QuicVersionVector& supported_versions, | |
40 EpollServer* epoll_server); | |
41 | |
42 MockableQuicClient(IPEndPoint server_address, | |
43 const QuicServerId& server_id, | |
44 const QuicConfig& config, | |
45 const QuicVersionVector& supported_versions, | |
46 EpollServer* epoll_server); | |
47 | |
48 ~MockableQuicClient() override; | |
49 QuicPacketWriter* CreateQuicPacketWriter() override; | |
50 QuicConnectionId GenerateConnectionId() override; | |
51 void UseWriter(QuicPacketWriterWrapper* writer); | |
52 void UseConnectionId(QuicConnectionId connection_id); | |
53 | |
54 private: | |
55 QuicConnectionId override_connection_id_; // ConnectionId to use, if nonzero | |
56 QuicPacketWriterWrapper* test_writer_; | |
57 | |
58 DISALLOW_COPY_AND_ASSIGN(MockableQuicClient); | |
59 }; | |
60 | |
61 // A toy QUIC client used for testing, mostly following the SimpleClient APIs. | |
62 class QuicTestClient : public SimpleClient, | |
63 public QuicDataStream::Visitor { | |
64 public: | |
65 QuicTestClient(IPEndPoint server_address, | |
66 const std::string& server_hostname, | |
67 bool secure, | |
68 const QuicVersionVector& supported_versions); | |
69 QuicTestClient(IPEndPoint server_address, | |
70 const std::string& server_hostname, | |
71 bool secure, | |
72 const QuicConfig& config, | |
73 const QuicVersionVector& supported_versions); | |
74 | |
75 ~QuicTestClient() override; | |
76 | |
77 // ExpectCertificates controls whether the server is expected to provide | |
78 // certificates. The certificates, if any, are not verified, but the common | |
79 // name is recorded and available with |cert_common_name()|. | |
80 void ExpectCertificates(bool on); | |
81 | |
82 // Sets the |user_agent_id| of the |client_|. | |
83 void SetUserAgentID(const std::string& user_agent_id); | |
84 | |
85 // Wraps data in a quic packet and sends it. | |
86 ssize_t SendData(std::string data, bool last_data); | |
87 // As above, but |delegate| will be notified when |data| is ACKed. | |
88 ssize_t SendData(std::string data, | |
89 bool last_data, | |
90 QuicAckNotifier::DelegateInterface* delegate); | |
91 | |
92 // From SimpleClient | |
93 // Clears any outstanding state and sends a simple GET of 'uri' to the | |
94 // server. Returns 0 if the request failed and no bytes were written. | |
95 ssize_t SendRequest(const std::string& uri) override; | |
96 ssize_t SendMessage(const HTTPMessage& message) override; | |
97 std::string SendCustomSynchronousRequest(const HTTPMessage& message) override; | |
98 std::string SendSynchronousRequest(const std::string& uri) override; | |
99 void Connect() override; | |
100 void ResetConnection() override; | |
101 void Disconnect() override; | |
102 IPEndPoint LocalSocketAddress() const override; | |
103 void ClearPerRequestState() override; | |
104 void WaitForResponseForMs(int timeout_ms) override; | |
105 void WaitForInitialResponseForMs(int timeout_ms) override; | |
106 ssize_t Send(const void* buffer, size_t size) override; | |
107 bool response_complete() const override; | |
108 bool response_headers_complete() const override; | |
109 const BalsaHeaders* response_headers() const override; | |
110 int64 response_size() const override; | |
111 int response_header_size() const override; | |
112 int64 response_body_size() const override; | |
113 size_t bytes_read() const override; | |
114 size_t bytes_written() const override; | |
115 bool buffer_body() const override; | |
116 void set_buffer_body(bool buffer_body) override; | |
117 bool ServerInLameDuckMode() const override; | |
118 const std::string& response_body() override; | |
119 bool connected() const override; | |
120 // These functions are all unimplemented functions from SimpleClient, and log | |
121 // DFATAL if called by users of SimpleClient. | |
122 ssize_t SendAndWaitForResponse(const void* buffer, size_t size) override; | |
123 void Bind(IPEndPoint* local_address) override; | |
124 std::string SerializeMessage(const HTTPMessage& message) override; | |
125 IPAddressNumber bind_to_address() const override; | |
126 void set_bind_to_address(IPAddressNumber address) override; | |
127 const IPEndPoint& address() const override; | |
128 size_t requests_sent() const override; | |
129 | |
130 // From QuicDataStream::Visitor | |
131 void OnClose(QuicDataStream* stream) override; | |
132 | |
133 // Configures client_ to take ownership of and use the writer. | |
134 // Must be called before initial connect. | |
135 void UseWriter(QuicPacketWriterWrapper* writer); | |
136 // If the given ConnectionId is nonzero, configures client_ to use a specific | |
137 // ConnectionId instead of a random one. | |
138 void UseConnectionId(QuicConnectionId connection_id); | |
139 | |
140 // Returns nullptr if the maximum number of streams have already been created. | |
141 QuicSpdyClientStream* GetOrCreateStream(); | |
142 | |
143 QuicRstStreamErrorCode stream_error() { return stream_error_; } | |
144 QuicErrorCode connection_error(); | |
145 | |
146 MockableQuicClient* client(); | |
147 | |
148 // cert_common_name returns the common name value of the server's certificate, | |
149 // or the empty string if no certificate was presented. | |
150 const std::string& cert_common_name() const; | |
151 | |
152 // Get the server config map. | |
153 QuicTagValueMap GetServerConfig() const; | |
154 | |
155 void set_auto_reconnect(bool reconnect) { auto_reconnect_ = reconnect; } | |
156 | |
157 void set_priority(QuicPriority priority) { priority_ = priority; } | |
158 | |
159 // Sets client's FEC policy. This policy applies to the data stream(s), and | |
160 // also to the headers and crypto streams. | |
161 void SetFecPolicy(FecPolicy fec_policy); | |
162 | |
163 void WaitForWriteToFlush(); | |
164 | |
165 EpollServer* epoll_server() { return &epoll_server_; } | |
166 | |
167 protected: | |
168 QuicTestClient(); | |
169 | |
170 void Initialize(bool secure); | |
171 | |
172 void set_client(MockableQuicClient* client) { client_.reset(client); } | |
173 | |
174 private: | |
175 EpollServer epoll_server_; | |
176 scoped_ptr<MockableQuicClient> client_; // The actual client | |
177 QuicSpdyClientStream* stream_; | |
178 | |
179 QuicRstStreamErrorCode stream_error_; | |
180 | |
181 bool response_complete_; | |
182 bool response_headers_complete_; | |
183 BalsaHeaders headers_; | |
184 QuicPriority priority_; | |
185 std::string response_; | |
186 uint64 bytes_read_; | |
187 uint64 bytes_written_; | |
188 // The number of uncompressed HTTP header bytes received. | |
189 int response_header_size_; | |
190 // The number of HTTP body bytes received. | |
191 int64 response_body_size_; | |
192 // True if we tried to connect already since the last call to Disconnect(). | |
193 bool connect_attempted_; | |
194 bool secure_; | |
195 // The client will auto-connect exactly once before sending data. If | |
196 // something causes a connection reset, it will not automatically reconnect | |
197 // unless auto_reconnect_ is true. | |
198 bool auto_reconnect_; | |
199 // Should we buffer the response body? Defaults to true. | |
200 bool buffer_body_; | |
201 // FEC policy for data sent by this client. | |
202 FecPolicy fec_policy_; | |
203 // proof_verifier_ points to a RecordingProofVerifier that is owned by | |
204 // client_. | |
205 ProofVerifier* proof_verifier_; | |
206 | |
207 DISALLOW_COPY_AND_ASSIGN(QuicTestClient); | |
208 }; | |
209 | |
210 } // namespace test | |
211 | |
212 } // namespace tools | |
213 } // namespace net | |
214 | |
215 #endif // NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_CLIENT_H_ | |
OLD | NEW |