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