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