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