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