[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 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/quic_test_utils.h" |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 6 | |
[email protected] | 701bc89 | 2013-01-17 04:51:54 | [diff] [blame] | 7 | #include "base/stl_util.h" |
[email protected] | b12764d | 2013-12-02 22:28:30 | [diff] [blame^] | 8 | #include "base/strings/string_number_conversions.h" |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 9 | #include "net/quic/crypto/crypto_framer.h" |
[email protected] | 6f54ab3 | 2013-03-02 17:43:35 | [diff] [blame] | 10 | #include "net/quic/crypto/crypto_handshake.h" |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 11 | #include "net/quic/crypto/crypto_utils.h" |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 12 | #include "net/quic/crypto/null_encrypter.h" |
[email protected] | 4df6984 | 2013-02-27 06:32:16 | [diff] [blame] | 13 | #include "net/quic/crypto/quic_decrypter.h" |
| 14 | #include "net/quic/crypto/quic_encrypter.h" |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 15 | #include "net/quic/quic_framer.h" |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 16 | #include "net/quic/quic_packet_creator.h" |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 17 | #include "net/quic/test_tools/quic_connection_peer.h" |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 18 | #include "net/spdy/spdy_frame_builder.h" |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 19 | |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 20 | using base::StringPiece; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 21 | using std::max; |
| 22 | using std::min; |
| 23 | using std::string; |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 24 | using testing::_; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 25 | |
| 26 | namespace net { |
| 27 | namespace test { |
[email protected] | 965dbe6 | 2013-08-09 21:34:31 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | // No-op alarm implementation used by MockHelper. |
| 31 | class TestAlarm : public QuicAlarm { |
| 32 | public: |
| 33 | explicit TestAlarm(QuicAlarm::Delegate* delegate) |
| 34 | : QuicAlarm(delegate) { |
| 35 | } |
| 36 | |
| 37 | virtual void SetImpl() OVERRIDE {} |
| 38 | virtual void CancelImpl() OVERRIDE {} |
| 39 | }; |
| 40 | |
| 41 | } // namespace |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 42 | |
| 43 | MockFramerVisitor::MockFramerVisitor() { |
| 44 | // By default, we want to accept packets. |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 45 | ON_CALL(*this, OnProtocolVersionMismatch(_)) |
| 46 | .WillByDefault(testing::Return(false)); |
| 47 | |
| 48 | // By default, we want to accept packets. |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 49 | ON_CALL(*this, OnUnauthenticatedHeader(_)) |
| 50 | .WillByDefault(testing::Return(true)); |
| 51 | |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 52 | ON_CALL(*this, OnPacketHeader(_)) |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 53 | .WillByDefault(testing::Return(true)); |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 54 | |
| 55 | ON_CALL(*this, OnStreamFrame(_)) |
| 56 | .WillByDefault(testing::Return(true)); |
| 57 | |
| 58 | ON_CALL(*this, OnAckFrame(_)) |
| 59 | .WillByDefault(testing::Return(true)); |
| 60 | |
| 61 | ON_CALL(*this, OnCongestionFeedbackFrame(_)) |
| 62 | .WillByDefault(testing::Return(true)); |
| 63 | |
| 64 | ON_CALL(*this, OnRstStreamFrame(_)) |
| 65 | .WillByDefault(testing::Return(true)); |
| 66 | |
| 67 | ON_CALL(*this, OnConnectionCloseFrame(_)) |
| 68 | .WillByDefault(testing::Return(true)); |
| 69 | |
| 70 | ON_CALL(*this, OnGoAwayFrame(_)) |
| 71 | .WillByDefault(testing::Return(true)); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 72 | } |
| 73 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 74 | MockFramerVisitor::~MockFramerVisitor() { |
| 75 | } |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 76 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 77 | bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersion version) { |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 81 | bool NoOpFramerVisitor::OnUnauthenticatedHeader( |
| 82 | const QuicPacketHeader& header) { |
| 83 | return true; |
| 84 | } |
| 85 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 86 | bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) { |
| 87 | return true; |
| 88 | } |
| 89 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 90 | bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& frame) { |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | bool NoOpFramerVisitor::OnAckFrame(const QuicAckFrame& frame) { |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | bool NoOpFramerVisitor::OnCongestionFeedbackFrame( |
| 99 | const QuicCongestionFeedbackFrame& frame) { |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | bool NoOpFramerVisitor::OnRstStreamFrame( |
| 104 | const QuicRstStreamFrame& frame) { |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | bool NoOpFramerVisitor::OnConnectionCloseFrame( |
| 109 | const QuicConnectionCloseFrame& frame) { |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& frame) { |
| 114 | return true; |
| 115 | } |
| 116 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 117 | FramerVisitorCapturingFrames::FramerVisitorCapturingFrames() : frame_count_(0) { |
[email protected] | 134e5c3 | 2012-12-12 19:20:36 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 120 | FramerVisitorCapturingFrames::~FramerVisitorCapturingFrames() { |
[email protected] | 0ac0c5b | 2013-11-20 05:55:58 | [diff] [blame] | 121 | Reset(); |
| 122 | } |
| 123 | |
| 124 | void FramerVisitorCapturingFrames::Reset() { |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 125 | STLDeleteElements(&stream_data_); |
[email protected] | 0ac0c5b | 2013-11-20 05:55:58 | [diff] [blame] | 126 | stream_frames_.clear(); |
| 127 | frame_count_ = 0; |
| 128 | ack_.reset(); |
| 129 | feedback_.reset(); |
| 130 | rst_.reset(); |
| 131 | close_.reset(); |
| 132 | goaway_.reset(); |
| 133 | version_negotiation_packet_.reset(); |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 134 | } |
| 135 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 136 | bool FramerVisitorCapturingFrames::OnPacketHeader( |
[email protected] | 4e6f0ed | 2012-11-02 22:15:38 | [diff] [blame] | 137 | const QuicPacketHeader& header) { |
| 138 | header_ = header; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 139 | frame_count_ = 0; |
[email protected] | 4e6f0ed | 2012-11-02 22:15:38 | [diff] [blame] | 140 | return true; |
| 141 | } |
| 142 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 143 | bool FramerVisitorCapturingFrames::OnStreamFrame(const QuicStreamFrame& frame) { |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 144 | // Make a copy of the frame and store a copy of underlying string, since |
| 145 | // frame.data may not exist outside this callback. |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 146 | stream_data_.push_back(frame.GetDataAsString()); |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 147 | QuicStreamFrame frame_copy = frame; |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 148 | frame_copy.data.Clear(); |
| 149 | frame_copy.data.Append(const_cast<char*>(stream_data_.back()->data()), |
| 150 | stream_data_.back()->size()); |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 151 | stream_frames_.push_back(frame_copy); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 152 | ++frame_count_; |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 153 | return true; |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 154 | } |
| 155 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 156 | bool FramerVisitorCapturingFrames::OnAckFrame(const QuicAckFrame& frame) { |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 157 | ack_.reset(new QuicAckFrame(frame)); |
| 158 | ++frame_count_; |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 159 | return true; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 160 | } |
| 161 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 162 | bool FramerVisitorCapturingFrames::OnCongestionFeedbackFrame( |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 163 | const QuicCongestionFeedbackFrame& frame) { |
| 164 | feedback_.reset(new QuicCongestionFeedbackFrame(frame)); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 165 | ++frame_count_; |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 166 | return true; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 169 | bool FramerVisitorCapturingFrames::OnRstStreamFrame( |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 170 | const QuicRstStreamFrame& frame) { |
| 171 | rst_.reset(new QuicRstStreamFrame(frame)); |
| 172 | ++frame_count_; |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 173 | return true; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 176 | bool FramerVisitorCapturingFrames::OnConnectionCloseFrame( |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 177 | const QuicConnectionCloseFrame& frame) { |
| 178 | close_.reset(new QuicConnectionCloseFrame(frame)); |
| 179 | ++frame_count_; |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 180 | return true; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 183 | bool FramerVisitorCapturingFrames::OnGoAwayFrame(const QuicGoAwayFrame& frame) { |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 184 | goaway_.reset(new QuicGoAwayFrame(frame)); |
| 185 | ++frame_count_; |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 186 | return true; |
[email protected] | 4e6f0ed | 2012-11-02 22:15:38 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 189 | void FramerVisitorCapturingFrames::OnVersionNegotiationPacket( |
| 190 | const QuicVersionNegotiationPacket& packet) { |
| 191 | version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(packet)); |
| 192 | frame_count_ = 0; |
| 193 | } |
| 194 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 195 | FramerVisitorCapturingPublicReset::FramerVisitorCapturingPublicReset() { |
| 196 | } |
| 197 | |
| 198 | FramerVisitorCapturingPublicReset::~FramerVisitorCapturingPublicReset() { |
| 199 | } |
| 200 | |
| 201 | void FramerVisitorCapturingPublicReset::OnPublicResetPacket( |
| 202 | const QuicPublicResetPacket& public_reset) { |
| 203 | public_reset_packet_ = public_reset; |
| 204 | } |
| 205 | |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 206 | MockConnectionVisitor::MockConnectionVisitor() { |
| 207 | } |
| 208 | |
| 209 | MockConnectionVisitor::~MockConnectionVisitor() { |
| 210 | } |
| 211 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 212 | MockHelper::MockHelper() { |
| 213 | } |
| 214 | |
| 215 | MockHelper::~MockHelper() { |
| 216 | } |
| 217 | |
[email protected] | 97693d1 | 2012-11-16 16:05:00 | [diff] [blame] | 218 | const QuicClock* MockHelper::GetClock() const { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 219 | return &clock_; |
| 220 | } |
| 221 | |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 222 | QuicRandom* MockHelper::GetRandomGenerator() { |
| 223 | return &random_generator_; |
| 224 | } |
| 225 | |
[email protected] | 965dbe6 | 2013-08-09 21:34:31 | [diff] [blame] | 226 | QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) { |
| 227 | return new TestAlarm(delegate); |
| 228 | } |
| 229 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 230 | void MockHelper::AdvanceTime(QuicTime::Delta delta) { |
| 231 | clock_.AdvanceTime(delta); |
| 232 | } |
| 233 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 234 | MockConnection::MockConnection(QuicGuid guid, |
| 235 | IPEndPoint address, |
| 236 | bool is_server) |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 237 | : QuicConnection(guid, address, new testing::NiceMock<MockHelper>(), |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 238 | new testing::NiceMock<MockPacketWriter>(), |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 239 | is_server, QuicSupportedVersions()), |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 240 | has_mock_helper_(true), |
[email protected] | 2cfc6bb8 | 2013-10-27 03:40:44 | [diff] [blame] | 241 | writer_(QuicConnectionPeer::GetWriter(this)), |
| 242 | helper_(helper()) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 243 | } |
| 244 | |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 245 | MockConnection::MockConnection(QuicGuid guid, |
| 246 | IPEndPoint address, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 247 | QuicConnectionHelperInterface* helper, |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 248 | QuicPacketWriter* writer, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 249 | bool is_server) |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 250 | : QuicConnection(guid, address, helper, writer, is_server, |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 251 | QuicSupportedVersions()), |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 252 | has_mock_helper_(false) { |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 253 | } |
| 254 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 255 | MockConnection::~MockConnection() { |
| 256 | } |
| 257 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 258 | void MockConnection::AdvanceTime(QuicTime::Delta delta) { |
| 259 | CHECK(has_mock_helper_) << "Cannot advance time unless a MockClock is being" |
| 260 | " used"; |
| 261 | static_cast<MockHelper*>(helper())->AdvanceTime(delta); |
| 262 | } |
| 263 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 264 | PacketSavingConnection::PacketSavingConnection(QuicGuid guid, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 265 | IPEndPoint address, |
| 266 | bool is_server) |
| 267 | : MockConnection(guid, address, is_server) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | PacketSavingConnection::~PacketSavingConnection() { |
[email protected] | 701bc89 | 2013-01-17 04:51:54 | [diff] [blame] | 271 | STLDeleteElements(&packets_); |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 272 | STLDeleteElements(&encrypted_packets_); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 273 | } |
| 274 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 275 | bool PacketSavingConnection::SendOrQueuePacket( |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 276 | EncryptionLevel level, |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 277 | const SerializedPacket& packet, |
| 278 | TransmissionType transmission_type) { |
| 279 | packets_.push_back(packet.packet); |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 280 | QuicEncryptedPacket* encrypted = |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 281 | framer_.EncryptPacket(level, packet.sequence_number, *packet.packet); |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 282 | encrypted_packets_.push_back(encrypted); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 283 | return true; |
| 284 | } |
| 285 | |
| 286 | MockSession::MockSession(QuicConnection* connection, bool is_server) |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 287 | : QuicSession(connection, DefaultQuicConfig(), is_server) { |
[email protected] | 0ac0c5b | 2013-11-20 05:55:58 | [diff] [blame] | 288 | ON_CALL(*this, WritevData(_, _, _, _, _, _)) |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 289 | .WillByDefault(testing::Return(QuicConsumedData(0, false))); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | MockSession::~MockSession() { |
| 293 | } |
| 294 | |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 295 | TestSession::TestSession(QuicConnection* connection, |
| 296 | const QuicConfig& config, |
| 297 | bool is_server) |
| 298 | : QuicSession(connection, config, is_server), |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 299 | crypto_stream_(NULL) { |
| 300 | } |
| 301 | |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 302 | TestSession::~TestSession() {} |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 303 | |
| 304 | void TestSession::SetCryptoStream(QuicCryptoStream* stream) { |
| 305 | crypto_stream_ = stream; |
| 306 | } |
| 307 | |
| 308 | QuicCryptoStream* TestSession::GetCryptoStream() { |
| 309 | return crypto_stream_; |
| 310 | } |
| 311 | |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 312 | MockPacketWriter::MockPacketWriter() { |
| 313 | } |
| 314 | |
| 315 | MockPacketWriter::~MockPacketWriter() { |
| 316 | } |
| 317 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 318 | MockSendAlgorithm::MockSendAlgorithm() { |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 319 | } |
| 320 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 321 | MockSendAlgorithm::~MockSendAlgorithm() { |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 322 | } |
| 323 | |
[email protected] | 97cf302 | 2013-09-05 14:30:16 | [diff] [blame] | 324 | MockAckNotifierDelegate::MockAckNotifierDelegate() { |
| 325 | } |
| 326 | |
| 327 | MockAckNotifierDelegate::~MockAckNotifierDelegate() { |
| 328 | } |
| 329 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 330 | namespace { |
| 331 | |
| 332 | string HexDumpWithMarks(const char* data, int length, |
| 333 | const bool* marks, int mark_length) { |
| 334 | static const char kHexChars[] = "0123456789abcdef"; |
| 335 | static const int kColumns = 4; |
| 336 | |
| 337 | const int kSizeLimit = 1024; |
| 338 | if (length > kSizeLimit || mark_length > kSizeLimit) { |
| 339 | LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes."; |
| 340 | length = min(length, kSizeLimit); |
| 341 | mark_length = min(mark_length, kSizeLimit); |
| 342 | } |
| 343 | |
| 344 | string hex; |
| 345 | for (const char* row = data; length > 0; |
| 346 | row += kColumns, length -= kColumns) { |
| 347 | for (const char *p = row; p < row + 4; ++p) { |
| 348 | if (p < row + length) { |
| 349 | const bool mark = |
| 350 | (marks && (p - data) < mark_length && marks[p - data]); |
| 351 | hex += mark ? '*' : ' '; |
| 352 | hex += kHexChars[(*p & 0xf0) >> 4]; |
| 353 | hex += kHexChars[*p & 0x0f]; |
| 354 | hex += mark ? '*' : ' '; |
| 355 | } else { |
| 356 | hex += " "; |
| 357 | } |
| 358 | } |
| 359 | hex = hex + " "; |
| 360 | |
| 361 | for (const char *p = row; p < row + 4 && p < row + length; ++p) |
| 362 | hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.'; |
| 363 | |
| 364 | hex = hex + '\n'; |
| 365 | } |
| 366 | return hex; |
| 367 | } |
| 368 | |
| 369 | } // namespace |
| 370 | |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 371 | QuicVersion QuicVersionMax() { return QuicSupportedVersions().front(); } |
| 372 | |
| 373 | QuicVersion QuicVersionMin() { return QuicSupportedVersions().back(); } |
| 374 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 375 | void CompareCharArraysWithHexError( |
| 376 | const string& description, |
| 377 | const char* actual, |
| 378 | const int actual_len, |
| 379 | const char* expected, |
| 380 | const int expected_len) { |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 381 | EXPECT_EQ(actual_len, expected_len); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 382 | const int min_len = min(actual_len, expected_len); |
| 383 | const int max_len = max(actual_len, expected_len); |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 384 | scoped_ptr<bool[]> marks(new bool[max_len]); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 385 | bool identical = (actual_len == expected_len); |
| 386 | for (int i = 0; i < min_len; ++i) { |
| 387 | if (actual[i] != expected[i]) { |
| 388 | marks[i] = true; |
| 389 | identical = false; |
| 390 | } else { |
| 391 | marks[i] = false; |
| 392 | } |
| 393 | } |
| 394 | for (int i = min_len; i < max_len; ++i) { |
| 395 | marks[i] = true; |
| 396 | } |
| 397 | if (identical) return; |
| 398 | ADD_FAILURE() |
| 399 | << "Description:\n" |
| 400 | << description |
| 401 | << "\n\nExpected:\n" |
| 402 | << HexDumpWithMarks(expected, expected_len, marks.get(), max_len) |
| 403 | << "\nActual:\n" |
| 404 | << HexDumpWithMarks(actual, actual_len, marks.get(), max_len); |
| 405 | } |
| 406 | |
[email protected] | b12764d | 2013-12-02 22:28:30 | [diff] [blame^] | 407 | bool DecodeHexString(const base::StringPiece& hex, std::string* bytes) { |
| 408 | bytes->clear(); |
| 409 | if (hex.empty()) |
| 410 | return true; |
| 411 | std::vector<uint8> v; |
| 412 | if (!base::HexStringToBytes(hex.as_string(), &v)) |
| 413 | return false; |
| 414 | if (!v.empty()) |
| 415 | bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size()); |
| 416 | return true; |
| 417 | } |
| 418 | |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 419 | static QuicPacket* ConstructPacketFromHandshakeMessage( |
| 420 | QuicGuid guid, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 421 | const CryptoHandshakeMessage& message, |
| 422 | bool should_include_version) { |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 423 | CryptoFramer crypto_framer; |
[email protected] | dc2cc74 | 2012-10-21 13:56:13 | [diff] [blame] | 424 | scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message)); |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 425 | QuicFramer quic_framer(QuicSupportedVersions(), QuicTime::Zero(), false); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 426 | |
| 427 | QuicPacketHeader header; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 428 | header.public_header.guid = guid; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 429 | header.public_header.reset_flag = false; |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 430 | header.public_header.version_flag = should_include_version; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 431 | header.packet_sequence_number = 1; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 432 | header.entropy_flag = false; |
| 433 | header.entropy_hash = 0; |
| 434 | header.fec_flag = false; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 435 | header.fec_group = 0; |
| 436 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 437 | QuicStreamFrame stream_frame(kCryptoStreamId, false, 0, |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 438 | MakeIOVector(data->AsStringPiece())); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 439 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 440 | QuicFrame frame(&stream_frame); |
| 441 | QuicFrames frames; |
| 442 | frames.push_back(frame); |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 443 | return quic_framer.BuildUnsizedDataPacket(header, frames).packet; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 444 | } |
| 445 | |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 446 | QuicPacket* ConstructHandshakePacket(QuicGuid guid, QuicTag tag) { |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 447 | CryptoHandshakeMessage message; |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 448 | message.set_tag(tag); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 449 | return ConstructPacketFromHandshakeMessage(guid, message, false); |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 450 | } |
| 451 | |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 452 | size_t GetPacketLengthForOneStream( |
| 453 | QuicVersion version, |
| 454 | bool include_version, |
| 455 | QuicSequenceNumberLength sequence_number_length, |
| 456 | InFecGroup is_in_fec_group, |
| 457 | size_t* payload_length) { |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 458 | *payload_length = 1; |
| 459 | const size_t stream_length = |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 460 | NullEncrypter().GetCiphertextSize(*payload_length) + |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 461 | QuicPacketCreator::StreamFramePacketOverhead( |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 462 | version, PACKET_8BYTE_GUID, include_version, |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 463 | sequence_number_length, is_in_fec_group); |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 464 | const size_t ack_length = NullEncrypter().GetCiphertextSize( |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 465 | QuicFramer::GetMinAckFrameSize( |
| 466 | version, sequence_number_length, PACKET_1BYTE_SEQUENCE_NUMBER)) + |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 467 | GetPacketHeaderSize(PACKET_8BYTE_GUID, include_version, |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 468 | sequence_number_length, is_in_fec_group); |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 469 | if (stream_length < ack_length) { |
| 470 | *payload_length = 1 + ack_length - stream_length; |
| 471 | } |
| 472 | |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 473 | return NullEncrypter().GetCiphertextSize(*payload_length) + |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 474 | QuicPacketCreator::StreamFramePacketOverhead( |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 475 | version, PACKET_8BYTE_GUID, include_version, |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 476 | sequence_number_length, is_in_fec_group); |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 477 | } |
| 478 | |
[email protected] | ea2ab47b | 2013-08-13 00:44:11 | [diff] [blame] | 479 | // Size in bytes of the stream frame fields for an arbitrary StreamID and |
| 480 | // offset and the last frame in a packet. |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 481 | size_t GetMinStreamFrameSize(QuicVersion version) { |
[email protected] | ea2ab47b | 2013-08-13 00:44:11 | [diff] [blame] | 482 | return kQuicFrameTypeSize + kQuicMaxStreamIdSize + kQuicMaxStreamOffsetSize; |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 483 | } |
| 484 | |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 485 | TestEntropyCalculator::TestEntropyCalculator() { } |
| 486 | |
| 487 | TestEntropyCalculator::~TestEntropyCalculator() { } |
| 488 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 489 | QuicPacketEntropyHash TestEntropyCalculator::EntropyHash( |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 490 | QuicPacketSequenceNumber sequence_number) const { |
| 491 | return 1u; |
| 492 | } |
| 493 | |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 494 | MockEntropyCalculator::MockEntropyCalculator() { } |
| 495 | |
| 496 | MockEntropyCalculator::~MockEntropyCalculator() { } |
| 497 | |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 498 | QuicConfig DefaultQuicConfig() { |
| 499 | QuicConfig config; |
| 500 | config.SetDefaults(); |
| 501 | return config; |
| 502 | } |
| 503 | |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 504 | bool TestDecompressorVisitor::OnDecompressedData(StringPiece data) { |
| 505 | data.AppendToString(&data_); |
| 506 | return true; |
| 507 | } |
| 508 | |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 509 | void TestDecompressorVisitor::OnDecompressionError() { |
| 510 | error_ = true; |
| 511 | } |
| 512 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 513 | } // namespace test |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 514 | } // namespace net |