spdy_test_util.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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_SPDY_SPDY_TEST_UTIL_H_ 6#define NET_SPDY_SPDY_TEST_UTIL_H_ 7#pragma once 8 9#include "base/basictypes.h" 10#include "net/base/cert_verifier.h" 11#include "net/base/mock_host_resolver.h" 12#include "net/base/request_priority.h" 13#include "net/base/ssl_config_service_defaults.h" 14#include "net/http/http_auth_handler_factory.h" 15#include "net/http/http_cache.h" 16#include "net/http/http_network_session.h" 17#include "net/http/http_network_layer.h" 18#include "net/http/http_transaction_factory.h" 19#include "net/proxy/proxy_service.h" 20#include "net/socket/socket_test_util.h" 21#include "net/spdy/spdy_framer.h" 22#include "net/spdy/spdy_session_pool.h" 23#include "net/url_request/url_request_context.h" 24 25namespace net { 26 27// Default upload data used by both, mock objects and framer when creating 28// data frames. 29const char kDefaultURL[] = "http://www.google.com"; 30const char kUploadData[] = "hello!"; 31const int kUploadDataSize = arraysize(kUploadData)-1; 32 33// NOTE: In GCC, on a Mac, this can't be in an anonymous namespace! 34// This struct holds information used to construct spdy control and data frames. 35struct SpdyHeaderInfo { 36 spdy::SpdyControlType kind; 37 spdy::SpdyStreamId id; 38 spdy::SpdyStreamId assoc_id; 39 spdy::SpdyPriority priority; 40 spdy::SpdyControlFlags control_flags; 41 bool compressed; 42 spdy::SpdyStatusCodes status; 43 const char* data; 44 uint32 data_length; 45 spdy::SpdyDataFlags data_flags; 46}; 47 48// Chop a frame into an array of MockWrites. 49// |data| is the frame to chop. 50// |length| is the length of the frame to chop. 51// |num_chunks| is the number of chunks to create. 52MockWrite* ChopWriteFrame(const char* data, int length, int num_chunks); 53 54// Chop a SpdyFrame into an array of MockWrites. 55// |frame| is the frame to chop. 56// |num_chunks| is the number of chunks to create. 57MockWrite* ChopWriteFrame(const spdy::SpdyFrame& frame, int num_chunks); 58 59// Chop a frame into an array of MockReads. 60// |data| is the frame to chop. 61// |length| is the length of the frame to chop. 62// |num_chunks| is the number of chunks to create. 63MockRead* ChopReadFrame(const char* data, int length, int num_chunks); 64 65// Chop a SpdyFrame into an array of MockReads. 66// |frame| is the frame to chop. 67// |num_chunks| is the number of chunks to create. 68MockRead* ChopReadFrame(const spdy::SpdyFrame& frame, int num_chunks); 69 70// Adds headers and values to a map. 71// |extra_headers| is an array of { name, value } pairs, arranged as strings 72// where the even entries are the header names, and the odd entries are the 73// header values. 74// |headers| gets filled in from |extra_headers|. 75void AppendHeadersToSpdyFrame(const char* const extra_headers[], 76 int extra_header_count, 77 spdy::SpdyHeaderBlock* headers); 78 79// Writes |str| of the given |len| to the buffer pointed to by |buffer_handle|. 80// Uses a template so buffer_handle can be a char* or an unsigned char*. 81// Updates the |*buffer_handle| pointer by |len| 82// Returns the number of bytes written into *|buffer_handle| 83template<class T> 84int AppendToBuffer(const char* str, 85 int len, 86 T** buffer_handle, 87 int* buffer_len_remaining) { 88 DCHECK_GT(len, 0); 89 DCHECK(NULL != buffer_handle) << "NULL buffer handle"; 90 DCHECK(NULL != *buffer_handle) << "NULL pointer"; 91 DCHECK(NULL != buffer_len_remaining) 92 << "NULL buffer remainder length pointer"; 93 DCHECK_GE(*buffer_len_remaining, len) << "Insufficient buffer size"; 94 memcpy(*buffer_handle, str, len); 95 *buffer_handle += len; 96 *buffer_len_remaining -= len; 97 return len; 98} 99 100// Writes |val| to a location of size |len|, in big-endian format. 101// in the buffer pointed to by |buffer_handle|. 102// Updates the |*buffer_handle| pointer by |len| 103// Returns the number of bytes written 104int AppendToBuffer(int val, 105 int len, 106 unsigned char** buffer_handle, 107 int* buffer_len_remaining); 108 109// Construct a SPDY packet. 110// |head| is the start of the packet, up to but not including 111// the header value pairs. 112// |extra_headers| are the extra header-value pairs, which typically 113// will vary the most between calls. 114// |tail| is any (relatively constant) header-value pairs to add. 115// |buffer| is the buffer we're filling in. 116// Returns a SpdyFrame. 117spdy::SpdyFrame* ConstructSpdyPacket(const SpdyHeaderInfo& header_info, 118 const char* const extra_headers[], 119 int extra_header_count, 120 const char* const tail[], 121 int tail_header_count); 122 123// Construct a generic SpdyControlFrame. 124spdy::SpdyFrame* ConstructSpdyControlFrame(const char* const extra_headers[], 125 int extra_header_count, 126 bool compressed, 127 int stream_id, 128 RequestPriority request_priority, 129 spdy::SpdyControlType type, 130 spdy::SpdyControlFlags flags, 131 const char* const* kHeaders, 132 int kHeadersSize); 133spdy::SpdyFrame* ConstructSpdyControlFrame(const char* const extra_headers[], 134 int extra_header_count, 135 bool compressed, 136 int stream_id, 137 RequestPriority request_priority, 138 spdy::SpdyControlType type, 139 spdy::SpdyControlFlags flags, 140 const char* const* kHeaders, 141 int kHeadersSize, 142 int associated_stream_id); 143 144// Construct an expected SPDY reply string. 145// |extra_headers| are the extra header-value pairs, which typically 146// will vary the most between calls. 147// |buffer| is the buffer we're filling in. 148// Returns the number of bytes written into |buffer|. 149int ConstructSpdyReplyString(const char* const extra_headers[], 150 int extra_header_count, 151 char* buffer, 152 int buffer_length); 153 154// Construct an expected SPDY SETTINGS frame. 155// |settings| are the settings to set. 156// Returns the constructed frame. The caller takes ownership of the frame. 157spdy::SpdyFrame* ConstructSpdySettings(spdy::SpdySettings settings); 158 159// Construct a SPDY GOAWAY frame. 160// Returns the constructed frame. The caller takes ownership of the frame. 161spdy::SpdyFrame* ConstructSpdyGoAway(); 162 163// Construct a SPDY WINDOW_UPDATE frame. 164// Returns the constructed frame. The caller takes ownership of the frame. 165spdy::SpdyFrame* ConstructSpdyWindowUpdate(spdy::SpdyStreamId, 166 uint32 delta_window_size); 167 168// Construct a SPDY RST_STREAM frame. 169// Returns the constructed frame. The caller takes ownership of the frame. 170spdy::SpdyFrame* ConstructSpdyRstStream(spdy::SpdyStreamId stream_id, 171 spdy::SpdyStatusCodes status); 172 173// Construct a single SPDY header entry, for validation. 174// |extra_headers| are the extra header-value pairs. 175// |buffer| is the buffer we're filling in. 176// |index| is the index of the header we want. 177// Returns the number of bytes written into |buffer|. 178int ConstructSpdyHeader(const char* const extra_headers[], 179 int extra_header_count, 180 char* buffer, 181 int buffer_length, 182 int index); 183 184// Constructs a standard SPDY GET SYN packet, optionally compressed 185// for the url |url|. 186// |extra_headers| are the extra header-value pairs, which typically 187// will vary the most between calls. 188// Returns a SpdyFrame. 189spdy::SpdyFrame* ConstructSpdyGet(const char* const url, 190 bool compressed, 191 int stream_id, 192 RequestPriority request_priority); 193 194// Constructs a standard SPDY GET SYN packet, optionally compressed. 195// |extra_headers| are the extra header-value pairs, which typically 196// will vary the most between calls. 197// Returns a SpdyFrame. 198spdy::SpdyFrame* ConstructSpdyGet(const char* const extra_headers[], 199 int extra_header_count, 200 bool compressed, 201 int stream_id, 202 RequestPriority request_priority); 203 204// Constructs a standard SPDY GET SYN packet, optionally compressed. 205// |extra_headers| are the extra header-value pairs, which typically 206// will vary the most between calls. If |direct| is false, the 207// the full url will be used instead of simply the path. 208// Returns a SpdyFrame. 209spdy::SpdyFrame* ConstructSpdyGet(const char* const extra_headers[], 210 int extra_header_count, 211 bool compressed, 212 int stream_id, 213 RequestPriority request_priority, 214 bool direct); 215 216// Constructs a standard SPDY SYN_STREAM frame for a CONNECT request. 217spdy::SpdyFrame* ConstructSpdyConnect(const char* const extra_headers[], 218 int extra_header_count, 219 int stream_id); 220 221// Constructs a standard SPDY push SYN packet. 222// |extra_headers| are the extra header-value pairs, which typically 223// will vary the most between calls. 224// Returns a SpdyFrame. 225spdy::SpdyFrame* ConstructSpdyPush(const char* const extra_headers[], 226 int extra_header_count, 227 int stream_id, 228 int associated_stream_id); 229spdy::SpdyFrame* ConstructSpdyPush(const char* const extra_headers[], 230 int extra_header_count, 231 int stream_id, 232 int associated_stream_id, 233 const char* url); 234spdy::SpdyFrame* ConstructSpdyPush(const char* const extra_headers[], 235 int extra_header_count, 236 int stream_id, 237 int associated_stream_id, 238 const char* url, 239 const char* status, 240 const char* location); 241spdy::SpdyFrame* ConstructSpdyPush(int stream_id, 242 int associated_stream_id, 243 const char* url); 244 245spdy::SpdyFrame* ConstructSpdyPushHeaders(int stream_id, 246 const char* const extra_headers[], 247 int extra_header_count); 248 249// Constructs a standard SPDY SYN_REPLY packet to match the SPDY GET. 250// |extra_headers| are the extra header-value pairs, which typically 251// will vary the most between calls. 252// Returns a SpdyFrame. 253spdy::SpdyFrame* ConstructSpdyGetSynReply(const char* const extra_headers[], 254 int extra_header_count, 255 int stream_id); 256 257// Constructs a standard SPDY SYN_REPLY packet to match the SPDY GET. 258// |extra_headers| are the extra header-value pairs, which typically 259// will vary the most between calls. 260// Returns a SpdyFrame. 261spdy::SpdyFrame* ConstructSpdyGetSynReplyRedirect(int stream_id); 262 263// Constructs a standard SPDY SYN_REPLY packet with an Internal Server 264// Error status code. 265// Returns a SpdyFrame. 266spdy::SpdyFrame* ConstructSpdySynReplyError(int stream_id); 267 268// Constructs a standard SPDY SYN_REPLY packet with the specified status code. 269// Returns a SpdyFrame. 270spdy::SpdyFrame* ConstructSpdySynReplyError( 271 const char* const status, 272 const char* const* const extra_headers, 273 int extra_header_count, 274 int stream_id); 275 276// Constructs a standard SPDY POST SYN packet. 277// |extra_headers| are the extra header-value pairs, which typically 278// will vary the most between calls. 279// Returns a SpdyFrame. 280spdy::SpdyFrame* ConstructSpdyPost(int64 content_length, 281 const char* const extra_headers[], 282 int extra_header_count); 283 284// Constructs a standard SPDY SYN_REPLY packet to match the SPDY POST. 285// |extra_headers| are the extra header-value pairs, which typically 286// will vary the most between calls. 287// Returns a SpdyFrame. 288spdy::SpdyFrame* ConstructSpdyPostSynReply(const char* const extra_headers[], 289 int extra_header_count); 290 291// Constructs a single SPDY data frame with the contents "hello!" 292spdy::SpdyFrame* ConstructSpdyBodyFrame(int stream_id, 293 bool fin); 294 295// Constructs a single SPDY data frame with the given content. 296spdy::SpdyFrame* ConstructSpdyBodyFrame(int stream_id, const char* data, 297 uint32 len, bool fin); 298 299// Wraps |frame| in the payload of a data frame in stream |stream_id|. 300spdy::SpdyFrame* ConstructWrappedSpdyFrame( 301 const scoped_ptr<spdy::SpdyFrame>& frame, int stream_id); 302 303// Create an async MockWrite from the given SpdyFrame. 304MockWrite CreateMockWrite(const spdy::SpdyFrame& req); 305 306// Create an async MockWrite from the given SpdyFrame and sequence number. 307MockWrite CreateMockWrite(const spdy::SpdyFrame& req, int seq); 308 309MockWrite CreateMockWrite(const spdy::SpdyFrame& req, int seq, bool async); 310 311// Create a MockRead from the given SpdyFrame. 312MockRead CreateMockRead(const spdy::SpdyFrame& resp); 313 314// Create a MockRead from the given SpdyFrame and sequence number. 315MockRead CreateMockRead(const spdy::SpdyFrame& resp, int seq); 316 317MockRead CreateMockRead(const spdy::SpdyFrame& resp, int seq, bool async); 318 319// Combines the given SpdyFrames into the given char array and returns 320// the total length. 321int CombineFrames(const spdy::SpdyFrame** frames, int num_frames, 322 char* buff, int buff_len); 323 324// Helper to manage the lifetimes of the dependencies for a 325// HttpNetworkTransaction. 326class SpdySessionDependencies { 327 public: 328 // Default set of dependencies -- "null" proxy service. 329 SpdySessionDependencies() 330 : host_resolver(new MockHostResolver), 331 cert_verifier(new CertVerifier), 332 proxy_service(ProxyService::CreateDirect()), 333 ssl_config_service(new SSLConfigServiceDefaults), 334 socket_factory(new MockClientSocketFactory), 335 deterministic_socket_factory(new DeterministicMockClientSocketFactory), 336 http_auth_handler_factory( 337 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())) { 338 // Note: The CancelledTransaction test does cleanup by running all 339 // tasks in the message loop (RunAllPending). Unfortunately, that 340 // doesn't clean up tasks on the host resolver thread; and 341 // TCPConnectJob is currently not cancellable. Using synchronous 342 // lookups allows the test to shutdown cleanly. Until we have 343 // cancellable TCPConnectJobs, use synchronous lookups. 344 host_resolver->set_synchronous_mode(true); 345 } 346 347 // Custom proxy service dependency. 348 explicit SpdySessionDependencies(ProxyService* proxy_service) 349 : host_resolver(new MockHostResolver), 350 cert_verifier(new CertVerifier), 351 proxy_service(proxy_service), 352 ssl_config_service(new SSLConfigServiceDefaults), 353 socket_factory(new MockClientSocketFactory), 354 deterministic_socket_factory(new DeterministicMockClientSocketFactory), 355 http_auth_handler_factory( 356 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())) {} 357 358 // NOTE: host_resolver must be ordered before http_auth_handler_factory. 359 scoped_ptr<MockHostResolverBase> host_resolver; 360 scoped_ptr<CertVerifier> cert_verifier; 361 scoped_refptr<ProxyService> proxy_service; 362 scoped_refptr<SSLConfigService> ssl_config_service; 363 scoped_ptr<MockClientSocketFactory> socket_factory; 364 scoped_ptr<DeterministicMockClientSocketFactory> deterministic_socket_factory; 365 scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory; 366 367 static HttpNetworkSession* SpdyCreateSession( 368 SpdySessionDependencies* session_deps) { 369 return new HttpNetworkSession(session_deps->host_resolver.get(), 370 session_deps->cert_verifier.get(), 371 NULL /* dnsrr_resolver */, 372 NULL /* dns_cert_checker */, 373 NULL /* ssl_host_info_factory */, 374 session_deps->proxy_service, 375 session_deps->socket_factory.get(), 376 session_deps->ssl_config_service, 377 new SpdySessionPool(NULL), 378 session_deps->http_auth_handler_factory.get(), 379 NULL, 380 NULL); 381 } 382 static HttpNetworkSession* SpdyCreateSessionDeterministic( 383 SpdySessionDependencies* session_deps) { 384 return new HttpNetworkSession(session_deps->host_resolver.get(), 385 session_deps->cert_verifier.get(), 386 NULL /* dnsrr_resolver */, 387 NULL /* dns_cert_checker */, 388 NULL /* ssl_host_info_factory */, 389 session_deps->proxy_service, 390 session_deps-> 391 deterministic_socket_factory.get(), 392 session_deps->ssl_config_service, 393 new SpdySessionPool(NULL), 394 session_deps->http_auth_handler_factory.get(), 395 NULL, 396 NULL); 397 } 398}; 399 400class SpdyURLRequestContext : public URLRequestContext { 401 public: 402 SpdyURLRequestContext() { 403 host_resolver_ = new MockHostResolver(); 404 cert_verifier_ = new CertVerifier; 405 proxy_service_ = ProxyService::CreateDirect(); 406 ssl_config_service_ = new SSLConfigServiceDefaults; 407 http_auth_handler_factory_ = HttpAuthHandlerFactory::CreateDefault( 408 host_resolver_); 409 http_transaction_factory_ = new net::HttpCache( 410 new HttpNetworkLayer(&socket_factory_, 411 host_resolver_, 412 cert_verifier_, 413 NULL /* dnsrr_resolver */, 414 NULL /* dns_cert_checker */, 415 NULL /* ssl_host_info_factory */, 416 proxy_service_, 417 ssl_config_service_, 418 new SpdySessionPool(NULL), 419 http_auth_handler_factory_, 420 network_delegate_, 421 NULL), 422 net::HttpCache::DefaultBackend::InMemory(0)); 423 } 424 425 MockClientSocketFactory& socket_factory() { return socket_factory_; } 426 427 protected: 428 virtual ~SpdyURLRequestContext() { 429 delete http_transaction_factory_; 430 delete http_auth_handler_factory_; 431 delete cert_verifier_; 432 delete host_resolver_; 433 } 434 435 private: 436 MockClientSocketFactory socket_factory_; 437}; 438 439const SpdyHeaderInfo make_spdy_header(spdy::SpdyControlType type); 440} // namespace net 441 442#endif // NET_SPDY_SPDY_TEST_UTIL_H_ 443