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(
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
.packet_number_length
= PACKET_1BYTE_PACKET_NUMBER
;
48 header
.packet_packet_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(
60 QuicStreamId stream_id
,
61 QuicRstStreamErrorCode error_code
,
62 QuicPacketNumber largest_received
,
63 QuicPacketNumber least_unacked
,
65 QuicPacketHeader header
;
66 header
.public_header
.connection_id
= connection_id_
;
67 header
.public_header
.reset_flag
= false;
68 header
.public_header
.version_flag
= include_version
;
69 header
.public_header
.packet_number_length
= PACKET_1BYTE_PACKET_NUMBER
;
70 header
.packet_packet_number
= num
;
71 header
.entropy_flag
= false;
72 header
.fec_flag
= false;
75 QuicAckFrame
ack(MakeAckFrame(largest_received
));
76 ack
.delta_time_largest_observed
= QuicTime::Delta::Zero();
77 for (QuicPacketNumber i
= least_unacked
; i
<= largest_received
; ++i
) {
78 ack
.received_packet_times
.push_back(make_pair(i
, clock_
->Now()));
81 frames
.push_back(QuicFrame(&ack
));
83 QuicStopWaitingFrame stop_waiting
;
84 stop_waiting
.least_unacked
= least_unacked
;
85 frames
.push_back(QuicFrame(&stop_waiting
));
87 QuicRstStreamFrame
rst(stream_id
, error_code
, 0);
88 frames
.push_back(QuicFrame(&rst
));
90 QuicFramer
framer(SupportedVersions(version_
), clock_
->Now(),
91 Perspective::IS_CLIENT
);
92 scoped_ptr
<QuicPacket
> packet(
93 BuildUnsizedDataPacket(&framer
, header
, frames
));
94 char buffer
[kMaxPacketSize
];
95 scoped_ptr
<QuicEncryptedPacket
> encrypted(
96 framer
.EncryptPayload(ENCRYPTION_NONE
, header
.packet_packet_number
,
97 *packet
, buffer
, kMaxPacketSize
));
98 EXPECT_TRUE(encrypted
!= nullptr);
99 return scoped_ptr
<QuicEncryptedPacket
>(encrypted
->Clone());
102 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeConnectionClosePacket(
103 QuicPacketNumber num
) {
104 QuicPacketHeader header
;
105 header
.public_header
.connection_id
= connection_id_
;
106 header
.public_header
.reset_flag
= false;
107 header
.public_header
.version_flag
= false;
108 header
.public_header
.packet_number_length
= PACKET_1BYTE_PACKET_NUMBER
;
109 header
.packet_packet_number
= num
;
110 header
.entropy_flag
= false;
111 header
.fec_flag
= false;
112 header
.fec_group
= 0;
114 QuicConnectionCloseFrame close
;
115 close
.error_code
= QUIC_CRYPTO_VERSION_NOT_SUPPORTED
;
116 close
.error_details
= "Time to panic!";
117 return scoped_ptr
<QuicEncryptedPacket
>(MakePacket(header
, QuicFrame(&close
)));
120 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeAckPacket(
121 QuicPacketNumber packet_number
,
122 QuicPacketNumber largest_received
,
123 QuicPacketNumber least_unacked
,
124 bool send_feedback
) {
125 QuicPacketHeader header
;
126 header
.public_header
.connection_id
= connection_id_
;
127 header
.public_header
.reset_flag
= false;
128 header
.public_header
.version_flag
= false;
129 header
.public_header
.packet_number_length
= PACKET_1BYTE_PACKET_NUMBER
;
130 header
.packet_packet_number
= packet_number
;
131 header
.entropy_flag
= false;
132 header
.fec_flag
= false;
133 header
.fec_group
= 0;
135 QuicAckFrame
ack(MakeAckFrame(largest_received
));
136 ack
.delta_time_largest_observed
= QuicTime::Delta::Zero();
137 for (QuicPacketNumber i
= least_unacked
; i
<= largest_received
; ++i
) {
138 ack
.received_packet_times
.push_back(make_pair(i
, clock_
->Now()));
141 QuicFramer
framer(SupportedVersions(version_
), clock_
->Now(),
142 Perspective::IS_CLIENT
);
144 frames
.push_back(QuicFrame(&ack
));
146 QuicStopWaitingFrame stop_waiting
;
147 stop_waiting
.least_unacked
= least_unacked
;
148 frames
.push_back(QuicFrame(&stop_waiting
));
150 scoped_ptr
<QuicPacket
> packet(
151 BuildUnsizedDataPacket(&framer
, header
, frames
));
152 char buffer
[kMaxPacketSize
];
153 scoped_ptr
<QuicEncryptedPacket
> encrypted(
154 framer
.EncryptPayload(ENCRYPTION_NONE
, header
.packet_packet_number
,
155 *packet
, buffer
, kMaxPacketSize
));
156 EXPECT_TRUE(encrypted
!= nullptr);
157 return scoped_ptr
<QuicEncryptedPacket
>(encrypted
->Clone());
160 // Returns a newly created packet to send kData on stream 1.
161 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeDataPacket(
162 QuicPacketNumber packet_number
,
163 QuicStreamId stream_id
,
164 bool should_include_version
,
166 QuicStreamOffset offset
,
167 base::StringPiece data
) {
168 InitializeHeader(packet_number
, should_include_version
);
169 QuicStreamFrame
frame(stream_id
, fin
, offset
, data
);
170 return MakePacket(header_
, QuicFrame(&frame
));
173 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeRequestHeadersPacket(
174 QuicPacketNumber packet_number
,
175 QuicStreamId stream_id
,
176 bool should_include_version
,
178 QuicPriority priority
,
179 const SpdyHeaderBlock
& headers
) {
180 InitializeHeader(packet_number
, should_include_version
);
181 scoped_ptr
<SpdySerializedFrame
> spdy_frame
;
182 if (spdy_request_framer_
.protocol_version() == SPDY3
) {
183 SpdySynStreamIR
syn_stream(stream_id
);
184 syn_stream
.set_header_block(headers
);
185 syn_stream
.set_fin(fin
);
186 syn_stream
.set_priority(priority
);
187 spdy_frame
.reset(spdy_request_framer_
.SerializeSynStream(syn_stream
));
189 SpdyHeadersIR
headers_frame(stream_id
);
190 headers_frame
.set_header_block(headers
);
191 headers_frame
.set_fin(fin
);
192 headers_frame
.set_priority(priority
);
193 headers_frame
.set_has_priority(true);
194 spdy_frame
.reset(spdy_request_framer_
.SerializeFrame(headers_frame
));
196 QuicStreamFrame
frame(
197 kHeadersStreamId
, false, 0,
198 base::StringPiece(spdy_frame
->data(), spdy_frame
->size()));
199 return MakePacket(header_
, QuicFrame(&frame
));
202 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakeResponseHeadersPacket(
203 QuicPacketNumber packet_number
,
204 QuicStreamId stream_id
,
205 bool should_include_version
,
207 const SpdyHeaderBlock
& headers
) {
208 InitializeHeader(packet_number
, should_include_version
);
209 scoped_ptr
<SpdySerializedFrame
> spdy_frame
;
210 if (spdy_request_framer_
.protocol_version() == SPDY3
) {
211 SpdySynReplyIR
syn_reply(stream_id
);
212 syn_reply
.set_header_block(headers
);
213 syn_reply
.set_fin(fin
);
214 spdy_frame
.reset(spdy_response_framer_
.SerializeSynReply(syn_reply
));
216 SpdyHeadersIR
headers_frame(stream_id
);
217 headers_frame
.set_header_block(headers
);
218 headers_frame
.set_fin(fin
);
219 spdy_frame
.reset(spdy_request_framer_
.SerializeFrame(headers_frame
));
221 QuicStreamFrame
frame(
222 kHeadersStreamId
, false, 0,
223 base::StringPiece(spdy_frame
->data(), spdy_frame
->size()));
224 return MakePacket(header_
, QuicFrame(&frame
));
227 SpdyHeaderBlock
QuicTestPacketMaker::GetRequestHeaders(
228 const std::string
& method
,
229 const std::string
& scheme
,
230 const std::string
& path
) {
231 SpdyHeaderBlock headers
;
232 if (version_
<= QUIC_VERSION_24
) {
233 headers
[":host"] = host_
;
235 headers
[":authority"] = host_
;
237 headers
[":path"] = path
;
238 headers
[":scheme"] = scheme
;
239 headers
[":method"] = method
;
240 if (version_
<= QUIC_VERSION_24
) {
241 headers
[":version"] = "HTTP/1.1";
246 SpdyHeaderBlock
QuicTestPacketMaker::GetResponseHeaders(
247 const std::string
& status
) {
248 SpdyHeaderBlock headers
;
249 headers
[":status"] = status
;
250 if (version_
<= QUIC_VERSION_24
) {
251 headers
[":version"] = "HTTP/1.1";
253 headers
["content-type"] = "text/plain";
257 scoped_ptr
<QuicEncryptedPacket
> QuicTestPacketMaker::MakePacket(
258 const QuicPacketHeader
& header
,
259 const QuicFrame
& frame
) {
260 QuicFramer
framer(SupportedVersions(version_
), QuicTime::Zero(),
261 Perspective::IS_CLIENT
);
263 frames
.push_back(frame
);
264 scoped_ptr
<QuicPacket
> packet(
265 BuildUnsizedDataPacket(&framer
, header
, frames
));
266 char buffer
[kMaxPacketSize
];
267 scoped_ptr
<QuicEncryptedPacket
> encrypted(
268 framer
.EncryptPayload(ENCRYPTION_NONE
, header
.packet_packet_number
,
269 *packet
, buffer
, kMaxPacketSize
));
270 EXPECT_TRUE(encrypted
!= nullptr);
271 return scoped_ptr
<QuicEncryptedPacket
>(encrypted
->Clone());
274 void QuicTestPacketMaker::InitializeHeader(QuicPacketNumber packet_number
,
275 bool should_include_version
) {
276 header_
.public_header
.connection_id
= connection_id_
;
277 header_
.public_header
.reset_flag
= false;
278 header_
.public_header
.version_flag
= should_include_version
;
279 header_
.public_header
.packet_number_length
= PACKET_1BYTE_PACKET_NUMBER
;
280 header_
.packet_packet_number
= packet_number
;
281 header_
.fec_group
= 0;
282 header_
.entropy_flag
= false;
283 header_
.fec_flag
= false;