[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) { |
rtenneti | 493d90ef | 2015-09-14 04:43:11 | [diff] [blame^] | 61 | ack.missing_packets.Add(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() { |
rtenneti | 493d90ef | 2015-09-14 04:43:11 | [diff] [blame^] | 386 | ON_CALL(*this, GetMaxPacketSize(_)) |
| 387 | .WillByDefault(testing::Return(kMaxPacketSize)); |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | MockPacketWriter::~MockPacketWriter() { |
| 391 | } |
| 392 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 393 | MockSendAlgorithm::MockSendAlgorithm() { |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 394 | } |
| 395 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 396 | MockSendAlgorithm::~MockSendAlgorithm() { |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 397 | } |
| 398 | |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 399 | MockLossAlgorithm::MockLossAlgorithm() { |
| 400 | } |
| 401 | |
| 402 | MockLossAlgorithm::~MockLossAlgorithm() { |
| 403 | } |
| 404 | |
[email protected] | 97cf302 | 2013-09-05 14:30:16 | [diff] [blame] | 405 | MockAckNotifierDelegate::MockAckNotifierDelegate() { |
| 406 | } |
| 407 | |
| 408 | MockAckNotifierDelegate::~MockAckNotifierDelegate() { |
| 409 | } |
| 410 | |
[email protected] | a692ad9d | 2014-07-18 21:35:24 | [diff] [blame] | 411 | MockNetworkChangeVisitor::MockNetworkChangeVisitor() { |
| 412 | } |
| 413 | |
| 414 | MockNetworkChangeVisitor::~MockNetworkChangeVisitor() { |
| 415 | } |
| 416 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 417 | namespace { |
| 418 | |
| 419 | string HexDumpWithMarks(const char* data, int length, |
| 420 | const bool* marks, int mark_length) { |
| 421 | static const char kHexChars[] = "0123456789abcdef"; |
| 422 | static const int kColumns = 4; |
| 423 | |
| 424 | const int kSizeLimit = 1024; |
| 425 | if (length > kSizeLimit || mark_length > kSizeLimit) { |
| 426 | LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes."; |
| 427 | length = min(length, kSizeLimit); |
| 428 | mark_length = min(mark_length, kSizeLimit); |
| 429 | } |
| 430 | |
| 431 | string hex; |
| 432 | for (const char* row = data; length > 0; |
| 433 | row += kColumns, length -= kColumns) { |
| 434 | for (const char *p = row; p < row + 4; ++p) { |
| 435 | if (p < row + length) { |
| 436 | const bool mark = |
| 437 | (marks && (p - data) < mark_length && marks[p - data]); |
| 438 | hex += mark ? '*' : ' '; |
| 439 | hex += kHexChars[(*p & 0xf0) >> 4]; |
| 440 | hex += kHexChars[*p & 0x0f]; |
| 441 | hex += mark ? '*' : ' '; |
| 442 | } else { |
| 443 | hex += " "; |
| 444 | } |
| 445 | } |
| 446 | hex = hex + " "; |
| 447 | |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 448 | for (const char* p = row; p < row + 4 && p < row + length; ++p) { |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 449 | hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.'; |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 450 | } |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 451 | |
| 452 | hex = hex + '\n'; |
| 453 | } |
| 454 | return hex; |
| 455 | } |
| 456 | |
| 457 | } // namespace |
| 458 | |
[email protected] | 300ccd5 | 2014-01-25 08:00:19 | [diff] [blame] | 459 | IPAddressNumber TestPeerIPAddress() { return Loopback4(); } |
| 460 | |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 461 | QuicVersion QuicVersionMax() { return QuicSupportedVersions().front(); } |
| 462 | |
| 463 | QuicVersion QuicVersionMin() { return QuicSupportedVersions().back(); } |
| 464 | |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 465 | IPAddressNumber Loopback4() { |
[email protected] | 300ccd5 | 2014-01-25 08:00:19 | [diff] [blame] | 466 | IPAddressNumber addr; |
| 467 | CHECK(ParseIPLiteralToNumber("127.0.0.1", &addr)); |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 468 | return addr; |
| 469 | } |
| 470 | |
[email protected] | 730b35d7 | 2014-06-05 03:23:22 | [diff] [blame] | 471 | IPAddressNumber Loopback6() { |
| 472 | IPAddressNumber addr; |
| 473 | CHECK(ParseIPLiteralToNumber("::1", &addr)); |
| 474 | return addr; |
| 475 | } |
| 476 | |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 477 | IPAddressNumber Any4() { |
| 478 | IPAddressNumber any4; |
| 479 | CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4)); |
| 480 | return any4; |
| 481 | } |
| 482 | |
[email protected] | 9bb57c7 | 2014-03-31 20:36:04 | [diff] [blame] | 483 | void GenerateBody(string* body, int length) { |
| 484 | body->clear(); |
| 485 | body->reserve(length); |
| 486 | for (int i = 0; i < length; ++i) { |
| 487 | body->append(1, static_cast<char>(32 + i % (126 - 32))); |
| 488 | } |
| 489 | } |
| 490 | |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 491 | QuicEncryptedPacket* ConstructEncryptedPacket(QuicConnectionId connection_id, |
| 492 | bool version_flag, |
| 493 | bool reset_flag, |
| 494 | QuicPacketNumber packet_number, |
| 495 | const string& data) { |
rtenneti | 9e0fb50 | 2015-03-08 06:07:16 | [diff] [blame] | 496 | return ConstructEncryptedPacket( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 497 | connection_id, version_flag, reset_flag, packet_number, data, |
| 498 | PACKET_8BYTE_CONNECTION_ID, PACKET_6BYTE_PACKET_NUMBER); |
rtenneti | 9e0fb50 | 2015-03-08 06:07:16 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | QuicEncryptedPacket* ConstructEncryptedPacket( |
| 502 | QuicConnectionId connection_id, |
| 503 | bool version_flag, |
| 504 | bool reset_flag, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 505 | QuicPacketNumber packet_number, |
rtenneti | 9e0fb50 | 2015-03-08 06:07:16 | [diff] [blame] | 506 | const string& data, |
| 507 | QuicConnectionIdLength connection_id_length, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 508 | QuicPacketNumberLength packet_number_length) { |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 509 | return ConstructEncryptedPacket(connection_id, version_flag, reset_flag, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 510 | packet_number, data, connection_id_length, |
| 511 | packet_number_length, nullptr); |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | QuicEncryptedPacket* ConstructEncryptedPacket( |
| 515 | QuicConnectionId connection_id, |
| 516 | bool version_flag, |
| 517 | bool reset_flag, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 518 | QuicPacketNumber packet_number, |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 519 | const string& data, |
| 520 | QuicConnectionIdLength connection_id_length, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 521 | QuicPacketNumberLength packet_number_length, |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 522 | QuicVersionVector* versions) { |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 523 | QuicPacketHeader header; |
| 524 | header.public_header.connection_id = connection_id; |
rtenneti | 9e0fb50 | 2015-03-08 06:07:16 | [diff] [blame] | 525 | header.public_header.connection_id_length = connection_id_length; |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 526 | header.public_header.version_flag = version_flag; |
| 527 | header.public_header.reset_flag = reset_flag; |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 528 | header.public_header.packet_number_length = packet_number_length; |
| 529 | header.packet_packet_number = packet_number; |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 530 | header.entropy_flag = false; |
| 531 | header.entropy_hash = 0; |
| 532 | header.fec_flag = false; |
| 533 | header.is_in_fec_group = NOT_IN_FEC_GROUP; |
| 534 | header.fec_group = 0; |
rtenneti | a4228ea | 2015-06-04 02:31:44 | [diff] [blame] | 535 | QuicStreamFrame stream_frame(1, false, 0, StringPiece(data)); |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 536 | QuicFrame frame(&stream_frame); |
| 537 | QuicFrames frames; |
| 538 | frames.push_back(frame); |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 539 | QuicFramer framer(versions != nullptr ? *versions : QuicSupportedVersions(), |
| 540 | QuicTime::Zero(), Perspective::IS_CLIENT); |
| 541 | |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 542 | scoped_ptr<QuicPacket> packet( |
rtenneti | b6ac61a5 | 2015-02-11 20:20:52 | [diff] [blame] | 543 | BuildUnsizedDataPacket(&framer, header, frames)); |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 544 | EXPECT_TRUE(packet != nullptr); |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 545 | char buffer[kMaxPacketSize]; |
rtenneti | d7d78b0 | 2015-06-17 16:54:34 | [diff] [blame] | 546 | scoped_ptr<QuicEncryptedPacket> encrypted(framer.EncryptPayload( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 547 | ENCRYPTION_NONE, packet_number, *packet, buffer, kMaxPacketSize)); |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 548 | EXPECT_TRUE(encrypted != nullptr); |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 549 | return encrypted->Clone(); |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 550 | } |
| 551 | |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 552 | QuicEncryptedPacket* ConstructMisFramedEncryptedPacket( |
| 553 | QuicConnectionId connection_id, |
| 554 | bool version_flag, |
| 555 | bool reset_flag, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 556 | QuicPacketNumber packet_number, |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 557 | const string& data, |
| 558 | QuicConnectionIdLength connection_id_length, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 559 | QuicPacketNumberLength packet_number_length, |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 560 | QuicVersionVector* versions) { |
| 561 | QuicPacketHeader header; |
| 562 | header.public_header.connection_id = connection_id; |
| 563 | header.public_header.connection_id_length = connection_id_length; |
| 564 | header.public_header.version_flag = version_flag; |
| 565 | header.public_header.reset_flag = reset_flag; |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 566 | header.public_header.packet_number_length = packet_number_length; |
| 567 | header.packet_packet_number = packet_number; |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 568 | header.entropy_flag = false; |
| 569 | header.entropy_hash = 0; |
| 570 | header.fec_flag = false; |
| 571 | header.is_in_fec_group = NOT_IN_FEC_GROUP; |
| 572 | header.fec_group = 0; |
rtenneti | a4228ea | 2015-06-04 02:31:44 | [diff] [blame] | 573 | QuicStreamFrame stream_frame(1, false, 0, StringPiece(data)); |
rtenneti | 85816fdf | 2015-05-25 03:01:10 | [diff] [blame] | 574 | QuicFrame frame(&stream_frame); |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 575 | QuicFrames frames; |
rtenneti | 85816fdf | 2015-05-25 03:01:10 | [diff] [blame] | 576 | frames.push_back(frame); |
| 577 | QuicFramer framer(versions != nullptr ? *versions : QuicSupportedVersions(), |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 578 | QuicTime::Zero(), Perspective::IS_CLIENT); |
rtenneti | 85816fdf | 2015-05-25 03:01:10 | [diff] [blame] | 579 | |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 580 | scoped_ptr<QuicPacket> packet( |
| 581 | BuildUnsizedDataPacket(&framer, header, frames)); |
| 582 | EXPECT_TRUE(packet != nullptr); |
rtenneti | 85816fdf | 2015-05-25 03:01:10 | [diff] [blame] | 583 | |
| 584 | // Now set the packet's private flags byte to 0xFF, which is an invalid value. |
| 585 | reinterpret_cast<unsigned char*>( |
| 586 | packet->mutable_data())[GetStartOfEncryptedData( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 587 | connection_id_length, version_flag, packet_number_length)] = 0xFF; |
rtenneti | 85816fdf | 2015-05-25 03:01:10 | [diff] [blame] | 588 | |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 589 | char buffer[kMaxPacketSize]; |
rtenneti | d7d78b0 | 2015-06-17 16:54:34 | [diff] [blame] | 590 | scoped_ptr<QuicEncryptedPacket> encrypted(framer.EncryptPayload( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 591 | ENCRYPTION_NONE, packet_number, *packet, buffer, kMaxPacketSize)); |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 592 | EXPECT_TRUE(encrypted != nullptr); |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 593 | return encrypted->Clone(); |
rch | c081544 | 2015-04-18 13:29:46 | [diff] [blame] | 594 | } |
| 595 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 596 | void CompareCharArraysWithHexError( |
| 597 | const string& description, |
| 598 | const char* actual, |
| 599 | const int actual_len, |
| 600 | const char* expected, |
| 601 | const int expected_len) { |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 602 | EXPECT_EQ(actual_len, expected_len); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 603 | const int min_len = min(actual_len, expected_len); |
| 604 | const int max_len = max(actual_len, expected_len); |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 605 | scoped_ptr<bool[]> marks(new bool[max_len]); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 606 | bool identical = (actual_len == expected_len); |
| 607 | for (int i = 0; i < min_len; ++i) { |
| 608 | if (actual[i] != expected[i]) { |
| 609 | marks[i] = true; |
| 610 | identical = false; |
| 611 | } else { |
| 612 | marks[i] = false; |
| 613 | } |
| 614 | } |
| 615 | for (int i = min_len; i < max_len; ++i) { |
| 616 | marks[i] = true; |
| 617 | } |
| 618 | if (identical) return; |
| 619 | ADD_FAILURE() |
| 620 | << "Description:\n" |
| 621 | << description |
| 622 | << "\n\nExpected:\n" |
| 623 | << HexDumpWithMarks(expected, expected_len, marks.get(), max_len) |
| 624 | << "\nActual:\n" |
| 625 | << HexDumpWithMarks(actual, actual_len, marks.get(), max_len); |
| 626 | } |
| 627 | |
[email protected] | b12764d | 2013-12-02 22:28:30 | [diff] [blame] | 628 | bool DecodeHexString(const base::StringPiece& hex, std::string* bytes) { |
| 629 | bytes->clear(); |
| 630 | if (hex.empty()) |
| 631 | return true; |
| 632 | std::vector<uint8> v; |
| 633 | if (!base::HexStringToBytes(hex.as_string(), &v)) |
| 634 | return false; |
| 635 | if (!v.empty()) |
| 636 | bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size()); |
| 637 | return true; |
| 638 | } |
| 639 | |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 640 | static QuicPacket* ConstructPacketFromHandshakeMessage( |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 641 | QuicConnectionId connection_id, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 642 | const CryptoHandshakeMessage& message, |
| 643 | bool should_include_version) { |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 644 | CryptoFramer crypto_framer; |
[email protected] | dc2cc74 | 2012-10-21 13:56:13 | [diff] [blame] | 645 | scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message)); |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 646 | QuicFramer quic_framer(QuicSupportedVersions(), QuicTime::Zero(), |
| 647 | Perspective::IS_CLIENT); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 648 | |
| 649 | QuicPacketHeader header; |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 650 | header.public_header.connection_id = connection_id; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 651 | header.public_header.reset_flag = false; |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 652 | header.public_header.version_flag = should_include_version; |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 653 | header.packet_packet_number = 1; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 654 | header.entropy_flag = false; |
| 655 | header.entropy_hash = 0; |
| 656 | header.fec_flag = false; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 657 | header.fec_group = 0; |
| 658 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 659 | QuicStreamFrame stream_frame(kCryptoStreamId, false, 0, |
rtenneti | a4228ea | 2015-06-04 02:31:44 | [diff] [blame] | 660 | data->AsStringPiece()); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 661 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 662 | QuicFrame frame(&stream_frame); |
| 663 | QuicFrames frames; |
| 664 | frames.push_back(frame); |
rtenneti | b6ac61a5 | 2015-02-11 20:20:52 | [diff] [blame] | 665 | return BuildUnsizedDataPacket(&quic_framer, header, frames); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 666 | } |
| 667 | |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 668 | QuicPacket* ConstructHandshakePacket(QuicConnectionId connection_id, |
| 669 | QuicTag tag) { |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 670 | CryptoHandshakeMessage message; |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 671 | message.set_tag(tag); |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 672 | return ConstructPacketFromHandshakeMessage(connection_id, message, false); |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 673 | } |
| 674 | |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 675 | size_t GetPacketLengthForOneStream(QuicVersion version, |
| 676 | bool include_version, |
| 677 | QuicConnectionIdLength connection_id_length, |
| 678 | QuicPacketNumberLength packet_number_length, |
| 679 | InFecGroup is_in_fec_group, |
| 680 | size_t* payload_length) { |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 681 | *payload_length = 1; |
| 682 | const size_t stream_length = |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 683 | NullEncrypter().GetCiphertextSize(*payload_length) + |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 684 | QuicPacketCreator::StreamFramePacketOverhead( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 685 | PACKET_8BYTE_CONNECTION_ID, include_version, packet_number_length, 0u, |
| 686 | is_in_fec_group); |
rtenneti | c14c8ab | 2015-06-18 05:47:40 | [diff] [blame] | 687 | const size_t ack_length = |
| 688 | NullEncrypter().GetCiphertextSize( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 689 | QuicFramer::GetMinAckFrameSize(PACKET_1BYTE_PACKET_NUMBER)) + |
rtenneti | 2318668 | 2014-10-30 01:49:33 | [diff] [blame] | 690 | GetPacketHeaderSize(connection_id_length, include_version, |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 691 | packet_number_length, is_in_fec_group); |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 692 | if (stream_length < ack_length) { |
| 693 | *payload_length = 1 + ack_length - stream_length; |
| 694 | } |
| 695 | |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 696 | return NullEncrypter().GetCiphertextSize(*payload_length) + |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 697 | QuicPacketCreator::StreamFramePacketOverhead( |
| 698 | connection_id_length, include_version, packet_number_length, 0u, |
| 699 | is_in_fec_group); |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 700 | } |
| 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] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 704 | TestEntropyCalculator::~TestEntropyCalculator() {} |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 705 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 706 | QuicPacketEntropyHash TestEntropyCalculator::EntropyHash( |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 707 | QuicPacketNumber packet_number) const { |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 708 | return 1u; |
| 709 | } |
| 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] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 713 | MockEntropyCalculator::~MockEntropyCalculator() {} |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 714 | |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 715 | QuicConfig DefaultQuicConfig() { |
| 716 | QuicConfig config; |
[email protected] | 7d56135 | 2014-06-20 09:09:21 | [diff] [blame] | 717 | config.SetInitialStreamFlowControlWindowToSend( |
| 718 | kInitialStreamFlowControlWindowForTest); |
| 719 | config.SetInitialSessionFlowControlWindowToSend( |
| 720 | kInitialSessionFlowControlWindowForTest); |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 721 | return config; |
| 722 | } |
| 723 | |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame] | 724 | QuicConfig DefaultQuicConfigStatelessRejects() { |
| 725 | QuicConfig config = DefaultQuicConfig(); |
| 726 | QuicTagVector copt; |
| 727 | copt.push_back(kSREJ); |
| 728 | config.SetConnectionOptionsToSend(copt); |
| 729 | return config; |
| 730 | } |
| 731 | |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 732 | QuicVersionVector SupportedVersions(QuicVersion version) { |
| 733 | QuicVersionVector versions; |
| 734 | versions.push_back(version); |
| 735 | return versions; |
| 736 | } |
| 737 | |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 738 | TestWriterFactory::TestWriterFactory() : current_writer_(nullptr) {} |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 739 | TestWriterFactory::~TestWriterFactory() {} |
| 740 | |
rch | c99f380c | 2015-03-26 19:50:56 | [diff] [blame] | 741 | QuicPacketWriter* TestWriterFactory::Create(QuicPacketWriter* writer, |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 742 | QuicConnection* connection) { |
| 743 | return new PerConnectionPacketWriter(this, writer, connection); |
| 744 | } |
| 745 | |
| 746 | void TestWriterFactory::OnPacketSent(WriteResult result) { |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 747 | if (current_writer_ != nullptr && result.status == WRITE_STATUS_ERROR) { |
[email protected] | ca4e0d9 | 2014-08-22 16:33:22 | [diff] [blame] | 748 | current_writer_->connection()->OnWriteError(result.error_code); |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 749 | current_writer_ = nullptr; |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | |
| 753 | void TestWriterFactory::Unregister(PerConnectionPacketWriter* writer) { |
| 754 | if (current_writer_ == writer) { |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 755 | current_writer_ = nullptr; |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
| 759 | TestWriterFactory::PerConnectionPacketWriter::PerConnectionPacketWriter( |
| 760 | TestWriterFactory* factory, |
rch | c99f380c | 2015-03-26 19:50:56 | [diff] [blame] | 761 | QuicPacketWriter* writer, |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 762 | QuicConnection* connection) |
| 763 | : QuicPerConnectionPacketWriter(writer, connection), |
| 764 | factory_(factory) { |
| 765 | } |
| 766 | |
| 767 | TestWriterFactory::PerConnectionPacketWriter::~PerConnectionPacketWriter() { |
| 768 | factory_->Unregister(this); |
| 769 | } |
| 770 | |
| 771 | WriteResult TestWriterFactory::PerConnectionPacketWriter::WritePacket( |
| 772 | const char* buffer, |
| 773 | size_t buf_len, |
| 774 | const IPAddressNumber& self_address, |
| 775 | const IPEndPoint& peer_address) { |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 776 | // A DCHECK(factory_current_writer_ == nullptr) would be wrong here -- this |
| 777 | // class may be used in a setting where connection()->OnPacketSent() is called |
| 778 | // in a different way, so TestWriterFactory::OnPacketSent might never be |
| 779 | // called. |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 780 | factory_->current_writer_ = this; |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame] | 781 | return tools::QuicPerConnectionPacketWriter::WritePacket( |
| 782 | buffer, buf_len, self_address, peer_address); |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 783 | } |
| 784 | |
rtenneti | 9529380 | 2015-03-27 18:59:23 | [diff] [blame] | 785 | MockQuicConnectionDebugVisitor::MockQuicConnectionDebugVisitor() { |
| 786 | } |
| 787 | |
| 788 | MockQuicConnectionDebugVisitor::~MockQuicConnectionDebugVisitor() { |
| 789 | } |
| 790 | |
rtenneti | d39bd76 | 2015-06-12 01:05:52 | [diff] [blame] | 791 | void CreateClientSessionForTest(QuicServerId server_id, |
| 792 | bool supports_stateless_rejects, |
| 793 | QuicTime::Delta connection_start_time, |
| 794 | QuicCryptoClientConfig* crypto_client_config, |
| 795 | PacketSavingConnection** client_connection, |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 796 | TestQuicSpdyClientSession** client_session) { |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame] | 797 | CHECK(crypto_client_config); |
| 798 | CHECK(client_connection); |
| 799 | CHECK(client_session); |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame] | 800 | CHECK(!connection_start_time.IsZero()) |
| 801 | << "Connections must start at non-zero times, otherwise the " |
| 802 | << "strike-register will be unhappy."; |
| 803 | |
| 804 | QuicConfig config = supports_stateless_rejects |
| 805 | ? DefaultQuicConfigStatelessRejects() |
| 806 | : DefaultQuicConfig(); |
| 807 | *client_connection = new PacketSavingConnection(Perspective::IS_CLIENT); |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 808 | *client_session = new TestQuicSpdyClientSession( |
| 809 | *client_connection, config, server_id, crypto_client_config); |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame] | 810 | (*client_connection)->AdvanceTime(connection_start_time); |
| 811 | } |
| 812 | |
rtenneti | d39bd76 | 2015-06-12 01:05:52 | [diff] [blame] | 813 | void CreateServerSessionForTest(QuicServerId server_id, |
| 814 | QuicTime::Delta connection_start_time, |
| 815 | QuicCryptoServerConfig* server_crypto_config, |
| 816 | PacketSavingConnection** server_connection, |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 817 | TestQuicSpdyServerSession** server_session) { |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame] | 818 | CHECK(server_crypto_config); |
| 819 | CHECK(server_connection); |
| 820 | CHECK(server_session); |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame] | 821 | CHECK(!connection_start_time.IsZero()) |
| 822 | << "Connections must start at non-zero times, otherwise the " |
| 823 | << "strike-register will be unhappy."; |
| 824 | |
| 825 | *server_connection = new PacketSavingConnection(Perspective::IS_SERVER); |
rtenneti | b865eb8 | 2015-06-17 20:21:46 | [diff] [blame] | 826 | *server_session = new TestQuicSpdyServerSession( |
rtenneti | d39bd76 | 2015-06-12 01:05:52 | [diff] [blame] | 827 | *server_connection, DefaultQuicConfig(), server_crypto_config); |
rtenneti | a2ea916 | 2015-05-15 19:26:44 | [diff] [blame] | 828 | |
| 829 | // We advance the clock initially because the default time is zero and the |
| 830 | // strike register worries that we've just overflowed a uint32 time. |
| 831 | (*server_connection)->AdvanceTime(connection_start_time); |
| 832 | } |
| 833 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 834 | } // namespace test |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 835 | } // namespace net |