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_
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"
28 class QuicPacketWriterWrapper
;
33 class MockableQuicClient
;
35 // A quic client which allows mocking out writes.
36 class MockableQuicClient
: public QuicClient
{
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
;
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
{
71 QuicTestClient(IPEndPoint server_address
,
72 const std::string
& server_hostname
,
74 const QuicVersionVector
& supported_versions
);
75 QuicTestClient(IPEndPoint server_address
,
76 const std::string
& server_hostname
,
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
,
96 QuicAckNotifier::DelegateInterface
* delegate
);
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 ssize_t
SendMessage(const HTTPMessage
& message
) override
;
103 std::string
SendCustomSynchronousRequest(const HTTPMessage
& message
) override
;
104 std::string
SendSynchronousRequest(const std::string
& uri
) override
;
105 void Connect() override
;
106 void ResetConnection() override
;
107 void Disconnect() override
;
108 IPEndPoint
LocalSocketAddress() const override
;
109 void ClearPerRequestState() override
;
110 void WaitForResponseForMs(int timeout_ms
) override
;
111 void WaitForInitialResponseForMs(int timeout_ms
) override
;
112 ssize_t
Send(const void* buffer
, size_t size
) override
;
113 bool response_complete() const override
;
114 bool response_headers_complete() const override
;
115 const BalsaHeaders
* response_headers() const override
;
116 int64
response_size() const override
;
117 int response_header_size() const override
;
118 int64
response_body_size() const override
;
119 size_t bytes_read() const override
;
120 size_t bytes_written() const override
;
121 bool buffer_body() const override
;
122 void set_buffer_body(bool buffer_body
) override
;
123 bool ServerInLameDuckMode() const override
;
124 const std::string
& response_body() override
;
125 bool connected() const override
;
126 // These functions are all unimplemented functions from SimpleClient, and log
127 // DFATAL if called by users of SimpleClient.
128 ssize_t
SendAndWaitForResponse(const void* buffer
, size_t size
) override
;
129 void Bind(IPEndPoint
* local_address
) override
;
130 std::string
SerializeMessage(const HTTPMessage
& message
) override
;
131 IPAddressNumber
bind_to_address() const override
;
132 void set_bind_to_address(IPAddressNumber address
) override
;
133 const IPEndPoint
& address() const override
;
134 size_t requests_sent() const override
;
136 // From QuicDataStream::Visitor
137 void OnClose(QuicDataStream
* stream
) override
;
139 // Configures client_ to take ownership of and use the writer.
140 // Must be called before initial connect.
141 void UseWriter(QuicPacketWriterWrapper
* writer
);
142 // If the given ConnectionId is nonzero, configures client_ to use a specific
143 // ConnectionId instead of a random one.
144 void UseConnectionId(QuicConnectionId connection_id
);
146 // Returns nullptr if the maximum number of streams have already been created.
147 QuicSpdyClientStream
* GetOrCreateStream();
149 QuicRstStreamErrorCode
stream_error() { return stream_error_
; }
150 QuicErrorCode
connection_error();
152 MockableQuicClient
* client();
154 // cert_common_name returns the common name value of the server's certificate,
155 // or the empty string if no certificate was presented.
156 const std::string
& cert_common_name() const;
158 // Get the server config map.
159 QuicTagValueMap
GetServerConfig() const;
161 void set_auto_reconnect(bool reconnect
) { auto_reconnect_
= reconnect
; }
163 void set_priority(QuicPriority priority
) { priority_
= priority
; }
165 // Sets client's FEC policy. This policy applies to the data stream(s), and
166 // also to the headers and crypto streams.
167 void SetFecPolicy(FecPolicy fec_policy
);
169 void WaitForWriteToFlush();
171 EpollServer
* epoll_server() { return &epoll_server_
; }
176 void Initialize(bool secure
);
178 void set_client(MockableQuicClient
* client
) { client_
.reset(client
); }
181 EpollServer epoll_server_
;
182 scoped_ptr
<MockableQuicClient
> client_
; // The actual client
183 QuicSpdyClientStream
* stream_
;
185 QuicRstStreamErrorCode stream_error_
;
187 bool response_complete_
;
188 bool response_headers_complete_
;
189 mutable BalsaHeaders headers_
;
190 QuicPriority priority_
;
191 std::string response_
;
193 uint64 bytes_written_
;
194 // The number of uncompressed HTTP header bytes received.
195 int response_header_size_
;
196 // The number of HTTP body bytes received.
197 int64 response_body_size_
;
198 // True if we tried to connect already since the last call to Disconnect().
199 bool connect_attempted_
;
201 // The client will auto-connect exactly once before sending data. If
202 // something causes a connection reset, it will not automatically reconnect
203 // unless auto_reconnect_ is true.
204 bool auto_reconnect_
;
205 // Should we buffer the response body? Defaults to true.
207 // FEC policy for data sent by this client.
208 FecPolicy fec_policy_
;
209 // proof_verifier_ points to a RecordingProofVerifier that is owned by
211 ProofVerifier
* proof_verifier_
;
213 DISALLOW_COPY_AND_ASSIGN(QuicTestClient
);
221 #endif // NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_CLIENT_H_