Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(882)

Side by Side Diff: net/spdy/spdy_test_util_common.h

Issue 2832973003: Split net/spdy into core and chromium subdirectories. (Closed)
Patch Set: Fix some more build rules. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/spdy/spdy_stream_unittest.cc ('k') | net/spdy/spdy_test_util_common.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #ifndef NET_SPDY_SPDY_TEST_UTIL_COMMON_H_
6 #define NET_SPDY_SPDY_TEST_UTIL_COMMON_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <map>
12 #include <memory>
13 #include <vector>
14
15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h"
17 #include "crypto/ec_private_key.h"
18 #include "crypto/ec_signature_creator.h"
19 #include "net/base/completion_callback.h"
20 #include "net/base/proxy_delegate.h"
21 #include "net/base/request_priority.h"
22 #include "net/base/test_completion_callback.h"
23 #include "net/cert/cert_verifier.h"
24 #include "net/dns/mock_host_resolver.h"
25 #include "net/http/http_auth_handler_factory.h"
26 #include "net/http/http_network_session.h"
27 #include "net/http/http_response_info.h"
28 #include "net/http/http_server_properties_impl.h"
29 #include "net/http/transport_security_state.h"
30 #include "net/proxy/proxy_server.h"
31 #include "net/proxy/proxy_service.h"
32 #include "net/socket/socket_test_util.h"
33 #include "net/spdy/platform/api/spdy_string.h"
34 #include "net/spdy/platform/api/spdy_string_piece.h"
35 #include "net/spdy/spdy_protocol.h"
36 #include "net/ssl/ssl_config_service_defaults.h"
37 #include "net/url_request/url_request_context.h"
38 #include "net/url_request/url_request_context_storage.h"
39 #include "testing/gtest/include/gtest/gtest.h"
40
41 class GURL;
42
43 namespace net {
44
45 class CTVerifier;
46 class CTPolicyEnforcer;
47 class HostPortPair;
48 class NetLogWithSource;
49 class SpdySession;
50 class SpdySessionKey;
51 class SpdySessionPool;
52 class SpdyStream;
53 class SpdyStreamRequest;
54
55 // Default upload data used by both, mock objects and framer when creating
56 // data frames.
57 const char kDefaultUrl[] = "https://www.example.org/";
58 const char kUploadData[] = "hello!";
59 const int kUploadDataSize = arraysize(kUploadData)-1;
60
61 // Chop a SpdySerializedFrame 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 SpdySerializedFrame& frame, int num_chunks);
65
66 // Adds headers and values to a map.
67 // |extra_headers| is an array of { name, value } pairs, arranged as strings
68 // where the even entries are the header names, and the odd entries are the
69 // header values.
70 // |headers| gets filled in from |extra_headers|.
71 void AppendToHeaderBlock(const char* const extra_headers[],
72 int extra_header_count,
73 SpdyHeaderBlock* headers);
74
75 // Create an async MockWrite from the given SpdySerializedFrame.
76 MockWrite CreateMockWrite(const SpdySerializedFrame& req);
77
78 // Create an async MockWrite from the given SpdySerializedFrame and sequence
79 // number.
80 MockWrite CreateMockWrite(const SpdySerializedFrame& req, int seq);
81
82 MockWrite CreateMockWrite(const SpdySerializedFrame& req, int seq, IoMode mode);
83
84 // Create a MockRead from the given SpdySerializedFrame.
85 MockRead CreateMockRead(const SpdySerializedFrame& resp);
86
87 // Create a MockRead from the given SpdySerializedFrame and sequence number.
88 MockRead CreateMockRead(const SpdySerializedFrame& resp, int seq);
89
90 MockRead CreateMockRead(const SpdySerializedFrame& resp, int seq, IoMode mode);
91
92 // Combines the given SpdySerializedFrame into the given char array and returns
93 // the total length.
94 int CombineFrames(const SpdySerializedFrame** frames,
95 int num_frames,
96 char* buf,
97 int buf_len);
98
99 // Returns the SpdyPriority embedded in the given frame. Returns true
100 // and fills in |priority| on success.
101 bool GetSpdyPriority(const SpdySerializedFrame& frame, SpdyPriority* priority);
102
103 // Tries to create a stream in |session| synchronously. Returns NULL
104 // on failure.
105 base::WeakPtr<SpdyStream> CreateStreamSynchronously(
106 SpdyStreamType type,
107 const base::WeakPtr<SpdySession>& session,
108 const GURL& url,
109 RequestPriority priority,
110 const NetLogWithSource& net_log);
111
112 // Helper class used by some tests to release a stream as soon as it's
113 // created.
114 class StreamReleaserCallback : public TestCompletionCallbackBase {
115 public:
116 StreamReleaserCallback();
117
118 ~StreamReleaserCallback() override;
119
120 // Returns a callback that releases |request|'s stream.
121 CompletionCallback MakeCallback(SpdyStreamRequest* request);
122
123 private:
124 void OnComplete(SpdyStreamRequest* request, int result);
125 };
126
127 // This struct holds information used to construct spdy control and data frames.
128 struct SpdyHeaderInfo {
129 SpdyFrameType kind;
130 SpdyStreamId id;
131 SpdyStreamId assoc_id;
132 SpdyPriority priority;
133 int weight;
134 SpdyControlFlags control_flags;
135 SpdyErrorCode error_code;
136 const char* data;
137 uint32_t data_length;
138 SpdyDataFlags data_flags;
139 };
140
141 // An ECSignatureCreator that returns deterministic signatures.
142 class MockECSignatureCreator : public crypto::ECSignatureCreator {
143 public:
144 explicit MockECSignatureCreator(crypto::ECPrivateKey* key);
145
146 // crypto::ECSignatureCreator
147 bool Sign(const uint8_t* data,
148 int data_len,
149 std::vector<uint8_t>* signature) override;
150 bool DecodeSignature(const std::vector<uint8_t>& signature,
151 std::vector<uint8_t>* out_raw_sig) override;
152
153 private:
154 crypto::ECPrivateKey* key_;
155
156 DISALLOW_COPY_AND_ASSIGN(MockECSignatureCreator);
157 };
158
159 // An ECSignatureCreatorFactory creates MockECSignatureCreator.
160 class MockECSignatureCreatorFactory : public crypto::ECSignatureCreatorFactory {
161 public:
162 MockECSignatureCreatorFactory();
163 ~MockECSignatureCreatorFactory() override;
164
165 // crypto::ECSignatureCreatorFactory
166 std::unique_ptr<crypto::ECSignatureCreator> Create(
167 crypto::ECPrivateKey* key) override;
168
169 private:
170 DISALLOW_COPY_AND_ASSIGN(MockECSignatureCreatorFactory);
171 };
172
173 // Helper to manage the lifetimes of the dependencies for a
174 // HttpNetworkTransaction.
175 struct SpdySessionDependencies {
176 // Default set of dependencies -- "null" proxy service.
177 SpdySessionDependencies();
178
179 // Custom proxy service dependency.
180 explicit SpdySessionDependencies(std::unique_ptr<ProxyService> proxy_service);
181
182 ~SpdySessionDependencies();
183
184 static std::unique_ptr<HttpNetworkSession> SpdyCreateSession(
185 SpdySessionDependencies* session_deps);
186
187 // Variant that ignores session_deps->socket_factory, and uses the passed in
188 // |factory| instead.
189 static std::unique_ptr<HttpNetworkSession> SpdyCreateSessionWithSocketFactory(
190 SpdySessionDependencies* session_deps,
191 ClientSocketFactory* factory);
192 static HttpNetworkSession::Params CreateSessionParams(
193 SpdySessionDependencies* session_deps);
194
195 // NOTE: host_resolver must be ordered before http_auth_handler_factory.
196 std::unique_ptr<MockHostResolverBase> host_resolver;
197 std::unique_ptr<CertVerifier> cert_verifier;
198 std::unique_ptr<ChannelIDService> channel_id_service;
199 std::unique_ptr<TransportSecurityState> transport_security_state;
200 std::unique_ptr<CTVerifier> cert_transparency_verifier;
201 std::unique_ptr<CTPolicyEnforcer> ct_policy_enforcer;
202 std::unique_ptr<ProxyService> proxy_service;
203 scoped_refptr<SSLConfigService> ssl_config_service;
204 std::unique_ptr<MockClientSocketFactory> socket_factory;
205 std::unique_ptr<HttpAuthHandlerFactory> http_auth_handler_factory;
206 std::unique_ptr<HttpServerPropertiesImpl> http_server_properties;
207 bool enable_ip_pooling;
208 bool enable_ping;
209 bool enable_user_alternate_protocol_ports;
210 bool enable_quic;
211 bool enable_server_push_cancellation;
212 size_t session_max_recv_window_size;
213 SettingsMap http2_settings;
214 SpdySession::TimeFunc time_func;
215 std::unique_ptr<ProxyDelegate> proxy_delegate;
216 bool enable_http2_alternative_service;
217 NetLog* net_log;
218 bool http_09_on_non_default_ports_enabled;
219 bool restrict_to_one_preconnect_for_proxies;
220 bool quic_do_not_mark_as_broken_on_network_change;
221 };
222
223 class SpdyURLRequestContext : public URLRequestContext {
224 public:
225 SpdyURLRequestContext();
226 ~SpdyURLRequestContext() override;
227
228 MockClientSocketFactory& socket_factory() { return socket_factory_; }
229
230 private:
231 MockClientSocketFactory socket_factory_;
232 URLRequestContextStorage storage_;
233 };
234
235 // Equivalent to pool->GetIfExists(spdy_session_key, NetLogWithSource()) !=
236 // NULL.
237 bool HasSpdySession(SpdySessionPool* pool, const SpdySessionKey& key);
238
239 // Creates a SPDY session for the given key and puts it in the SPDY
240 // session pool in |http_session|. A SPDY session for |key| must not
241 // already exist.
242 base::WeakPtr<SpdySession> CreateInsecureSpdySession(
243 HttpNetworkSession* http_session,
244 const SpdySessionKey& key,
245 const NetLogWithSource& net_log);
246
247 // Tries to create a SPDY session for the given key but expects the
248 // attempt to fail with the given error. A SPDY session for |key| must
249 // not already exist. The session will be created but close in the
250 // next event loop iteration.
251 base::WeakPtr<SpdySession> TryCreateSpdySessionExpectingFailure(
252 HttpNetworkSession* http_session,
253 const SpdySessionKey& key,
254 Error expected_error,
255 const NetLogWithSource& net_log);
256
257 // Like CreateInsecureSpdySession(), but uses TLS.
258 base::WeakPtr<SpdySession> CreateSecureSpdySession(
259 HttpNetworkSession* http_session,
260 const SpdySessionKey& key,
261 const NetLogWithSource& net_log);
262
263 // Like CreateSecureSpdySession(), but does not fail if there is already an IP
264 // pooled session for |key|.
265 base::WeakPtr<SpdySession> CreateSecureSpdySessionWithIpBasedPoolingDisabled(
266 HttpNetworkSession* http_session,
267 const SpdySessionKey& key,
268 const NetLogWithSource& net_log);
269
270 // Creates an insecure SPDY session for the given key and puts it in
271 // |pool|. The returned session will neither receive nor send any
272 // data. A SPDY session for |key| must not already exist.
273 base::WeakPtr<SpdySession> CreateFakeSpdySession(SpdySessionPool* pool,
274 const SpdySessionKey& key);
275
276 // Tries to create an insecure SPDY session for the given key but
277 // expects the attempt to fail with the given error. The session will
278 // neither receive nor send any data. A SPDY session for |key| must
279 // not already exist. The session will be created but close in the
280 // next event loop iteration.
281 base::WeakPtr<SpdySession> TryCreateFakeSpdySessionExpectingFailure(
282 SpdySessionPool* pool,
283 const SpdySessionKey& key,
284 Error expected_error);
285
286 class SpdySessionPoolPeer {
287 public:
288 explicit SpdySessionPoolPeer(SpdySessionPool* pool);
289
290 void RemoveAliases(const SpdySessionKey& key);
291 void SetEnableSendingInitialData(bool enabled);
292
293 private:
294 SpdySessionPool* const pool_;
295
296 DISALLOW_COPY_AND_ASSIGN(SpdySessionPoolPeer);
297 };
298
299 class SpdyTestUtil {
300 public:
301 SpdyTestUtil();
302 ~SpdyTestUtil();
303
304 // Add the appropriate headers to put |url| into |block|.
305 void AddUrlToHeaderBlock(SpdyStringPiece url, SpdyHeaderBlock* headers) const;
306
307 static SpdyHeaderBlock ConstructGetHeaderBlock(SpdyStringPiece url);
308 static SpdyHeaderBlock ConstructGetHeaderBlockForProxy(SpdyStringPiece url);
309 static SpdyHeaderBlock ConstructHeadHeaderBlock(SpdyStringPiece url,
310 int64_t content_length);
311 static SpdyHeaderBlock ConstructPostHeaderBlock(SpdyStringPiece url,
312 int64_t content_length);
313 static SpdyHeaderBlock ConstructPutHeaderBlock(SpdyStringPiece url,
314 int64_t content_length);
315
316 // Construct an expected SPDY reply string from the given headers.
317 SpdyString ConstructSpdyReplyString(const SpdyHeaderBlock& headers) const;
318
319 // Construct an expected SPDY SETTINGS frame.
320 // |settings| are the settings to set.
321 // Returns the constructed frame. The caller takes ownership of the frame.
322 SpdySerializedFrame ConstructSpdySettings(const SettingsMap& settings);
323
324 // Constructs an expected SPDY SETTINGS acknowledgement frame.
325 SpdySerializedFrame ConstructSpdySettingsAck();
326
327 // Construct a SPDY PING frame.
328 // Returns the constructed frame. The caller takes ownership of the frame.
329 SpdySerializedFrame ConstructSpdyPing(uint32_t ping_id, bool is_ack);
330
331 // Construct a SPDY GOAWAY frame with last_good_stream_id = 0.
332 // Returns the constructed frame. The caller takes ownership of the frame.
333 SpdySerializedFrame ConstructSpdyGoAway();
334
335 // Construct a SPDY GOAWAY frame with the specified last_good_stream_id.
336 // Returns the constructed frame. The caller takes ownership of the frame.
337 SpdySerializedFrame ConstructSpdyGoAway(SpdyStreamId last_good_stream_id);
338
339 // Construct a SPDY GOAWAY frame with the specified last_good_stream_id,
340 // status, and description. Returns the constructed frame. The caller takes
341 // ownership of the frame.
342 SpdySerializedFrame ConstructSpdyGoAway(SpdyStreamId last_good_stream_id,
343 SpdyErrorCode error_code,
344 const SpdyString& desc);
345
346 // Construct a SPDY WINDOW_UPDATE frame.
347 // Returns the constructed frame. The caller takes ownership of the frame.
348 SpdySerializedFrame ConstructSpdyWindowUpdate(SpdyStreamId stream_id,
349 uint32_t delta_window_size);
350
351 // Construct a SPDY RST_STREAM frame.
352 // Returns the constructed frame. The caller takes ownership of the frame.
353 SpdySerializedFrame ConstructSpdyRstStream(SpdyStreamId stream_id,
354 SpdyErrorCode error_code);
355
356 // Construct a PRIORITY frame. The weight is derived from |request_priority|.
357 // Returns the constructed frame. The caller takes ownership of the frame.
358 SpdySerializedFrame ConstructSpdyPriority(SpdyStreamId stream_id,
359 SpdyStreamId parent_stream_id,
360 RequestPriority request_priority,
361 bool exclusive);
362
363 // Constructs a standard SPDY GET HEADERS frame for |url| with header
364 // compression.
365 // |extra_headers| are the extra header-value pairs, which typically
366 // will vary the most between calls.
367 // Returns a SpdySerializedFrame.
368 SpdySerializedFrame ConstructSpdyGet(const char* const url,
369 SpdyStreamId stream_id,
370 RequestPriority request_priority);
371
372 // Constructs a standard SPDY GET HEADERS frame with header compression.
373 // |extra_headers| are the extra header-value pairs, which typically
374 // will vary the most between calls. If |direct| is false, the
375 // the full url will be used instead of simply the path.
376 // Returns a SpdySerializedFrame.
377 SpdySerializedFrame ConstructSpdyGet(const char* const extra_headers[],
378 int extra_header_count,
379 int stream_id,
380 RequestPriority request_priority,
381 bool direct);
382
383 // Constructs a SPDY HEADERS frame for a CONNECT request.
384 SpdySerializedFrame ConstructSpdyConnect(const char* const extra_headers[],
385 int extra_header_count,
386 int stream_id,
387 RequestPriority priority,
388 const HostPortPair& host_port_pair);
389
390 // Constructs a SPDY PUSH_PROMISE frame.
391 // |extra_headers| are the extra header-value pairs, which typically
392 // will vary the most between calls.
393 // Returns a SpdySerializedFrame.
394 SpdySerializedFrame ConstructSpdyPush(const char* const extra_headers[],
395 int extra_header_count,
396 int stream_id,
397 int associated_stream_id,
398 const char* url);
399 SpdySerializedFrame ConstructSpdyPush(const char* const extra_headers[],
400 int extra_header_count,
401 int stream_id,
402 int associated_stream_id,
403 const char* url,
404 const char* status,
405 const char* location);
406
407 SpdySerializedFrame ConstructInitialSpdyPushFrame(SpdyHeaderBlock headers,
408 int stream_id,
409 int associated_stream_id);
410
411 SpdySerializedFrame ConstructSpdyPushHeaders(
412 int stream_id,
413 const char* const extra_headers[],
414 int extra_header_count);
415
416 // Constructs a HEADERS frame with the request header compression context with
417 // END_STREAM flag set to |fin|.
418 SpdySerializedFrame ConstructSpdyResponseHeaders(int stream_id,
419 SpdyHeaderBlock headers,
420 bool fin);
421
422 // Construct a HEADERS frame carrying exactly the given headers and priority.
423 SpdySerializedFrame ConstructSpdyHeaders(int stream_id,
424 SpdyHeaderBlock headers,
425 RequestPriority priority,
426 bool fin);
427
428 // Construct a reply HEADERS frame carrying exactly the given headers and the
429 // default priority.
430 SpdySerializedFrame ConstructSpdyReply(int stream_id,
431 SpdyHeaderBlock headers);
432
433 // Constructs a standard SPDY HEADERS frame to match the SPDY GET.
434 // |extra_headers| are the extra header-value pairs, which typically
435 // will vary the most between calls.
436 // Returns a SpdySerializedFrame.
437 SpdySerializedFrame ConstructSpdyGetReply(const char* const extra_headers[],
438 int extra_header_count,
439 int stream_id);
440
441 // Constructs a standard SPDY HEADERS frame to match the SPDY GET.
442 // |extra_headers| are the extra header-value pairs, which typically
443 // will vary the most between calls.
444 // Returns a SpdySerializedFrame.
445 SpdySerializedFrame ConstructSpdyGetReplyRedirect(int stream_id);
446
447 // Constructs a standard SPDY HEADERS frame with an Internal Server
448 // Error status code.
449 // Returns a SpdySerializedFrame.
450 SpdySerializedFrame ConstructSpdyReplyError(int stream_id);
451
452 // Constructs a standard SPDY HEADERS frame with the specified status code.
453 // Returns a SpdySerializedFrame.
454 SpdySerializedFrame ConstructSpdyReplyError(
455 const char* const status,
456 const char* const* const extra_headers,
457 int extra_header_count,
458 int stream_id);
459
460 // Constructs a standard SPDY POST HEADERS frame.
461 // |extra_headers| are the extra header-value pairs, which typically
462 // will vary the most between calls.
463 // Returns a SpdySerializedFrame.
464 SpdySerializedFrame ConstructSpdyPost(const char* url,
465 SpdyStreamId stream_id,
466 int64_t content_length,
467 RequestPriority priority,
468 const char* const extra_headers[],
469 int extra_header_count);
470
471 // Constructs a chunked transfer SPDY POST HEADERS frame.
472 // |extra_headers| are the extra header-value pairs, which typically
473 // will vary the most between calls.
474 // Returns a SpdySerializedFrame.
475 SpdySerializedFrame ConstructChunkedSpdyPost(
476 const char* const extra_headers[],
477 int extra_header_count);
478
479 // Constructs a standard SPDY HEADERS frame to match the SPDY POST.
480 // |extra_headers| are the extra header-value pairs, which typically
481 // will vary the most between calls.
482 // Returns a SpdySerializedFrame.
483 SpdySerializedFrame ConstructSpdyPostReply(const char* const extra_headers[],
484 int extra_header_count);
485
486 // Constructs a single SPDY data frame with the contents "hello!"
487 SpdySerializedFrame ConstructSpdyDataFrame(int stream_id, bool fin);
488
489 // Constructs a single SPDY data frame with the given content.
490 SpdySerializedFrame ConstructSpdyDataFrame(int stream_id,
491 const char* data,
492 uint32_t len,
493 bool fin);
494
495 // Constructs a single SPDY data frame with the given content and padding.
496 SpdySerializedFrame ConstructSpdyDataFrame(int stream_id,
497 const char* data,
498 uint32_t len,
499 bool fin,
500 int padding_length);
501
502 // Wraps |frame| in the payload of a data frame in stream |stream_id|.
503 SpdySerializedFrame ConstructWrappedSpdyFrame(
504 const SpdySerializedFrame& frame,
505 int stream_id);
506
507 // Serialize a SpdyFrameIR with |headerless_spdy_framer_|.
508 SpdySerializedFrame SerializeFrame(const SpdyFrameIR& frame_ir);
509
510 // Called when necessary (when it will affect stream dependency specification
511 // when setting dependencies based on priorioties) to notify the utility
512 // class of stream destruction.
513 void UpdateWithStreamDestruction(int stream_id);
514
515 void set_default_url(const GURL& url) { default_url_ = url; }
516
517 static const char* GetMethodKey();
518 static const char* GetStatusKey();
519 static const char* GetHostKey();
520 static const char* GetSchemeKey();
521 static const char* GetPathKey();
522
523 private:
524 // |content_length| may be NULL, in which case the content-length
525 // header will be omitted.
526 static SpdyHeaderBlock ConstructHeaderBlock(SpdyStringPiece method,
527 SpdyStringPiece url,
528 int64_t* content_length);
529
530 // Multiple SpdyFramers are required to keep track of header compression
531 // state.
532 // Use to serialize frames (request or response) without headers.
533 SpdyFramer headerless_spdy_framer_;
534 // Use to serialize request frames with headers.
535 SpdyFramer request_spdy_framer_;
536 // Use to serialize response frames with headers.
537 SpdyFramer response_spdy_framer_;
538
539 GURL default_url_;
540
541 // Track a FIFO list of the stream_id of all created requests by priority.
542 std::map<int, std::vector<int>> priority_to_stream_id_list_;
543 };
544
545 } // namespace net
546
547 #endif // NET_SPDY_SPDY_TEST_UTIL_COMMON_H_
OLDNEW
« no previous file with comments | « net/spdy/spdy_stream_unittest.cc ('k') | net/spdy/spdy_test_util_common.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698