1 // Copyright 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 #include "net/quic/test_tools/quic_test_packet_maker.h"
9 #include "net/quic/quic_framer.h"
10 #include "net/quic/quic_http_utils.h"
11 #include "net/quic/quic_utils.h"
12 #include "net/quic/test_tools/quic_test_utils.h"
19 QuicTestPacketMaker::QuicTestPacketMaker(QuicVersion version
,
20 QuicConnectionId connection_id
,
22 const std::string
& host
)
24 connection_id_(connection_id
),
27 spdy_request_framer_(HTTP2
),
28 spdy_response_framer_(HTTP2
) {
31 QuicTestPacketMaker::~QuicTestPacketMaker() {
34 void QuicTestPacketMaker::set_hostname(const std::string
& host
) {
38 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeRstPacket(
39 QuicPacketSequenceNumber num
,
41 QuicStreamId stream_id
,
42 QuicRstStreamErrorCode error_code
) {
43 QuicPacketHeader header
;
44 header
.public_header
.connection_id
= connection_id_
;
45 header
.public_header
.reset_flag
= false;
46 header
.public_header
.version_flag
= include_version
;
47 header
.public_header
.sequence_number_length
= PACKET_1BYTE_SEQUENCE_NUMBER
;
48 header
.packet_sequence_number
= num
;
49 header
.entropy_flag
= false;
50 header
.fec_flag
= false;
53 QuicRstStreamFrame
rst(stream_id
, error_code
, 0);
54 return scoped_ptr
<QuicEncryptedPacket
>(MakePacket(header
, QuicFrame(&rst
)));
57 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeAckAndRstPacket(
58 QuicPacketSequenceNumber num
,
60 QuicStreamId stream_id
,
61 QuicRstStreamErrorCode error_code
,
62 QuicPacketSequenceNumber largest_received
,
63 QuicPacketSequenceNumber least_unacked
,
66 QuicPacketHeader header
;
67 header
.public_header
.connection_id
= connection_id_
;
68 header
.public_header
.reset_flag
= false;
69 header
.public_header
.version_flag
= include_version
;
70 header
.public_header
.sequence_number_length
= PACKET_1BYTE_SEQUENCE_NUMBER
;
71 header
.packet_sequence_number
= num
;
72 header
.entropy_flag
= false;
73 header
.fec_flag
= false;
76 QuicAckFrame
ack(MakeAckFrame(largest_received
));
77 ack
.delta_time_largest_observed
= QuicTime::Delta::Zero();
78 for (QuicPacketSequenceNumber i
= least_unacked
; i
<= largest_received
; ++i
) {
79 ack
.received_packet_times
.push_back(make_pair(i
, clock_
->Now()));
82 frames
.push_back(QuicFrame(&ack
));
84 QuicStopWaitingFrame stop_waiting
;
85 stop_waiting
.least_unacked
= least_unacked
;
86 frames
.push_back(QuicFrame(&stop_waiting
));
88 QuicRstStreamFrame
rst(stream_id
, error_code
, 0);
89 frames
.push_back(QuicFrame(&rst
));
91 QuicFramer
framer(SupportedVersions(version_
), clock_
->Now(),
92 Perspective::IS_CLIENT
);
93 scoped_ptr
<QuicPacket
> packet(
94 BuildUnsizedDataPacket(&framer
, header
, frames
));
95 char buffer
[kMaxPacketSize
];
96 scoped_ptr
<QuicEncryptedPacket
> encrypted(
97 framer
.EncryptPayload(ENCRYPTION_NONE
, header
.packet_sequence_number
,
98 *packet
, buffer
, kMaxPacketSize
));
99 EXPECT_TRUE(encrypted
!= nullptr);
100 return scoped_ptr
<QuicEncryptedPacket
>(encrypted
->Clone());
103 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeConnectionClosePacket(
104 QuicPacketSequenceNumber num
) {
105 QuicPacketHeader header
;
106 header
.public_header
.connection_id
= connection_id_
;
107 header
.public_header
.reset_flag
= false;
108 header
.public_header
.version_flag
= false;
109 header
.public_header
.sequence_number_length
= PACKET_1BYTE_SEQUENCE_NUMBER
;
110 header
.packet_sequence_number
= num
;
111 header
.entropy_flag
= false;
112 header
.fec_flag
= false;
113 header
.fec_group
= 0;
115 QuicConnectionCloseFrame close
;
116 close
.error_code
= QUIC_CRYPTO_VERSION_NOT_SUPPORTED
;
117 close
.error_details
= "Time to panic!";
118 return scoped_ptr
<QuicEncryptedPacket
>(MakePacket(header
, QuicFrame(&close
)));
121 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeAckPacket(
122 QuicPacketSequenceNumber sequence_number
,
123 QuicPacketSequenceNumber largest_received
,
124 QuicPacketSequenceNumber least_unacked
,
125 bool send_feedback
) {
126 QuicPacketHeader header
;
127 header
.public_header
.connection_id
= connection_id_
;
128 header
.public_header
.reset_flag
= false;
129 header
.public_header
.version_flag
= false;
130 header
.public_header
.sequence_number_length
= PACKET_1BYTE_SEQUENCE_NUMBER
;
131 header
.packet_sequence_number
= sequence_number
;
132 header
.entropy_flag
= false;
133 header
.fec_flag
= false;
134 header
.fec_group
= 0;
136 QuicAckFrame
ack(MakeAckFrame(largest_received
));
137 ack
.delta_time_largest_observed
= QuicTime::Delta::Zero();
138 for (QuicPacketSequenceNumber i
= least_unacked
; i
<= largest_received
; ++i
) {
139 ack
.received_packet_times
.push_back(make_pair(i
, clock_
->Now()));
142 QuicFramer
framer(SupportedVersions(version_
), clock_
->Now(),
143 Perspective::IS_CLIENT
);
145 frames
.push_back(QuicFrame(&ack
));
147 QuicStopWaitingFrame stop_waiting
;
148 stop_waiting
.least_unacked
= least_unacked
;
149 frames
.push_back(QuicFrame(&stop_waiting
));
151 scoped_ptr
<QuicPacket
> packet(
152 BuildUnsizedDataPacket(&framer
, header
, frames
));
153 char buffer
[kMaxPacketSize
];
154 scoped_ptr
<QuicEncryptedPacket
> encrypted(
155 framer
.EncryptPayload(ENCRYPTION_NONE
, header
.packet_sequence_number
,
156 *packet
, buffer
, kMaxPacketSize
));
157 EXPECT_TRUE(encrypted
!= nullptr);
158 return scoped_ptr
<QuicEncryptedPacket
>(encrypted
->Clone());
161 // Returns a newly created packet to send kData on stream 1.
162 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeDataPacket(
163 QuicPacketSequenceNumber sequence_number
,
164 QuicStreamId stream_id
,
165 bool should_include_version
,
167 QuicStreamOffset offset
,
168 base::StringPiece data
) {
169 InitializeHeader(sequence_number
, should_include_version
);
170 QuicStreamFrame
frame(stream_id
, fin
, offset
, data
);
171 return MakePacket(header_
, QuicFrame(&frame
));
174 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeRequestHeadersPacket(
175 QuicPacketSequenceNumber sequence_number
,
176 QuicStreamId stream_id
,
177 bool should_include_version
,
179 QuicPriority priority
,
180 const SpdyHeaderBlock
& headers
) {
181 InitializeHeader(sequence_number
, should_include_version
);
182 scoped_ptr
<SpdySerializedFrame
> spdy_frame
;
183 if (spdy_request_framer_
.protocol_version() == SPDY3
) {
184 SpdySynStreamIR
syn_stream(stream_id
);
185 syn_stream
.set_name_value_block(headers
);
186 syn_stream
.set_fin(fin
);
187 syn_stream
.set_priority(priority
);
188 spdy_frame
.reset(spdy_request_framer_
.SerializeSynStream(syn_stream
));
190 SpdyHeadersIR
headers_frame(stream_id
);
191 headers_frame
.set_name_value_block(headers
);
192 headers_frame
.set_fin(fin
);
193 headers_frame
.set_priority(priority
);
194 headers_frame
.set_has_priority(true);
195 spdy_frame
.reset(spdy_request_framer_
.SerializeFrame(headers_frame
));
197 QuicStreamFrame
frame(
198 kHeadersStreamId
, false, 0,
199 base::StringPiece(spdy_frame
->data(), spdy_frame
->size()));
200 return MakePacket(header_
, QuicFrame(&frame
));
203 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeResponseHeadersPacket(
204 QuicPacketSequenceNumber sequence_number
,
205 QuicStreamId stream_id
,
206 bool should_include_version
,
208 const SpdyHeaderBlock
& headers
) {
209 InitializeHeader(sequence_number
, should_include_version
);
210 scoped_ptr
<SpdySerializedFrame
> spdy_frame
;
211 if (spdy_request_framer_
.protocol_version() == SPDY3
) {
212 SpdySynReplyIR
syn_reply(stream_id
);
213 syn_reply
.set_name_value_block(headers
);
214 syn_reply
.set_fin(fin
);
215 spdy_frame
.reset(spdy_response_framer_
.SerializeSynReply(syn_reply
));
217 SpdyHeadersIR
headers_frame(stream_id
);
218 headers_frame
.set_name_value_block(headers
);
219 headers_frame
.set_fin(fin
);
220 spdy_frame
.reset(spdy_request_framer_
.SerializeFrame(headers_frame
));
222 QuicStreamFrame
frame(
223 kHeadersStreamId
, false, 0,
224 base::StringPiece(spdy_frame
->data(), spdy_frame
->size()));
225 return MakePacket(header_
, QuicFrame(&frame
));
228 SpdyHeaderBlock
QuicTestPacketMaker::GetRequestHeaders(
229 const std::string
& method
,
230 const std::string
& scheme
,
231 const std::string
& path
) {
232 SpdyHeaderBlock headers
;
233 headers
[":method"] = method
;
234 if (version_
<= QUIC_VERSION_24
) {
235 headers
[":host"] = host_
;
237 headers
[":authority"] = host_
;
239 headers
[":path"] = path
;
240 headers
[":scheme"] = scheme
;
241 if (version_
<= QUIC_VERSION_24
) {
242 headers
[":version"] = "HTTP/1.1";
247 SpdyHeaderBlock
QuicTestPacketMaker::GetResponseHeaders(
248 const std::string
& status
) {
249 SpdyHeaderBlock headers
;
250 headers
[":status"] = status
;
251 if (version_
<= QUIC_VERSION_24
) {
252 headers
[":version"] = "HTTP/1.1";
254 headers
["content-type"] = "text/plain";
258 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakePacket(
259 const QuicPacketHeader
& header
,
260 const QuicFrame
& frame
) {
261 QuicFramer
framer(SupportedVersions(version_
), QuicTime::Zero(),
262 Perspective::IS_CLIENT
);
264 frames
.push_back(frame
);
265 scoped_ptr
<QuicPacket
> packet(
266 BuildUnsizedDataPacket(&framer
, header
, frames
));
267 char buffer
[kMaxPacketSize
];
268 scoped_ptr
<QuicEncryptedPacket
> encrypted(
269 framer
.EncryptPayload(ENCRYPTION_NONE
, header
.packet_sequence_number
,
270 *packet
, buffer
, kMaxPacketSize
));
271 EXPECT_TRUE(encrypted
!= nullptr);
272 return scoped_ptr
<QuicEncryptedPacket
>(encrypted
->Clone());
275 void QuicTestPacketMaker::InitializeHeader(
276 QuicPacketSequenceNumber sequence_number
,
277 bool should_include_version
) {
278 header_
.public_header
.connection_id
= connection_id_
;
279 header_
.public_header
.reset_flag
= false;
280 header_
.public_header
.version_flag
= should_include_version
;
281 header_
.public_header
.sequence_number_length
= PACKET_1BYTE_SEQUENCE_NUMBER
;
282 header_
.packet_sequence_number
= sequence_number
;
283 header_
.fec_group
= 0;
284 header_
.entropy_flag
= false;
285 header_
.fec_flag
= false;