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();
146 QuicErrorCode error_
;
148 QuicPacketHeader header_
;
149 QuicFecData fec_data_
;
150 scoped_ptr
<QuicVersionNegotiationPacket
> version_negotiation_packet_
;
151 scoped_ptr
<QuicPublicResetPacket
> public_reset_packet_
;
152 string fec_redundancy_
;
153 vector
<QuicAckFrame
> ack_frames_
;
154 vector
<QuicStopWaitingFrame
> stop_waiting_frames_
;
155 vector
<QuicPingFrame
> ping_frames_
;
156 vector
<QuicStreamFrame
> stream_frames_
;
157 vector
<QuicRstStreamFrame
> rst_stream_frames_
;
158 vector
<QuicGoAwayFrame
> goaway_frames_
;
159 vector
<QuicConnectionCloseFrame
> connection_close_frames_
;
160 vector
<QuicWindowUpdateFrame
> window_update_frames_
;
161 vector
<QuicBlockedFrame
> blocked_frames_
;
162 vector
<string
*> stream_data_
;
164 DISALLOW_COPY_AND_ASSIGN(SimpleFramerVisitor
);
167 SimpleQuicFramer::SimpleQuicFramer()
168 : framer_(QuicSupportedVersions(),
170 Perspective::IS_SERVER
) {
173 SimpleQuicFramer::SimpleQuicFramer(const QuicVersionVector
& supported_versions
)
174 : framer_(supported_versions
, QuicTime::Zero(), Perspective::IS_SERVER
) {
177 SimpleQuicFramer::~SimpleQuicFramer() {
180 bool SimpleQuicFramer::ProcessPacket(const QuicEncryptedPacket
& packet
) {
181 visitor_
.reset(new SimpleFramerVisitor
);
182 framer_
.set_visitor(visitor_
.get());
183 return framer_
.ProcessPacket(packet
);
186 void SimpleQuicFramer::Reset() {
187 visitor_
.reset(new SimpleFramerVisitor
);
191 const QuicPacketHeader
& SimpleQuicFramer::header() const {
192 return visitor_
->header();
195 const QuicFecData
& SimpleQuicFramer::fec_data() const {
196 return visitor_
->fec_data();
199 const QuicVersionNegotiationPacket
*
200 SimpleQuicFramer::version_negotiation_packet() const {
201 return visitor_
->version_negotiation_packet();
204 QuicFramer
* SimpleQuicFramer::framer() {
208 size_t SimpleQuicFramer::num_frames() const {
209 return ack_frames().size() +
210 goaway_frames().size() +
211 rst_stream_frames().size() +
212 stop_waiting_frames().size() +
213 stream_frames().size() +
214 ping_frames().size() +
215 connection_close_frames().size();
218 const vector
<QuicAckFrame
>& SimpleQuicFramer::ack_frames() const {
219 return visitor_
->ack_frames();
222 const vector
<QuicStopWaitingFrame
>&
223 SimpleQuicFramer::stop_waiting_frames() const {
224 return visitor_
->stop_waiting_frames();
227 const vector
<QuicPingFrame
>& SimpleQuicFramer::ping_frames() const {
228 return visitor_
->ping_frames();
231 const vector
<QuicStreamFrame
>& SimpleQuicFramer::stream_frames() const {
232 return visitor_
->stream_frames();
235 const vector
<QuicRstStreamFrame
>& SimpleQuicFramer::rst_stream_frames() const {
236 return visitor_
->rst_stream_frames();
239 const vector
<QuicGoAwayFrame
>&
240 SimpleQuicFramer::goaway_frames() const {
241 return visitor_
->goaway_frames();
244 const vector
<QuicConnectionCloseFrame
>&
245 SimpleQuicFramer::connection_close_frames() const {
246 return visitor_
->connection_close_frames();