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