Update Polymer and pull in iron-list
[chromium-blink-merge.git] / net / spdy / spdy_test_util_common.h
blob68bdfdf329666df99c4824f895991fbfd52f81df
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_
8 #include <string>
9 #include <vector>
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"
34 class GURL;
36 namespace net {
38 class BoundNetLog;
39 class SpdySession;
40 class SpdySessionKey;
41 class SpdySessionPool;
42 class SpdyStream;
43 class SpdyStreamRequest;
45 // Default upload data used by both, mock objects and framer when creating
46 // data frames.
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
52 // SPDY.
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
80 // header values.
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
103 // the total length.
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
114 // on failure.
115 base::WeakPtr<SpdyStream> CreateStreamSynchronously(
116 SpdyStreamType type,
117 const base::WeakPtr<SpdySession>& session,
118 const GURL& url,
119 RequestPriority priority,
120 const BoundNetLog& net_log);
122 // Helper class used by some tests to release a stream as soon as it's
123 // created.
124 class StreamReleaserCallback : public TestCompletionCallbackBase {
125 public:
126 StreamReleaserCallback();
128 ~StreamReleaserCallback() override;
130 // Returns a callback that releases |request|'s stream.
131 CompletionCallback MakeCallback(SpdyStreamRequest* request);
133 private:
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 {
141 SpdyFrameType kind;
142 SpdyStreamId id;
143 SpdyStreamId assoc_id;
144 SpdyPriority priority;
145 size_t credential_slot; // SPDY3 only
146 SpdyControlFlags control_flags;
147 bool compressed;
148 SpdyRstStreamStatus status;
149 const char* data;
150 uint32 data_length;
151 SpdyDataFlags data_flags;
154 // An ECSignatureCreator that returns deterministic signatures.
155 class MockECSignatureCreator : public crypto::ECSignatureCreator {
156 public:
157 explicit MockECSignatureCreator(crypto::ECPrivateKey* key);
159 // crypto::ECSignatureCreator
160 bool Sign(const uint8* data,
161 int data_len,
162 std::vector<uint8>* signature) override;
163 bool DecodeSignature(const std::vector<uint8>& signature,
164 std::vector<uint8>* out_raw_sig) override;
166 private:
167 crypto::ECPrivateKey* key_;
169 DISALLOW_COPY_AND_ASSIGN(MockECSignatureCreator);
172 // An ECSignatureCreatorFactory creates MockECSignatureCreator.
173 class MockECSignatureCreatorFactory : public crypto::ECSignatureCreatorFactory {
174 public:
175 MockECSignatureCreatorFactory();
176 ~MockECSignatureCreatorFactory() override;
178 // crypto::ECSignatureCreatorFactory
179 crypto::ECSignatureCreator* Create(crypto::ECPrivateKey* key) override;
181 private:
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,
193 scoped_ptr<ProxyService> proxy_service);
195 ~SpdySessionDependencies();
197 static HttpNetworkSession* SpdyCreateSession(
198 SpdySessionDependencies* session_deps);
199 static HttpNetworkSession* SpdyCreateSessionDeterministic(
200 SpdySessionDependencies* session_deps);
201 static HttpNetworkSession::Params CreateSessionParams(
202 SpdySessionDependencies* session_deps);
204 // NOTE: host_resolver must be ordered before http_auth_handler_factory.
205 scoped_ptr<MockHostResolverBase> host_resolver;
206 scoped_ptr<CertVerifier> cert_verifier;
207 scoped_ptr<TransportSecurityState> transport_security_state;
208 scoped_ptr<ProxyService> proxy_service;
209 scoped_refptr<SSLConfigService> ssl_config_service;
210 scoped_ptr<MockClientSocketFactory> socket_factory;
211 scoped_ptr<DeterministicMockClientSocketFactory> deterministic_socket_factory;
212 scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory;
213 HttpServerPropertiesImpl http_server_properties;
214 bool enable_ip_pooling;
215 bool enable_compression;
216 bool enable_ping;
217 bool enable_user_alternate_protocol_ports;
218 NextProto protocol;
219 size_t session_max_recv_window_size;
220 size_t stream_max_recv_window_size;
221 SpdySession::TimeFunc time_func;
222 NextProtoVector next_protos;
223 std::string trusted_spdy_proxy;
224 bool use_alternative_services;
225 NetLog* net_log;
228 class SpdyURLRequestContext : public URLRequestContext {
229 public:
230 explicit SpdyURLRequestContext(NextProto protocol);
231 ~SpdyURLRequestContext() override;
233 MockClientSocketFactory& socket_factory() { return socket_factory_; }
235 private:
236 MockClientSocketFactory socket_factory_;
237 URLRequestContextStorage storage_;
240 // Equivalent to pool->GetIfExists(spdy_session_key, BoundNetLog()) != NULL.
241 bool HasSpdySession(SpdySessionPool* pool, const SpdySessionKey& key);
243 // Creates a SPDY session for the given key and puts it in the SPDY
244 // session pool in |http_session|. A SPDY session for |key| must not
245 // already exist.
246 base::WeakPtr<SpdySession> CreateInsecureSpdySession(
247 const scoped_refptr<HttpNetworkSession>& http_session,
248 const SpdySessionKey& key,
249 const BoundNetLog& net_log);
251 // Tries to create a SPDY session for the given key but expects the
252 // attempt to fail with the given error. A SPDY session for |key| must
253 // not already exist. The session will be created but close in the
254 // next event loop iteration.
255 base::WeakPtr<SpdySession> TryCreateInsecureSpdySessionExpectingFailure(
256 const scoped_refptr<HttpNetworkSession>& http_session,
257 const SpdySessionKey& key,
258 Error expected_error,
259 const BoundNetLog& net_log);
261 // Like CreateInsecureSpdySession(), but uses TLS.
262 base::WeakPtr<SpdySession> CreateSecureSpdySession(
263 const scoped_refptr<HttpNetworkSession>& http_session,
264 const SpdySessionKey& key,
265 const BoundNetLog& net_log);
267 // Creates an insecure SPDY session for the given key and puts it in
268 // |pool|. The returned session will neither receive nor send any
269 // data. A SPDY session for |key| must not already exist.
270 base::WeakPtr<SpdySession> CreateFakeSpdySession(SpdySessionPool* pool,
271 const SpdySessionKey& key);
273 // Tries to create an insecure SPDY session for the given key but
274 // expects the attempt to fail with the given error. The session will
275 // neither receive nor send any data. A SPDY session for |key| must
276 // not already exist. The session will be created but close in the
277 // next event loop iteration.
278 base::WeakPtr<SpdySession> TryCreateFakeSpdySessionExpectingFailure(
279 SpdySessionPool* pool,
280 const SpdySessionKey& key,
281 Error expected_error);
283 class SpdySessionPoolPeer {
284 public:
285 explicit SpdySessionPoolPeer(SpdySessionPool* pool);
287 void RemoveAliases(const SpdySessionKey& key);
288 void DisableDomainAuthenticationVerification();
289 void SetEnableSendingInitialData(bool enabled);
290 void SetSessionMaxRecvWindowSize(size_t window);
291 void SetStreamInitialRecvWindowSize(size_t window);
293 private:
294 SpdySessionPool* const pool_;
296 DISALLOW_COPY_AND_ASSIGN(SpdySessionPoolPeer);
299 class SpdyTestUtil {
300 public:
301 explicit SpdyTestUtil(NextProto protocol);
303 // Add the appropriate headers to put |url| into |block|.
304 void AddUrlToHeaderBlock(base::StringPiece url,
305 SpdyHeaderBlock* headers) const;
307 scoped_ptr<SpdyHeaderBlock> ConstructGetHeaderBlock(
308 base::StringPiece url) const;
309 scoped_ptr<SpdyHeaderBlock> ConstructGetHeaderBlockForProxy(
310 base::StringPiece url) const;
311 scoped_ptr<SpdyHeaderBlock> ConstructHeadHeaderBlock(
312 base::StringPiece url,
313 int64 content_length) const;
314 scoped_ptr<SpdyHeaderBlock> ConstructPostHeaderBlock(
315 base::StringPiece url,
316 int64 content_length) const;
317 scoped_ptr<SpdyHeaderBlock> ConstructPutHeaderBlock(
318 base::StringPiece url,
319 int64 content_length) const;
321 // Construct a SPDY frame. If it is a SYN_STREAM or SYN_REPLY frame (as
322 // specified in header_info.kind), the provided headers are included in the
323 // frame.
324 SpdyFrame* ConstructSpdyFrame(
325 const SpdyHeaderInfo& header_info,
326 scoped_ptr<SpdyHeaderBlock> headers) const;
328 // Construct a SPDY frame. If it is a SYN_STREAM or SYN_REPLY frame (as
329 // specified in header_info.kind), the headers provided in extra_headers and
330 // (if non-NULL) tail_headers are concatenated and included in the frame.
331 // (extra_headers must always be non-NULL.)
332 SpdyFrame* ConstructSpdyFrame(const SpdyHeaderInfo& header_info,
333 const char* const extra_headers[],
334 int extra_header_count,
335 const char* const tail_headers[],
336 int tail_header_count) const;
338 // Construct a generic SpdyControlFrame.
339 SpdyFrame* ConstructSpdyControlFrame(
340 scoped_ptr<SpdyHeaderBlock> headers,
341 bool compressed,
342 SpdyStreamId stream_id,
343 RequestPriority request_priority,
344 SpdyFrameType type,
345 SpdyControlFlags flags,
346 SpdyStreamId associated_stream_id) const;
348 // Construct a generic SpdyControlFrame.
350 // Warning: extra_header_count is the number of header-value pairs in
351 // extra_headers (so half the number of elements), but tail_headers_size is
352 // the actual number of elements (both keys and values) in tail_headers.
353 // TODO(ttuttle): Fix this inconsistency.
354 SpdyFrame* ConstructSpdyControlFrame(
355 const char* const extra_headers[],
356 int extra_header_count,
357 bool compressed,
358 SpdyStreamId stream_id,
359 RequestPriority request_priority,
360 SpdyFrameType type,
361 SpdyControlFlags flags,
362 const char* const* tail_headers,
363 int tail_headers_size,
364 SpdyStreamId associated_stream_id) const;
366 // Construct an expected SPDY reply string from the given headers.
367 std::string ConstructSpdyReplyString(const SpdyHeaderBlock& headers) const;
369 // Construct an expected SPDY SETTINGS frame.
370 // |settings| are the settings to set.
371 // Returns the constructed frame. The caller takes ownership of the frame.
372 SpdyFrame* ConstructSpdySettings(const SettingsMap& settings) const;
374 // Constructs an expected SPDY SETTINGS acknowledgement frame, if the protocol
375 // version is SPDY4 or higher, or an empty placeholder frame otherwise.
376 SpdyFrame* ConstructSpdySettingsAck() const;
378 // Construct a SPDY PING frame.
379 // Returns the constructed frame. The caller takes ownership of the frame.
380 SpdyFrame* ConstructSpdyPing(uint32 ping_id, bool is_ack) const;
382 // Construct a SPDY GOAWAY frame with last_good_stream_id = 0.
383 // Returns the constructed frame. The caller takes ownership of the frame.
384 SpdyFrame* ConstructSpdyGoAway() const;
386 // Construct a SPDY GOAWAY frame with the specified last_good_stream_id.
387 // Returns the constructed frame. The caller takes ownership of the frame.
388 SpdyFrame* ConstructSpdyGoAway(SpdyStreamId last_good_stream_id) const;
390 // Construct a SPDY GOAWAY frame with the specified last_good_stream_id,
391 // status, and description. Returns the constructed frame. The caller takes
392 // ownership of the frame.
393 SpdyFrame* ConstructSpdyGoAway(SpdyStreamId last_good_stream_id,
394 SpdyGoAwayStatus status,
395 const std::string& desc) const;
397 // Construct a SPDY WINDOW_UPDATE frame.
398 // Returns the constructed frame. The caller takes ownership of the frame.
399 SpdyFrame* ConstructSpdyWindowUpdate(
400 SpdyStreamId stream_id,
401 uint32 delta_window_size) const;
403 // Construct a SPDY RST_STREAM frame.
404 // Returns the constructed frame. The caller takes ownership of the frame.
405 SpdyFrame* ConstructSpdyRstStream(SpdyStreamId stream_id,
406 SpdyRstStreamStatus status) const;
408 // Constructs a standard SPDY GET SYN frame, optionally compressed
409 // for |url|.
410 // |extra_headers| are the extra header-value pairs, which typically
411 // will vary the most between calls.
412 // Returns a SpdyFrame.
413 SpdyFrame* ConstructSpdyGet(const char* const url,
414 bool compressed,
415 SpdyStreamId stream_id,
416 RequestPriority request_priority) const;
418 SpdyFrame* ConstructSpdyGetForProxy(const char* const url,
419 bool compressed,
420 SpdyStreamId stream_id,
421 RequestPriority request_priority) const;
423 // Constructs a standard SPDY GET SYN frame, optionally compressed.
424 // |extra_headers| are the extra header-value pairs, which typically
425 // will vary the most between calls. If |direct| is false, the
426 // the full url will be used instead of simply the path.
427 // Returns a SpdyFrame.
428 SpdyFrame* ConstructSpdyGet(const char* const extra_headers[],
429 int extra_header_count,
430 bool compressed,
431 int stream_id,
432 RequestPriority request_priority,
433 bool direct) const;
435 // Constructs a standard SPDY SYN_STREAM frame for a CONNECT request.
436 SpdyFrame* ConstructSpdyConnect(const char* const extra_headers[],
437 int extra_header_count,
438 int stream_id,
439 RequestPriority priority,
440 const HostPortPair& host_port_pair) const;
442 // Constructs a standard SPDY push SYN frame.
443 // |extra_headers| are the extra header-value pairs, which typically
444 // will vary the most between calls.
445 // Returns a SpdyFrame.
446 SpdyFrame* ConstructSpdyPush(const char* const extra_headers[],
447 int extra_header_count,
448 int stream_id,
449 int associated_stream_id,
450 const char* url);
451 SpdyFrame* ConstructSpdyPush(const char* const extra_headers[],
452 int extra_header_count,
453 int stream_id,
454 int associated_stream_id,
455 const char* url,
456 const char* status,
457 const char* location);
459 SpdyFrame* ConstructInitialSpdyPushFrame(scoped_ptr<SpdyHeaderBlock> headers,
460 int stream_id,
461 int associated_stream_id);
463 SpdyFrame* ConstructSpdyPushHeaders(int stream_id,
464 const char* const extra_headers[],
465 int extra_header_count);
467 SpdyFrame* ConstructSpdyHeaderFrame(int stream_id,
468 const char* const headers[],
469 int header_count);
471 // Construct a SPDY syn (HEADERS or SYN_STREAM, depending on protocol
472 // version) carrying exactly the given headers and priority.
473 SpdyFrame* ConstructSpdySyn(int stream_id,
474 const SpdyHeaderBlock& headers,
475 RequestPriority priority,
476 bool compressed,
477 bool fin) const;
479 // Construct a SPDY reply (HEADERS or SYN_REPLY, depending on protocol
480 // version) carrying exactly the given headers, and the default priority
481 // (or no priority, depending on protocl version).
482 // The |headers| parameter variant is preferred.
483 SpdyFrame* ConstructSpdyReply(int stream_id, const SpdyHeaderBlock& headers);
485 // Constructs a standard SPDY SYN_REPLY frame to match the SPDY GET.
486 // |extra_headers| are the extra header-value pairs, which typically
487 // will vary the most between calls.
488 // Returns a SpdyFrame.
489 SpdyFrame* ConstructSpdyGetSynReply(const char* const extra_headers[],
490 int extra_header_count,
491 int stream_id);
493 // Constructs a standard SPDY SYN_REPLY frame to match the SPDY GET.
494 // |extra_headers| are the extra header-value pairs, which typically
495 // will vary the most between calls.
496 // Returns a SpdyFrame.
497 SpdyFrame* ConstructSpdyGetSynReplyRedirect(int stream_id);
499 // Constructs a standard SPDY SYN_REPLY frame with an Internal Server
500 // Error status code.
501 // Returns a SpdyFrame.
502 SpdyFrame* ConstructSpdySynReplyError(int stream_id);
504 // Constructs a standard SPDY SYN_REPLY frame with the specified status code.
505 // Returns a SpdyFrame.
506 SpdyFrame* ConstructSpdySynReplyError(const char* const status,
507 const char* const* const extra_headers,
508 int extra_header_count,
509 int stream_id);
511 // Constructs a standard SPDY POST SYN frame.
512 // |extra_headers| are the extra header-value pairs, which typically
513 // will vary the most between calls.
514 // Returns a SpdyFrame.
515 SpdyFrame* ConstructSpdyPost(const char* url,
516 SpdyStreamId stream_id,
517 int64 content_length,
518 RequestPriority priority,
519 const char* const extra_headers[],
520 int extra_header_count);
522 // Constructs a chunked transfer SPDY POST SYN frame.
523 // |extra_headers| are the extra header-value pairs, which typically
524 // will vary the most between calls.
525 // Returns a SpdyFrame.
526 SpdyFrame* ConstructChunkedSpdyPost(const char* const extra_headers[],
527 int extra_header_count);
529 // Constructs a standard SPDY SYN_REPLY frame to match the SPDY POST.
530 // |extra_headers| are the extra header-value pairs, which typically
531 // will vary the most between calls.
532 // Returns a SpdyFrame.
533 SpdyFrame* ConstructSpdyPostSynReply(const char* const extra_headers[],
534 int extra_header_count);
536 // Constructs a single SPDY data frame with the contents "hello!"
537 SpdyFrame* ConstructSpdyBodyFrame(int stream_id,
538 bool fin);
540 // Constructs a single SPDY data frame with the given content.
541 SpdyFrame* ConstructSpdyBodyFrame(int stream_id, const char* data,
542 uint32 len, bool fin);
544 // Constructs a single SPDY data frame with the given content and padding.
545 SpdyFrame* ConstructSpdyBodyFrame(int stream_id,
546 const char* data,
547 uint32 len,
548 bool fin,
549 int padding_length);
551 // Wraps |frame| in the payload of a data frame in stream |stream_id|.
552 SpdyFrame* ConstructWrappedSpdyFrame(const scoped_ptr<SpdyFrame>& frame,
553 int stream_id);
555 const SpdyHeaderInfo MakeSpdyHeader(SpdyFrameType type);
557 // For versions below SPDY4, adds the version HTTP/1.1 header.
558 void MaybeAddVersionHeader(SpdyFrameWithHeaderBlockIR* frame_ir) const;
559 void MaybeAddVersionHeader(SpdyHeaderBlock* block) const;
561 // Maps |priority| to SPDY version priority, and sets it on |frame_ir|.
562 void SetPriority(RequestPriority priority, SpdySynStreamIR* frame_ir) const;
564 NextProto protocol() const { return protocol_; }
565 SpdyMajorVersion spdy_version() const { return spdy_version_; }
566 bool include_version_header() const { return protocol_ < kProtoHTTP2; }
567 scoped_ptr<SpdyFramer> CreateFramer(bool compressed) const;
569 const GURL& default_url() const { return default_url_; }
570 void set_default_url(const GURL& url) { default_url_ = url; }
572 const char* GetMethodKey() const;
573 const char* GetStatusKey() const;
574 const char* GetHostKey() const;
575 const char* GetSchemeKey() const;
576 const char* GetVersionKey() const;
577 const char* GetPathKey() const;
579 private:
580 // |content_length| may be NULL, in which case the content-length
581 // header will be omitted.
582 scoped_ptr<SpdyHeaderBlock> ConstructHeaderBlock(
583 base::StringPiece method,
584 base::StringPiece url,
585 int64* content_length) const;
587 const NextProto protocol_;
588 const SpdyMajorVersion spdy_version_;
589 GURL default_url_;
592 } // namespace net
594 #endif // NET_SPDY_SPDY_TEST_UTIL_COMMON_H_