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. 4 5 #include "net/quic/test_tools/simple_quic_framer.h" 6 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" 11 12 using base::StringPiece; 13 using std::string; 14 using std::vector; 15 16 namespace net { 17 namespace test { 18 19 class SimpleFramerVisitor : public QuicFramerVisitorInterface { 20 public: 21 SimpleFramerVisitor() 22 : error_(QUIC_NO_ERROR) { 23 } 24 25 virtual ~SimpleFramerVisitor() { 26 STLDeleteElements(&stream_data_); 27 } 28 29 virtual void OnError(QuicFramer* framer) OVERRIDE { 30 error_ = framer->error(); 31 } 32 33 virtual bool OnProtocolVersionMismatch(QuicVersion version) OVERRIDE { 34 return false; 35 } 36 37 virtual void OnPacket() OVERRIDE {} 38 virtual void OnPublicResetPacket( 39 const QuicPublicResetPacket& packet) OVERRIDE {} 40 virtual void OnVersionNegotiationPacket( 41 const QuicVersionNegotiationPacket& packet) OVERRIDE {} 42 virtual void OnRevivedPacket() OVERRIDE {} 43 44 virtual bool OnPacketHeader(const QuicPacketHeader& header) OVERRIDE { 45 has_header_ = true; 46 header_ = header; 47 return true; 48 } 49 50 virtual bool OnUnauthenticatedHeader( 51 const QuicPacketHeader& header) OVERRIDE { 52 return true; 53 } 54 55 virtual void OnFecProtectedPayload(StringPiece payload) OVERRIDE {} 56 57 virtual bool OnStreamFrame(const QuicStreamFrame& frame) OVERRIDE { 58 // Save a copy of the data so it is valid after the packet is processed. 59 stream_data_.push_back(frame.GetDataAsString()); 60 QuicStreamFrame stream_frame(frame); 61 // Make sure that the stream frame points to this data. 62 stream_frame.data.Clear(); 63 stream_frame.data.Append(const_cast<char*>(stream_data_.back()->data()), 64 stream_data_.back()->size()); 65 stream_frames_.push_back(stream_frame); 66 return true; 67 } 68 69 virtual bool OnAckFrame(const QuicAckFrame& frame) OVERRIDE { 70 ack_frames_.push_back(frame); 71 return true; 72 } 73 74 virtual bool OnCongestionFeedbackFrame( 75 const QuicCongestionFeedbackFrame& frame) OVERRIDE { 76 feedback_frames_.push_back(frame); 77 return true; 78 } 79 80 virtual void OnFecData(const QuicFecData& fec) OVERRIDE { 81 fec_data_ = fec; 82 fec_redundancy_ = fec_data_.redundancy.as_string(); 83 fec_data_.redundancy = fec_redundancy_; 84 } 85 86 virtual bool OnRstStreamFrame(const QuicRstStreamFrame& frame) OVERRIDE { 87 rst_stream_frames_.push_back(frame); 88 return true; 89 } 90 91 virtual bool OnConnectionCloseFrame( 92 const QuicConnectionCloseFrame& frame) OVERRIDE { 93 connection_close_frames_.push_back(frame); 94 return true; 95 } 96 97 virtual bool OnGoAwayFrame(const QuicGoAwayFrame& frame) OVERRIDE { 98 goaway_frames_.push_back(frame); 99 return true; 100 } 101 102 virtual void OnPacketComplete() OVERRIDE {} 103 104 const QuicPacketHeader& header() const { return header_; } 105 const vector<QuicAckFrame>& ack_frames() const { return ack_frames_; } 106 const vector<QuicConnectionCloseFrame>& connection_close_frames() const { 107 return connection_close_frames_; 108 } 109 const vector<QuicCongestionFeedbackFrame>& feedback_frames() const { 110 return feedback_frames_; 111 } 112 const vector<QuicGoAwayFrame>& goaway_frames() const { 113 return goaway_frames_; 114 } 115 const vector<QuicRstStreamFrame>& rst_stream_frames() const { 116 return rst_stream_frames_; 117 } 118 const vector<QuicStreamFrame>& stream_frames() const { 119 return stream_frames_; 120 } 121 const QuicFecData& fec_data() const { 122 return fec_data_; 123 } 124 125 private: 126 QuicErrorCode error_; 127 bool has_header_; 128 QuicPacketHeader header_; 129 QuicFecData fec_data_; 130 string fec_redundancy_; 131 vector<QuicAckFrame> ack_frames_; 132 vector<QuicCongestionFeedbackFrame> feedback_frames_; 133 vector<QuicStreamFrame> stream_frames_; 134 vector<QuicRstStreamFrame> rst_stream_frames_; 135 vector<QuicGoAwayFrame> goaway_frames_; 136 vector<QuicConnectionCloseFrame> connection_close_frames_; 137 vector<string*> stream_data_; 138 139 DISALLOW_COPY_AND_ASSIGN(SimpleFramerVisitor); 140 }; 141 142 SimpleQuicFramer::SimpleQuicFramer() 143 : framer_(QuicSupportedVersions(), QuicTime::Zero(), true) { 144 } 145 146 SimpleQuicFramer::~SimpleQuicFramer() { 147 } 148 149 bool SimpleQuicFramer::ProcessPacket(const QuicPacket& packet) { 150 scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket( 151 ENCRYPTION_NONE, 0, packet)); 152 return ProcessPacket(*encrypted); 153 } 154 155 bool SimpleQuicFramer::ProcessPacket(const QuicEncryptedPacket& packet) { 156 visitor_.reset(new SimpleFramerVisitor); 157 framer_.set_visitor(visitor_.get()); 158 return framer_.ProcessPacket(packet); 159 } 160 161 const QuicPacketHeader& SimpleQuicFramer::header() const { 162 return visitor_->header(); 163 } 164 165 const QuicFecData& SimpleQuicFramer::fec_data() const { 166 return visitor_->fec_data(); 167 } 168 169 QuicFramer* SimpleQuicFramer::framer() { 170 return &framer_; 171 } 172 173 size_t SimpleQuicFramer::num_frames() const { 174 return ack_frames().size() + 175 stream_frames().size() + 176 feedback_frames().size() + 177 rst_stream_frames().size() + 178 goaway_frames().size() + 179 connection_close_frames().size(); 180 } 181 182 const vector<QuicAckFrame>& SimpleQuicFramer::ack_frames() const { 183 return visitor_->ack_frames(); 184 } 185 186 const vector<QuicStreamFrame>& SimpleQuicFramer::stream_frames() const { 187 return visitor_->stream_frames(); 188 } 189 190 const vector<QuicRstStreamFrame>& SimpleQuicFramer::rst_stream_frames() const { 191 return visitor_->rst_stream_frames(); 192 } 193 194 const vector<QuicCongestionFeedbackFrame>& 195 SimpleQuicFramer::feedback_frames() const { 196 return visitor_->feedback_frames(); 197 } 198 199 const vector<QuicGoAwayFrame>& 200 SimpleQuicFramer::goaway_frames() const { 201 return visitor_->goaway_frames(); 202 } 203 204 const vector<QuicConnectionCloseFrame>& 205 SimpleQuicFramer::connection_close_frames() const { 206 return visitor_->connection_close_frames(); 207 } 208 209 } // namespace test 210 } // namespace net 211