1 // Copyright (c) 2012 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/simple_quic_framer.h"
7 #include "base/stl_util.h"
8 #include "net/quic/crypto/crypto_framer.h"
9 #include "net/quic/crypto/quic_decrypter.h"
10 #include "net/quic/crypto/quic_encrypter.h"
12 using base::StringPiece
;
19 class SimpleFramerVisitor
: public QuicFramerVisitorInterface
{
22 : error_(QUIC_NO_ERROR
) {
25 virtual ~SimpleFramerVisitor() OVERRIDE
{
26 STLDeleteElements(&stream_data_
);
29 virtual void OnError(QuicFramer
* framer
) OVERRIDE
{
30 error_
= framer
->error();
33 virtual bool OnProtocolVersionMismatch(QuicVersion version
) OVERRIDE
{
37 virtual void OnPacket() OVERRIDE
{}
38 virtual void OnPublicResetPacket(
39 const QuicPublicResetPacket
& packet
) OVERRIDE
{
40 public_reset_packet_
.reset(new QuicPublicResetPacket(packet
));
42 virtual void OnVersionNegotiationPacket(
43 const QuicVersionNegotiationPacket
& packet
) OVERRIDE
{
44 version_negotiation_packet_
.reset(
45 new QuicVersionNegotiationPacket(packet
));
47 virtual void OnRevivedPacket() OVERRIDE
{}
49 virtual bool OnUnauthenticatedPublicHeader(
50 const QuicPacketPublicHeader
& header
) OVERRIDE
{
53 virtual bool OnUnauthenticatedHeader(
54 const QuicPacketHeader
& header
) OVERRIDE
{
57 virtual void OnDecryptedPacket(EncryptionLevel level
) OVERRIDE
{}
58 virtual bool OnPacketHeader(const QuicPacketHeader
& header
) OVERRIDE
{
64 virtual void OnFecProtectedPayload(StringPiece payload
) OVERRIDE
{}
66 virtual bool OnStreamFrame(const QuicStreamFrame
& frame
) OVERRIDE
{
67 // Save a copy of the data so it is valid after the packet is processed.
68 stream_data_
.push_back(frame
.GetDataAsString());
69 QuicStreamFrame
stream_frame(frame
);
70 // Make sure that the stream frame points to this data.
71 stream_frame
.data
.Clear();
72 stream_frame
.data
.Append(const_cast<char*>(stream_data_
.back()->data()),
73 stream_data_
.back()->size());
74 stream_frames_
.push_back(stream_frame
);
78 virtual bool OnAckFrame(const QuicAckFrame
& frame
) OVERRIDE
{
79 ack_frames_
.push_back(frame
);
83 virtual bool OnCongestionFeedbackFrame(
84 const QuicCongestionFeedbackFrame
& frame
) OVERRIDE
{
85 feedback_frames_
.push_back(frame
);
89 virtual bool OnStopWaitingFrame(const QuicStopWaitingFrame
& frame
) OVERRIDE
{
90 stop_waiting_frames_
.push_back(frame
);
94 virtual bool OnPingFrame(const QuicPingFrame
& frame
) OVERRIDE
{
95 ping_frames_
.push_back(frame
);
99 virtual void OnFecData(const QuicFecData
& fec
) OVERRIDE
{
101 fec_redundancy_
= fec_data_
.redundancy
.as_string();
102 fec_data_
.redundancy
= fec_redundancy_
;
105 virtual bool OnRstStreamFrame(const QuicRstStreamFrame
& frame
) OVERRIDE
{
106 rst_stream_frames_
.push_back(frame
);
110 virtual bool OnConnectionCloseFrame(
111 const QuicConnectionCloseFrame
& frame
) OVERRIDE
{
112 connection_close_frames_
.push_back(frame
);
116 virtual bool OnGoAwayFrame(const QuicGoAwayFrame
& frame
) OVERRIDE
{
117 goaway_frames_
.push_back(frame
);
121 virtual bool OnWindowUpdateFrame(
122 const QuicWindowUpdateFrame
& frame
) OVERRIDE
{
123 window_update_frames_
.push_back(frame
);
127 virtual bool OnBlockedFrame(const QuicBlockedFrame
& frame
) OVERRIDE
{
128 blocked_frames_
.push_back(frame
);
132 virtual void OnPacketComplete() OVERRIDE
{}
134 const QuicPacketHeader
& header() const { return header_
; }
135 const vector
<QuicAckFrame
>& ack_frames() const { return ack_frames_
; }
136 const vector
<QuicConnectionCloseFrame
>& connection_close_frames() const {
137 return connection_close_frames_
;
139 const vector
<QuicCongestionFeedbackFrame
>& feedback_frames() const {
140 return feedback_frames_
;
142 const vector
<QuicGoAwayFrame
>& goaway_frames() const {
143 return goaway_frames_
;
145 const vector
<QuicRstStreamFrame
>& rst_stream_frames() const {
146 return rst_stream_frames_
;
148 const vector
<QuicStreamFrame
>& stream_frames() const {
149 return stream_frames_
;
151 const vector
<QuicStopWaitingFrame
>& stop_waiting_frames() const {
152 return stop_waiting_frames_
;
154 const vector
<QuicPingFrame
>& ping_frames() const {
157 const QuicFecData
& fec_data() const {
160 const QuicVersionNegotiationPacket
* version_negotiation_packet() const {
161 return version_negotiation_packet_
.get();
163 const QuicPublicResetPacket
* public_reset_packet() const {
164 return public_reset_packet_
.get();
168 QuicErrorCode error_
;
170 QuicPacketHeader header_
;
171 QuicFecData fec_data_
;
172 scoped_ptr
<QuicVersionNegotiationPacket
> version_negotiation_packet_
;
173 scoped_ptr
<QuicPublicResetPacket
> public_reset_packet_
;
174 string fec_redundancy_
;
175 vector
<QuicAckFrame
> ack_frames_
;
176 vector
<QuicCongestionFeedbackFrame
> feedback_frames_
;
177 vector
<QuicStopWaitingFrame
> stop_waiting_frames_
;
178 vector
<QuicPingFrame
> ping_frames_
;
179 vector
<QuicStreamFrame
> stream_frames_
;
180 vector
<QuicRstStreamFrame
> rst_stream_frames_
;
181 vector
<QuicGoAwayFrame
> goaway_frames_
;
182 vector
<QuicConnectionCloseFrame
> connection_close_frames_
;
183 vector
<QuicWindowUpdateFrame
> window_update_frames_
;
184 vector
<QuicBlockedFrame
> blocked_frames_
;
185 vector
<string
*> stream_data_
;
187 DISALLOW_COPY_AND_ASSIGN(SimpleFramerVisitor
);
190 SimpleQuicFramer::SimpleQuicFramer()
191 : framer_(QuicSupportedVersions(), QuicTime::Zero(), true) {
194 SimpleQuicFramer::SimpleQuicFramer(const QuicVersionVector
& supported_versions
)
195 : framer_(supported_versions
, QuicTime::Zero(), true) {
198 SimpleQuicFramer::~SimpleQuicFramer() {
201 bool SimpleQuicFramer::ProcessPacket(const QuicPacket
& packet
) {
202 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
203 ENCRYPTION_NONE
, 0, packet
));
204 return ProcessPacket(*encrypted
);
207 bool SimpleQuicFramer::ProcessPacket(const QuicEncryptedPacket
& packet
) {
208 visitor_
.reset(new SimpleFramerVisitor
);
209 framer_
.set_visitor(visitor_
.get());
210 return framer_
.ProcessPacket(packet
);
213 void SimpleQuicFramer::Reset() {
214 visitor_
.reset(new SimpleFramerVisitor
);
218 const QuicPacketHeader
& SimpleQuicFramer::header() const {
219 return visitor_
->header();
222 const QuicFecData
& SimpleQuicFramer::fec_data() const {
223 return visitor_
->fec_data();
226 const QuicVersionNegotiationPacket
*
227 SimpleQuicFramer::version_negotiation_packet() const {
228 return visitor_
->version_negotiation_packet();
231 const QuicPublicResetPacket
* SimpleQuicFramer::public_reset_packet() const {
232 return visitor_
->public_reset_packet();
235 QuicFramer
* SimpleQuicFramer::framer() {
239 size_t SimpleQuicFramer::num_frames() const {
240 return ack_frames().size() +
241 feedback_frames().size() +
242 goaway_frames().size() +
243 rst_stream_frames().size() +
244 stop_waiting_frames().size() +
245 stream_frames().size() +
246 ping_frames().size() +
247 connection_close_frames().size();
250 const vector
<QuicAckFrame
>& SimpleQuicFramer::ack_frames() const {
251 return visitor_
->ack_frames();
254 const vector
<QuicStopWaitingFrame
>&
255 SimpleQuicFramer::stop_waiting_frames() const {
256 return visitor_
->stop_waiting_frames();
259 const vector
<QuicPingFrame
>& SimpleQuicFramer::ping_frames() const {
260 return visitor_
->ping_frames();
263 const vector
<QuicStreamFrame
>& SimpleQuicFramer::stream_frames() const {
264 return visitor_
->stream_frames();
267 const vector
<QuicRstStreamFrame
>& SimpleQuicFramer::rst_stream_frames() const {
268 return visitor_
->rst_stream_frames();
271 const vector
<QuicCongestionFeedbackFrame
>&
272 SimpleQuicFramer::feedback_frames() const {
273 return visitor_
->feedback_frames();
276 const vector
<QuicGoAwayFrame
>&
277 SimpleQuicFramer::goaway_frames() const {
278 return visitor_
->goaway_frames();
281 const vector
<QuicConnectionCloseFrame
>&
282 SimpleQuicFramer::connection_close_frames() const {
283 return visitor_
->connection_close_frames();