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