[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] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 7 | #include "base/sha1.h" |
[email protected] | 701bc89 | 2013-01-17 04:51:54 | [diff] [blame] | 8 | #include "base/stl_util.h" |
[email protected] | b12764d | 2013-12-02 22:28:30 | [diff] [blame] | 9 | #include "base/strings/string_number_conversions.h" |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 10 | #include "net/quic/crypto/crypto_framer.h" |
[email protected] | 6f54ab3 | 2013-03-02 17:43:35 | [diff] [blame] | 11 | #include "net/quic/crypto/crypto_handshake.h" |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 12 | #include "net/quic/crypto/crypto_utils.h" |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 13 | #include "net/quic/crypto/null_encrypter.h" |
[email protected] | 4df6984 | 2013-02-27 06:32:16 | [diff] [blame] | 14 | #include "net/quic/crypto/quic_decrypter.h" |
| 15 | #include "net/quic/crypto/quic_encrypter.h" |
rtenneti | 16a2077 | 2015-02-17 18:58:48 | [diff] [blame] | 16 | #include "net/quic/quic_data_writer.h" |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 17 | #include "net/quic/quic_framer.h" |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 18 | #include "net/quic/quic_packet_creator.h" |
[email protected] | 79d13dcb | 2014-02-05 07:23:13 | [diff] [blame] | 19 | #include "net/quic/quic_utils.h" |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 20 | #include "net/quic/test_tools/quic_connection_peer.h" |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 21 | #include "net/spdy/spdy_frame_builder.h" |
rch | c99f380c | 2015-03-26 19:50:56 | [diff] [blame] | 22 | #include "net/tools/quic/quic_per_connection_packet_writer.h" |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 23 | |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 24 | using base::StringPiece; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 25 | using std::max; |
| 26 | using std::min; |
| 27 | using std::string; |
[email protected] | 64c1223 | 2014-03-26 05:43:59 | [diff] [blame] | 28 | using testing::AnyNumber; |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 29 | using testing::Invoke; |
[email protected] | bc356fe | 2014-06-19 11:14:14 | [diff] [blame] | 30 | using testing::_; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 31 | |
| 32 | namespace net { |
| 33 | namespace test { |
[email protected] | 965dbe6 | 2013-08-09 21:34:31 | [diff] [blame] | 34 | namespace { |
| 35 | |
| 36 | // No-op alarm implementation used by MockHelper. |
| 37 | class TestAlarm : public QuicAlarm { |
| 38 | public: |
| 39 | explicit TestAlarm(QuicAlarm::Delegate* delegate) |
| 40 | : QuicAlarm(delegate) { |
| 41 | } |
| 42 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 43 | void SetImpl() override {} |
| 44 | void CancelImpl() override {} |
[email protected] | 965dbe6 | 2013-08-09 21:34:31 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | } // namespace |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 48 | |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 49 | QuicAckFrame MakeAckFrame(QuicPacketSequenceNumber largest_observed) { |
[email protected] | fb35b0a | 2014-04-15 21:06:49 | [diff] [blame] | 50 | QuicAckFrame ack; |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 51 | ack.largest_observed = largest_observed; |
| 52 | ack.entropy_hash = 0; |
[email protected] | fb35b0a | 2014-04-15 21:06:49 | [diff] [blame] | 53 | return ack; |
| 54 | } |
| 55 | |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 56 | QuicAckFrame MakeAckFrameWithNackRanges( |
| 57 | size_t num_nack_ranges, QuicPacketSequenceNumber least_unacked) { |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 58 | QuicAckFrame ack = MakeAckFrame(2 * num_nack_ranges + least_unacked); |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 59 | // Add enough missing packets to get num_nack_ranges nack ranges. |
| 60 | for (QuicPacketSequenceNumber i = 1; i < 2 * num_nack_ranges; i += 2) { |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 61 | ack.missing_packets.insert(least_unacked + i); |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 62 | } |
| 63 | return ack; |
| 64 | } |
| 65 | |
rtenneti | b6ac61a5 | 2015-02-11 20:20:52 | [diff] [blame] | 66 | QuicPacket* BuildUnsizedDataPacket(QuicFramer* framer, |
| 67 | const QuicPacketHeader& header, |
| 68 | const QuicFrames& frames) { |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 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 | } |
rtenneti | 16a2077 | 2015-02-17 18:58:48 | [diff] [blame] | 82 | return BuildUnsizedDataPacket(framer, header, frames, packet_size); |
| 83 | } |
| 84 | |
| 85 | QuicPacket* BuildUnsizedDataPacket(QuicFramer* framer, |
| 86 | const QuicPacketHeader& header, |
| 87 | const QuicFrames& frames, |
| 88 | size_t packet_size) { |
| 89 | char* buffer = new char[packet_size]; |
| 90 | scoped_ptr<QuicPacket> packet( |
| 91 | framer->BuildDataPacket(header, frames, buffer, packet_size)); |
| 92 | DCHECK(packet.get() != nullptr); |
| 93 | // Now I have to re-construct the data packet with data ownership. |
| 94 | return new QuicPacket(buffer, packet->length(), true, |
| 95 | header.public_header.connection_id_length, |
| 96 | header.public_header.version_flag, |
| 97 | header.public_header.sequence_number_length); |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 100 | uint64 SimpleRandom::RandUint64() { |
| 101 | unsigned char hash[base::kSHA1Length]; |
| 102 | base::SHA1HashBytes(reinterpret_cast<unsigned char*>(&seed_), sizeof(seed_), |
| 103 | hash); |
| 104 | memcpy(&seed_, hash, sizeof(seed_)); |
| 105 | return seed_; |
| 106 | } |
| 107 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 108 | MockFramerVisitor::MockFramerVisitor() { |
| 109 | // By default, we want to accept packets. |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 110 | ON_CALL(*this, OnProtocolVersionMismatch(_)) |
| 111 | .WillByDefault(testing::Return(false)); |
| 112 | |
| 113 | // By default, we want to accept packets. |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 114 | ON_CALL(*this, OnUnauthenticatedHeader(_)) |
| 115 | .WillByDefault(testing::Return(true)); |
| 116 | |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 117 | ON_CALL(*this, OnUnauthenticatedPublicHeader(_)) |
| 118 | .WillByDefault(testing::Return(true)); |
| 119 | |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 120 | ON_CALL(*this, OnPacketHeader(_)) |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 121 | .WillByDefault(testing::Return(true)); |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 122 | |
| 123 | ON_CALL(*this, OnStreamFrame(_)) |
| 124 | .WillByDefault(testing::Return(true)); |
| 125 | |
| 126 | ON_CALL(*this, OnAckFrame(_)) |
| 127 | .WillByDefault(testing::Return(true)); |
| 128 | |
[email protected] | 93dd91f | 2014-02-27 00:09:03 | [diff] [blame] | 129 | ON_CALL(*this, OnStopWaitingFrame(_)) |
| 130 | .WillByDefault(testing::Return(true)); |
| 131 | |
[email protected] | d8c52211 | 2014-04-23 09:23:25 | [diff] [blame] | 132 | ON_CALL(*this, OnPingFrame(_)) |
| 133 | .WillByDefault(testing::Return(true)); |
| 134 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 135 | ON_CALL(*this, OnRstStreamFrame(_)) |
| 136 | .WillByDefault(testing::Return(true)); |
| 137 | |
| 138 | ON_CALL(*this, OnConnectionCloseFrame(_)) |
| 139 | .WillByDefault(testing::Return(true)); |
| 140 | |
| 141 | ON_CALL(*this, OnGoAwayFrame(_)) |
| 142 | .WillByDefault(testing::Return(true)); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 145 | MockFramerVisitor::~MockFramerVisitor() { |
| 146 | } |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 147 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 148 | bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersion version) { |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 149 | return false; |
| 150 | } |
| 151 | |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 152 | bool NoOpFramerVisitor::OnUnauthenticatedPublicHeader( |
| 153 | const QuicPacketPublicHeader& header) { |
| 154 | return true; |
| 155 | } |
| 156 | |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 157 | bool NoOpFramerVisitor::OnUnauthenticatedHeader( |
| 158 | const QuicPacketHeader& header) { |
| 159 | return true; |
| 160 | } |
| 161 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 162 | bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) { |
| 163 | return true; |
| 164 | } |
| 165 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 166 | bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& frame) { |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | bool NoOpFramerVisitor::OnAckFrame(const QuicAckFrame& frame) { |
| 171 | return true; |
| 172 | } |
| 173 | |
[email protected] | 93dd91f | 2014-02-27 00:09:03 | [diff] [blame] | 174 | bool NoOpFramerVisitor::OnStopWaitingFrame( |
| 175 | const QuicStopWaitingFrame& frame) { |
| 176 | return true; |
| 177 | } |
| 178 | |
[email protected] | d8c52211 | 2014-04-23 09:23:25 | [diff] [blame] | 179 | bool NoOpFramerVisitor::OnPingFrame(const QuicPingFrame& frame) { |
| 180 | return true; |
| 181 | } |
| 182 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 183 | bool NoOpFramerVisitor::OnRstStreamFrame( |
| 184 | const QuicRstStreamFrame& frame) { |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | bool NoOpFramerVisitor::OnConnectionCloseFrame( |
| 189 | const QuicConnectionCloseFrame& frame) { |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& frame) { |
| 194 | return true; |
| 195 | } |
| 196 | |
[email protected] | cb23a92 | 2014-02-20 17:42:38 | [diff] [blame] | 197 | bool NoOpFramerVisitor::OnWindowUpdateFrame( |
| 198 | const QuicWindowUpdateFrame& frame) { |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& frame) { |
| 203 | return true; |
| 204 | } |
| 205 | |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 206 | MockConnectionVisitor::MockConnectionVisitor() { |
| 207 | } |
| 208 | |
| 209 | MockConnectionVisitor::~MockConnectionVisitor() { |
| 210 | } |
| 211 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 212 | MockHelper::MockHelper() { |
| 213 | } |
| 214 | |
| 215 | MockHelper::~MockHelper() { |
| 216 | } |
| 217 | |
[email protected] | 97693d1 | 2012-11-16 16:05:00 | [diff] [blame] | 218 | const QuicClock* MockHelper::GetClock() const { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 219 | return &clock_; |
| 220 | } |
| 221 | |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 222 | QuicRandom* MockHelper::GetRandomGenerator() { |
| 223 | return &random_generator_; |
| 224 | } |
| 225 | |
[email protected] | 965dbe6 | 2013-08-09 21:34:31 | [diff] [blame] | 226 | QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) { |
| 227 | return new TestAlarm(delegate); |
| 228 | } |
| 229 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 230 | void MockHelper::AdvanceTime(QuicTime::Delta delta) { |
| 231 | clock_.AdvanceTime(delta); |
| 232 | } |
| 233 | |
rtenneti | 3fe4ebbc | 2014-11-16 16:43:47 | [diff] [blame] | 234 | QuicPacketWriter* NiceMockPacketWriterFactory::Create( |
| 235 | QuicConnection* /*connection*/) const { |
| 236 | return new testing::NiceMock<MockPacketWriter>(); |
| 237 | } |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 238 | |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 239 | MockConnection::MockConnection(Perspective perspective) |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 240 | : MockConnection(perspective, |
| 241 | /* is_secure= */ false) { |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 242 | } |
| 243 | |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 244 | MockConnection::MockConnection(Perspective perspective, bool is_secure) |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 245 | : MockConnection(kTestConnectionId, |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 246 | IPEndPoint(TestPeerIPAddress(), kTestPort), |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 247 | perspective, |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 248 | is_secure, |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 249 | QuicSupportedVersions()) { |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 250 | } |
| 251 | |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 252 | MockConnection::MockConnection(IPEndPoint address, Perspective perspective) |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 253 | : MockConnection(kTestConnectionId, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 254 | address, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 255 | perspective, |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 256 | /* is_secure= */ false, |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 257 | QuicSupportedVersions()) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 258 | } |
| 259 | |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 260 | MockConnection::MockConnection(QuicConnectionId connection_id, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 261 | Perspective perspective) |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 262 | : MockConnection(connection_id, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 263 | perspective, |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 264 | /* is_secure= */ false) { |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 265 | } |
| 266 | |
rtenneti | fb3fa6c | 2015-03-16 23:04:55 | [diff] [blame] | 267 | MockConnection::MockConnection(QuicConnectionId connection_id, |
| 268 | Perspective perspective, |
| 269 | bool is_secure) |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 270 | : MockConnection(connection_id, |
rtenneti | fb3fa6c | 2015-03-16 23:04:55 | [diff] [blame] | 271 | IPEndPoint(TestPeerIPAddress(), kTestPort), |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 272 | perspective, |
| 273 | is_secure, |
| 274 | QuicSupportedVersions()) { |
| 275 | } |
| 276 | |
| 277 | MockConnection::MockConnection(Perspective perspective, |
| 278 | const QuicVersionVector& supported_versions) |
| 279 | : MockConnection(kTestConnectionId, |
| 280 | IPEndPoint(TestPeerIPAddress(), kTestPort), |
| 281 | perspective, |
| 282 | /* is_secure= */ false, |
| 283 | supported_versions) { |
| 284 | } |
| 285 | |
| 286 | MockConnection::MockConnection(QuicConnectionId connection_id, |
| 287 | IPEndPoint address, |
| 288 | Perspective perspective, |
| 289 | bool is_secure, |
| 290 | const QuicVersionVector& supported_versions) |
| 291 | : QuicConnection(connection_id, |
| 292 | address, |
rtenneti | fb3fa6c | 2015-03-16 23:04:55 | [diff] [blame] | 293 | new testing::NiceMock<MockHelper>(), |
| 294 | NiceMockPacketWriterFactory(), |
| 295 | /* owns_writer= */ true, |
| 296 | perspective, |
| 297 | is_secure, |
rtenneti | e3779d83 | 2014-11-14 02:08:17 | [diff] [blame] | 298 | supported_versions), |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 299 | helper_(helper()) { |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 300 | ON_CALL(*this, OnError(_)) |
| 301 | .WillByDefault( |
| 302 | Invoke(this, &PacketSavingConnection::QuicConnection_OnError)); |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 303 | } |
| 304 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 305 | MockConnection::~MockConnection() { |
| 306 | } |
| 307 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 308 | void MockConnection::AdvanceTime(QuicTime::Delta delta) { |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 309 | static_cast<MockHelper*>(helper())->AdvanceTime(delta); |
| 310 | } |
| 311 | |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 312 | PacketSavingConnection::PacketSavingConnection(Perspective perspective) |
| 313 | : MockConnection(perspective) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 314 | } |
| 315 | |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 316 | PacketSavingConnection::PacketSavingConnection( |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 317 | Perspective perspective, |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 318 | const QuicVersionVector& supported_versions) |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 319 | : MockConnection(perspective, supported_versions) { |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 320 | } |
| 321 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 322 | PacketSavingConnection::~PacketSavingConnection() { |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 323 | STLDeleteElements(&encrypted_packets_); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 324 | } |
| 325 | |
rtenneti | 31e9fd6 | 2014-09-16 05:22:15 | [diff] [blame] | 326 | void PacketSavingConnection::SendOrQueuePacket(QueuedPacket packet) { |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 327 | if (!packet.serialized_packet.packet->owns_buffer()) { |
| 328 | scoped_ptr<QuicEncryptedPacket> encrypted_deleter( |
| 329 | packet.serialized_packet.packet); |
| 330 | packet.serialized_packet.packet = packet.serialized_packet.packet->Clone(); |
| 331 | } |
rtenneti | b6ac61a5 | 2015-02-11 20:20:52 | [diff] [blame] | 332 | encrypted_packets_.push_back(packet.serialized_packet.packet); |
rtenneti | 31e9fd6 | 2014-09-16 05:22:15 | [diff] [blame] | 333 | // Transfer ownership of the packet to the SentPacketManager and the |
| 334 | // ack notifier to the AckNotifierManager. |
rtenneti | a4dcff9 | 2014-09-29 18:16:08 | [diff] [blame] | 335 | sent_packet_manager_.OnPacketSent( |
| 336 | &packet.serialized_packet, 0, QuicTime::Zero(), 1000, |
| 337 | NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 338 | } |
| 339 | |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 340 | MockSession::MockSession(QuicConnection* connection) |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 341 | : QuicSession(connection, DefaultQuicConfig()) { |
[email protected] | ccb3421 | 2014-07-18 09:27:50 | [diff] [blame] | 342 | InitializeSession(); |
[email protected] | bbb1007 | 2014-06-13 07:41:59 | [diff] [blame] | 343 | ON_CALL(*this, WritevData(_, _, _, _, _, _)) |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 344 | .WillByDefault(testing::Return(QuicConsumedData(0, false))); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | MockSession::~MockSession() { |
| 348 | } |
| 349 | |
[email protected] | ce7bb141 | 2014-05-17 15:51:33 | [diff] [blame] | 350 | TestSession::TestSession(QuicConnection* connection, const QuicConfig& config) |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 351 | : QuicSession(connection, config), |
rtenneti | 4a5df26 | 2014-11-07 00:43:58 | [diff] [blame] | 352 | crypto_stream_(nullptr) { |
[email protected] | ccb3421 | 2014-07-18 09:27:50 | [diff] [blame] | 353 | InitializeSession(); |
| 354 | } |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 355 | |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 356 | TestSession::~TestSession() {} |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 357 | |
| 358 | void TestSession::SetCryptoStream(QuicCryptoStream* stream) { |
| 359 | crypto_stream_ = stream; |
| 360 | } |
| 361 | |
| 362 | QuicCryptoStream* TestSession::GetCryptoStream() { |
| 363 | return crypto_stream_; |
| 364 | } |
| 365 | |
[email protected] | 90f62f09 | 2014-03-24 02:41:23 | [diff] [blame] | 366 | TestClientSession::TestClientSession(QuicConnection* connection, |
| 367 | const QuicConfig& config) |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 368 | : QuicClientSessionBase(connection, config), |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 369 | crypto_stream_(nullptr) { |
[email protected] | ccb3421 | 2014-07-18 09:27:50 | [diff] [blame] | 370 | EXPECT_CALL(*this, OnProofValid(_)).Times(AnyNumber()); |
| 371 | InitializeSession(); |
[email protected] | 90f62f09 | 2014-03-24 02:41:23 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | TestClientSession::~TestClientSession() {} |
| 375 | |
| 376 | void TestClientSession::SetCryptoStream(QuicCryptoStream* stream) { |
| 377 | crypto_stream_ = stream; |
| 378 | } |
| 379 | |
| 380 | QuicCryptoStream* TestClientSession::GetCryptoStream() { |
| 381 | return crypto_stream_; |
| 382 | } |
| 383 | |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame^] | 384 | TestServerSession::TestServerSession(const QuicConfig& config, |
| 385 | QuicConnection* connection) |
| 386 | : QuicServerSession(config, connection, nullptr) { |
| 387 | } |
| 388 | |
| 389 | TestServerSession::~TestServerSession() { |
| 390 | } |
| 391 | |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 392 | MockPacketWriter::MockPacketWriter() { |
| 393 | } |
| 394 | |
| 395 | MockPacketWriter::~MockPacketWriter() { |
| 396 | } |
| 397 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 398 | MockSendAlgorithm::MockSendAlgorithm() { |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 399 | } |
| 400 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 401 | MockSendAlgorithm::~MockSendAlgorithm() { |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 402 | } |
| 403 | |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 404 | MockLossAlgorithm::MockLossAlgorithm() { |
| 405 | } |
| 406 | |
| 407 | MockLossAlgorithm::~MockLossAlgorithm() { |
| 408 | } |
| 409 | |
[email protected] | 97cf302 | 2013-09-05 14:30:16 | [diff] [blame] | 410 | MockAckNotifierDelegate::MockAckNotifierDelegate() { |
| 411 | } |
| 412 | |
| 413 | MockAckNotifierDelegate::~MockAckNotifierDelegate() { |
| 414 | } |
| 415 | |
[email protected] | a692ad9d | 2014-07-18 21:35:24 | [diff] [blame] | 416 | MockNetworkChangeVisitor::MockNetworkChangeVisitor() { |
| 417 | } |
| 418 | |
| 419 | MockNetworkChangeVisitor::~MockNetworkChangeVisitor() { |
| 420 | } |
| 421 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 422 | namespace { |
| 423 | |
| 424 | string HexDumpWithMarks(const char* data, int length, |
| 425 | const bool* marks, int mark_length) { |
| 426 | static const char kHexChars[] = "0123456789abcdef"; |
| 427 | static const int kColumns = 4; |
| 428 | |
| 429 | const int kSizeLimit = 1024; |
| 430 | if (length > kSizeLimit || mark_length > kSizeLimit) { |
| 431 | LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes."; |
| 432 | length = min(length, kSizeLimit); |
| 433 | mark_length = min(mark_length, kSizeLimit); |
| 434 | } |
| 435 | |
| 436 | string hex; |
| 437 | for (const char* row = data; length > 0; |
| 438 | row += kColumns, length -= kColumns) { |
| 439 | for (const char *p = row; p < row + 4; ++p) { |
| 440 | if (p < row + length) { |
| 441 | const bool mark = |
| 442 | (marks && (p - data) < mark_length && marks[p - data]); |
| 443 | hex += mark ? '*' : ' '; |
| 444 | hex += kHexChars[(*p & 0xf0) >> 4]; |
| 445 | hex += kHexChars[*p & 0x0f]; |
| 446 | hex += mark ? '*' : ' '; |
| 447 | } else { |
| 448 | hex += " "; |
| 449 | } |
| 450 | } |
| 451 | hex = hex + " "; |
| 452 | |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 453 | for (const char* p = row; p < row + 4 && p < row + length; ++p) { |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 454 | hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.'; |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 455 | } |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 456 | |
| 457 | hex = hex + '\n'; |
| 458 | } |
| 459 | return hex; |
| 460 | } |
| 461 | |
| 462 | } // namespace |
| 463 | |
[email protected] | 300ccd5 | 2014-01-25 08:00:19 | [diff] [blame] | 464 | IPAddressNumber TestPeerIPAddress() { return Loopback4(); } |
| 465 | |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 466 | QuicVersion QuicVersionMax() { return QuicSupportedVersions().front(); } |
| 467 | |
| 468 | QuicVersion QuicVersionMin() { return QuicSupportedVersions().back(); } |
| 469 | |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 470 | IPAddressNumber Loopback4() { |
[email protected] | 300ccd5 | 2014-01-25 08:00:19 | [diff] [blame] | 471 | IPAddressNumber addr; |
| 472 | CHECK(ParseIPLiteralToNumber("127.0.0.1", &addr)); |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 473 | return addr; |
| 474 | } |
| 475 | |
[email protected] | 730b35d7 | 2014-06-05 03:23:22 | [diff] [blame] | 476 | IPAddressNumber Loopback6() { |
| 477 | IPAddressNumber addr; |
| 478 | CHECK(ParseIPLiteralToNumber("::1", &addr)); |
| 479 | return addr; |
| 480 | } |
| 481 | |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 482 | IPAddressNumber Any4() { |
| 483 | IPAddressNumber any4; |
| 484 | CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4)); |
| 485 | return any4; |
| 486 | } |
| 487 | |
[email protected] | 9bb57c7 | 2014-03-31 20:36:04 | [diff] [blame] | 488 | void GenerateBody(string* body, int length) { |
| 489 | body->clear(); |
| 490 | body->reserve(length); |
| 491 | for (int i = 0; i < length; ++i) { |
| 492 | body->append(1, static_cast<char>(32 + i % (126 - 32))); |
| 493 | } |
| 494 | } |
| 495 | |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 496 | QuicEncryptedPacket* ConstructEncryptedPacket( |
| 497 | QuicConnectionId connection_id, |
| 498 | bool version_flag, |
| 499 | bool reset_flag, |
| 500 | QuicPacketSequenceNumber sequence_number, |
| 501 | const string& data) { |
rtenneti | 9e0fb50 | 2015-03-08 06:07:16 | [diff] [blame] | 502 | return ConstructEncryptedPacket( |
| 503 | connection_id, version_flag, reset_flag, sequence_number, data, |
| 504 | PACKET_8BYTE_CONNECTION_ID, PACKET_6BYTE_SEQUENCE_NUMBER); |
| 505 | } |
| 506 | |
| 507 | QuicEncryptedPacket* ConstructEncryptedPacket( |
| 508 | QuicConnectionId connection_id, |
| 509 | bool version_flag, |
| 510 | bool reset_flag, |
| 511 | QuicPacketSequenceNumber sequence_number, |
| 512 | const string& data, |
| 513 | QuicConnectionIdLength connection_id_length, |
| 514 | QuicSequenceNumberLength sequence_number_length) { |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 515 | return ConstructEncryptedPacket(connection_id, version_flag, reset_flag, |
| 516 | sequence_number, data, connection_id_length, |
| 517 | sequence_number_length, nullptr); |
| 518 | } |
| 519 | |
| 520 | QuicEncryptedPacket* ConstructEncryptedPacket( |
| 521 | QuicConnectionId connection_id, |
| 522 | bool version_flag, |
| 523 | bool reset_flag, |
| 524 | QuicPacketSequenceNumber sequence_number, |
| 525 | const string& data, |
| 526 | QuicConnectionIdLength connection_id_length, |
| 527 | QuicSequenceNumberLength sequence_number_length, |
| 528 | QuicVersionVector* versions) { |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 529 | QuicPacketHeader header; |
| 530 | header.public_header.connection_id = connection_id; |
rtenneti | 9e0fb50 | 2015-03-08 06:07:16 | [diff] [blame] | 531 | header.public_header.connection_id_length = connection_id_length; |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 532 | header.public_header.version_flag = version_flag; |
| 533 | header.public_header.reset_flag = reset_flag; |
rtenneti | 9e0fb50 | 2015-03-08 06:07:16 | [diff] [blame] | 534 | header.public_header.sequence_number_length = sequence_number_length; |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 535 | header.packet_sequence_number = sequence_number; |
| 536 | header.entropy_flag = false; |
| 537 | header.entropy_hash = 0; |
| 538 | header.fec_flag = false; |
| 539 | header.is_in_fec_group = NOT_IN_FEC_GROUP; |
| 540 | header.fec_group = 0; |
| 541 | QuicStreamFrame stream_frame(1, false, 0, MakeIOVector(data)); |
| 542 | QuicFrame frame(&stream_frame); |
| 543 | QuicFrames frames; |
| 544 | frames.push_back(frame); |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 545 | QuicFramer framer(versions != nullptr ? *versions : QuicSupportedVersions(), |
| 546 | QuicTime::Zero(), Perspective::IS_CLIENT); |
| 547 | |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 548 | scoped_ptr<QuicPacket> packet( |
rtenneti | b6ac61a5 | 2015-02-11 20:20:52 | [diff] [blame] | 549 | BuildUnsizedDataPacket(&framer, header, frames)); |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 550 | EXPECT_TRUE(packet != nullptr); |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 551 | char buffer[kMaxPacketSize]; |
| 552 | scoped_ptr<QuicEncryptedPacket> encrypted(framer.EncryptPacket( |
| 553 | ENCRYPTION_NONE, sequence_number, *packet, buffer, kMaxPacketSize)); |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 554 | EXPECT_TRUE(encrypted != nullptr); |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 555 | return encrypted->Clone(); |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 556 | } |
| 557 | |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 558 | QuicEncryptedPacket* ConstructMisFramedEncryptedPacket( |
| 559 | QuicConnectionId connection_id, |
| 560 | bool version_flag, |
| 561 | bool reset_flag, |
| 562 | QuicPacketSequenceNumber sequence_number, |
| 563 | const string& data, |
| 564 | QuicConnectionIdLength connection_id_length, |
| 565 | QuicSequenceNumberLength sequence_number_length, |
| 566 | QuicVersionVector* versions) { |
| 567 | QuicPacketHeader header; |
| 568 | header.public_header.connection_id = connection_id; |
| 569 | header.public_header.connection_id_length = connection_id_length; |
| 570 | header.public_header.version_flag = version_flag; |
| 571 | header.public_header.reset_flag = reset_flag; |
| 572 | header.public_header.sequence_number_length = sequence_number_length; |
| 573 | header.packet_sequence_number = sequence_number; |
| 574 | header.entropy_flag = false; |
| 575 | header.entropy_hash = 0; |
| 576 | header.fec_flag = false; |
| 577 | header.is_in_fec_group = NOT_IN_FEC_GROUP; |
| 578 | header.fec_group = 0; |
| 579 | QuicFrames frames; |
| 580 | QuicFramer framer(versions ? *versions : QuicSupportedVersions(), |
| 581 | QuicTime::Zero(), Perspective::IS_CLIENT); |
| 582 | // Build a packet with zero frames, which is an error. |
| 583 | scoped_ptr<QuicPacket> packet( |
| 584 | BuildUnsizedDataPacket(&framer, header, frames)); |
| 585 | EXPECT_TRUE(packet != nullptr); |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 586 | char buffer[kMaxPacketSize]; |
| 587 | scoped_ptr<QuicEncryptedPacket> encrypted(framer.EncryptPacket( |
| 588 | ENCRYPTION_NONE, sequence_number, *packet, buffer, kMaxPacketSize)); |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 589 | EXPECT_TRUE(encrypted != nullptr); |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 590 | return encrypted->Clone(); |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 591 | } |
| 592 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 593 | void CompareCharArraysWithHexError( |
| 594 | const string& description, |
| 595 | const char* actual, |
| 596 | const int actual_len, |
| 597 | const char* expected, |
| 598 | const int expected_len) { |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 599 | EXPECT_EQ(actual_len, expected_len); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 600 | const int min_len = min(actual_len, expected_len); |
| 601 | const int max_len = max(actual_len, expected_len); |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 602 | scoped_ptr<bool[]> marks(new bool[max_len]); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 603 | bool identical = (actual_len == expected_len); |
| 604 | for (int i = 0; i < min_len; ++i) { |
| 605 | if (actual[i] != expected[i]) { |
| 606 | marks[i] = true; |
| 607 | identical = false; |
| 608 | } else { |
| 609 | marks[i] = false; |
| 610 | } |
| 611 | } |
| 612 | for (int i = min_len; i < max_len; ++i) { |
| 613 | marks[i] = true; |
| 614 | } |
| 615 | if (identical) return; |
| 616 | ADD_FAILURE() |
| 617 | << "Description:\n" |
| 618 | << description |
| 619 | << "\n\nExpected:\n" |
| 620 | << HexDumpWithMarks(expected, expected_len, marks.get(), max_len) |
| 621 | << "\nActual:\n" |
| 622 | << HexDumpWithMarks(actual, actual_len, marks.get(), max_len); |
| 623 | } |
| 624 | |
[email protected] | b12764d | 2013-12-02 22:28:30 | [diff] [blame] | 625 | bool DecodeHexString(const base::StringPiece& hex, std::string* bytes) { |
| 626 | bytes->clear(); |
| 627 | if (hex.empty()) |
| 628 | return true; |
| 629 | std::vector<uint8> v; |
| 630 | if (!base::HexStringToBytes(hex.as_string(), &v)) |
| 631 | return false; |
| 632 | if (!v.empty()) |
| 633 | bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size()); |
| 634 | return true; |
| 635 | } |
| 636 | |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 637 | static QuicPacket* ConstructPacketFromHandshakeMessage( |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 638 | QuicConnectionId connection_id, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 639 | const CryptoHandshakeMessage& message, |
| 640 | bool should_include_version) { |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 641 | CryptoFramer crypto_framer; |
[email protected] | dc2cc74 | 2012-10-21 13:56:13 | [diff] [blame] | 642 | scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message)); |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 643 | QuicFramer quic_framer(QuicSupportedVersions(), QuicTime::Zero(), |
| 644 | Perspective::IS_CLIENT); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 645 | |
| 646 | QuicPacketHeader header; |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 647 | header.public_header.connection_id = connection_id; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 648 | header.public_header.reset_flag = false; |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 649 | header.public_header.version_flag = should_include_version; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 650 | header.packet_sequence_number = 1; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 651 | header.entropy_flag = false; |
| 652 | header.entropy_hash = 0; |
| 653 | header.fec_flag = false; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 654 | header.fec_group = 0; |
| 655 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 656 | QuicStreamFrame stream_frame(kCryptoStreamId, false, 0, |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 657 | MakeIOVector(data->AsStringPiece())); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 658 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 659 | QuicFrame frame(&stream_frame); |
| 660 | QuicFrames frames; |
| 661 | frames.push_back(frame); |
rtenneti | b6ac61a5 | 2015-02-11 20:20:52 | [diff] [blame] | 662 | return BuildUnsizedDataPacket(&quic_framer, header, frames); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 663 | } |
| 664 | |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 665 | QuicPacket* ConstructHandshakePacket(QuicConnectionId connection_id, |
| 666 | QuicTag tag) { |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 667 | CryptoHandshakeMessage message; |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 668 | message.set_tag(tag); |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 669 | return ConstructPacketFromHandshakeMessage(connection_id, message, false); |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 670 | } |
| 671 | |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 672 | size_t GetPacketLengthForOneStream( |
| 673 | QuicVersion version, |
| 674 | bool include_version, |
rtenneti | 2318668 | 2014-10-30 01:49:33 | [diff] [blame] | 675 | QuicConnectionIdLength connection_id_length, |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 676 | QuicSequenceNumberLength sequence_number_length, |
| 677 | InFecGroup is_in_fec_group, |
| 678 | size_t* payload_length) { |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 679 | *payload_length = 1; |
| 680 | const size_t stream_length = |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 681 | NullEncrypter().GetCiphertextSize(*payload_length) + |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 682 | QuicPacketCreator::StreamFramePacketOverhead( |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 683 | PACKET_8BYTE_CONNECTION_ID, include_version, |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 684 | sequence_number_length, 0u, is_in_fec_group); |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 685 | const size_t ack_length = NullEncrypter().GetCiphertextSize( |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 686 | QuicFramer::GetMinAckFrameSize( |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 687 | sequence_number_length, PACKET_1BYTE_SEQUENCE_NUMBER)) + |
rtenneti | 2318668 | 2014-10-30 01:49:33 | [diff] [blame] | 688 | GetPacketHeaderSize(connection_id_length, include_version, |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 689 | sequence_number_length, is_in_fec_group); |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 690 | if (stream_length < ack_length) { |
| 691 | *payload_length = 1 + ack_length - stream_length; |
| 692 | } |
| 693 | |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 694 | return NullEncrypter().GetCiphertextSize(*payload_length) + |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 695 | QuicPacketCreator::StreamFramePacketOverhead( |
rtenneti | 2318668 | 2014-10-30 01:49:33 | [diff] [blame] | 696 | connection_id_length, include_version, |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 697 | sequence_number_length, 0u, is_in_fec_group); |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 698 | } |
| 699 | |
[email protected] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 700 | TestEntropyCalculator::TestEntropyCalculator() {} |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 701 | |
[email protected] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 702 | TestEntropyCalculator::~TestEntropyCalculator() {} |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 703 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 704 | QuicPacketEntropyHash TestEntropyCalculator::EntropyHash( |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 705 | QuicPacketSequenceNumber sequence_number) const { |
| 706 | return 1u; |
| 707 | } |
| 708 | |
[email protected] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 709 | MockEntropyCalculator::MockEntropyCalculator() {} |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 710 | |
[email protected] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 711 | MockEntropyCalculator::~MockEntropyCalculator() {} |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 712 | |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 713 | QuicConfig DefaultQuicConfig() { |
| 714 | QuicConfig config; |
[email protected] | 7d56135 | 2014-06-20 09:09:21 | [diff] [blame] | 715 | config.SetInitialStreamFlowControlWindowToSend( |
| 716 | kInitialStreamFlowControlWindowForTest); |
| 717 | config.SetInitialSessionFlowControlWindowToSend( |
| 718 | kInitialSessionFlowControlWindowForTest); |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 719 | return config; |
| 720 | } |
| 721 | |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame^] | 722 | QuicConfig DefaultQuicConfigStatelessRejects() { |
| 723 | QuicConfig config = DefaultQuicConfig(); |
| 724 | QuicTagVector copt; |
| 725 | copt.push_back(kSREJ); |
| 726 | config.SetConnectionOptionsToSend(copt); |
| 727 | return config; |
| 728 | } |
| 729 | |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 730 | QuicVersionVector SupportedVersions(QuicVersion version) { |
| 731 | QuicVersionVector versions; |
| 732 | versions.push_back(version); |
| 733 | return versions; |
| 734 | } |
| 735 | |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 736 | TestWriterFactory::TestWriterFactory() : current_writer_(nullptr) {} |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 737 | TestWriterFactory::~TestWriterFactory() {} |
| 738 | |
rch | c99f380c | 2015-03-26 19:50:56 | [diff] [blame] | 739 | QuicPacketWriter* TestWriterFactory::Create(QuicPacketWriter* writer, |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 740 | QuicConnection* connection) { |
| 741 | return new PerConnectionPacketWriter(this, writer, connection); |
| 742 | } |
| 743 | |
| 744 | void TestWriterFactory::OnPacketSent(WriteResult result) { |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 745 | if (current_writer_ != nullptr && result.status == WRITE_STATUS_ERROR) { |
[email protected] | ca4e0d9 | 2014-08-22 16:33:22 | [diff] [blame] | 746 | current_writer_->connection()->OnWriteError(result.error_code); |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 747 | current_writer_ = nullptr; |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
| 751 | void TestWriterFactory::Unregister(PerConnectionPacketWriter* writer) { |
| 752 | if (current_writer_ == writer) { |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 753 | current_writer_ = nullptr; |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
| 757 | TestWriterFactory::PerConnectionPacketWriter::PerConnectionPacketWriter( |
| 758 | TestWriterFactory* factory, |
rch | c99f380c | 2015-03-26 19:50:56 | [diff] [blame] | 759 | QuicPacketWriter* writer, |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 760 | QuicConnection* connection) |
| 761 | : QuicPerConnectionPacketWriter(writer, connection), |
| 762 | factory_(factory) { |
| 763 | } |
| 764 | |
| 765 | TestWriterFactory::PerConnectionPacketWriter::~PerConnectionPacketWriter() { |
| 766 | factory_->Unregister(this); |
| 767 | } |
| 768 | |
| 769 | WriteResult TestWriterFactory::PerConnectionPacketWriter::WritePacket( |
| 770 | const char* buffer, |
| 771 | size_t buf_len, |
| 772 | const IPAddressNumber& self_address, |
| 773 | const IPEndPoint& peer_address) { |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 774 | // A DCHECK(factory_current_writer_ == nullptr) would be wrong here -- this |
| 775 | // class may be used in a setting where connection()->OnPacketSent() is called |
| 776 | // in a different way, so TestWriterFactory::OnPacketSent might never be |
| 777 | // called. |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 778 | factory_->current_writer_ = this; |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame^] | 779 | return tools::QuicPerConnectionPacketWriter::WritePacket( |
| 780 | buffer, buf_len, self_address, peer_address); |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 781 | } |
| 782 | |
rtenneti | 9529380 | 2015-03-27 18:59:23 | [diff] [blame] | 783 | MockQuicConnectionDebugVisitor::MockQuicConnectionDebugVisitor() { |
| 784 | } |
| 785 | |
| 786 | MockQuicConnectionDebugVisitor::~MockQuicConnectionDebugVisitor() { |
| 787 | } |
| 788 | |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame^] | 789 | void SetupCryptoClientStreamForTest( |
| 790 | QuicServerId server_id, |
| 791 | bool supports_stateless_rejects, |
| 792 | QuicTime::Delta connection_start_time, |
| 793 | QuicCryptoClientConfig* crypto_client_config, |
| 794 | PacketSavingConnection** client_connection, |
| 795 | TestClientSession** client_session, |
| 796 | QuicCryptoClientStream** client_stream) { |
| 797 | CHECK(crypto_client_config); |
| 798 | CHECK(client_connection); |
| 799 | CHECK(client_session); |
| 800 | CHECK(client_stream); |
| 801 | CHECK(!connection_start_time.IsZero()) |
| 802 | << "Connections must start at non-zero times, otherwise the " |
| 803 | << "strike-register will be unhappy."; |
| 804 | |
| 805 | QuicConfig config = supports_stateless_rejects |
| 806 | ? DefaultQuicConfigStatelessRejects() |
| 807 | : DefaultQuicConfig(); |
| 808 | *client_connection = new PacketSavingConnection(Perspective::IS_CLIENT); |
| 809 | *client_session = new TestClientSession(*client_connection, config); |
| 810 | *client_stream = new QuicCryptoClientStream(server_id, *client_session, |
| 811 | nullptr, crypto_client_config); |
| 812 | (*client_session)->SetCryptoStream(*client_stream); |
| 813 | (*client_connection)->AdvanceTime(connection_start_time); |
| 814 | } |
| 815 | |
| 816 | // Setup or create? |
| 817 | void SetupCryptoServerStreamForTest( |
| 818 | QuicServerId server_id, |
| 819 | QuicTime::Delta connection_start_time, |
| 820 | QuicCryptoServerConfig* server_crypto_config, |
| 821 | PacketSavingConnection** server_connection, |
| 822 | TestServerSession** server_session, |
| 823 | QuicCryptoServerStream** server_stream) { |
| 824 | CHECK(server_crypto_config); |
| 825 | CHECK(server_connection); |
| 826 | CHECK(server_session); |
| 827 | CHECK(server_stream); |
| 828 | CHECK(!connection_start_time.IsZero()) |
| 829 | << "Connections must start at non-zero times, otherwise the " |
| 830 | << "strike-register will be unhappy."; |
| 831 | |
| 832 | *server_connection = new PacketSavingConnection(Perspective::IS_SERVER); |
| 833 | *server_session = |
| 834 | new TestServerSession(DefaultQuicConfig(), *server_connection); |
| 835 | *server_stream = |
| 836 | new QuicCryptoServerStream(server_crypto_config, *server_session); |
| 837 | (*server_session)->InitializeSession(server_crypto_config); |
| 838 | |
| 839 | // We advance the clock initially because the default time is zero and the |
| 840 | // strike register worries that we've just overflowed a uint32 time. |
| 841 | (*server_connection)->AdvanceTime(connection_start_time); |
| 842 | } |
| 843 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 844 | } // namespace test |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 845 | } // namespace net |