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
,
23 connection_id_(connection_id
),
25 spdy_request_framer_(version
> QUIC_VERSION_23
? SPDY4
: SPDY3
),
26 spdy_response_framer_(version
> QUIC_VERSION_23
? SPDY4
: SPDY3
) {
29 QuicTestPacketMaker::~QuicTestPacketMaker() {
32 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeRstPacket(
33 QuicPacketSequenceNumber num
,
35 QuicStreamId stream_id
,
36 QuicRstStreamErrorCode error_code
) {
37 QuicPacketHeader header
;
38 header
.public_header
.connection_id
= connection_id_
;
39 header
.public_header
.reset_flag
= false;
40 header
.public_header
.version_flag
= include_version
;
41 header
.public_header
.sequence_number_length
= PACKET_1BYTE_SEQUENCE_NUMBER
;
42 header
.packet_sequence_number
= num
;
43 header
.entropy_flag
= false;
44 header
.fec_flag
= false;
47 QuicRstStreamFrame
rst(stream_id
, error_code
, 0);
48 return scoped_ptr
<QuicEncryptedPacket
>(MakePacket(header
, QuicFrame(&rst
)));
51 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeAckAndRstPacket(
52 QuicPacketSequenceNumber num
,
54 QuicStreamId stream_id
,
55 QuicRstStreamErrorCode error_code
,
56 QuicPacketSequenceNumber largest_received
,
57 QuicPacketSequenceNumber least_unacked
,
60 QuicPacketHeader header
;
61 header
.public_header
.connection_id
= connection_id_
;
62 header
.public_header
.reset_flag
= false;
63 header
.public_header
.version_flag
= include_version
;
64 header
.public_header
.sequence_number_length
= PACKET_1BYTE_SEQUENCE_NUMBER
;
65 header
.packet_sequence_number
= num
;
66 header
.entropy_flag
= false;
67 header
.fec_flag
= false;
70 QuicAckFrame
ack(MakeAckFrame(largest_received
));
71 ack
.delta_time_largest_observed
= QuicTime::Delta::Zero();
72 for (QuicPacketSequenceNumber i
= least_unacked
; i
<= largest_received
; ++i
) {
73 ack
.received_packet_times
.push_back(make_pair(i
, clock_
->Now()));
76 frames
.push_back(QuicFrame(&ack
));
78 QuicStopWaitingFrame stop_waiting
;
79 stop_waiting
.least_unacked
= least_unacked
;
80 frames
.push_back(QuicFrame(&stop_waiting
));
82 QuicRstStreamFrame
rst(stream_id
, error_code
, 0);
83 frames
.push_back(QuicFrame(&rst
));
85 QuicFramer
framer(SupportedVersions(version_
), clock_
->Now(),
86 Perspective::IS_CLIENT
);
87 scoped_ptr
<QuicPacket
> packet(
88 BuildUnsizedDataPacket(&framer
, header
, frames
));
89 return scoped_ptr
<QuicEncryptedPacket
>(framer
.EncryptPacket(
90 ENCRYPTION_NONE
, header
.packet_sequence_number
, *packet
));
93 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeConnectionClosePacket(
94 QuicPacketSequenceNumber num
) {
95 QuicPacketHeader header
;
96 header
.public_header
.connection_id
= connection_id_
;
97 header
.public_header
.reset_flag
= false;
98 header
.public_header
.version_flag
= false;
99 header
.public_header
.sequence_number_length
= PACKET_1BYTE_SEQUENCE_NUMBER
;
100 header
.packet_sequence_number
= num
;
101 header
.entropy_flag
= false;
102 header
.fec_flag
= false;
103 header
.fec_group
= 0;
105 QuicConnectionCloseFrame close
;
106 close
.error_code
= QUIC_CRYPTO_VERSION_NOT_SUPPORTED
;
107 close
.error_details
= "Time to panic!";
108 return scoped_ptr
<QuicEncryptedPacket
>(MakePacket(header
, QuicFrame(&close
)));
111 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeAckPacket(
112 QuicPacketSequenceNumber sequence_number
,
113 QuicPacketSequenceNumber largest_received
,
114 QuicPacketSequenceNumber least_unacked
,
115 bool send_feedback
) {
116 QuicPacketHeader header
;
117 header
.public_header
.connection_id
= connection_id_
;
118 header
.public_header
.reset_flag
= false;
119 header
.public_header
.version_flag
= false;
120 header
.public_header
.sequence_number_length
= PACKET_1BYTE_SEQUENCE_NUMBER
;
121 header
.packet_sequence_number
= sequence_number
;
122 header
.entropy_flag
= false;
123 header
.fec_flag
= false;
124 header
.fec_group
= 0;
126 QuicAckFrame
ack(MakeAckFrame(largest_received
));
127 ack
.delta_time_largest_observed
= QuicTime::Delta::Zero();
128 for (QuicPacketSequenceNumber i
= least_unacked
; i
<= largest_received
; ++i
) {
129 ack
.received_packet_times
.push_back(make_pair(i
, clock_
->Now()));
132 QuicFramer
framer(SupportedVersions(version_
), clock_
->Now(),
133 Perspective::IS_CLIENT
);
135 frames
.push_back(QuicFrame(&ack
));
137 QuicStopWaitingFrame stop_waiting
;
138 stop_waiting
.least_unacked
= least_unacked
;
139 frames
.push_back(QuicFrame(&stop_waiting
));
141 scoped_ptr
<QuicPacket
> packet(
142 BuildUnsizedDataPacket(&framer
, header
, frames
));
143 return scoped_ptr
<QuicEncryptedPacket
>(framer
.EncryptPacket(
144 ENCRYPTION_NONE
, header
.packet_sequence_number
, *packet
));
147 // Returns a newly created packet to send kData on stream 1.
148 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeDataPacket(
149 QuicPacketSequenceNumber sequence_number
,
150 QuicStreamId stream_id
,
151 bool should_include_version
,
153 QuicStreamOffset offset
,
154 base::StringPiece data
) {
155 InitializeHeader(sequence_number
, should_include_version
);
156 QuicStreamFrame
frame(stream_id
, fin
, offset
, MakeIOVector(data
));
157 return MakePacket(header_
, QuicFrame(&frame
));
160 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeRequestHeadersPacket(
161 QuicPacketSequenceNumber sequence_number
,
162 QuicStreamId stream_id
,
163 bool should_include_version
,
165 QuicPriority priority
,
166 const SpdyHeaderBlock
& headers
) {
167 InitializeHeader(sequence_number
, should_include_version
);
168 scoped_ptr
<SpdySerializedFrame
> spdy_frame
;
169 if (spdy_request_framer_
.protocol_version() == SPDY3
) {
170 SpdySynStreamIR
syn_stream(stream_id
);
171 syn_stream
.set_name_value_block(headers
);
172 syn_stream
.set_fin(fin
);
173 syn_stream
.set_priority(priority
);
174 spdy_frame
.reset(spdy_request_framer_
.SerializeSynStream(syn_stream
));
176 SpdyHeadersIR
headers_frame(stream_id
);
177 headers_frame
.set_name_value_block(headers
);
178 headers_frame
.set_fin(fin
);
179 headers_frame
.set_priority(priority
);
180 headers_frame
.set_has_priority(true);
181 spdy_frame
.reset(spdy_request_framer_
.SerializeFrame(headers_frame
));
183 QuicStreamFrame
frame(kHeadersStreamId
, false, 0,
184 MakeIOVector(base::StringPiece(spdy_frame
->data(),
185 spdy_frame
->size())));
186 return MakePacket(header_
, QuicFrame(&frame
));
189 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeResponseHeadersPacket(
190 QuicPacketSequenceNumber sequence_number
,
191 QuicStreamId stream_id
,
192 bool should_include_version
,
194 const SpdyHeaderBlock
& headers
) {
195 InitializeHeader(sequence_number
, should_include_version
);
196 scoped_ptr
<SpdySerializedFrame
> spdy_frame
;
197 if (spdy_request_framer_
.protocol_version() == SPDY3
) {
198 SpdySynReplyIR
syn_reply(stream_id
);
199 syn_reply
.set_name_value_block(headers
);
200 syn_reply
.set_fin(fin
);
201 spdy_frame
.reset(spdy_response_framer_
.SerializeSynReply(syn_reply
));
203 SpdyHeadersIR
headers_frame(stream_id
);
204 headers_frame
.set_name_value_block(headers
);
205 headers_frame
.set_fin(fin
);
206 spdy_frame
.reset(spdy_request_framer_
.SerializeFrame(headers_frame
));
208 QuicStreamFrame
frame(kHeadersStreamId
, false, 0,
209 MakeIOVector(base::StringPiece(spdy_frame
->data(),
210 spdy_frame
->size())));
211 return MakePacket(header_
, QuicFrame(&frame
));
214 SpdyHeaderBlock
QuicTestPacketMaker::GetRequestHeaders(
215 const std::string
& method
,
216 const std::string
& scheme
,
217 const std::string
& path
) {
218 SpdyHeaderBlock headers
;
219 headers
[":method"] = method
;
220 headers
[":host"] = "www.google.com";
221 headers
[":path"] = path
;
222 headers
[":scheme"] = scheme
;
223 headers
[":version"] = "HTTP/1.1";
227 SpdyHeaderBlock
QuicTestPacketMaker::GetResponseHeaders(
228 const std::string
& status
) {
229 SpdyHeaderBlock headers
;
230 headers
[":status"] = status
;
231 headers
[":version"] = "HTTP/1.1";
232 headers
["content-type"] = "text/plain";
236 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakePacket(
237 const QuicPacketHeader
& header
,
238 const QuicFrame
& frame
) {
239 QuicFramer
framer(SupportedVersions(version_
), QuicTime::Zero(),
240 Perspective::IS_CLIENT
);
242 frames
.push_back(frame
);
243 scoped_ptr
<QuicPacket
> packet(
244 BuildUnsizedDataPacket(&framer
, header
, frames
));
245 return scoped_ptr
<QuicEncryptedPacket
>(framer
.EncryptPacket(
246 ENCRYPTION_NONE
, header
.packet_sequence_number
, *packet
));
249 void QuicTestPacketMaker::InitializeHeader(
250 QuicPacketSequenceNumber sequence_number
,
251 bool should_include_version
) {
252 header_
.public_header
.connection_id
= connection_id_
;
253 header_
.public_header
.reset_flag
= false;
254 header_
.public_header
.version_flag
= should_include_version
;
255 header_
.public_header
.sequence_number_length
= PACKET_1BYTE_SEQUENCE_NUMBER
;
256 header_
.packet_sequence_number
= sequence_number
;
257 header_
.fec_group
= 0;
258 header_
.entropy_flag
= false;
259 header_
.fec_flag
= false;