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