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_QUIC_TEST_TOOLS_QUIC_TEST_CLIENT_H_ 6 #define NET_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/quic/quic_framer.h" 13 #include "net/quic/quic_packet_creator.h" 14 #include "net/quic/quic_protocol.h" 15 #include "net/quic/test_tools/quic_test_writer.h" 16 #include "net/tools/quic/quic_client.h" 17 18 namespace net { 19 20 class ProofVerifier; 21 22 namespace tools { 23 24 namespace test { 25 26 class HTTPMessage; 27 28 // A toy QUIC client used for testing. 29 class QuicTestClient : public QuicDataStream::Visitor { 30 public: 31 QuicTestClient(IPEndPoint server_address, const string& server_hostname, 32 const QuicVersionVector& supported_versions); 33 QuicTestClient(IPEndPoint server_address, 34 const string& server_hostname, 35 bool secure, 36 const QuicVersionVector& supported_versions); 37 QuicTestClient(IPEndPoint server_address, 38 const string& server_hostname, 39 bool secure, 40 const QuicConfig& config, 41 const QuicVersionVector& supported_versions); 42 43 virtual ~QuicTestClient(); 44 45 // ExpectCertificates controls whether the server is expected to provide 46 // certificates. The certificates, if any, are not verified, but the common 47 // name is recorded and available with |cert_common_name()|. 48 void ExpectCertificates(bool on); 49 50 // Clears any outstanding state and sends a simple GET of 'uri' to the 51 // server. Returns 0 if the request failed and no bytes were written. 52 ssize_t SendRequest(const string& uri); 53 ssize_t SendMessage(const HTTPMessage& message); 54 55 string SendCustomSynchronousRequest(const HTTPMessage& message); 56 string SendSynchronousRequest(const string& uri); 57 58 // Wraps data in a quic packet and sends it. 59 ssize_t SendData(string data, bool last_data); 60 61 QuicPacketCreator::Options* options() { return client_->options(); } 62 63 void WaitForResponse(); 64 65 void Connect(); 66 void ResetConnection(); 67 void Disconnect(); 68 IPEndPoint LocalSocketAddress() const; 69 void ClearPerRequestState(); 70 void WaitForResponseForMs(int timeout_ms); 71 void WaitForInitialResponseForMs(int timeout_ms); 72 ssize_t Send(const void *buffer, size_t size); 73 bool response_complete() const { return response_complete_; } 74 bool response_headers_complete() const; 75 const BalsaHeaders* response_headers() const; 76 int response_size() const; 77 int response_header_size() const { return response_header_size_; } 78 int response_body_size() const { return response_body_size_; } 79 size_t bytes_read() const; 80 size_t bytes_written() const; 81 bool buffer_body() const { return buffer_body_; } 82 void set_buffer_body(bool buffer_body) { buffer_body_ = buffer_body; } 83 84 // From QuicDataStream::Visitor 85 virtual void OnClose(QuicDataStream* stream) OVERRIDE; 86 87 // Configures client_ to take ownership of and use the writer. 88 // Must be called before initial connect. 89 void UseWriter(net::test::QuicTestWriter* writer); 90 // If the given GUID is nonzero, configures client_ to use a specific GUID 91 // instead of a random one. 92 void UseGuid(QuicGuid guid); 93 94 // Returns NULL if the maximum number of streams have already been created. 95 QuicSpdyClientStream* GetOrCreateStream(); 96 97 QuicRstStreamErrorCode stream_error() { return stream_error_; } 98 QuicErrorCode connection_error() { return client()->session()->error(); } 99 100 QuicClient* client() { return client_.get(); } 101 102 // cert_common_name returns the common name value of the server's certificate, 103 // or the empty string if no certificate was presented. 104 const string& cert_common_name() const; 105 106 const string& response_body() {return response_;} 107 bool connected() const; 108 109 void set_auto_reconnect(bool reconnect) { auto_reconnect_ = reconnect; } 110 111 void set_priority(QuicPriority priority) { priority_ = priority; } 112 113 void WaitForWriteToFlush(); 114 115 private: 116 void Initialize(IPEndPoint address, const string& hostname, bool secure); 117 118 IPEndPoint server_address_; 119 IPEndPoint client_address_; 120 scoped_ptr<QuicClient> client_; // The actual client 121 QuicSpdyClientStream* stream_; 122 123 QuicRstStreamErrorCode stream_error_; 124 125 bool response_complete_; 126 bool response_headers_complete_; 127 BalsaHeaders headers_; 128 QuicPriority priority_; 129 string response_; 130 uint64 bytes_read_; 131 uint64 bytes_written_; 132 // The number of uncompressed HTTP header bytes received. 133 int response_header_size_; 134 // The number of HTTP body bytes received. 135 int response_body_size_; 136 // True if we tried to connect already since the last call to Disconnect(). 137 bool connect_attempted_; 138 bool secure_; 139 // The client will auto-connect exactly once before sending data. If 140 // something causes a connection reset, it will not automatically reconnect 141 // unless auto_reconnect_ is true. 142 bool auto_reconnect_; 143 // Should we buffer the response body? Defaults to true. 144 bool buffer_body_; 145 146 // proof_verifier_ points to a RecordingProofVerifier that is owned by 147 // client_. 148 ProofVerifier* proof_verifier_; 149 }; 150 151 } // namespace test 152 153 } // namespace tools 154 } // namespace net 155 156 #endif // NET_QUIC_TEST_TOOLS_QUIC_TEST_CLIENT_H_ 157