[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/quic/test_tools/quic_test_utils.h" |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 6 | |
[email protected] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 7 | #include "base/sha1.h" |
[email protected] | 701bc89 | 2013-01-17 04:51:54 | [diff] [blame] | 8 | #include "base/stl_util.h" |
[email protected] | b12764d | 2013-12-02 22:28:30 | [diff] [blame] | 9 | #include "base/strings/string_number_conversions.h" |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 10 | #include "net/quic/crypto/crypto_framer.h" |
[email protected] | 6f54ab3 | 2013-03-02 17:43:35 | [diff] [blame] | 11 | #include "net/quic/crypto/crypto_handshake.h" |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 12 | #include "net/quic/crypto/crypto_utils.h" |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 13 | #include "net/quic/crypto/null_encrypter.h" |
[email protected] | 4df6984 | 2013-02-27 06:32:16 | [diff] [blame] | 14 | #include "net/quic/crypto/quic_decrypter.h" |
| 15 | #include "net/quic/crypto/quic_encrypter.h" |
rtenneti | 16a2077 | 2015-02-17 18:58:48 | [diff] [blame] | 16 | #include "net/quic/quic_data_writer.h" |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 17 | #include "net/quic/quic_framer.h" |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 18 | #include "net/quic/quic_packet_creator.h" |
[email protected] | 79d13dcb | 2014-02-05 07:23:13 | [diff] [blame] | 19 | #include "net/quic/quic_utils.h" |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 20 | #include "net/quic/test_tools/quic_connection_peer.h" |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 21 | #include "net/spdy/spdy_frame_builder.h" |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 22 | |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 23 | using base::StringPiece; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 24 | using std::max; |
| 25 | using std::min; |
| 26 | using std::string; |
[email protected] | 64c1223 | 2014-03-26 05:43:59 | [diff] [blame] | 27 | using testing::AnyNumber; |
[email protected] | bc356fe | 2014-06-19 11:14:14 | [diff] [blame] | 28 | using testing::_; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 29 | |
| 30 | namespace net { |
| 31 | namespace test { |
[email protected] | 965dbe6 | 2013-08-09 21:34:31 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | // No-op alarm implementation used by MockHelper. |
| 35 | class TestAlarm : public QuicAlarm { |
| 36 | public: |
| 37 | explicit TestAlarm(QuicAlarm::Delegate* delegate) |
| 38 | : QuicAlarm(delegate) { |
| 39 | } |
| 40 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 41 | void SetImpl() override {} |
| 42 | void CancelImpl() override {} |
[email protected] | 965dbe6 | 2013-08-09 21:34:31 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | } // namespace |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 46 | |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 47 | QuicAckFrame MakeAckFrame(QuicPacketSequenceNumber largest_observed) { |
[email protected] | fb35b0a | 2014-04-15 21:06:49 | [diff] [blame] | 48 | QuicAckFrame ack; |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 49 | ack.largest_observed = largest_observed; |
| 50 | ack.entropy_hash = 0; |
[email protected] | fb35b0a | 2014-04-15 21:06:49 | [diff] [blame] | 51 | return ack; |
| 52 | } |
| 53 | |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 54 | QuicAckFrame MakeAckFrameWithNackRanges( |
| 55 | size_t num_nack_ranges, QuicPacketSequenceNumber least_unacked) { |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 56 | QuicAckFrame ack = MakeAckFrame(2 * num_nack_ranges + least_unacked); |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 57 | // Add enough missing packets to get num_nack_ranges nack ranges. |
| 58 | for (QuicPacketSequenceNumber i = 1; i < 2 * num_nack_ranges; i += 2) { |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 59 | ack.missing_packets.insert(least_unacked + i); |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 60 | } |
| 61 | return ack; |
| 62 | } |
| 63 | |
rtenneti | b6ac61a5 | 2015-02-11 20:20:52 | [diff] [blame] | 64 | QuicPacket* BuildUnsizedDataPacket(QuicFramer* framer, |
| 65 | const QuicPacketHeader& header, |
| 66 | const QuicFrames& frames) { |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 67 | const size_t max_plaintext_size = framer->GetMaxPlaintextSize(kMaxPacketSize); |
| 68 | size_t packet_size = GetPacketHeaderSize(header); |
| 69 | for (size_t i = 0; i < frames.size(); ++i) { |
| 70 | DCHECK_LE(packet_size, max_plaintext_size); |
| 71 | bool first_frame = i == 0; |
| 72 | bool last_frame = i == frames.size() - 1; |
| 73 | const size_t frame_size = framer->GetSerializedFrameLength( |
| 74 | frames[i], max_plaintext_size - packet_size, first_frame, last_frame, |
| 75 | header.is_in_fec_group, |
| 76 | header.public_header.sequence_number_length); |
| 77 | DCHECK(frame_size); |
| 78 | packet_size += frame_size; |
| 79 | } |
rtenneti | 16a2077 | 2015-02-17 18:58:48 | [diff] [blame] | 80 | return BuildUnsizedDataPacket(framer, header, frames, packet_size); |
| 81 | } |
| 82 | |
| 83 | QuicPacket* BuildUnsizedDataPacket(QuicFramer* framer, |
| 84 | const QuicPacketHeader& header, |
| 85 | const QuicFrames& frames, |
| 86 | size_t packet_size) { |
| 87 | char* buffer = new char[packet_size]; |
| 88 | scoped_ptr<QuicPacket> packet( |
| 89 | framer->BuildDataPacket(header, frames, buffer, packet_size)); |
| 90 | DCHECK(packet.get() != nullptr); |
| 91 | // Now I have to re-construct the data packet with data ownership. |
| 92 | return new QuicPacket(buffer, packet->length(), true, |
| 93 | header.public_header.connection_id_length, |
| 94 | header.public_header.version_flag, |
| 95 | header.public_header.sequence_number_length); |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 98 | uint64 SimpleRandom::RandUint64() { |
| 99 | unsigned char hash[base::kSHA1Length]; |
| 100 | base::SHA1HashBytes(reinterpret_cast<unsigned char*>(&seed_), sizeof(seed_), |
| 101 | hash); |
| 102 | memcpy(&seed_, hash, sizeof(seed_)); |
| 103 | return seed_; |
| 104 | } |
| 105 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 106 | MockFramerVisitor::MockFramerVisitor() { |
| 107 | // By default, we want to accept packets. |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 108 | ON_CALL(*this, OnProtocolVersionMismatch(_)) |
| 109 | .WillByDefault(testing::Return(false)); |
| 110 | |
| 111 | // By default, we want to accept packets. |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 112 | ON_CALL(*this, OnUnauthenticatedHeader(_)) |
| 113 | .WillByDefault(testing::Return(true)); |
| 114 | |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 115 | ON_CALL(*this, OnUnauthenticatedPublicHeader(_)) |
| 116 | .WillByDefault(testing::Return(true)); |
| 117 | |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 118 | ON_CALL(*this, OnPacketHeader(_)) |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 119 | .WillByDefault(testing::Return(true)); |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 120 | |
| 121 | ON_CALL(*this, OnStreamFrame(_)) |
| 122 | .WillByDefault(testing::Return(true)); |
| 123 | |
| 124 | ON_CALL(*this, OnAckFrame(_)) |
| 125 | .WillByDefault(testing::Return(true)); |
| 126 | |
[email protected] | 93dd91f | 2014-02-27 00:09:03 | [diff] [blame] | 127 | ON_CALL(*this, OnStopWaitingFrame(_)) |
| 128 | .WillByDefault(testing::Return(true)); |
| 129 | |
[email protected] | d8c52211 | 2014-04-23 09:23:25 | [diff] [blame] | 130 | ON_CALL(*this, OnPingFrame(_)) |
| 131 | .WillByDefault(testing::Return(true)); |
| 132 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 133 | ON_CALL(*this, OnRstStreamFrame(_)) |
| 134 | .WillByDefault(testing::Return(true)); |
| 135 | |
| 136 | ON_CALL(*this, OnConnectionCloseFrame(_)) |
| 137 | .WillByDefault(testing::Return(true)); |
| 138 | |
| 139 | ON_CALL(*this, OnGoAwayFrame(_)) |
| 140 | .WillByDefault(testing::Return(true)); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 143 | MockFramerVisitor::~MockFramerVisitor() { |
| 144 | } |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 145 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 146 | bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersion version) { |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 147 | return false; |
| 148 | } |
| 149 | |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 150 | bool NoOpFramerVisitor::OnUnauthenticatedPublicHeader( |
| 151 | const QuicPacketPublicHeader& header) { |
| 152 | return true; |
| 153 | } |
| 154 | |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 155 | bool NoOpFramerVisitor::OnUnauthenticatedHeader( |
| 156 | const QuicPacketHeader& header) { |
| 157 | return true; |
| 158 | } |
| 159 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 160 | bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) { |
| 161 | return true; |
| 162 | } |
| 163 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 164 | bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& frame) { |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | bool NoOpFramerVisitor::OnAckFrame(const QuicAckFrame& frame) { |
| 169 | return true; |
| 170 | } |
| 171 | |
[email protected] | 93dd91f | 2014-02-27 00:09:03 | [diff] [blame] | 172 | bool NoOpFramerVisitor::OnStopWaitingFrame( |
| 173 | const QuicStopWaitingFrame& frame) { |
| 174 | return true; |
| 175 | } |
| 176 | |
[email protected] | d8c52211 | 2014-04-23 09:23:25 | [diff] [blame] | 177 | bool NoOpFramerVisitor::OnPingFrame(const QuicPingFrame& frame) { |
| 178 | return true; |
| 179 | } |
| 180 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 181 | bool NoOpFramerVisitor::OnRstStreamFrame( |
| 182 | const QuicRstStreamFrame& frame) { |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | bool NoOpFramerVisitor::OnConnectionCloseFrame( |
| 187 | const QuicConnectionCloseFrame& frame) { |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& frame) { |
| 192 | return true; |
| 193 | } |
| 194 | |
[email protected] | cb23a92 | 2014-02-20 17:42:38 | [diff] [blame] | 195 | bool NoOpFramerVisitor::OnWindowUpdateFrame( |
| 196 | const QuicWindowUpdateFrame& frame) { |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& frame) { |
| 201 | return true; |
| 202 | } |
| 203 | |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 204 | MockConnectionVisitor::MockConnectionVisitor() { |
| 205 | } |
| 206 | |
| 207 | MockConnectionVisitor::~MockConnectionVisitor() { |
| 208 | } |
| 209 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 210 | MockHelper::MockHelper() { |
| 211 | } |
| 212 | |
| 213 | MockHelper::~MockHelper() { |
| 214 | } |
| 215 | |
[email protected] | 97693d1 | 2012-11-16 16:05:00 | [diff] [blame] | 216 | const QuicClock* MockHelper::GetClock() const { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 217 | return &clock_; |
| 218 | } |
| 219 | |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 220 | QuicRandom* MockHelper::GetRandomGenerator() { |
| 221 | return &random_generator_; |
| 222 | } |
| 223 | |
[email protected] | 965dbe6 | 2013-08-09 21:34:31 | [diff] [blame] | 224 | QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) { |
| 225 | return new TestAlarm(delegate); |
| 226 | } |
| 227 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 228 | void MockHelper::AdvanceTime(QuicTime::Delta delta) { |
| 229 | clock_.AdvanceTime(delta); |
| 230 | } |
| 231 | |
rtenneti | 3fe4ebbc | 2014-11-16 16:43:47 | [diff] [blame] | 232 | QuicPacketWriter* NiceMockPacketWriterFactory::Create( |
| 233 | QuicConnection* /*connection*/) const { |
| 234 | return new testing::NiceMock<MockPacketWriter>(); |
| 235 | } |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 236 | |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 237 | MockConnection::MockConnection(Perspective perspective) |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 238 | : QuicConnection(kTestConnectionId, |
[email protected] | 300ccd5 | 2014-01-25 08:00:19 | [diff] [blame] | 239 | IPEndPoint(TestPeerIPAddress(), kTestPort), |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 240 | new testing::NiceMock<MockHelper>(), |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 241 | NiceMockPacketWriterFactory(), |
| 242 | /* owns_writer= */ true, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 243 | perspective, |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 244 | /* is_secure= */ false, |
| 245 | QuicSupportedVersions()), |
| 246 | helper_(helper()) { |
| 247 | } |
| 248 | |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 249 | MockConnection::MockConnection(Perspective perspective, bool is_secure) |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 250 | : QuicConnection(kTestConnectionId, |
| 251 | IPEndPoint(TestPeerIPAddress(), kTestPort), |
| 252 | new testing::NiceMock<MockHelper>(), |
| 253 | NiceMockPacketWriterFactory(), |
| 254 | /* owns_writer= */ true, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 255 | perspective, |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 256 | is_secure, |
rtenneti | e3779d83 | 2014-11-14 02:08:17 | [diff] [blame] | 257 | QuicSupportedVersions()), |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 258 | helper_(helper()) { |
| 259 | } |
| 260 | |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 261 | MockConnection::MockConnection(IPEndPoint address, Perspective perspective) |
| 262 | : QuicConnection(kTestConnectionId, |
| 263 | address, |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 264 | new testing::NiceMock<MockHelper>(), |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 265 | NiceMockPacketWriterFactory(), |
| 266 | /* owns_writer= */ true, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 267 | perspective, |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 268 | /* is_secure= */ false, |
rtenneti | e3779d83 | 2014-11-14 02:08:17 | [diff] [blame] | 269 | QuicSupportedVersions()), |
[email protected] | 2cfc6bb8 | 2013-10-27 03:40:44 | [diff] [blame] | 270 | helper_(helper()) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 271 | } |
| 272 | |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 273 | MockConnection::MockConnection(QuicConnectionId connection_id, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 274 | Perspective perspective) |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 275 | : QuicConnection(connection_id, |
| 276 | IPEndPoint(TestPeerIPAddress(), kTestPort), |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 277 | new testing::NiceMock<MockHelper>(), |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 278 | NiceMockPacketWriterFactory(), |
| 279 | /* owns_writer= */ true, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 280 | perspective, |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 281 | /* is_secure= */ false, |
rtenneti | e3779d83 | 2014-11-14 02:08:17 | [diff] [blame] | 282 | QuicSupportedVersions()), |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 283 | helper_(helper()) { |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 284 | } |
| 285 | |
rtenneti | fb3fa6c | 2015-03-16 23:04:55 | [diff] [blame^] | 286 | MockConnection::MockConnection(QuicConnectionId connection_id, |
| 287 | Perspective perspective, |
| 288 | bool is_secure) |
| 289 | : QuicConnection(connection_id, |
| 290 | IPEndPoint(TestPeerIPAddress(), kTestPort), |
| 291 | new testing::NiceMock<MockHelper>(), |
| 292 | NiceMockPacketWriterFactory(), |
| 293 | /* owns_writer= */ true, |
| 294 | perspective, |
| 295 | is_secure, |
| 296 | QuicSupportedVersions()), |
| 297 | helper_(helper()) { |
| 298 | } |
| 299 | |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 300 | MockConnection::MockConnection(Perspective perspective, |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 301 | const QuicVersionVector& supported_versions) |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 302 | : QuicConnection(kTestConnectionId, |
[email protected] | 300ccd5 | 2014-01-25 08:00:19 | [diff] [blame] | 303 | IPEndPoint(TestPeerIPAddress(), kTestPort), |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 304 | new testing::NiceMock<MockHelper>(), |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 305 | NiceMockPacketWriterFactory(), |
| 306 | /* owns_writer= */ true, |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 307 | perspective, |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 308 | /* is_secure= */ false, |
rtenneti | e3779d83 | 2014-11-14 02:08:17 | [diff] [blame] | 309 | supported_versions), |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 310 | helper_(helper()) { |
| 311 | } |
| 312 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 313 | MockConnection::~MockConnection() { |
| 314 | } |
| 315 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 316 | void MockConnection::AdvanceTime(QuicTime::Delta delta) { |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 317 | static_cast<MockHelper*>(helper())->AdvanceTime(delta); |
| 318 | } |
| 319 | |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 320 | PacketSavingConnection::PacketSavingConnection(Perspective perspective) |
| 321 | : MockConnection(perspective) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 322 | } |
| 323 | |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 324 | PacketSavingConnection::PacketSavingConnection( |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 325 | Perspective perspective, |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 326 | const QuicVersionVector& supported_versions) |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 327 | : MockConnection(perspective, supported_versions) { |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 328 | } |
| 329 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 330 | PacketSavingConnection::~PacketSavingConnection() { |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 331 | STLDeleteElements(&encrypted_packets_); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 332 | } |
| 333 | |
rtenneti | 31e9fd6 | 2014-09-16 05:22:15 | [diff] [blame] | 334 | void PacketSavingConnection::SendOrQueuePacket(QueuedPacket packet) { |
rtenneti | b6ac61a5 | 2015-02-11 20:20:52 | [diff] [blame] | 335 | encrypted_packets_.push_back(packet.serialized_packet.packet); |
rtenneti | 31e9fd6 | 2014-09-16 05:22:15 | [diff] [blame] | 336 | // Transfer ownership of the packet to the SentPacketManager and the |
| 337 | // ack notifier to the AckNotifierManager. |
rtenneti | a4dcff9 | 2014-09-29 18:16:08 | [diff] [blame] | 338 | sent_packet_manager_.OnPacketSent( |
| 339 | &packet.serialized_packet, 0, QuicTime::Zero(), 1000, |
| 340 | NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 341 | } |
| 342 | |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 343 | MockSession::MockSession(QuicConnection* connection) |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 344 | : QuicSession(connection, DefaultQuicConfig()) { |
[email protected] | ccb3421 | 2014-07-18 09:27:50 | [diff] [blame] | 345 | InitializeSession(); |
[email protected] | bbb1007 | 2014-06-13 07:41:59 | [diff] [blame] | 346 | ON_CALL(*this, WritevData(_, _, _, _, _, _)) |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 347 | .WillByDefault(testing::Return(QuicConsumedData(0, false))); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | MockSession::~MockSession() { |
| 351 | } |
| 352 | |
[email protected] | ce7bb141 | 2014-05-17 15:51:33 | [diff] [blame] | 353 | TestSession::TestSession(QuicConnection* connection, const QuicConfig& config) |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 354 | : QuicSession(connection, config), |
rtenneti | 4a5df26 | 2014-11-07 00:43:58 | [diff] [blame] | 355 | crypto_stream_(nullptr) { |
[email protected] | ccb3421 | 2014-07-18 09:27:50 | [diff] [blame] | 356 | InitializeSession(); |
| 357 | } |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 358 | |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 359 | TestSession::~TestSession() {} |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 360 | |
| 361 | void TestSession::SetCryptoStream(QuicCryptoStream* stream) { |
| 362 | crypto_stream_ = stream; |
| 363 | } |
| 364 | |
| 365 | QuicCryptoStream* TestSession::GetCryptoStream() { |
| 366 | return crypto_stream_; |
| 367 | } |
| 368 | |
[email protected] | 90f62f09 | 2014-03-24 02:41:23 | [diff] [blame] | 369 | TestClientSession::TestClientSession(QuicConnection* connection, |
| 370 | const QuicConfig& config) |
rtenneti | 8696cf9 | 2014-11-14 21:12:12 | [diff] [blame] | 371 | : QuicClientSessionBase(connection, config), |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 372 | crypto_stream_(nullptr) { |
[email protected] | ccb3421 | 2014-07-18 09:27:50 | [diff] [blame] | 373 | EXPECT_CALL(*this, OnProofValid(_)).Times(AnyNumber()); |
| 374 | InitializeSession(); |
[email protected] | 90f62f09 | 2014-03-24 02:41:23 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | TestClientSession::~TestClientSession() {} |
| 378 | |
| 379 | void TestClientSession::SetCryptoStream(QuicCryptoStream* stream) { |
| 380 | crypto_stream_ = stream; |
| 381 | } |
| 382 | |
| 383 | QuicCryptoStream* TestClientSession::GetCryptoStream() { |
| 384 | return crypto_stream_; |
| 385 | } |
| 386 | |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 387 | MockPacketWriter::MockPacketWriter() { |
| 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 | |
[email protected] | 9bb57c7 | 2014-03-31 20:36:04 | [diff] [blame] | 477 | void GenerateBody(string* body, int length) { |
| 478 | body->clear(); |
| 479 | body->reserve(length); |
| 480 | for (int i = 0; i < length; ++i) { |
| 481 | body->append(1, static_cast<char>(32 + i % (126 - 32))); |
| 482 | } |
| 483 | } |
| 484 | |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 485 | QuicEncryptedPacket* ConstructEncryptedPacket( |
| 486 | QuicConnectionId connection_id, |
| 487 | bool version_flag, |
| 488 | bool reset_flag, |
| 489 | QuicPacketSequenceNumber sequence_number, |
| 490 | const string& data) { |
rtenneti | 9e0fb50 | 2015-03-08 06:07:16 | [diff] [blame] | 491 | return ConstructEncryptedPacket( |
| 492 | connection_id, version_flag, reset_flag, sequence_number, data, |
| 493 | PACKET_8BYTE_CONNECTION_ID, PACKET_6BYTE_SEQUENCE_NUMBER); |
| 494 | } |
| 495 | |
| 496 | QuicEncryptedPacket* ConstructEncryptedPacket( |
| 497 | QuicConnectionId connection_id, |
| 498 | bool version_flag, |
| 499 | bool reset_flag, |
| 500 | QuicPacketSequenceNumber sequence_number, |
| 501 | const string& data, |
| 502 | QuicConnectionIdLength connection_id_length, |
| 503 | QuicSequenceNumberLength sequence_number_length) { |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 504 | QuicPacketHeader header; |
| 505 | header.public_header.connection_id = connection_id; |
rtenneti | 9e0fb50 | 2015-03-08 06:07:16 | [diff] [blame] | 506 | header.public_header.connection_id_length = connection_id_length; |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 507 | header.public_header.version_flag = version_flag; |
| 508 | header.public_header.reset_flag = reset_flag; |
rtenneti | 9e0fb50 | 2015-03-08 06:07:16 | [diff] [blame] | 509 | header.public_header.sequence_number_length = sequence_number_length; |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 510 | header.packet_sequence_number = sequence_number; |
| 511 | header.entropy_flag = false; |
| 512 | header.entropy_hash = 0; |
| 513 | header.fec_flag = false; |
| 514 | header.is_in_fec_group = NOT_IN_FEC_GROUP; |
| 515 | header.fec_group = 0; |
| 516 | QuicStreamFrame stream_frame(1, false, 0, MakeIOVector(data)); |
| 517 | QuicFrame frame(&stream_frame); |
| 518 | QuicFrames frames; |
| 519 | frames.push_back(frame); |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 520 | QuicFramer framer(QuicSupportedVersions(), QuicTime::Zero(), |
| 521 | Perspective::IS_CLIENT); |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 522 | scoped_ptr<QuicPacket> packet( |
rtenneti | b6ac61a5 | 2015-02-11 20:20:52 | [diff] [blame] | 523 | BuildUnsizedDataPacket(&framer, header, frames)); |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 524 | EXPECT_TRUE(packet != nullptr); |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 525 | QuicEncryptedPacket* encrypted = framer.EncryptPacket(ENCRYPTION_NONE, |
| 526 | sequence_number, |
| 527 | *packet); |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 528 | EXPECT_TRUE(encrypted != nullptr); |
[email protected] | ffc34bf | 2014-03-07 02:42:02 | [diff] [blame] | 529 | return encrypted; |
| 530 | } |
| 531 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 532 | void CompareCharArraysWithHexError( |
| 533 | const string& description, |
| 534 | const char* actual, |
| 535 | const int actual_len, |
| 536 | const char* expected, |
| 537 | const int expected_len) { |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 538 | EXPECT_EQ(actual_len, expected_len); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 539 | const int min_len = min(actual_len, expected_len); |
| 540 | const int max_len = max(actual_len, expected_len); |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 541 | scoped_ptr<bool[]> marks(new bool[max_len]); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 542 | bool identical = (actual_len == expected_len); |
| 543 | for (int i = 0; i < min_len; ++i) { |
| 544 | if (actual[i] != expected[i]) { |
| 545 | marks[i] = true; |
| 546 | identical = false; |
| 547 | } else { |
| 548 | marks[i] = false; |
| 549 | } |
| 550 | } |
| 551 | for (int i = min_len; i < max_len; ++i) { |
| 552 | marks[i] = true; |
| 553 | } |
| 554 | if (identical) return; |
| 555 | ADD_FAILURE() |
| 556 | << "Description:\n" |
| 557 | << description |
| 558 | << "\n\nExpected:\n" |
| 559 | << HexDumpWithMarks(expected, expected_len, marks.get(), max_len) |
| 560 | << "\nActual:\n" |
| 561 | << HexDumpWithMarks(actual, actual_len, marks.get(), max_len); |
| 562 | } |
| 563 | |
[email protected] | b12764d | 2013-12-02 22:28:30 | [diff] [blame] | 564 | bool DecodeHexString(const base::StringPiece& hex, std::string* bytes) { |
| 565 | bytes->clear(); |
| 566 | if (hex.empty()) |
| 567 | return true; |
| 568 | std::vector<uint8> v; |
| 569 | if (!base::HexStringToBytes(hex.as_string(), &v)) |
| 570 | return false; |
| 571 | if (!v.empty()) |
| 572 | bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size()); |
| 573 | return true; |
| 574 | } |
| 575 | |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 576 | static QuicPacket* ConstructPacketFromHandshakeMessage( |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 577 | QuicConnectionId connection_id, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 578 | const CryptoHandshakeMessage& message, |
| 579 | bool should_include_version) { |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 580 | CryptoFramer crypto_framer; |
[email protected] | dc2cc74 | 2012-10-21 13:56:13 | [diff] [blame] | 581 | scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message)); |
rtenneti | 6f48aa9 | 2015-03-16 02:18:48 | [diff] [blame] | 582 | QuicFramer quic_framer(QuicSupportedVersions(), QuicTime::Zero(), |
| 583 | Perspective::IS_CLIENT); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 584 | |
| 585 | QuicPacketHeader header; |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 586 | header.public_header.connection_id = connection_id; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 587 | header.public_header.reset_flag = false; |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 588 | header.public_header.version_flag = should_include_version; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 589 | header.packet_sequence_number = 1; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 590 | header.entropy_flag = false; |
| 591 | header.entropy_hash = 0; |
| 592 | header.fec_flag = false; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 593 | header.fec_group = 0; |
| 594 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 595 | QuicStreamFrame stream_frame(kCryptoStreamId, false, 0, |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 596 | MakeIOVector(data->AsStringPiece())); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 597 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 598 | QuicFrame frame(&stream_frame); |
| 599 | QuicFrames frames; |
| 600 | frames.push_back(frame); |
rtenneti | b6ac61a5 | 2015-02-11 20:20:52 | [diff] [blame] | 601 | return BuildUnsizedDataPacket(&quic_framer, header, frames); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 602 | } |
| 603 | |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 604 | QuicPacket* ConstructHandshakePacket(QuicConnectionId connection_id, |
| 605 | QuicTag tag) { |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 606 | CryptoHandshakeMessage message; |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 607 | message.set_tag(tag); |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 608 | return ConstructPacketFromHandshakeMessage(connection_id, message, false); |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 609 | } |
| 610 | |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 611 | size_t GetPacketLengthForOneStream( |
| 612 | QuicVersion version, |
| 613 | bool include_version, |
rtenneti | 2318668 | 2014-10-30 01:49:33 | [diff] [blame] | 614 | QuicConnectionIdLength connection_id_length, |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 615 | QuicSequenceNumberLength sequence_number_length, |
| 616 | InFecGroup is_in_fec_group, |
| 617 | size_t* payload_length) { |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 618 | *payload_length = 1; |
| 619 | const size_t stream_length = |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 620 | NullEncrypter().GetCiphertextSize(*payload_length) + |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 621 | QuicPacketCreator::StreamFramePacketOverhead( |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 622 | PACKET_8BYTE_CONNECTION_ID, include_version, |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 623 | sequence_number_length, 0u, is_in_fec_group); |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 624 | const size_t ack_length = NullEncrypter().GetCiphertextSize( |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 625 | QuicFramer::GetMinAckFrameSize( |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 626 | sequence_number_length, PACKET_1BYTE_SEQUENCE_NUMBER)) + |
rtenneti | 2318668 | 2014-10-30 01:49:33 | [diff] [blame] | 627 | GetPacketHeaderSize(connection_id_length, include_version, |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 628 | sequence_number_length, is_in_fec_group); |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 629 | if (stream_length < ack_length) { |
| 630 | *payload_length = 1 + ack_length - stream_length; |
| 631 | } |
| 632 | |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 633 | return NullEncrypter().GetCiphertextSize(*payload_length) + |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 634 | QuicPacketCreator::StreamFramePacketOverhead( |
rtenneti | 2318668 | 2014-10-30 01:49:33 | [diff] [blame] | 635 | connection_id_length, include_version, |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 636 | sequence_number_length, 0u, is_in_fec_group); |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 637 | } |
| 638 | |
[email protected] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 639 | TestEntropyCalculator::TestEntropyCalculator() {} |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 640 | |
[email protected] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 641 | TestEntropyCalculator::~TestEntropyCalculator() {} |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 642 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 643 | QuicPacketEntropyHash TestEntropyCalculator::EntropyHash( |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 644 | QuicPacketSequenceNumber sequence_number) const { |
| 645 | return 1u; |
| 646 | } |
| 647 | |
[email protected] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 648 | MockEntropyCalculator::MockEntropyCalculator() {} |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 649 | |
[email protected] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 650 | MockEntropyCalculator::~MockEntropyCalculator() {} |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 651 | |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 652 | QuicConfig DefaultQuicConfig() { |
| 653 | QuicConfig config; |
[email protected] | 7d56135 | 2014-06-20 09:09:21 | [diff] [blame] | 654 | config.SetInitialStreamFlowControlWindowToSend( |
| 655 | kInitialStreamFlowControlWindowForTest); |
| 656 | config.SetInitialSessionFlowControlWindowToSend( |
| 657 | kInitialSessionFlowControlWindowForTest); |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 658 | return config; |
| 659 | } |
| 660 | |
[email protected] | 4d64079 | 2013-12-18 22:21:08 | [diff] [blame] | 661 | QuicVersionVector SupportedVersions(QuicVersion version) { |
| 662 | QuicVersionVector versions; |
| 663 | versions.push_back(version); |
| 664 | return versions; |
| 665 | } |
| 666 | |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 667 | TestWriterFactory::TestWriterFactory() : current_writer_(nullptr) {} |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 668 | TestWriterFactory::~TestWriterFactory() {} |
| 669 | |
| 670 | QuicPacketWriter* TestWriterFactory::Create(QuicServerPacketWriter* writer, |
| 671 | QuicConnection* connection) { |
| 672 | return new PerConnectionPacketWriter(this, writer, connection); |
| 673 | } |
| 674 | |
| 675 | void TestWriterFactory::OnPacketSent(WriteResult result) { |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 676 | if (current_writer_ != nullptr && result.status == WRITE_STATUS_ERROR) { |
[email protected] | ca4e0d9 | 2014-08-22 16:33:22 | [diff] [blame] | 677 | current_writer_->connection()->OnWriteError(result.error_code); |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 678 | current_writer_ = nullptr; |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
| 682 | void TestWriterFactory::Unregister(PerConnectionPacketWriter* writer) { |
| 683 | if (current_writer_ == writer) { |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 684 | current_writer_ = nullptr; |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
| 688 | TestWriterFactory::PerConnectionPacketWriter::PerConnectionPacketWriter( |
| 689 | TestWriterFactory* factory, |
| 690 | QuicServerPacketWriter* writer, |
| 691 | QuicConnection* connection) |
| 692 | : QuicPerConnectionPacketWriter(writer, connection), |
| 693 | factory_(factory) { |
| 694 | } |
| 695 | |
| 696 | TestWriterFactory::PerConnectionPacketWriter::~PerConnectionPacketWriter() { |
| 697 | factory_->Unregister(this); |
| 698 | } |
| 699 | |
| 700 | WriteResult TestWriterFactory::PerConnectionPacketWriter::WritePacket( |
| 701 | const char* buffer, |
| 702 | size_t buf_len, |
| 703 | const IPAddressNumber& self_address, |
| 704 | const IPEndPoint& peer_address) { |
rtenneti | be63573 | 2014-10-02 22:51:42 | [diff] [blame] | 705 | // A DCHECK(factory_current_writer_ == nullptr) would be wrong here -- this |
| 706 | // class may be used in a setting where connection()->OnPacketSent() is called |
| 707 | // in a different way, so TestWriterFactory::OnPacketSent might never be |
| 708 | // called. |
[email protected] | 6d51582 | 2014-08-22 01:58:06 | [diff] [blame] | 709 | factory_->current_writer_ = this; |
| 710 | return QuicPerConnectionPacketWriter::WritePacket(buffer, |
| 711 | buf_len, |
| 712 | self_address, |
| 713 | peer_address); |
| 714 | } |
| 715 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 716 | } // namespace test |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 717 | } // namespace net |