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 ~SimpleFramerVisitor() override
{ STLDeleteElements(&stream_data_
); }
27 void OnError(QuicFramer
* framer
) override
{ error_
= framer
->error(); }
29 bool OnProtocolVersionMismatch(QuicVersion version
) override
{ return false; }
31 void OnPacket() override
{}
32 void OnPublicResetPacket(const QuicPublicResetPacket
& packet
) override
{
33 public_reset_packet_
.reset(new QuicPublicResetPacket(packet
));
35 void OnVersionNegotiationPacket(
36 const QuicVersionNegotiationPacket
& packet
) override
{
37 version_negotiation_packet_
.reset(
38 new QuicVersionNegotiationPacket(packet
));
40 void OnRevivedPacket() override
{}
42 bool OnUnauthenticatedPublicHeader(
43 const QuicPacketPublicHeader
& header
) override
{
46 bool OnUnauthenticatedHeader(const QuicPacketHeader
& header
) override
{
49 void OnDecryptedPacket(EncryptionLevel level
) override
{}
50 bool OnPacketHeader(const QuicPacketHeader
& header
) override
{
56 void OnFecProtectedPayload(StringPiece payload
) override
{}
58 bool OnStreamFrame(const QuicStreamFrame
& frame
) override
{
59 // Save a copy of the data so it is valid after the packet is processed.
60 stream_data_
.push_back(frame
.GetDataAsString());
61 QuicStreamFrame
stream_frame(frame
);
62 // Make sure that the stream frame points to this data.
63 stream_frame
.data
.Clear();
64 stream_frame
.data
.Append(const_cast<char*>(stream_data_
.back()->data()),
65 stream_data_
.back()->size());
66 stream_frames_
.push_back(stream_frame
);
70 bool OnAckFrame(const QuicAckFrame
& frame
) override
{
71 ack_frames_
.push_back(frame
);
75 bool OnStopWaitingFrame(const QuicStopWaitingFrame
& frame
) override
{
76 stop_waiting_frames_
.push_back(frame
);
80 bool OnPingFrame(const QuicPingFrame
& frame
) override
{
81 ping_frames_
.push_back(frame
);
85 void OnFecData(const QuicFecData
& fec
) override
{
87 fec_redundancy_
= fec_data_
.redundancy
.as_string();
88 fec_data_
.redundancy
= fec_redundancy_
;
91 bool OnRstStreamFrame(const QuicRstStreamFrame
& frame
) override
{
92 rst_stream_frames_
.push_back(frame
);
96 bool OnConnectionCloseFrame(const QuicConnectionCloseFrame
& frame
) override
{
97 connection_close_frames_
.push_back(frame
);
101 bool OnGoAwayFrame(const QuicGoAwayFrame
& frame
) override
{
102 goaway_frames_
.push_back(frame
);
106 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame
& frame
) override
{
107 window_update_frames_
.push_back(frame
);
111 bool OnBlockedFrame(const QuicBlockedFrame
& frame
) override
{
112 blocked_frames_
.push_back(frame
);
116 void OnPacketComplete() override
{}
118 const QuicPacketHeader
& header() const { return header_
; }
119 const vector
<QuicAckFrame
>& ack_frames() const { return ack_frames_
; }
120 const vector
<QuicConnectionCloseFrame
>& connection_close_frames() const {
121 return connection_close_frames_
;
123 const vector
<QuicGoAwayFrame
>& goaway_frames() const {
124 return goaway_frames_
;
126 const vector
<QuicRstStreamFrame
>& rst_stream_frames() const {
127 return rst_stream_frames_
;
129 const vector
<QuicStreamFrame
>& stream_frames() const {
130 return stream_frames_
;
132 const vector
<QuicStopWaitingFrame
>& stop_waiting_frames() const {
133 return stop_waiting_frames_
;
135 const vector
<QuicPingFrame
>& ping_frames() const {
138 const QuicFecData
& fec_data() const {
141 const QuicVersionNegotiationPacket
* version_negotiation_packet() const {
142 return version_negotiation_packet_
.get();
144 const QuicPublicResetPacket
* public_reset_packet() const {
145 return public_reset_packet_
.get();
149 QuicErrorCode error_
;
151 QuicPacketHeader header_
;
152 QuicFecData fec_data_
;
153 scoped_ptr
<QuicVersionNegotiationPacket
> version_negotiation_packet_
;
154 scoped_ptr
<QuicPublicResetPacket
> public_reset_packet_
;
155 string fec_redundancy_
;
156 vector
<QuicAckFrame
> ack_frames_
;
157 vector
<QuicStopWaitingFrame
> stop_waiting_frames_
;
158 vector
<QuicPingFrame
> ping_frames_
;
159 vector
<QuicStreamFrame
> stream_frames_
;
160 vector
<QuicRstStreamFrame
> rst_stream_frames_
;
161 vector
<QuicGoAwayFrame
> goaway_frames_
;
162 vector
<QuicConnectionCloseFrame
> connection_close_frames_
;
163 vector
<QuicWindowUpdateFrame
> window_update_frames_
;
164 vector
<QuicBlockedFrame
> blocked_frames_
;
165 vector
<string
*> stream_data_
;
167 DISALLOW_COPY_AND_ASSIGN(SimpleFramerVisitor
);
170 SimpleQuicFramer::SimpleQuicFramer()
171 : framer_(QuicSupportedVersions(), QuicTime::Zero(), true) {
174 SimpleQuicFramer::SimpleQuicFramer(const QuicVersionVector
& supported_versions
)
175 : framer_(supported_versions
, QuicTime::Zero(), true) {
178 SimpleQuicFramer::~SimpleQuicFramer() {
181 bool SimpleQuicFramer::ProcessPacket(const QuicPacket
& packet
) {
182 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
183 ENCRYPTION_NONE
, 0, packet
));
184 return ProcessPacket(*encrypted
);
187 bool SimpleQuicFramer::ProcessPacket(const QuicEncryptedPacket
& packet
) {
188 visitor_
.reset(new SimpleFramerVisitor
);
189 framer_
.set_visitor(visitor_
.get());
190 return framer_
.ProcessPacket(packet
);
193 void SimpleQuicFramer::Reset() {
194 visitor_
.reset(new SimpleFramerVisitor
);
198 const QuicPacketHeader
& SimpleQuicFramer::header() const {
199 return visitor_
->header();
202 const QuicFecData
& SimpleQuicFramer::fec_data() const {
203 return visitor_
->fec_data();
206 const QuicVersionNegotiationPacket
*
207 SimpleQuicFramer::version_negotiation_packet() const {
208 return visitor_
->version_negotiation_packet();
211 const QuicPublicResetPacket
* SimpleQuicFramer::public_reset_packet() const {
212 return visitor_
->public_reset_packet();
215 QuicFramer
* SimpleQuicFramer::framer() {
219 size_t SimpleQuicFramer::num_frames() const {
220 return ack_frames().size() +
221 goaway_frames().size() +
222 rst_stream_frames().size() +
223 stop_waiting_frames().size() +
224 stream_frames().size() +
225 ping_frames().size() +
226 connection_close_frames().size();
229 const vector
<QuicAckFrame
>& SimpleQuicFramer::ack_frames() const {
230 return visitor_
->ack_frames();
233 const vector
<QuicStopWaitingFrame
>&
234 SimpleQuicFramer::stop_waiting_frames() const {
235 return visitor_
->stop_waiting_frames();
238 const vector
<QuicPingFrame
>& SimpleQuicFramer::ping_frames() const {
239 return visitor_
->ping_frames();
242 const vector
<QuicStreamFrame
>& SimpleQuicFramer::stream_frames() const {
243 return visitor_
->stream_frames();
246 const vector
<QuicRstStreamFrame
>& SimpleQuicFramer::rst_stream_frames() const {
247 return visitor_
->rst_stream_frames();
250 const vector
<QuicGoAwayFrame
>&
251 SimpleQuicFramer::goaway_frames() const {
252 return visitor_
->goaway_frames();
255 const vector
<QuicConnectionCloseFrame
>&
256 SimpleQuicFramer::connection_close_frames() const {
257 return visitor_
->connection_close_frames();