Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / net / quic / test_tools / quic_test_packet_maker.cc
blobecdd6bf882cc71d757ec6acb0b8e70ca26c3306d
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"
7 #include <list>
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"
14 using std::make_pair;
16 namespace net {
17 namespace test {
19 QuicTestPacketMaker::QuicTestPacketMaker(QuicVersion version,
20 QuicConnectionId connection_id,
21 MockClock* clock)
22 : version_(version),
23 connection_id_(connection_id),
24 clock_(clock),
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,
34 bool include_version,
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;
45 header.fec_group = 0;
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,
53 bool include_version,
54 QuicStreamId stream_id,
55 QuicRstStreamErrorCode error_code,
56 QuicPacketSequenceNumber largest_received,
57 QuicPacketSequenceNumber least_unacked,
58 bool send_feedback) {
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;
68 header.fec_group = 0;
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()));
75 QuicFrames frames;
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(), false);
86 scoped_ptr<QuicPacket> packet(
87 BuildUnsizedDataPacket(&framer, header, frames));
88 return scoped_ptr<QuicEncryptedPacket>(framer.EncryptPacket(
89 ENCRYPTION_NONE, header.packet_sequence_number, *packet));
92 scoped_ptr<QuicEncryptedPacket> QuicTestPacketMaker::MakeConnectionClosePacket(
93 QuicPacketSequenceNumber num) {
94 QuicPacketHeader header;
95 header.public_header.connection_id = connection_id_;
96 header.public_header.reset_flag = false;
97 header.public_header.version_flag = false;
98 header.public_header.sequence_number_length = PACKET_1BYTE_SEQUENCE_NUMBER;
99 header.packet_sequence_number = num;
100 header.entropy_flag = false;
101 header.fec_flag = false;
102 header.fec_group = 0;
104 QuicConnectionCloseFrame close;
105 close.error_code = QUIC_CRYPTO_VERSION_NOT_SUPPORTED;
106 close.error_details = "Time to panic!";
107 return scoped_ptr<QuicEncryptedPacket>(MakePacket(header, QuicFrame(&close)));
110 scoped_ptr<QuicEncryptedPacket> QuicTestPacketMaker::MakeAckPacket(
111 QuicPacketSequenceNumber sequence_number,
112 QuicPacketSequenceNumber largest_received,
113 QuicPacketSequenceNumber least_unacked,
114 bool send_feedback) {
115 QuicPacketHeader header;
116 header.public_header.connection_id = connection_id_;
117 header.public_header.reset_flag = false;
118 header.public_header.version_flag = false;
119 header.public_header.sequence_number_length = PACKET_1BYTE_SEQUENCE_NUMBER;
120 header.packet_sequence_number = sequence_number;
121 header.entropy_flag = false;
122 header.fec_flag = false;
123 header.fec_group = 0;
125 QuicAckFrame ack(MakeAckFrame(largest_received));
126 ack.delta_time_largest_observed = QuicTime::Delta::Zero();
127 for (QuicPacketSequenceNumber i = least_unacked; i <= largest_received; ++i) {
128 ack.received_packet_times.push_back(make_pair(i, clock_->Now()));
131 QuicFramer framer(SupportedVersions(version_), clock_->Now(), false);
132 QuicFrames frames;
133 frames.push_back(QuicFrame(&ack));
135 QuicStopWaitingFrame stop_waiting;
136 stop_waiting.least_unacked = least_unacked;
137 frames.push_back(QuicFrame(&stop_waiting));
139 scoped_ptr<QuicPacket> packet(
140 BuildUnsizedDataPacket(&framer, header, frames));
141 return scoped_ptr<QuicEncryptedPacket>(framer.EncryptPacket(
142 ENCRYPTION_NONE, header.packet_sequence_number, *packet));
145 // Returns a newly created packet to send kData on stream 1.
146 scoped_ptr<QuicEncryptedPacket> QuicTestPacketMaker::MakeDataPacket(
147 QuicPacketSequenceNumber sequence_number,
148 QuicStreamId stream_id,
149 bool should_include_version,
150 bool fin,
151 QuicStreamOffset offset,
152 base::StringPiece data) {
153 InitializeHeader(sequence_number, should_include_version);
154 QuicStreamFrame frame(stream_id, fin, offset, MakeIOVector(data));
155 return MakePacket(header_, QuicFrame(&frame));
158 scoped_ptr<QuicEncryptedPacket> QuicTestPacketMaker::MakeRequestHeadersPacket(
159 QuicPacketSequenceNumber sequence_number,
160 QuicStreamId stream_id,
161 bool should_include_version,
162 bool fin,
163 QuicPriority priority,
164 const SpdyHeaderBlock& headers) {
165 InitializeHeader(sequence_number, should_include_version);
166 scoped_ptr<SpdySerializedFrame> spdy_frame;
167 if (spdy_request_framer_.protocol_version() == SPDY3) {
168 SpdySynStreamIR syn_stream(stream_id);
169 syn_stream.set_name_value_block(headers);
170 syn_stream.set_fin(fin);
171 syn_stream.set_priority(priority);
172 spdy_frame.reset(spdy_request_framer_.SerializeSynStream(syn_stream));
173 } else {
174 SpdyHeadersIR headers_frame(stream_id);
175 headers_frame.set_name_value_block(headers);
176 headers_frame.set_fin(fin);
177 headers_frame.set_priority(priority);
178 headers_frame.set_has_priority(true);
179 spdy_frame.reset(spdy_request_framer_.SerializeFrame(headers_frame));
181 QuicStreamFrame frame(kHeadersStreamId, false, 0,
182 MakeIOVector(base::StringPiece(spdy_frame->data(),
183 spdy_frame->size())));
184 return MakePacket(header_, QuicFrame(&frame));
187 scoped_ptr<QuicEncryptedPacket> QuicTestPacketMaker::MakeResponseHeadersPacket(
188 QuicPacketSequenceNumber sequence_number,
189 QuicStreamId stream_id,
190 bool should_include_version,
191 bool fin,
192 const SpdyHeaderBlock& headers) {
193 InitializeHeader(sequence_number, should_include_version);
194 scoped_ptr<SpdySerializedFrame> spdy_frame;
195 if (spdy_request_framer_.protocol_version() == SPDY3) {
196 SpdySynReplyIR syn_reply(stream_id);
197 syn_reply.set_name_value_block(headers);
198 syn_reply.set_fin(fin);
199 spdy_frame.reset(spdy_response_framer_.SerializeSynReply(syn_reply));
200 } else {
201 SpdyHeadersIR headers_frame(stream_id);
202 headers_frame.set_name_value_block(headers);
203 headers_frame.set_fin(fin);
204 spdy_frame.reset(spdy_request_framer_.SerializeFrame(headers_frame));
206 QuicStreamFrame frame(kHeadersStreamId, false, 0,
207 MakeIOVector(base::StringPiece(spdy_frame->data(),
208 spdy_frame->size())));
209 return MakePacket(header_, QuicFrame(&frame));
212 SpdyHeaderBlock QuicTestPacketMaker::GetRequestHeaders(
213 const std::string& method,
214 const std::string& scheme,
215 const std::string& path) {
216 SpdyHeaderBlock headers;
217 headers[":method"] = method;
218 headers[":host"] = "www.google.com";
219 headers[":path"] = path;
220 headers[":scheme"] = scheme;
221 headers[":version"] = "HTTP/1.1";
222 return headers;
225 SpdyHeaderBlock QuicTestPacketMaker::GetResponseHeaders(
226 const std::string& status) {
227 SpdyHeaderBlock headers;
228 headers[":status"] = status;
229 headers[":version"] = "HTTP/1.1";
230 headers["content-type"] = "text/plain";
231 return headers;
234 scoped_ptr<QuicEncryptedPacket> QuicTestPacketMaker::MakePacket(
235 const QuicPacketHeader& header,
236 const QuicFrame& frame) {
237 QuicFramer framer(SupportedVersions(version_), QuicTime::Zero(), false);
238 QuicFrames frames;
239 frames.push_back(frame);
240 scoped_ptr<QuicPacket> packet(
241 BuildUnsizedDataPacket(&framer, header, frames));
242 return scoped_ptr<QuicEncryptedPacket>(framer.EncryptPacket(
243 ENCRYPTION_NONE, header.packet_sequence_number, *packet));
246 void QuicTestPacketMaker::InitializeHeader(
247 QuicPacketSequenceNumber sequence_number,
248 bool should_include_version) {
249 header_.public_header.connection_id = connection_id_;
250 header_.public_header.reset_flag = false;
251 header_.public_header.version_flag = should_include_version;
252 header_.public_header.sequence_number_length = PACKET_1BYTE_SEQUENCE_NUMBER;
253 header_.packet_sequence_number = sequence_number;
254 header_.fec_group = 0;
255 header_.entropy_flag = false;
256 header_.fec_flag = false;
259 } // namespace test
260 } // namespace net