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