[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] | 701bc89 | 2013-01-17 04:51:54 | [diff] [blame] | 7 | #include "base/stl_util.h" |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 8 | #include "net/quic/crypto/crypto_framer.h" |
[email protected] | 6f54ab3 | 2013-03-02 17:43:35 | [diff] [blame] | 9 | #include "net/quic/crypto/crypto_handshake.h" |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 10 | #include "net/quic/crypto/crypto_utils.h" |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 11 | #include "net/quic/crypto/null_encrypter.h" |
[email protected] | 4df6984 | 2013-02-27 06:32:16 | [diff] [blame] | 12 | #include "net/quic/crypto/quic_decrypter.h" |
| 13 | #include "net/quic/crypto/quic_encrypter.h" |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 14 | #include "net/quic/quic_framer.h" |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 15 | #include "net/quic/quic_packet_creator.h" |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 16 | #include "net/quic/test_tools/quic_connection_peer.h" |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 17 | #include "net/spdy/spdy_frame_builder.h" |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 18 | |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 19 | using base::StringPiece; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 20 | using std::max; |
| 21 | using std::min; |
| 22 | using std::string; |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 23 | using testing::_; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 24 | |
| 25 | namespace net { |
| 26 | namespace test { |
[email protected] | 965dbe6 | 2013-08-09 21:34:31 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | // No-op alarm implementation used by MockHelper. |
| 30 | class TestAlarm : public QuicAlarm { |
| 31 | public: |
| 32 | explicit TestAlarm(QuicAlarm::Delegate* delegate) |
| 33 | : QuicAlarm(delegate) { |
| 34 | } |
| 35 | |
| 36 | virtual void SetImpl() OVERRIDE {} |
| 37 | virtual void CancelImpl() OVERRIDE {} |
| 38 | }; |
| 39 | |
| 40 | } // namespace |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 41 | |
| 42 | MockFramerVisitor::MockFramerVisitor() { |
| 43 | // By default, we want to accept packets. |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 44 | ON_CALL(*this, OnProtocolVersionMismatch(_)) |
| 45 | .WillByDefault(testing::Return(false)); |
| 46 | |
| 47 | // By default, we want to accept packets. |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame^] | 48 | ON_CALL(*this, OnUnauthenticatedHeader(_)) |
| 49 | .WillByDefault(testing::Return(true)); |
| 50 | |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 51 | ON_CALL(*this, OnPacketHeader(_)) |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 52 | .WillByDefault(testing::Return(true)); |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 53 | |
| 54 | ON_CALL(*this, OnStreamFrame(_)) |
| 55 | .WillByDefault(testing::Return(true)); |
| 56 | |
| 57 | ON_CALL(*this, OnAckFrame(_)) |
| 58 | .WillByDefault(testing::Return(true)); |
| 59 | |
| 60 | ON_CALL(*this, OnCongestionFeedbackFrame(_)) |
| 61 | .WillByDefault(testing::Return(true)); |
| 62 | |
| 63 | ON_CALL(*this, OnRstStreamFrame(_)) |
| 64 | .WillByDefault(testing::Return(true)); |
| 65 | |
| 66 | ON_CALL(*this, OnConnectionCloseFrame(_)) |
| 67 | .WillByDefault(testing::Return(true)); |
| 68 | |
| 69 | ON_CALL(*this, OnGoAwayFrame(_)) |
| 70 | .WillByDefault(testing::Return(true)); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 73 | MockFramerVisitor::~MockFramerVisitor() { |
| 74 | } |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 75 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 76 | bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersion version) { |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 77 | return false; |
| 78 | } |
| 79 | |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame^] | 80 | bool NoOpFramerVisitor::OnUnauthenticatedHeader( |
| 81 | const QuicPacketHeader& header) { |
| 82 | return true; |
| 83 | } |
| 84 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 85 | bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) { |
| 86 | return true; |
| 87 | } |
| 88 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 89 | bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& frame) { |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | bool NoOpFramerVisitor::OnAckFrame(const QuicAckFrame& frame) { |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | bool NoOpFramerVisitor::OnCongestionFeedbackFrame( |
| 98 | const QuicCongestionFeedbackFrame& frame) { |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | bool NoOpFramerVisitor::OnRstStreamFrame( |
| 103 | const QuicRstStreamFrame& frame) { |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | bool NoOpFramerVisitor::OnConnectionCloseFrame( |
| 108 | const QuicConnectionCloseFrame& frame) { |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& frame) { |
| 113 | return true; |
| 114 | } |
| 115 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 116 | FramerVisitorCapturingFrames::FramerVisitorCapturingFrames() : frame_count_(0) { |
[email protected] | 134e5c3 | 2012-12-12 19:20:36 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 119 | FramerVisitorCapturingFrames::~FramerVisitorCapturingFrames() { |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 120 | STLDeleteElements(&stream_data_); |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 123 | bool FramerVisitorCapturingFrames::OnPacketHeader( |
[email protected] | 4e6f0ed | 2012-11-02 22:15:38 | [diff] [blame] | 124 | const QuicPacketHeader& header) { |
| 125 | header_ = header; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 126 | frame_count_ = 0; |
[email protected] | 4e6f0ed | 2012-11-02 22:15:38 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 130 | bool FramerVisitorCapturingFrames::OnStreamFrame(const QuicStreamFrame& frame) { |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 131 | // Make a copy of the frame and store a copy of underlying string, since |
| 132 | // frame.data may not exist outside this callback. |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 133 | stream_data_.push_back(frame.GetDataAsString()); |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 134 | QuicStreamFrame frame_copy = frame; |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 135 | frame_copy.data.Clear(); |
| 136 | frame_copy.data.Append(const_cast<char*>(stream_data_.back()->data()), |
| 137 | stream_data_.back()->size()); |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 138 | stream_frames_.push_back(frame_copy); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 139 | ++frame_count_; |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 140 | return true; |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 143 | bool FramerVisitorCapturingFrames::OnAckFrame(const QuicAckFrame& frame) { |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 144 | ack_.reset(new QuicAckFrame(frame)); |
| 145 | ++frame_count_; |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 146 | return true; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 147 | } |
| 148 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 149 | bool FramerVisitorCapturingFrames::OnCongestionFeedbackFrame( |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 150 | const QuicCongestionFeedbackFrame& frame) { |
| 151 | feedback_.reset(new QuicCongestionFeedbackFrame(frame)); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 152 | ++frame_count_; |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 153 | return true; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 154 | } |
| 155 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 156 | bool FramerVisitorCapturingFrames::OnRstStreamFrame( |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 157 | const QuicRstStreamFrame& frame) { |
| 158 | rst_.reset(new QuicRstStreamFrame(frame)); |
| 159 | ++frame_count_; |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 160 | return true; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 163 | bool FramerVisitorCapturingFrames::OnConnectionCloseFrame( |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 164 | const QuicConnectionCloseFrame& frame) { |
| 165 | close_.reset(new QuicConnectionCloseFrame(frame)); |
| 166 | ++frame_count_; |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 167 | return true; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 170 | bool FramerVisitorCapturingFrames::OnGoAwayFrame(const QuicGoAwayFrame& frame) { |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 171 | goaway_.reset(new QuicGoAwayFrame(frame)); |
| 172 | ++frame_count_; |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 173 | return true; |
[email protected] | 4e6f0ed | 2012-11-02 22:15:38 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 176 | void FramerVisitorCapturingFrames::OnVersionNegotiationPacket( |
| 177 | const QuicVersionNegotiationPacket& packet) { |
| 178 | version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(packet)); |
| 179 | frame_count_ = 0; |
| 180 | } |
| 181 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 182 | FramerVisitorCapturingPublicReset::FramerVisitorCapturingPublicReset() { |
| 183 | } |
| 184 | |
| 185 | FramerVisitorCapturingPublicReset::~FramerVisitorCapturingPublicReset() { |
| 186 | } |
| 187 | |
| 188 | void FramerVisitorCapturingPublicReset::OnPublicResetPacket( |
| 189 | const QuicPublicResetPacket& public_reset) { |
| 190 | public_reset_packet_ = public_reset; |
| 191 | } |
| 192 | |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 193 | MockConnectionVisitor::MockConnectionVisitor() { |
| 194 | } |
| 195 | |
| 196 | MockConnectionVisitor::~MockConnectionVisitor() { |
| 197 | } |
| 198 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 199 | MockHelper::MockHelper() { |
| 200 | } |
| 201 | |
| 202 | MockHelper::~MockHelper() { |
| 203 | } |
| 204 | |
[email protected] | 97693d1 | 2012-11-16 16:05:00 | [diff] [blame] | 205 | const QuicClock* MockHelper::GetClock() const { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 206 | return &clock_; |
| 207 | } |
| 208 | |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 209 | QuicRandom* MockHelper::GetRandomGenerator() { |
| 210 | return &random_generator_; |
| 211 | } |
| 212 | |
[email protected] | 965dbe6 | 2013-08-09 21:34:31 | [diff] [blame] | 213 | QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) { |
| 214 | return new TestAlarm(delegate); |
| 215 | } |
| 216 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 217 | void MockHelper::AdvanceTime(QuicTime::Delta delta) { |
| 218 | clock_.AdvanceTime(delta); |
| 219 | } |
| 220 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 221 | MockConnection::MockConnection(QuicGuid guid, |
| 222 | IPEndPoint address, |
| 223 | bool is_server) |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 224 | : QuicConnection(guid, address, new testing::NiceMock<MockHelper>(), |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 225 | new testing::NiceMock<MockPacketWriter>(), |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 226 | is_server, QuicSupportedVersions()), |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 227 | has_mock_helper_(true), |
[email protected] | 2cfc6bb8 | 2013-10-27 03:40:44 | [diff] [blame] | 228 | writer_(QuicConnectionPeer::GetWriter(this)), |
| 229 | helper_(helper()) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 230 | } |
| 231 | |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 232 | MockConnection::MockConnection(QuicGuid guid, |
| 233 | IPEndPoint address, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 234 | QuicConnectionHelperInterface* helper, |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 235 | QuicPacketWriter* writer, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 236 | bool is_server) |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 237 | : QuicConnection(guid, address, helper, writer, is_server, |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 238 | QuicSupportedVersions()), |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 239 | has_mock_helper_(false) { |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 240 | } |
| 241 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 242 | MockConnection::~MockConnection() { |
| 243 | } |
| 244 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 245 | void MockConnection::AdvanceTime(QuicTime::Delta delta) { |
| 246 | CHECK(has_mock_helper_) << "Cannot advance time unless a MockClock is being" |
| 247 | " used"; |
| 248 | static_cast<MockHelper*>(helper())->AdvanceTime(delta); |
| 249 | } |
| 250 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 251 | PacketSavingConnection::PacketSavingConnection(QuicGuid guid, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 252 | IPEndPoint address, |
| 253 | bool is_server) |
| 254 | : MockConnection(guid, address, is_server) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | PacketSavingConnection::~PacketSavingConnection() { |
[email protected] | 701bc89 | 2013-01-17 04:51:54 | [diff] [blame] | 258 | STLDeleteElements(&packets_); |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 259 | STLDeleteElements(&encrypted_packets_); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 260 | } |
| 261 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 262 | bool PacketSavingConnection::SendOrQueuePacket( |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 263 | EncryptionLevel level, |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame^] | 264 | const SerializedPacket& packet, |
| 265 | TransmissionType transmission_type) { |
| 266 | packets_.push_back(packet.packet); |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 267 | QuicEncryptedPacket* encrypted = |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame^] | 268 | framer_.EncryptPacket(level, packet.sequence_number, *packet.packet); |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 269 | encrypted_packets_.push_back(encrypted); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 270 | return true; |
| 271 | } |
| 272 | |
| 273 | MockSession::MockSession(QuicConnection* connection, bool is_server) |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 274 | : QuicSession(connection, DefaultQuicConfig(), is_server) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 275 | ON_CALL(*this, WritevData(_, _, _, _, _)) |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 276 | .WillByDefault(testing::Return(QuicConsumedData(0, false))); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | MockSession::~MockSession() { |
| 280 | } |
| 281 | |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 282 | TestSession::TestSession(QuicConnection* connection, |
| 283 | const QuicConfig& config, |
| 284 | bool is_server) |
| 285 | : QuicSession(connection, config, is_server), |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 286 | crypto_stream_(NULL) { |
| 287 | } |
| 288 | |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 289 | TestSession::~TestSession() {} |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 290 | |
| 291 | void TestSession::SetCryptoStream(QuicCryptoStream* stream) { |
| 292 | crypto_stream_ = stream; |
| 293 | } |
| 294 | |
| 295 | QuicCryptoStream* TestSession::GetCryptoStream() { |
| 296 | return crypto_stream_; |
| 297 | } |
| 298 | |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 299 | MockPacketWriter::MockPacketWriter() { |
| 300 | } |
| 301 | |
| 302 | MockPacketWriter::~MockPacketWriter() { |
| 303 | } |
| 304 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 305 | MockSendAlgorithm::MockSendAlgorithm() { |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 306 | } |
| 307 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 308 | MockSendAlgorithm::~MockSendAlgorithm() { |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 309 | } |
| 310 | |
[email protected] | 97cf302 | 2013-09-05 14:30:16 | [diff] [blame] | 311 | MockAckNotifierDelegate::MockAckNotifierDelegate() { |
| 312 | } |
| 313 | |
| 314 | MockAckNotifierDelegate::~MockAckNotifierDelegate() { |
| 315 | } |
| 316 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 317 | namespace { |
| 318 | |
| 319 | string HexDumpWithMarks(const char* data, int length, |
| 320 | const bool* marks, int mark_length) { |
| 321 | static const char kHexChars[] = "0123456789abcdef"; |
| 322 | static const int kColumns = 4; |
| 323 | |
| 324 | const int kSizeLimit = 1024; |
| 325 | if (length > kSizeLimit || mark_length > kSizeLimit) { |
| 326 | LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes."; |
| 327 | length = min(length, kSizeLimit); |
| 328 | mark_length = min(mark_length, kSizeLimit); |
| 329 | } |
| 330 | |
| 331 | string hex; |
| 332 | for (const char* row = data; length > 0; |
| 333 | row += kColumns, length -= kColumns) { |
| 334 | for (const char *p = row; p < row + 4; ++p) { |
| 335 | if (p < row + length) { |
| 336 | const bool mark = |
| 337 | (marks && (p - data) < mark_length && marks[p - data]); |
| 338 | hex += mark ? '*' : ' '; |
| 339 | hex += kHexChars[(*p & 0xf0) >> 4]; |
| 340 | hex += kHexChars[*p & 0x0f]; |
| 341 | hex += mark ? '*' : ' '; |
| 342 | } else { |
| 343 | hex += " "; |
| 344 | } |
| 345 | } |
| 346 | hex = hex + " "; |
| 347 | |
| 348 | for (const char *p = row; p < row + 4 && p < row + length; ++p) |
| 349 | hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.'; |
| 350 | |
| 351 | hex = hex + '\n'; |
| 352 | } |
| 353 | return hex; |
| 354 | } |
| 355 | |
| 356 | } // namespace |
| 357 | |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 358 | QuicVersion QuicVersionMax() { return QuicSupportedVersions().front(); } |
| 359 | |
| 360 | QuicVersion QuicVersionMin() { return QuicSupportedVersions().back(); } |
| 361 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 362 | void CompareCharArraysWithHexError( |
| 363 | const string& description, |
| 364 | const char* actual, |
| 365 | const int actual_len, |
| 366 | const char* expected, |
| 367 | const int expected_len) { |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 368 | EXPECT_EQ(actual_len, expected_len); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 369 | const int min_len = min(actual_len, expected_len); |
| 370 | const int max_len = max(actual_len, expected_len); |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 371 | scoped_ptr<bool[]> marks(new bool[max_len]); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 372 | bool identical = (actual_len == expected_len); |
| 373 | for (int i = 0; i < min_len; ++i) { |
| 374 | if (actual[i] != expected[i]) { |
| 375 | marks[i] = true; |
| 376 | identical = false; |
| 377 | } else { |
| 378 | marks[i] = false; |
| 379 | } |
| 380 | } |
| 381 | for (int i = min_len; i < max_len; ++i) { |
| 382 | marks[i] = true; |
| 383 | } |
| 384 | if (identical) return; |
| 385 | ADD_FAILURE() |
| 386 | << "Description:\n" |
| 387 | << description |
| 388 | << "\n\nExpected:\n" |
| 389 | << HexDumpWithMarks(expected, expected_len, marks.get(), max_len) |
| 390 | << "\nActual:\n" |
| 391 | << HexDumpWithMarks(actual, actual_len, marks.get(), max_len); |
| 392 | } |
| 393 | |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 394 | static QuicPacket* ConstructPacketFromHandshakeMessage( |
| 395 | QuicGuid guid, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 396 | const CryptoHandshakeMessage& message, |
| 397 | bool should_include_version) { |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 398 | CryptoFramer crypto_framer; |
[email protected] | dc2cc74 | 2012-10-21 13:56:13 | [diff] [blame] | 399 | scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message)); |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 400 | QuicFramer quic_framer(QuicSupportedVersions(), QuicTime::Zero(), false); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 401 | |
| 402 | QuicPacketHeader header; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 403 | header.public_header.guid = guid; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 404 | header.public_header.reset_flag = false; |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 405 | header.public_header.version_flag = should_include_version; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 406 | header.packet_sequence_number = 1; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 407 | header.entropy_flag = false; |
| 408 | header.entropy_hash = 0; |
| 409 | header.fec_flag = false; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 410 | header.fec_group = 0; |
| 411 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 412 | QuicStreamFrame stream_frame(kCryptoStreamId, false, 0, |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 413 | MakeIOVector(data->AsStringPiece())); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 414 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 415 | QuicFrame frame(&stream_frame); |
| 416 | QuicFrames frames; |
| 417 | frames.push_back(frame); |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 418 | return quic_framer.BuildUnsizedDataPacket(header, frames).packet; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 419 | } |
| 420 | |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 421 | QuicPacket* ConstructHandshakePacket(QuicGuid guid, QuicTag tag) { |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 422 | CryptoHandshakeMessage message; |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 423 | message.set_tag(tag); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 424 | return ConstructPacketFromHandshakeMessage(guid, message, false); |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 425 | } |
| 426 | |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 427 | size_t GetPacketLengthForOneStream( |
| 428 | QuicVersion version, |
| 429 | bool include_version, |
| 430 | QuicSequenceNumberLength sequence_number_length, |
| 431 | InFecGroup is_in_fec_group, |
| 432 | size_t* payload_length) { |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 433 | *payload_length = 1; |
| 434 | const size_t stream_length = |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 435 | NullEncrypter().GetCiphertextSize(*payload_length) + |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 436 | QuicPacketCreator::StreamFramePacketOverhead( |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 437 | version, PACKET_8BYTE_GUID, include_version, |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 438 | sequence_number_length, is_in_fec_group); |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 439 | const size_t ack_length = NullEncrypter().GetCiphertextSize( |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 440 | QuicFramer::GetMinAckFrameSize( |
| 441 | version, sequence_number_length, PACKET_1BYTE_SEQUENCE_NUMBER)) + |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 442 | GetPacketHeaderSize(PACKET_8BYTE_GUID, include_version, |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 443 | sequence_number_length, is_in_fec_group); |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 444 | if (stream_length < ack_length) { |
| 445 | *payload_length = 1 + ack_length - stream_length; |
| 446 | } |
| 447 | |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 448 | return NullEncrypter().GetCiphertextSize(*payload_length) + |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 449 | QuicPacketCreator::StreamFramePacketOverhead( |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 450 | version, PACKET_8BYTE_GUID, include_version, |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 451 | sequence_number_length, is_in_fec_group); |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 452 | } |
| 453 | |
[email protected] | ea2ab47b | 2013-08-13 00:44:11 | [diff] [blame] | 454 | // Size in bytes of the stream frame fields for an arbitrary StreamID and |
| 455 | // offset and the last frame in a packet. |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 456 | size_t GetMinStreamFrameSize(QuicVersion version) { |
[email protected] | ea2ab47b | 2013-08-13 00:44:11 | [diff] [blame] | 457 | return kQuicFrameTypeSize + kQuicMaxStreamIdSize + kQuicMaxStreamOffsetSize; |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 458 | } |
| 459 | |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 460 | TestEntropyCalculator::TestEntropyCalculator() { } |
| 461 | |
| 462 | TestEntropyCalculator::~TestEntropyCalculator() { } |
| 463 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 464 | QuicPacketEntropyHash TestEntropyCalculator::EntropyHash( |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 465 | QuicPacketSequenceNumber sequence_number) const { |
| 466 | return 1u; |
| 467 | } |
| 468 | |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 469 | MockEntropyCalculator::MockEntropyCalculator() { } |
| 470 | |
| 471 | MockEntropyCalculator::~MockEntropyCalculator() { } |
| 472 | |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 473 | QuicConfig DefaultQuicConfig() { |
| 474 | QuicConfig config; |
| 475 | config.SetDefaults(); |
| 476 | return config; |
| 477 | } |
| 478 | |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 479 | bool TestDecompressorVisitor::OnDecompressedData(StringPiece data) { |
| 480 | data.AppendToString(&data_); |
| 481 | return true; |
| 482 | } |
| 483 | |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 484 | void TestDecompressorVisitor::OnDecompressionError() { |
| 485 | error_ = true; |
| 486 | } |
| 487 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 488 | } // namespace test |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 489 | } // namespace net |