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 "base/memory/ref_counted.h"
9 #include "net/base/completion_callback.h"
10 #include "net/base/request_priority.h"
11 #include "net/base/test_completion_callback.h"
12 #include "net/socket/socket_test_util.h"
13 #include "net/spdy/spdy_protocol.h"
22 class SpdyStreamRequest
;
24 // Default upload data used by both, mock objects and framer when creating
26 const char kDefaultURL
[] = "http://www.google.com";
27 const char kUploadData
[] = "hello!";
28 const int kUploadDataSize
= arraysize(kUploadData
)-1;
30 // Chop a frame into an array of MockWrites.
31 // |data| is the frame to chop.
32 // |length| is the length of the frame to chop.
33 // |num_chunks| is the number of chunks to create.
34 MockWrite
* ChopWriteFrame(const char* data
, int length
, int num_chunks
);
36 // Chop a SpdyFrame into an array of MockWrites.
37 // |frame| is the frame to chop.
38 // |num_chunks| is the number of chunks to create.
39 MockWrite
* ChopWriteFrame(const SpdyFrame
& frame
, int num_chunks
);
41 // Chop a frame into an array of MockReads.
42 // |data| is the frame to chop.
43 // |length| is the length of the frame to chop.
44 // |num_chunks| is the number of chunks to create.
45 MockRead
* ChopReadFrame(const char* data
, int length
, int num_chunks
);
47 // Chop a SpdyFrame into an array of MockReads.
48 // |frame| is the frame to chop.
49 // |num_chunks| is the number of chunks to create.
50 MockRead
* ChopReadFrame(const SpdyFrame
& frame
, int num_chunks
);
52 // Adds headers and values to a map.
53 // |extra_headers| is an array of { name, value } pairs, arranged as strings
54 // where the even entries are the header names, and the odd entries are the
56 // |headers| gets filled in from |extra_headers|.
57 void AppendToHeaderBlock(const char* const extra_headers
[],
58 int extra_header_count
,
59 SpdyHeaderBlock
* headers
);
61 // Writes |str| of the given |len| to the buffer pointed to by |buffer_handle|.
62 // Uses a template so buffer_handle can be a char* or an unsigned char*.
63 // Updates the |*buffer_handle| pointer by |len|
64 // Returns the number of bytes written into *|buffer_handle|
66 int AppendToBuffer(const char* str
,
69 int* buffer_len_remaining
) {
71 DCHECK(NULL
!= buffer_handle
) << "NULL buffer handle";
72 DCHECK(NULL
!= *buffer_handle
) << "NULL pointer";
73 DCHECK(NULL
!= buffer_len_remaining
)
74 << "NULL buffer remainder length pointer";
75 DCHECK_GE(*buffer_len_remaining
, len
) << "Insufficient buffer size";
76 memcpy(*buffer_handle
, str
, len
);
77 *buffer_handle
+= len
;
78 *buffer_len_remaining
-= len
;
82 // Writes |val| to a location of size |len|, in big-endian format.
83 // in the buffer pointed to by |buffer_handle|.
84 // Updates the |*buffer_handle| pointer by |len|
85 // Returns the number of bytes written
86 int AppendToBuffer(int val
,
88 unsigned char** buffer_handle
,
89 int* buffer_len_remaining
);
91 // Create an async MockWrite from the given SpdyFrame.
92 MockWrite
CreateMockWrite(const SpdyFrame
& req
);
94 // Create an async MockWrite from the given SpdyFrame and sequence number.
95 MockWrite
CreateMockWrite(const SpdyFrame
& req
, int seq
);
97 MockWrite
CreateMockWrite(const SpdyFrame
& req
, int seq
, IoMode mode
);
99 // Create a MockRead from the given SpdyFrame.
100 MockRead
CreateMockRead(const SpdyFrame
& resp
);
102 // Create a MockRead from the given SpdyFrame and sequence number.
103 MockRead
CreateMockRead(const SpdyFrame
& resp
, int seq
);
105 MockRead
CreateMockRead(const SpdyFrame
& resp
, int seq
, IoMode mode
);
107 // Combines the given SpdyFrames into the given char array and returns
109 int CombineFrames(const SpdyFrame
** frames
, int num_frames
,
110 char* buff
, int buff_len
);
112 // Returns the SpdyPriority embedded in the given frame. Returns true
113 // and fills in |priority| on success.
114 bool GetSpdyPriority(int version
,
115 const SpdyFrame
& frame
,
116 SpdyPriority
* priority
);
118 // Tries to create a stream in |session| synchronously. Returns NULL
120 scoped_refptr
<SpdyStream
> CreateStreamSynchronously(
121 const scoped_refptr
<SpdySession
>& session
,
123 RequestPriority priority
,
124 const BoundNetLog
& net_log
);
126 // Helper class used by some tests to release two streams as soon as
128 class StreamReleaserCallback
: public TestCompletionCallbackBase
{
130 StreamReleaserCallback(SpdySession
* session
,
131 SpdyStream
* first_stream
);
133 virtual ~StreamReleaserCallback();
135 // Returns a callback that releases |request|'s stream as well as
137 CompletionCallback
MakeCallback(SpdyStreamRequest
* request
);
140 void OnComplete(SpdyStreamRequest
* request
, int result
);
142 scoped_refptr
<SpdySession
> session_
;
143 scoped_refptr
<SpdyStream
> first_stream_
;
146 const size_t kSpdyCredentialSlotUnused
= 0;
148 // This struct holds information used to construct spdy control and data frames.
149 struct SpdyHeaderInfo
{
152 SpdyStreamId assoc_id
;
153 SpdyPriority priority
;
154 size_t credential_slot
; // SPDY3 only
155 SpdyControlFlags control_flags
;
157 SpdyRstStreamStatus status
;
160 SpdyDataFlags data_flags
;
165 #endif // NET_SPDY_SPDY_TEST_UTIL_COMMON_H_