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 virtual ~MockableQuicClient() OVERRIDE; 49 virtual QuicPacketWriter* CreateQuicPacketWriter() OVERRIDE; 50 virtual 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 string& server_hostname, 67 const QuicVersionVector& supported_versions); 68 QuicTestClient(IPEndPoint server_address, 69 const string& server_hostname, 70 bool secure, 71 const QuicVersionVector& supported_versions); 72 QuicTestClient(IPEndPoint server_address, 73 const string& server_hostname, 74 bool secure, 75 const QuicConfig& config, 76 const QuicVersionVector& supported_versions); 77 78 virtual ~QuicTestClient(); 79 80 // ExpectCertificates controls whether the server is expected to provide 81 // certificates. The certificates, if any, are not verified, but the common 82 // name is recorded and available with |cert_common_name()|. 83 void ExpectCertificates(bool on); 84 85 // Sets the |user_agent_id| of the |client_|. 86 void SetUserAgentID(const string& user_agent_id); 87 88 // Wraps data in a quic packet and sends it. 89 ssize_t SendData(string data, bool last_data); 90 91 // From SimpleClient 92 // Clears any outstanding state and sends a simple GET of 'uri' to the 93 // server. Returns 0 if the request failed and no bytes were written. 94 virtual ssize_t SendRequest(const string& uri) OVERRIDE; 95 virtual ssize_t SendMessage(const HTTPMessage& message) OVERRIDE; 96 virtual string SendCustomSynchronousRequest( 97 const HTTPMessage& message) OVERRIDE; 98 virtual string SendSynchronousRequest(const string& uri) OVERRIDE; 99 virtual void Connect() OVERRIDE; 100 virtual void ResetConnection() OVERRIDE; 101 virtual void Disconnect() OVERRIDE; 102 virtual IPEndPoint LocalSocketAddress() const OVERRIDE; 103 virtual void ClearPerRequestState() OVERRIDE; 104 virtual void WaitForResponseForMs(int timeout_ms) OVERRIDE; 105 virtual void WaitForInitialResponseForMs(int timeout_ms) OVERRIDE; 106 virtual ssize_t Send(const void *buffer, size_t size) OVERRIDE; 107 virtual bool response_complete() const OVERRIDE; 108 virtual bool response_headers_complete() const OVERRIDE; 109 virtual const BalsaHeaders* response_headers() const OVERRIDE; 110 virtual int64 response_size() const OVERRIDE; 111 virtual int response_header_size() const OVERRIDE; 112 virtual int64 response_body_size() const OVERRIDE; 113 virtual size_t bytes_read() const OVERRIDE; 114 virtual size_t bytes_written() const OVERRIDE; 115 virtual bool buffer_body() const OVERRIDE; 116 virtual void set_buffer_body(bool buffer_body) OVERRIDE; 117 virtual bool ServerInLameDuckMode() const OVERRIDE; 118 virtual const string& response_body() OVERRIDE; 119 virtual bool connected() const OVERRIDE; 120 // These functions are all unimplemented functions from SimpleClient, and log 121 // DFATAL if called by users of SimpleClient. 122 virtual ssize_t SendAndWaitForResponse(const void *buffer, 123 size_t size) OVERRIDE; 124 virtual void Bind(IPEndPoint* local_address) OVERRIDE; 125 virtual string SerializeMessage(const HTTPMessage& message) OVERRIDE; 126 virtual IPAddressNumber bind_to_address() const OVERRIDE; 127 virtual void set_bind_to_address(IPAddressNumber address) OVERRIDE; 128 virtual const IPEndPoint& address() const OVERRIDE; 129 virtual size_t requests_sent() const OVERRIDE; 130 131 // From QuicDataStream::Visitor 132 virtual void OnClose(QuicDataStream* stream) OVERRIDE; 133 134 // Configures client_ to take ownership of and use the writer. 135 // Must be called before initial connect. 136 void UseWriter(QuicPacketWriterWrapper* writer); 137 // If the given ConnectionId is nonzero, configures client_ to use a specific 138 // ConnectionId instead of a random one. 139 void UseConnectionId(QuicConnectionId connection_id); 140 141 // Returns NULL if the maximum number of streams have already been created. 142 QuicSpdyClientStream* GetOrCreateStream(); 143 144 QuicRstStreamErrorCode stream_error() { return stream_error_; } 145 QuicErrorCode connection_error(); 146 147 MockableQuicClient* client(); 148 149 // cert_common_name returns the common name value of the server's certificate, 150 // or the empty string if no certificate was presented. 151 const string& cert_common_name() const; 152 153 // Get the server config map. 154 QuicTagValueMap GetServerConfig() const; 155 156 void set_auto_reconnect(bool reconnect) { auto_reconnect_ = reconnect; } 157 158 void set_priority(QuicPriority priority) { priority_ = priority; } 159 160 // Sets client's FEC policy. This policy applies to the data stream(s), and 161 // also to the headers and crypto streams. 162 void SetFecPolicy(FecPolicy fec_policy); 163 164 void WaitForWriteToFlush(); 165 166 EpollServer* epoll_server() { return &epoll_server_; } 167 168 protected: 169 QuicTestClient(); 170 171 void Initialize(bool secure); 172 173 void set_client(MockableQuicClient* client) { client_.reset(client); } 174 175 private: 176 EpollServer epoll_server_; 177 scoped_ptr<MockableQuicClient> client_; // The actual client 178 QuicSpdyClientStream* stream_; 179 180 QuicRstStreamErrorCode stream_error_; 181 182 bool response_complete_; 183 bool response_headers_complete_; 184 BalsaHeaders headers_; 185 QuicPriority priority_; 186 string response_; 187 uint64 bytes_read_; 188 uint64 bytes_written_; 189 // The number of uncompressed HTTP header bytes received. 190 int response_header_size_; 191 // The number of HTTP body bytes received. 192 int64 response_body_size_; 193 // True if we tried to connect already since the last call to Disconnect(). 194 bool connect_attempted_; 195 bool secure_; 196 // The client will auto-connect exactly once before sending data. If 197 // something causes a connection reset, it will not automatically reconnect 198 // unless auto_reconnect_ is true. 199 bool auto_reconnect_; 200 // Should we buffer the response body? Defaults to true. 201 bool buffer_body_; 202 // FEC policy for data sent by this client. 203 FecPolicy fec_policy_; 204 // proof_verifier_ points to a RecordingProofVerifier that is owned by 205 // client_. 206 ProofVerifier* proof_verifier_; 207 208 DISALLOW_COPY_AND_ASSIGN(QuicTestClient); 209 }; 210 211 } // namespace test 212 213 } // namespace tools 214 } // namespace net 215 216 #endif // NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_CLIENT_H_ 217