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