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