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