1 // Copyright (c) 2013 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_SPDY_SPDY_TEST_UTIL_COMMON_H_
6 #define NET_SPDY_SPDY_TEST_UTIL_COMMON_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "crypto/ec_private_key.h"
14 #include "crypto/ec_signature_creator.h"
15 #include "net/base/completion_callback.h"
16 #include "net/base/request_priority.h"
17 #include "net/base/test_completion_callback.h"
18 #include "net/cert/cert_verifier.h"
19 #include "net/dns/mock_host_resolver.h"
20 #include "net/http/http_auth_handler_factory.h"
21 #include "net/http/http_network_session.h"
22 #include "net/http/http_response_info.h"
23 #include "net/http/http_server_properties_impl.h"
24 #include "net/http/transport_security_state.h"
25 #include "net/proxy/proxy_service.h"
26 #include "net/socket/next_proto.h"
27 #include "net/socket/socket_test_util.h"
28 #include "net/spdy/spdy_protocol.h"
29 #include "net/ssl/ssl_config_service_defaults.h"
30 #include "net/url_request/url_request_context.h"
31 #include "net/url_request/url_request_context_storage.h"
32 #include "testing/gtest/include/gtest/gtest.h"
41 class SpdySessionPool
;
43 class SpdyStreamRequest
;
45 // Default upload data used by both, mock objects and framer when creating
47 const char kDefaultURL
[] = "http://www.example.org/";
48 const char kUploadData
[] = "hello!";
49 const int kUploadDataSize
= arraysize(kUploadData
)-1;
51 // SpdyNextProtos returns a vector of next protocols for negotiating
53 NextProtoVector
SpdyNextProtos();
55 // Chop a frame into an array of MockWrites.
56 // |data| is the frame to chop.
57 // |length| is the length of the frame to chop.
58 // |num_chunks| is the number of chunks to create.
59 MockWrite
* ChopWriteFrame(const char* data
, int length
, int num_chunks
);
61 // Chop a SpdyFrame into an array of MockWrites.
62 // |frame| is the frame to chop.
63 // |num_chunks| is the number of chunks to create.
64 MockWrite
* ChopWriteFrame(const SpdyFrame
& frame
, int num_chunks
);
66 // Chop a frame into an array of MockReads.
67 // |data| is the frame to chop.
68 // |length| is the length of the frame to chop.
69 // |num_chunks| is the number of chunks to create.
70 MockRead
* ChopReadFrame(const char* data
, int length
, int num_chunks
);
72 // Chop a SpdyFrame into an array of MockReads.
73 // |frame| is the frame to chop.
74 // |num_chunks| is the number of chunks to create.
75 MockRead
* ChopReadFrame(const SpdyFrame
& frame
, int num_chunks
);
77 // Adds headers and values to a map.
78 // |extra_headers| is an array of { name, value } pairs, arranged as strings
79 // where the even entries are the header names, and the odd entries are the
81 // |headers| gets filled in from |extra_headers|.
82 void AppendToHeaderBlock(const char* const extra_headers
[],
83 int extra_header_count
,
84 SpdyHeaderBlock
* headers
);
86 // Create an async MockWrite from the given SpdyFrame.
87 MockWrite
CreateMockWrite(const SpdyFrame
& req
);
89 // Create an async MockWrite from the given SpdyFrame and sequence number.
90 MockWrite
CreateMockWrite(const SpdyFrame
& req
, int seq
);
92 MockWrite
CreateMockWrite(const SpdyFrame
& req
, int seq
, IoMode mode
);
94 // Create a MockRead from the given SpdyFrame.
95 MockRead
CreateMockRead(const SpdyFrame
& resp
);
97 // Create a MockRead from the given SpdyFrame and sequence number.
98 MockRead
CreateMockRead(const SpdyFrame
& resp
, int seq
);
100 MockRead
CreateMockRead(const SpdyFrame
& resp
, int seq
, IoMode mode
);
102 // Combines the given SpdyFrames into the given char array and returns
104 int CombineFrames(const SpdyFrame
** frames
, int num_frames
,
105 char* buff
, int buff_len
);
107 // Returns the SpdyPriority embedded in the given frame. Returns true
108 // and fills in |priority| on success.
109 bool GetSpdyPriority(SpdyMajorVersion version
,
110 const SpdyFrame
& frame
,
111 SpdyPriority
* priority
);
113 // Tries to create a stream in |session| synchronously. Returns NULL
115 base::WeakPtr
<SpdyStream
> CreateStreamSynchronously(
117 const base::WeakPtr
<SpdySession
>& session
,
119 RequestPriority priority
,
120 const BoundNetLog
& net_log
);
122 // Helper class used by some tests to release a stream as soon as it's
124 class StreamReleaserCallback
: public TestCompletionCallbackBase
{
126 StreamReleaserCallback();
128 ~StreamReleaserCallback() override
;
130 // Returns a callback that releases |request|'s stream.
131 CompletionCallback
MakeCallback(SpdyStreamRequest
* request
);
134 void OnComplete(SpdyStreamRequest
* request
, int result
);
137 const size_t kSpdyCredentialSlotUnused
= 0;
139 // This struct holds information used to construct spdy control and data frames.
140 struct SpdyHeaderInfo
{
143 SpdyStreamId assoc_id
;
144 SpdyPriority priority
;
145 size_t credential_slot
; // SPDY3 only
146 SpdyControlFlags control_flags
;
148 SpdyRstStreamStatus status
;
151 SpdyDataFlags data_flags
;
154 // An ECSignatureCreator that returns deterministic signatures.
155 class MockECSignatureCreator
: public crypto::ECSignatureCreator
{
157 explicit MockECSignatureCreator(crypto::ECPrivateKey
* key
);
159 // crypto::ECSignatureCreator
160 bool Sign(const uint8
* data
,
162 std::vector
<uint8
>* signature
) override
;
163 bool DecodeSignature(const std::vector
<uint8
>& signature
,
164 std::vector
<uint8
>* out_raw_sig
) override
;
167 crypto::ECPrivateKey
* key_
;
169 DISALLOW_COPY_AND_ASSIGN(MockECSignatureCreator
);
172 // An ECSignatureCreatorFactory creates MockECSignatureCreator.
173 class MockECSignatureCreatorFactory
: public crypto::ECSignatureCreatorFactory
{
175 MockECSignatureCreatorFactory();
176 ~MockECSignatureCreatorFactory() override
;
178 // crypto::ECSignatureCreatorFactory
179 crypto::ECSignatureCreator
* Create(crypto::ECPrivateKey
* key
) override
;
182 DISALLOW_COPY_AND_ASSIGN(MockECSignatureCreatorFactory
);
185 // Helper to manage the lifetimes of the dependencies for a
186 // HttpNetworkTransaction.
187 struct SpdySessionDependencies
{
188 // Default set of dependencies -- "null" proxy service.
189 explicit SpdySessionDependencies(NextProto protocol
);
191 // Custom proxy service dependency.
192 SpdySessionDependencies(NextProto protocol
, ProxyService
* proxy_service
);
194 ~SpdySessionDependencies();
196 static HttpNetworkSession
* SpdyCreateSession(
197 SpdySessionDependencies
* session_deps
);
198 static HttpNetworkSession
* SpdyCreateSessionDeterministic(
199 SpdySessionDependencies
* session_deps
);
200 static HttpNetworkSession::Params
CreateSessionParams(
201 SpdySessionDependencies
* session_deps
);
203 // NOTE: host_resolver must be ordered before http_auth_handler_factory.
204 scoped_ptr
<MockHostResolverBase
> host_resolver
;
205 scoped_ptr
<CertVerifier
> cert_verifier
;
206 scoped_ptr
<TransportSecurityState
> transport_security_state
;
207 scoped_ptr
<ProxyService
> proxy_service
;
208 scoped_refptr
<SSLConfigService
> ssl_config_service
;
209 scoped_ptr
<MockClientSocketFactory
> socket_factory
;
210 scoped_ptr
<DeterministicMockClientSocketFactory
> deterministic_socket_factory
;
211 scoped_ptr
<HttpAuthHandlerFactory
> http_auth_handler_factory
;
212 HttpServerPropertiesImpl http_server_properties
;
213 bool enable_ip_pooling
;
214 bool enable_compression
;
216 bool enable_user_alternate_protocol_ports
;
218 size_t session_max_recv_window_size
;
219 size_t stream_max_recv_window_size
;
220 SpdySession::TimeFunc time_func
;
221 NextProtoVector next_protos
;
222 std::string trusted_spdy_proxy
;
223 bool use_alternative_services
;
227 class SpdyURLRequestContext
: public URLRequestContext
{
229 explicit SpdyURLRequestContext(NextProto protocol
);
230 ~SpdyURLRequestContext() override
;
232 MockClientSocketFactory
& socket_factory() { return socket_factory_
; }
235 MockClientSocketFactory socket_factory_
;
236 URLRequestContextStorage storage_
;
239 // Equivalent to pool->GetIfExists(spdy_session_key, BoundNetLog()) != NULL.
240 bool HasSpdySession(SpdySessionPool
* pool
, const SpdySessionKey
& key
);
242 // Creates a SPDY session for the given key and puts it in the SPDY
243 // session pool in |http_session|. A SPDY session for |key| must not
245 base::WeakPtr
<SpdySession
> CreateInsecureSpdySession(
246 const scoped_refptr
<HttpNetworkSession
>& http_session
,
247 const SpdySessionKey
& key
,
248 const BoundNetLog
& net_log
);
250 // Tries to create a SPDY session for the given key but expects the
251 // attempt to fail with the given error. A SPDY session for |key| must
252 // not already exist. The session will be created but close in the
253 // next event loop iteration.
254 base::WeakPtr
<SpdySession
> TryCreateInsecureSpdySessionExpectingFailure(
255 const scoped_refptr
<HttpNetworkSession
>& http_session
,
256 const SpdySessionKey
& key
,
257 Error expected_error
,
258 const BoundNetLog
& net_log
);
260 // Like CreateInsecureSpdySession(), but uses TLS.
261 base::WeakPtr
<SpdySession
> CreateSecureSpdySession(
262 const scoped_refptr
<HttpNetworkSession
>& http_session
,
263 const SpdySessionKey
& key
,
264 const BoundNetLog
& net_log
);
266 // Creates an insecure SPDY session for the given key and puts it in
267 // |pool|. The returned session will neither receive nor send any
268 // data. A SPDY session for |key| must not already exist.
269 base::WeakPtr
<SpdySession
> CreateFakeSpdySession(SpdySessionPool
* pool
,
270 const SpdySessionKey
& key
);
272 // Tries to create an insecure SPDY session for the given key but
273 // expects the attempt to fail with the given error. The session will
274 // neither receive nor send any data. A SPDY session for |key| must
275 // not already exist. The session will be created but close in the
276 // next event loop iteration.
277 base::WeakPtr
<SpdySession
> TryCreateFakeSpdySessionExpectingFailure(
278 SpdySessionPool
* pool
,
279 const SpdySessionKey
& key
,
280 Error expected_error
);
282 class SpdySessionPoolPeer
{
284 explicit SpdySessionPoolPeer(SpdySessionPool
* pool
);
286 void RemoveAliases(const SpdySessionKey
& key
);
287 void DisableDomainAuthenticationVerification();
288 void SetEnableSendingInitialData(bool enabled
);
289 void SetSessionMaxRecvWindowSize(size_t window
);
290 void SetStreamInitialRecvWindowSize(size_t window
);
293 SpdySessionPool
* const pool_
;
295 DISALLOW_COPY_AND_ASSIGN(SpdySessionPoolPeer
);
300 explicit SpdyTestUtil(NextProto protocol
);
302 // Add the appropriate headers to put |url| into |block|.
303 void AddUrlToHeaderBlock(base::StringPiece url
,
304 SpdyHeaderBlock
* headers
) const;
306 scoped_ptr
<SpdyHeaderBlock
> ConstructGetHeaderBlock(
307 base::StringPiece url
) const;
308 scoped_ptr
<SpdyHeaderBlock
> ConstructGetHeaderBlockForProxy(
309 base::StringPiece url
) const;
310 scoped_ptr
<SpdyHeaderBlock
> ConstructHeadHeaderBlock(
311 base::StringPiece url
,
312 int64 content_length
) const;
313 scoped_ptr
<SpdyHeaderBlock
> ConstructPostHeaderBlock(
314 base::StringPiece url
,
315 int64 content_length
) const;
316 scoped_ptr
<SpdyHeaderBlock
> ConstructPutHeaderBlock(
317 base::StringPiece url
,
318 int64 content_length
) const;
320 // Construct a SPDY frame. If it is a SYN_STREAM or SYN_REPLY frame (as
321 // specified in header_info.kind), the provided headers are included in the
323 SpdyFrame
* ConstructSpdyFrame(
324 const SpdyHeaderInfo
& header_info
,
325 scoped_ptr
<SpdyHeaderBlock
> headers
) const;
327 // Construct a SPDY frame. If it is a SYN_STREAM or SYN_REPLY frame (as
328 // specified in header_info.kind), the headers provided in extra_headers and
329 // (if non-NULL) tail_headers are concatenated and included in the frame.
330 // (extra_headers must always be non-NULL.)
331 SpdyFrame
* ConstructSpdyFrame(const SpdyHeaderInfo
& header_info
,
332 const char* const extra_headers
[],
333 int extra_header_count
,
334 const char* const tail_headers
[],
335 int tail_header_count
) const;
337 // Construct a generic SpdyControlFrame.
338 SpdyFrame
* ConstructSpdyControlFrame(
339 scoped_ptr
<SpdyHeaderBlock
> headers
,
341 SpdyStreamId stream_id
,
342 RequestPriority request_priority
,
344 SpdyControlFlags flags
,
345 SpdyStreamId associated_stream_id
) const;
347 // Construct a generic SpdyControlFrame.
349 // Warning: extra_header_count is the number of header-value pairs in
350 // extra_headers (so half the number of elements), but tail_headers_size is
351 // the actual number of elements (both keys and values) in tail_headers.
352 // TODO(ttuttle): Fix this inconsistency.
353 SpdyFrame
* ConstructSpdyControlFrame(
354 const char* const extra_headers
[],
355 int extra_header_count
,
357 SpdyStreamId stream_id
,
358 RequestPriority request_priority
,
360 SpdyControlFlags flags
,
361 const char* const* tail_headers
,
362 int tail_headers_size
,
363 SpdyStreamId associated_stream_id
) const;
365 // Construct an expected SPDY reply string from the given headers.
366 std::string
ConstructSpdyReplyString(const SpdyHeaderBlock
& headers
) const;
368 // Construct an expected SPDY SETTINGS frame.
369 // |settings| are the settings to set.
370 // Returns the constructed frame. The caller takes ownership of the frame.
371 SpdyFrame
* ConstructSpdySettings(const SettingsMap
& settings
) const;
373 // Constructs an expected SPDY SETTINGS acknowledgement frame, if the protocol
374 // version is SPDY4 or higher, or an empty placeholder frame otherwise.
375 SpdyFrame
* ConstructSpdySettingsAck() const;
377 // Construct a SPDY PING frame.
378 // Returns the constructed frame. The caller takes ownership of the frame.
379 SpdyFrame
* ConstructSpdyPing(uint32 ping_id
, bool is_ack
) const;
381 // Construct a SPDY GOAWAY frame with last_good_stream_id = 0.
382 // Returns the constructed frame. The caller takes ownership of the frame.
383 SpdyFrame
* ConstructSpdyGoAway() const;
385 // Construct a SPDY GOAWAY frame with the specified last_good_stream_id.
386 // Returns the constructed frame. The caller takes ownership of the frame.
387 SpdyFrame
* ConstructSpdyGoAway(SpdyStreamId last_good_stream_id
) const;
389 // Construct a SPDY GOAWAY frame with the specified last_good_stream_id,
390 // status, and description. Returns the constructed frame. The caller takes
391 // ownership of the frame.
392 SpdyFrame
* ConstructSpdyGoAway(SpdyStreamId last_good_stream_id
,
393 SpdyGoAwayStatus status
,
394 const std::string
& desc
) const;
396 // Construct a SPDY WINDOW_UPDATE frame.
397 // Returns the constructed frame. The caller takes ownership of the frame.
398 SpdyFrame
* ConstructSpdyWindowUpdate(
399 SpdyStreamId stream_id
,
400 uint32 delta_window_size
) const;
402 // Construct a SPDY RST_STREAM frame.
403 // Returns the constructed frame. The caller takes ownership of the frame.
404 SpdyFrame
* ConstructSpdyRstStream(SpdyStreamId stream_id
,
405 SpdyRstStreamStatus status
) const;
407 // Constructs a standard SPDY GET SYN frame, optionally compressed
409 // |extra_headers| are the extra header-value pairs, which typically
410 // will vary the most between calls.
411 // Returns a SpdyFrame.
412 SpdyFrame
* ConstructSpdyGet(const char* const url
,
414 SpdyStreamId stream_id
,
415 RequestPriority request_priority
) const;
417 SpdyFrame
* ConstructSpdyGetForProxy(const char* const url
,
419 SpdyStreamId stream_id
,
420 RequestPriority request_priority
) const;
422 // Constructs a standard SPDY GET SYN frame, optionally compressed.
423 // |extra_headers| are the extra header-value pairs, which typically
424 // will vary the most between calls. If |direct| is false, the
425 // the full url will be used instead of simply the path.
426 // Returns a SpdyFrame.
427 SpdyFrame
* ConstructSpdyGet(const char* const extra_headers
[],
428 int extra_header_count
,
431 RequestPriority request_priority
,
434 // Constructs a standard SPDY SYN_STREAM frame for a CONNECT request.
435 SpdyFrame
* ConstructSpdyConnect(const char* const extra_headers
[],
436 int extra_header_count
,
438 RequestPriority priority
,
439 const HostPortPair
& host_port_pair
) const;
441 // Constructs a standard SPDY push SYN frame.
442 // |extra_headers| are the extra header-value pairs, which typically
443 // will vary the most between calls.
444 // Returns a SpdyFrame.
445 SpdyFrame
* ConstructSpdyPush(const char* const extra_headers
[],
446 int extra_header_count
,
448 int associated_stream_id
,
450 SpdyFrame
* ConstructSpdyPush(const char* const extra_headers
[],
451 int extra_header_count
,
453 int associated_stream_id
,
456 const char* location
);
458 SpdyFrame
* ConstructInitialSpdyPushFrame(scoped_ptr
<SpdyHeaderBlock
> headers
,
460 int associated_stream_id
);
462 SpdyFrame
* ConstructSpdyPushHeaders(int stream_id
,
463 const char* const extra_headers
[],
464 int extra_header_count
);
466 SpdyFrame
* ConstructSpdyHeaderFrame(int stream_id
,
467 const char* const headers
[],
470 // Construct a SPDY syn (HEADERS or SYN_STREAM, depending on protocol
471 // version) carrying exactly the given headers and priority.
472 SpdyFrame
* ConstructSpdySyn(int stream_id
,
473 const SpdyHeaderBlock
& headers
,
474 RequestPriority priority
,
478 // Construct a SPDY reply (HEADERS or SYN_REPLY, depending on protocol
479 // version) carrying exactly the given headers, and the default priority
480 // (or no priority, depending on protocl version).
481 // The |headers| parameter variant is preferred.
482 SpdyFrame
* ConstructSpdyReply(int stream_id
, const SpdyHeaderBlock
& headers
);
484 // Constructs a standard SPDY SYN_REPLY frame to match the SPDY GET.
485 // |extra_headers| are the extra header-value pairs, which typically
486 // will vary the most between calls.
487 // Returns a SpdyFrame.
488 SpdyFrame
* ConstructSpdyGetSynReply(const char* const extra_headers
[],
489 int extra_header_count
,
492 // Constructs a standard SPDY SYN_REPLY frame to match the SPDY GET.
493 // |extra_headers| are the extra header-value pairs, which typically
494 // will vary the most between calls.
495 // Returns a SpdyFrame.
496 SpdyFrame
* ConstructSpdyGetSynReplyRedirect(int stream_id
);
498 // Constructs a standard SPDY SYN_REPLY frame with an Internal Server
499 // Error status code.
500 // Returns a SpdyFrame.
501 SpdyFrame
* ConstructSpdySynReplyError(int stream_id
);
503 // Constructs a standard SPDY SYN_REPLY frame with the specified status code.
504 // Returns a SpdyFrame.
505 SpdyFrame
* ConstructSpdySynReplyError(const char* const status
,
506 const char* const* const extra_headers
,
507 int extra_header_count
,
510 // Constructs a standard SPDY POST SYN frame.
511 // |extra_headers| are the extra header-value pairs, which typically
512 // will vary the most between calls.
513 // Returns a SpdyFrame.
514 SpdyFrame
* ConstructSpdyPost(const char* url
,
515 SpdyStreamId stream_id
,
516 int64 content_length
,
517 RequestPriority priority
,
518 const char* const extra_headers
[],
519 int extra_header_count
);
521 // Constructs a chunked transfer SPDY POST SYN frame.
522 // |extra_headers| are the extra header-value pairs, which typically
523 // will vary the most between calls.
524 // Returns a SpdyFrame.
525 SpdyFrame
* ConstructChunkedSpdyPost(const char* const extra_headers
[],
526 int extra_header_count
);
528 // Constructs a standard SPDY SYN_REPLY frame to match the SPDY POST.
529 // |extra_headers| are the extra header-value pairs, which typically
530 // will vary the most between calls.
531 // Returns a SpdyFrame.
532 SpdyFrame
* ConstructSpdyPostSynReply(const char* const extra_headers
[],
533 int extra_header_count
);
535 // Constructs a single SPDY data frame with the contents "hello!"
536 SpdyFrame
* ConstructSpdyBodyFrame(int stream_id
,
539 // Constructs a single SPDY data frame with the given content.
540 SpdyFrame
* ConstructSpdyBodyFrame(int stream_id
, const char* data
,
541 uint32 len
, bool fin
);
543 // Constructs a single SPDY data frame with the given content and padding.
544 SpdyFrame
* ConstructSpdyBodyFrame(int stream_id
,
550 // Wraps |frame| in the payload of a data frame in stream |stream_id|.
551 SpdyFrame
* ConstructWrappedSpdyFrame(const scoped_ptr
<SpdyFrame
>& frame
,
554 const SpdyHeaderInfo
MakeSpdyHeader(SpdyFrameType type
);
556 // For versions below SPDY4, adds the version HTTP/1.1 header.
557 void MaybeAddVersionHeader(SpdyFrameWithHeaderBlockIR
* frame_ir
) const;
558 void MaybeAddVersionHeader(SpdyHeaderBlock
* block
) const;
560 // Maps |priority| to SPDY version priority, and sets it on |frame_ir|.
561 void SetPriority(RequestPriority priority
, SpdySynStreamIR
* frame_ir
) const;
563 NextProto
protocol() const { return protocol_
; }
564 SpdyMajorVersion
spdy_version() const { return spdy_version_
; }
565 bool include_version_header() const { return protocol_
< kProtoHTTP2
; }
566 scoped_ptr
<SpdyFramer
> CreateFramer(bool compressed
) const;
568 const GURL
& default_url() const { return default_url_
; }
569 void set_default_url(const GURL
& url
) { default_url_
= url
; }
571 const char* GetMethodKey() const;
572 const char* GetStatusKey() const;
573 const char* GetHostKey() const;
574 const char* GetSchemeKey() const;
575 const char* GetVersionKey() const;
576 const char* GetPathKey() const;
579 // |content_length| may be NULL, in which case the content-length
580 // header will be omitted.
581 scoped_ptr
<SpdyHeaderBlock
> ConstructHeaderBlock(
582 base::StringPiece method
,
583 base::StringPiece url
,
584 int64
* content_length
) const;
586 const NextProto protocol_
;
587 const SpdyMajorVersion spdy_version_
;
593 #endif // NET_SPDY_SPDY_TEST_UTIL_COMMON_H_