[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" |
rtenneti | d39bd76 | 2015-06-12 01:05:52 | [diff] [blame] | 20 | #include "net/quic/test_tools/crypto_test_utils.h" |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 21 | #include "net/quic/test_tools/quic_connection_peer.h" |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 22 | #include "net/spdy/spdy_frame_builder.h" |
rch | c99f380c | 2015-03-26 19:50:56 | [diff] [blame] | 23 | #include "net/tools/quic/quic_per_connection_packet_writer.h" |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 24 | |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 25 | using base::StringPiece; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 26 | using std::max; |
| 27 | using std::min; |
| 28 | using std::string; |
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 | |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 49 | QuicAckFrame MakeAckFrame(QuicPacketNumber 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 | |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 56 | QuicAckFrame MakeAckFrameWithNackRanges(size_t num_nack_ranges, |
| 57 | QuicPacketNumber 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. |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 60 | for (QuicPacketNumber 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, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 77 | header.is_in_fec_group, header.public_header.packet_number_length); |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 78 | DCHECK(frame_size); |
| 79 | packet_size += frame_size; |
| 80 | } |
rtenneti | 16a2077 | 2015-02-17 18:58:48 | [diff] [blame] | 81 | return BuildUnsizedDataPacket(framer, header, frames, packet_size); |
| 82 | } |
| 83 | |
| 84 | QuicPacket* BuildUnsizedDataPacket(QuicFramer* framer, |
| 85 | const QuicPacketHeader& header, |
| 86 | const QuicFrames& frames, |
| 87 | size_t packet_size) { |
| 88 | char* buffer = new char[packet_size]; |
| 89 | scoped_ptr<QuicPacket> packet( |
| 90 | framer->BuildDataPacket(header, frames, buffer, packet_size)); |
| 91 | DCHECK(packet.get() != nullptr); |
| 92 | // Now I have to re-construct the data packet with data ownership. |
| 93 | return new QuicPacket(buffer, packet->length(), true, |
| 94 | header.public_header.connection_id_length, |
| 95 | header.public_header.version_flag, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 96 | header.public_header.packet_number_length); |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 99 | uint64 SimpleRandom::RandUint64() { |
| 100 | unsigned char hash[base::kSHA1Length]; |
| 101 | base::SHA1HashBytes(reinterpret_cast<unsigned char*>(&seed_), sizeof(seed_), |
| 102 | hash); |
| 103 | memcpy(&seed_, hash, sizeof(seed_)); |
| 104 | return seed_; |
| 105 | } |
| 106 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 107 | MockFramerVisitor::MockFramerVisitor() { |
| 108 | // By default, we want to accept packets. |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 109 | ON_CALL(*this, OnProtocolVersionMismatch(_)) |
| 110 | .WillByDefault(testing::Return(false)); |
| 111 | |
| 112 | // By default, we want to accept packets. |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 113 | ON_CALL(*this, OnUnauthenticatedHeader(_)) |
| 114 | .WillByDefault(testing::Return(true)); |
| 115 | |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 116 | ON_CALL(*this, OnUnauthenticatedPublicHeader(_)) |
| 117 | .WillByDefault(testing::Return(true)); |
| 118 | |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 119 | ON_CALL(*this, OnPacketHeader(_)) |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 120 | .WillByDefault(testing::Return(true)); |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 121 | |
| 122 | ON_CALL(*this, OnStreamFrame(_)) |
| 123 | .WillByDefault(testing::Return(true)); |
| 124 | |
| 125 | ON_CALL(*this, OnAckFrame(_)) |
| 126 | .WillByDefault(testing::Return(true)); |
| 127 | |
[email protected] | 93dd91f | 2014-02-27 00:09:03 | [diff] [blame] | 128 | ON_CALL(*this, OnStopWaitingFrame(_)) |
| 129 | .WillByDefault(testing::Return(true)); |
| 130 | |
[email protected] | d8c52211 | 2014-04-23 09:23:25 | [diff] [blame] | 131 | ON_CALL(*this, OnPingFrame(_)) |
| 132 | .WillByDefault(testing::Return(true)); |
| 133 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 134 | ON_CALL(*this, OnRstStreamFrame(_)) |
| 135 | .WillByDefault(testing::Return(true)); |
| 136 | |
| 137 | ON_CALL(*this, OnConnectionCloseFrame(_)) |
| 138 | .WillByDefault(testing::Return(true)); |
| 139 | |
| 140 | ON_CALL(*this, OnGoAwayFrame(_)) |
| 141 | .WillByDefault(testing::Return(true)); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 142 | } |
| 143 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 144 | MockFramerVisitor::~MockFramerVisitor() { |
| 145 | } |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 146 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 147 | bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersion version) { |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 148 | return false; |
| 149 | } |
| 150 | |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 151 | bool NoOpFramerVisitor::OnUnauthenticatedPublicHeader( |
| 152 | const QuicPacketPublicHeader& header) { |
| 153 | return true; |
| 154 | } |
| 155 | |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 156 | bool NoOpFramerVisitor::OnUnauthenticatedHeader( |
| 157 | const QuicPacketHeader& header) { |
| 158 | return true; |
| 159 | } |
| 160 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 161 | bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) { |
| 162 | return true; |
| 163 | } |
| 164 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 165 | bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& frame) { |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | bool NoOpFramerVisitor::OnAckFrame(const QuicAckFrame& frame) { |
| 170 | return true; |
| 171 | } |
| 172 | |
[email protected] | 93dd91f | 2014-02-27 00:09:03 | [diff] [blame] | 173 | bool NoOpFramerVisitor::OnStopWaitingFrame( |
| 174 | const QuicStopWaitingFrame& frame) { |
| 175 | return true; |
| 176 | } |
| 177 | |
[email protected] | d8c52211 | 2014-04-23 09:23:25 | [diff] [blame] | 178 | bool NoOpFramerVisitor::OnPingFrame(const QuicPingFrame& frame) { |
| 179 | return true; |
| 180 | } |
| 181 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 182 | bool NoOpFramerVisitor::OnRstStreamFrame( |
| 183 | const QuicRstStreamFrame& frame) { |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | bool NoOpFramerVisitor::OnConnectionCloseFrame( |
| 188 | const QuicConnectionCloseFrame& frame) { |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& frame) { |
| 193 | return true; |
| 194 | } |
| 195 | |
[email protected] | cb23a92 | 2014-02-20 17:42:38 | [diff] [blame] | 196 | bool NoOpFramerVisitor::OnWindowUpdateFrame( |
| 197 | const QuicWindowUpdateFrame& frame) { |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& frame) { |
| 202 | return true; |
| 203 | } |
| 204 | |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 205 | MockConnectionVisitor::MockConnectionVisitor() { |
| 206 | } |
| 207 | |
| 208 | MockConnectionVisitor::~MockConnectionVisitor() { |
| 209 | } |
| 210 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 211 | MockHelper::MockHelper() { |
| 212 | } |
| 213 | |
| 214 | MockHelper::~MockHelper() { |
| 215 | } |
| 216 | |
[email protected] | 97693d1 | 2012-11-16 16:05:00 | [diff] [blame] | 217 | const QuicClock* MockHelper::GetClock() const { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 218 | return &clock_; |
| 219 | } |
| 220 | |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 221 | QuicRandom* MockHelper::GetRandomGenerator() { |
| 222 | return &random_generator_; |
| 223 | } |
| 224 | |
[email protected] | 965dbe6 | 2013-08-09 21:34:31 | [diff] [blame] | 225 | QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) { |
| 226 | return new TestAlarm(delegate); |
| 227 | } |
| 228 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 229 | void MockHelper::AdvanceTime(QuicTime::Delta delta) { |
| 230 | clock_.AdvanceTime(delta); |
| 231 | } |
| 232 | |
rtenneti | 3fe4ebbc | 2014-11-16 16:43:47 | [diff] [blame] | 233 | QuicPacketWriter* NiceMockPacketWriterFactory::Create( |
| 234 | QuicConnection* /*connection*/) const { |
| 235 | return new testing::NiceMock<MockPacketWriter>(); |
| 236 | } |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 237 | |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 238 | MockConnection::MockConnection(Perspective perspective) |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 239 | : MockConnection(perspective, |
| 240 | /* is_secure= */ false) { |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 241 | } |
| 242 | |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 243 | MockConnection::MockConnection(Perspective perspective, bool is_secure) |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 244 | : MockConnection(kTestConnectionId, |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 245 | IPEndPoint(TestPeerIPAddress(), kTestPort), |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 246 | perspective, |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 247 | is_secure, |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 248 | QuicSupportedVersions()) { |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 249 | } |
| 250 | |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 251 | MockConnection::MockConnection(IPEndPoint address, Perspective perspective) |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 252 | : MockConnection(kTestConnectionId, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 253 | address, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 254 | perspective, |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 255 | /* is_secure= */ false, |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 256 | QuicSupportedVersions()) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 257 | } |
| 258 | |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 259 | MockConnection::MockConnection(QuicConnectionId connection_id, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 260 | Perspective perspective) |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 261 | : MockConnection(connection_id, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 262 | perspective, |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 263 | /* is_secure= */ false) { |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 264 | } |
| 265 | |
rtenneti | fb3fa6c | 2015-03-16 23:04:55 | [diff] [blame] | 266 | MockConnection::MockConnection(QuicConnectionId connection_id, |
| 267 | Perspective perspective, |
| 268 | bool is_secure) |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 269 | : MockConnection(connection_id, |
rtenneti | fb3fa6c | 2015-03-16 23:04:55 | [diff] [blame] | 270 | IPEndPoint(TestPeerIPAddress(), kTestPort), |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 271 | perspective, |
| 272 | is_secure, |
| 273 | QuicSupportedVersions()) { |
| 274 | } |
| 275 | |
| 276 | MockConnection::MockConnection(Perspective perspective, |
| 277 | const QuicVersionVector& supported_versions) |
| 278 | : MockConnection(kTestConnectionId, |
| 279 | IPEndPoint(TestPeerIPAddress(), kTestPort), |
| 280 | perspective, |
| 281 | /* is_secure= */ false, |
| 282 | supported_versions) { |
| 283 | } |
| 284 | |
| 285 | MockConnection::MockConnection(QuicConnectionId connection_id, |
| 286 | IPEndPoint address, |
| 287 | Perspective perspective, |
| 288 | bool is_secure, |
| 289 | const QuicVersionVector& supported_versions) |
| 290 | : QuicConnection(connection_id, |
| 291 | address, |
rtenneti | fb3fa6c | 2015-03-16 23:04:55 | [diff] [blame] | 292 | new testing::NiceMock<MockHelper>(), |
| 293 | NiceMockPacketWriterFactory(), |
| 294 | /* owns_writer= */ true, |
| 295 | perspective, |
| 296 | is_secure, |
rtenneti | e3779d83 | 2014-11-14 02:08:17 | [diff] [blame] | 297 | supported_versions), |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 298 | helper_(helper()) { |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 299 | ON_CALL(*this, OnError(_)) |
| 300 | .WillByDefault( |
| 301 | Invoke(this, &PacketSavingConnection::QuicConnection_OnError)); |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 302 | } |
| 303 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 304 | MockConnection::~MockConnection() { |
| 305 | } |
| 306 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 307 | void MockConnection::AdvanceTime(QuicTime::Delta delta) { |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 308 | static_cast<MockHelper*>(helper())->AdvanceTime(delta); |
| 309 | } |
| 310 | |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 311 | PacketSavingConnection::PacketSavingConnection(Perspective perspective) |
| 312 | : MockConnection(perspective) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 313 | } |
| 314 | |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 315 | PacketSavingConnection::PacketSavingConnection( |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 316 | Perspective perspective, |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 317 | const QuicVersionVector& supported_versions) |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 318 | : MockConnection(perspective, supported_versions) { |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 319 | } |
| 320 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 321 | PacketSavingConnection::~PacketSavingConnection() { |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 322 | STLDeleteElements(&encrypted_packets_); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 323 | } |
| 324 | |
rtenneti | 31e9fd6 | 2014-09-16 05:22:15 | [diff] [blame] | 325 | void PacketSavingConnection::SendOrQueuePacket(QueuedPacket packet) { |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 326 | if (!packet.serialized_packet.packet->owns_buffer()) { |
| 327 | scoped_ptr<QuicEncryptedPacket> encrypted_deleter( |
| 328 | packet.serialized_packet.packet); |
| 329 | packet.serialized_packet.packet = packet.serialized_packet.packet->Clone(); |
| 330 | } |
rtenneti | b6ac61a5 | 2015-02-11 20:20:52 | [diff] [blame] | 331 | encrypted_packets_.push_back(packet.serialized_packet.packet); |
rtenneti | 31e9fd6 | 2014-09-16 05:22:15 | [diff] [blame] | 332 | // Transfer ownership of the packet to the SentPacketManager and the |
| 333 | // ack notifier to the AckNotifierManager. |
rtenneti | a4dcff9 | 2014-09-29 18:16:08 | [diff] [blame] | 334 | sent_packet_manager_.OnPacketSent( |
| 335 | &packet.serialized_packet, 0, QuicTime::Zero(), 1000, |
| 336 | NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 337 | } |
| 338 | |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 339 | MockQuicSpdySession::MockQuicSpdySession(QuicConnection* connection) |
| 340 | : QuicSpdySession(connection, DefaultQuicConfig()) { |
rtenneti | d39bd76 | 2015-06-12 01:05:52 | [diff] [blame] | 341 | crypto_stream_.reset(new QuicCryptoStream(this)); |
| 342 | Initialize(); |
[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 | |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 347 | MockQuicSpdySession::~MockQuicSpdySession() { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 348 | } |
| 349 | |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 350 | TestQuicSpdyServerSession::TestQuicSpdyServerSession( |
rtenneti | d39bd76 | 2015-06-12 01:05:52 | [diff] [blame] | 351 | QuicConnection* connection, |
| 352 | const QuicConfig& config, |
| 353 | const QuicCryptoServerConfig* crypto_config) |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 354 | : QuicSpdySession(connection, config) { |
rtenneti | d39bd76 | 2015-06-12 01:05:52 | [diff] [blame] | 355 | crypto_stream_.reset(new QuicCryptoServerStream(crypto_config, this)); |
| 356 | Initialize(); |
[email protected] | ccb3421 | 2014-07-18 09:27:50 | [diff] [blame] | 357 | } |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 358 | |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 359 | TestQuicSpdyServerSession::~TestQuicSpdyServerSession() { |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 360 | } |
| 361 | |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 362 | QuicCryptoServerStream* TestQuicSpdyServerSession::GetCryptoStream() { |
rtenneti | d39bd76 | 2015-06-12 01:05:52 | [diff] [blame] | 363 | return crypto_stream_.get(); |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 364 | } |
| 365 | |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 366 | TestQuicSpdyClientSession::TestQuicSpdyClientSession( |
| 367 | QuicConnection* connection, |
| 368 | const QuicConfig& config, |
| 369 | const QuicServerId& server_id, |
| 370 | QuicCryptoClientConfig* crypto_config) |
rtenneti | d39bd76 | 2015-06-12 01:05:52 | [diff] [blame] | 371 | : QuicClientSessionBase(connection, config) { |
| 372 | crypto_stream_.reset(new QuicCryptoClientStream( |
| 373 | server_id, this, CryptoTestUtils::ProofVerifyContextForTesting(), |
| 374 | crypto_config)); |
| 375 | Initialize(); |
[email protected] | 90f62f09 | 2014-03-24 02:41:23 | [diff] [blame] | 376 | } |
| 377 | |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 378 | TestQuicSpdyClientSession::~TestQuicSpdyClientSession() { |
| 379 | } |
[email protected] | 90f62f09 | 2014-03-24 02:41:23 | [diff] [blame] | 380 | |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 381 | QuicCryptoClientStream* TestQuicSpdyClientSession::GetCryptoStream() { |
rtenneti | d39bd76 | 2015-06-12 01:05:52 | [diff] [blame] | 382 | return crypto_stream_.get(); |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame] | 383 | } |
| 384 | |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 385 | MockPacketWriter::MockPacketWriter() { |
| 386 | } |
| 387 | |
| 388 | MockPacketWriter::~MockPacketWriter() { |
| 389 | } |
| 390 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 391 | MockSendAlgorithm::MockSendAlgorithm() { |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 392 | } |
| 393 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 394 | MockSendAlgorithm::~MockSendAlgorithm() { |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 395 | } |
| 396 | |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 397 | MockLossAlgorithm::MockLossAlgorithm() { |
| 398 | } |
| 399 | |
| 400 | MockLossAlgorithm::~MockLossAlgorithm() { |
| 401 | } |
| 402 | |
[email protected] | 97cf302 | 2013-09-05 14:30:16 | [diff] [blame] | 403 | MockAckNotifierDelegate::MockAckNotifierDelegate() { |
| 404 | } |
| 405 | |
| 406 | MockAckNotifierDelegate::~MockAckNotifierDelegate() { |
| 407 | } |
| 408 | |
[email protected] | a692ad9d | 2014-07-18 21:35:24 | [diff] [blame] | 409 | MockNetworkChangeVisitor::MockNetworkChangeVisitor() { |
| 410 | } |
| 411 | |
| 412 | MockNetworkChangeVisitor::~MockNetworkChangeVisitor() { |
| 413 | } |
| 414 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 415 | namespace { |
| 416 | |
| 417 | string HexDumpWithMarks(const char* data, int length, |
| 418 | const bool* marks, int mark_length) { |
| 419 | static const char kHexChars[] = "0123456789abcdef"; |
| 420 | static const int kColumns = 4; |
| 421 | |
| 422 | const int kSizeLimit = 1024; |
| 423 | if (length > kSizeLimit || mark_length > kSizeLimit) { |
| 424 | LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes."; |
| 425 | length = min(length, kSizeLimit); |
| 426 | mark_length = min(mark_length, kSizeLimit); |
| 427 | } |
| 428 | |
| 429 | string hex; |
| 430 | for (const char* row = data; length > 0; |
| 431 | row += kColumns, length -= kColumns) { |
| 432 | for (const char *p = row; p < row + 4; ++p) { |
| 433 | if (p < row + length) { |
| 434 | const bool mark = |
| 435 | (marks && (p - data) < mark_length && marks[p - data]); |
| 436 | hex += mark ? '*' : ' '; |
| 437 | hex += kHexChars[(*p & 0xf0) >> 4]; |
| 438 | hex += kHexChars[*p & 0x0f]; |
| 439 | hex += mark ? '*' : ' '; |
| 440 | } else { |
| 441 | hex += " "; |
| 442 | } |
| 443 | } |
| 444 | hex = hex + " "; |
| 445 | |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 446 | for (const char* p = row; p < row + 4 && p < row + length; ++p) { |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 447 | hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.'; |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 448 | } |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 449 | |
| 450 | hex = hex + '\n'; |
| 451 | } |
| 452 | return hex; |
| 453 | } |
| 454 | |
| 455 | } // namespace |
| 456 | |
[email protected] | 300ccd5 | 2014-01-25 08:00:19 | [diff] [blame] | 457 | IPAddressNumber TestPeerIPAddress() { return Loopback4(); } |
| 458 | |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 459 | QuicVersion QuicVersionMax() { return QuicSupportedVersions().front(); } |
| 460 | |
| 461 | QuicVersion QuicVersionMin() { return QuicSupportedVersions().back(); } |
| 462 | |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 463 | IPAddressNumber Loopback4() { |
[email protected] | 300ccd5 | 2014-01-25 08:00:19 | [diff] [blame] | 464 | IPAddressNumber addr; |
| 465 | CHECK(ParseIPLiteralToNumber("127.0.0.1", &addr)); |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 466 | return addr; |
| 467 | } |
| 468 | |
[email protected] | 730b35d7 | 2014-06-05 03:23:22 | [diff] [blame] | 469 | IPAddressNumber Loopback6() { |
| 470 | IPAddressNumber addr; |
| 471 | CHECK(ParseIPLiteralToNumber("::1", &addr)); |
| 472 | return addr; |
| 473 | } |
| 474 | |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 475 | IPAddressNumber Any4() { |
| 476 | IPAddressNumber any4; |
| 477 | CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4)); |
| 478 | return any4; |
| 479 | } |
| 480 | |
[email protected] | 9bb57c7 | 2014-03-31 20:36:04 | [diff] [blame] | 481 | void GenerateBody(string* body, int length) { |
| 482 | body->clear(); |
| 483 | body->reserve(length); |
| 484 | for (int i = 0; i < length; ++i) { |
| 485 | body->append(1, static_cast<char>(32 + i % (126 - 32))); |
| 486 | } |
| 487 | } |
| 488 | |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 489 | QuicEncryptedPacket* ConstructEncryptedPacket(QuicConnectionId connection_id, |
| 490 | bool version_flag, |
| 491 | bool reset_flag, |
| 492 | QuicPacketNumber packet_number, |
| 493 | const string& data) { |
rtenneti | 9e0fb50 | 2015-03-08 06:07:16 | [diff] [blame] | 494 | return ConstructEncryptedPacket( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 495 | connection_id, version_flag, reset_flag, packet_number, data, |
| 496 | PACKET_8BYTE_CONNECTION_ID, PACKET_6BYTE_PACKET_NUMBER); |
rtenneti | 9e0fb50 | 2015-03-08 06:07:16 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | QuicEncryptedPacket* ConstructEncryptedPacket( |
| 500 | QuicConnectionId connection_id, |
| 501 | bool version_flag, |
| 502 | bool reset_flag, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 503 | QuicPacketNumber packet_number, |
rtenneti | 9e0fb50 | 2015-03-08 06:07:16 | [diff] [blame] | 504 | const string& data, |
| 505 | QuicConnectionIdLength connection_id_length, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 506 | QuicPacketNumberLength packet_number_length) { |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 507 | return ConstructEncryptedPacket(connection_id, version_flag, reset_flag, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 508 | packet_number, data, connection_id_length, |
| 509 | packet_number_length, nullptr); |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | QuicEncryptedPacket* ConstructEncryptedPacket( |
| 513 | QuicConnectionId connection_id, |
| 514 | bool version_flag, |
| 515 | bool reset_flag, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 516 | QuicPacketNumber packet_number, |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 517 | const string& data, |
| 518 | QuicConnectionIdLength connection_id_length, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 519 | QuicPacketNumberLength packet_number_length, |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 520 | QuicVersionVector* versions) { |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 521 | QuicPacketHeader header; |
| 522 | header.public_header.connection_id = connection_id; |
rtenneti | 9e0fb50 | 2015-03-08 06:07:16 | [diff] [blame] | 523 | header.public_header.connection_id_length = connection_id_length; |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 524 | header.public_header.version_flag = version_flag; |
| 525 | header.public_header.reset_flag = reset_flag; |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 526 | header.public_header.packet_number_length = packet_number_length; |
| 527 | header.packet_packet_number = packet_number; |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 528 | header.entropy_flag = false; |
| 529 | header.entropy_hash = 0; |
| 530 | header.fec_flag = false; |
| 531 | header.is_in_fec_group = NOT_IN_FEC_GROUP; |
| 532 | header.fec_group = 0; |
rtenneti | a4228ea | 2015-06-04 02:31:44 | [diff] [blame] | 533 | QuicStreamFrame stream_frame(1, false, 0, StringPiece(data)); |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 534 | QuicFrame frame(&stream_frame); |
| 535 | QuicFrames frames; |
| 536 | frames.push_back(frame); |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 537 | QuicFramer framer(versions != nullptr ? *versions : QuicSupportedVersions(), |
| 538 | QuicTime::Zero(), Perspective::IS_CLIENT); |
| 539 | |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 540 | scoped_ptr<QuicPacket> packet( |
rtenneti | b6ac61a5 | 2015-02-11 20:20:52 | [diff] [blame] | 541 | BuildUnsizedDataPacket(&framer, header, frames)); |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 542 | EXPECT_TRUE(packet != nullptr); |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 543 | char buffer[kMaxPacketSize]; |
rtenneti | d7d78b0 | 2015-06-17 16:54:34 | [diff] [blame] | 544 | scoped_ptr<QuicEncryptedPacket> encrypted(framer.EncryptPayload( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 545 | ENCRYPTION_NONE, packet_number, *packet, buffer, kMaxPacketSize)); |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 546 | EXPECT_TRUE(encrypted != nullptr); |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 547 | return encrypted->Clone(); |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 548 | } |
| 549 | |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 550 | QuicEncryptedPacket* ConstructMisFramedEncryptedPacket( |
| 551 | QuicConnectionId connection_id, |
| 552 | bool version_flag, |
| 553 | bool reset_flag, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 554 | QuicPacketNumber packet_number, |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 555 | const string& data, |
| 556 | QuicConnectionIdLength connection_id_length, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 557 | QuicPacketNumberLength packet_number_length, |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 558 | QuicVersionVector* versions) { |
| 559 | QuicPacketHeader header; |
| 560 | header.public_header.connection_id = connection_id; |
| 561 | header.public_header.connection_id_length = connection_id_length; |
| 562 | header.public_header.version_flag = version_flag; |
| 563 | header.public_header.reset_flag = reset_flag; |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 564 | header.public_header.packet_number_length = packet_number_length; |
| 565 | header.packet_packet_number = packet_number; |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 566 | header.entropy_flag = false; |
| 567 | header.entropy_hash = 0; |
| 568 | header.fec_flag = false; |
| 569 | header.is_in_fec_group = NOT_IN_FEC_GROUP; |
| 570 | header.fec_group = 0; |
rtenneti | a4228ea | 2015-06-04 02:31:44 | [diff] [blame] | 571 | QuicStreamFrame stream_frame(1, false, 0, StringPiece(data)); |
rtenneti | 85816fdf | 2015-05-25 03:01:10 | [diff] [blame] | 572 | QuicFrame frame(&stream_frame); |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 573 | QuicFrames frames; |
rtenneti | 85816fdf | 2015-05-25 03:01:10 | [diff] [blame] | 574 | frames.push_back(frame); |
| 575 | QuicFramer framer(versions != nullptr ? *versions : QuicSupportedVersions(), |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 576 | QuicTime::Zero(), Perspective::IS_CLIENT); |
rtenneti | 85816fdf | 2015-05-25 03:01:10 | [diff] [blame] | 577 | |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 578 | scoped_ptr<QuicPacket> packet( |
| 579 | BuildUnsizedDataPacket(&framer, header, frames)); |
| 580 | EXPECT_TRUE(packet != nullptr); |
rtenneti | 85816fdf | 2015-05-25 03:01:10 | [diff] [blame] | 581 | |
| 582 | // Now set the packet's private flags byte to 0xFF, which is an invalid value. |
| 583 | reinterpret_cast<unsigned char*>( |
| 584 | packet->mutable_data())[GetStartOfEncryptedData( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 585 | connection_id_length, version_flag, packet_number_length)] = 0xFF; |
rtenneti | 85816fdf | 2015-05-25 03:01:10 | [diff] [blame] | 586 | |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 587 | char buffer[kMaxPacketSize]; |
rtenneti | d7d78b0 | 2015-06-17 16:54:34 | [diff] [blame] | 588 | scoped_ptr<QuicEncryptedPacket> encrypted(framer.EncryptPayload( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 589 | ENCRYPTION_NONE, packet_number, *packet, buffer, kMaxPacketSize)); |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 590 | EXPECT_TRUE(encrypted != nullptr); |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 591 | return encrypted->Clone(); |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 592 | } |
| 593 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 594 | void CompareCharArraysWithHexError( |
| 595 | const string& description, |
| 596 | const char* actual, |
| 597 | const int actual_len, |
| 598 | const char* expected, |
| 599 | const int expected_len) { |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 600 | EXPECT_EQ(actual_len, expected_len); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 601 | const int min_len = min(actual_len, expected_len); |
| 602 | const int max_len = max(actual_len, expected_len); |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 603 | scoped_ptr<bool[]> marks(new bool[max_len]); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 604 | bool identical = (actual_len == expected_len); |
| 605 | for (int i = 0; i < min_len; ++i) { |
| 606 | if (actual[i] != expected[i]) { |
| 607 | marks[i] = true; |
| 608 | identical = false; |
| 609 | } else { |
| 610 | marks[i] = false; |
| 611 | } |
| 612 | } |
| 613 | for (int i = min_len; i < max_len; ++i) { |
| 614 | marks[i] = true; |
| 615 | } |
| 616 | if (identical) return; |
| 617 | ADD_FAILURE() |
| 618 | << "Description:\n" |
| 619 | << description |
| 620 | << "\n\nExpected:\n" |
| 621 | << HexDumpWithMarks(expected, expected_len, marks.get(), max_len) |
| 622 | << "\nActual:\n" |
| 623 | << HexDumpWithMarks(actual, actual_len, marks.get(), max_len); |
| 624 | } |
| 625 | |
[email protected] | b12764d | 2013-12-02 22:28:30 | [diff] [blame] | 626 | bool DecodeHexString(const base::StringPiece& hex, std::string* bytes) { |
| 627 | bytes->clear(); |
| 628 | if (hex.empty()) |
| 629 | return true; |
| 630 | std::vector<uint8> v; |
| 631 | if (!base::HexStringToBytes(hex.as_string(), &v)) |
| 632 | return false; |
| 633 | if (!v.empty()) |
| 634 | bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size()); |
| 635 | return true; |
| 636 | } |
| 637 | |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 638 | static QuicPacket* ConstructPacketFromHandshakeMessage( |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 639 | QuicConnectionId connection_id, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 640 | const CryptoHandshakeMessage& message, |
| 641 | bool should_include_version) { |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 642 | CryptoFramer crypto_framer; |
[email protected] | dc2cc74 | 2012-10-21 13:56:13 | [diff] [blame] | 643 | scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message)); |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 644 | QuicFramer quic_framer(QuicSupportedVersions(), QuicTime::Zero(), |
| 645 | Perspective::IS_CLIENT); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 646 | |
| 647 | QuicPacketHeader header; |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 648 | header.public_header.connection_id = connection_id; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 649 | header.public_header.reset_flag = false; |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 650 | header.public_header.version_flag = should_include_version; |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 651 | header.packet_packet_number = 1; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 652 | header.entropy_flag = false; |
| 653 | header.entropy_hash = 0; |
| 654 | header.fec_flag = false; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 655 | header.fec_group = 0; |
| 656 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 657 | QuicStreamFrame stream_frame(kCryptoStreamId, false, 0, |
rtenneti | a4228ea | 2015-06-04 02:31:44 | [diff] [blame] | 658 | data->AsStringPiece()); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 659 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 660 | QuicFrame frame(&stream_frame); |
| 661 | QuicFrames frames; |
| 662 | frames.push_back(frame); |
rtenneti | b6ac61a5 | 2015-02-11 20:20:52 | [diff] [blame] | 663 | return BuildUnsizedDataPacket(&quic_framer, header, frames); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 664 | } |
| 665 | |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 666 | QuicPacket* ConstructHandshakePacket(QuicConnectionId connection_id, |
| 667 | QuicTag tag) { |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 668 | CryptoHandshakeMessage message; |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 669 | message.set_tag(tag); |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 670 | return ConstructPacketFromHandshakeMessage(connection_id, message, false); |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 671 | } |
| 672 | |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 673 | size_t GetPacketLengthForOneStream(QuicVersion version, |
| 674 | bool include_version, |
| 675 | QuicConnectionIdLength connection_id_length, |
| 676 | QuicPacketNumberLength packet_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( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 683 | PACKET_8BYTE_CONNECTION_ID, include_version, packet_number_length, 0u, |
| 684 | is_in_fec_group); |
rtenneti | c14c8ab | 2015-06-18 05:47:40 | [diff] [blame] | 685 | const size_t ack_length = |
| 686 | NullEncrypter().GetCiphertextSize( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 687 | QuicFramer::GetMinAckFrameSize(PACKET_1BYTE_PACKET_NUMBER)) + |
rtenneti | 2318668 | 2014-10-30 01:49:33 | [diff] [blame] | 688 | GetPacketHeaderSize(connection_id_length, include_version, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 689 | packet_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) + |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 695 | QuicPacketCreator::StreamFramePacketOverhead( |
| 696 | connection_id_length, include_version, packet_number_length, 0u, |
| 697 | 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( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 705 | QuicPacketNumber packet_number) const { |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 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 | d39bd76 | 2015-06-12 01:05:52 | [diff] [blame] | 789 | void CreateClientSessionForTest(QuicServerId server_id, |
| 790 | bool supports_stateless_rejects, |
| 791 | QuicTime::Delta connection_start_time, |
| 792 | QuicCryptoClientConfig* crypto_client_config, |
| 793 | PacketSavingConnection** client_connection, |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 794 | TestQuicSpdyClientSession** client_session) { |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame] | 795 | CHECK(crypto_client_config); |
| 796 | CHECK(client_connection); |
| 797 | CHECK(client_session); |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame] | 798 | CHECK(!connection_start_time.IsZero()) |
| 799 | << "Connections must start at non-zero times, otherwise the " |
| 800 | << "strike-register will be unhappy."; |
| 801 | |
| 802 | QuicConfig config = supports_stateless_rejects |
| 803 | ? DefaultQuicConfigStatelessRejects() |
| 804 | : DefaultQuicConfig(); |
| 805 | *client_connection = new PacketSavingConnection(Perspective::IS_CLIENT); |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 806 | *client_session = new TestQuicSpdyClientSession( |
| 807 | *client_connection, config, server_id, crypto_client_config); |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame] | 808 | (*client_connection)->AdvanceTime(connection_start_time); |
| 809 | } |
| 810 | |
rtenneti | d39bd76 | 2015-06-12 01:05:52 | [diff] [blame] | 811 | void CreateServerSessionForTest(QuicServerId server_id, |
| 812 | QuicTime::Delta connection_start_time, |
| 813 | QuicCryptoServerConfig* server_crypto_config, |
| 814 | PacketSavingConnection** server_connection, |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 815 | TestQuicSpdyServerSession** server_session) { |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame] | 816 | CHECK(server_crypto_config); |
| 817 | CHECK(server_connection); |
| 818 | CHECK(server_session); |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame] | 819 | CHECK(!connection_start_time.IsZero()) |
| 820 | << "Connections must start at non-zero times, otherwise the " |
| 821 | << "strike-register will be unhappy."; |
| 822 | |
| 823 | *server_connection = new PacketSavingConnection(Perspective::IS_SERVER); |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 824 | *server_session = new TestQuicSpdyServerSession( |
rtenneti | d39bd76 | 2015-06-12 01:05:52 | [diff] [blame] | 825 | *server_connection, DefaultQuicConfig(), server_crypto_config); |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame] | 826 | |
| 827 | // We advance the clock initially because the default time is zero and the |
| 828 | // strike register worries that we've just overflowed a uint32 time. |
| 829 | (*server_connection)->AdvanceTime(connection_start_time); |
| 830 | } |
| 831 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 832 | } // namespace test |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 833 | } // namespace net |