[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] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 14 | #include "net/quic/quic_packet_creator.h" |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 15 | |
| 16 | using std::max; |
| 17 | using std::min; |
| 18 | using std::string; |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 19 | using testing::_; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 20 | |
| 21 | namespace net { |
| 22 | namespace test { |
| 23 | |
| 24 | MockFramerVisitor::MockFramerVisitor() { |
| 25 | // By default, we want to accept packets. |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 26 | ON_CALL(*this, OnProtocolVersionMismatch(_)) |
| 27 | .WillByDefault(testing::Return(false)); |
| 28 | |
| 29 | // By default, we want to accept packets. |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 30 | ON_CALL(*this, OnPacketHeader(_)) |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 31 | .WillByDefault(testing::Return(true)); |
| 32 | } |
| 33 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 34 | MockFramerVisitor::~MockFramerVisitor() { |
| 35 | } |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 36 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 37 | bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersionTag version) { |
| 38 | return false; |
| 39 | } |
| 40 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 41 | bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) { |
| 42 | return true; |
| 43 | } |
| 44 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 45 | FramerVisitorCapturingFrames::FramerVisitorCapturingFrames() : frame_count_(0) { |
[email protected] | 134e5c3 | 2012-12-12 19:20:36 | [diff] [blame] | 46 | } |
| 47 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 48 | FramerVisitorCapturingFrames::~FramerVisitorCapturingFrames() { |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 49 | } |
| 50 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 51 | bool FramerVisitorCapturingFrames::OnPacketHeader( |
[email protected] | 4e6f0ed | 2012-11-02 22:15:38 | [diff] [blame] | 52 | const QuicPacketHeader& header) { |
| 53 | header_ = header; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 54 | frame_count_ = 0; |
[email protected] | 4e6f0ed | 2012-11-02 22:15:38 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 58 | void FramerVisitorCapturingFrames::OnStreamFrame(const QuicStreamFrame& frame) { |
| 59 | // TODO(ianswett): Own the underlying string, so it will not exist outside |
| 60 | // this callback. |
| 61 | stream_frames_.push_back(frame); |
| 62 | ++frame_count_; |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 65 | void FramerVisitorCapturingFrames::OnAckFrame(const QuicAckFrame& frame) { |
| 66 | ack_.reset(new QuicAckFrame(frame)); |
| 67 | ++frame_count_; |
| 68 | } |
| 69 | |
| 70 | void FramerVisitorCapturingFrames::OnCongestionFeedbackFrame( |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 71 | const QuicCongestionFeedbackFrame& frame) { |
| 72 | feedback_.reset(new QuicCongestionFeedbackFrame(frame)); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 73 | ++frame_count_; |
| 74 | } |
| 75 | |
| 76 | void FramerVisitorCapturingFrames::OnRstStreamFrame( |
| 77 | const QuicRstStreamFrame& frame) { |
| 78 | rst_.reset(new QuicRstStreamFrame(frame)); |
| 79 | ++frame_count_; |
| 80 | } |
| 81 | |
| 82 | void FramerVisitorCapturingFrames::OnConnectionCloseFrame( |
| 83 | const QuicConnectionCloseFrame& frame) { |
| 84 | close_.reset(new QuicConnectionCloseFrame(frame)); |
| 85 | ++frame_count_; |
| 86 | } |
| 87 | |
| 88 | void FramerVisitorCapturingFrames::OnGoAwayFrame(const QuicGoAwayFrame& frame) { |
| 89 | goaway_.reset(new QuicGoAwayFrame(frame)); |
| 90 | ++frame_count_; |
[email protected] | 4e6f0ed | 2012-11-02 22:15:38 | [diff] [blame] | 91 | } |
| 92 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 93 | void FramerVisitorCapturingFrames::OnVersionNegotiationPacket( |
| 94 | const QuicVersionNegotiationPacket& packet) { |
| 95 | version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(packet)); |
| 96 | frame_count_ = 0; |
| 97 | } |
| 98 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 99 | FramerVisitorCapturingPublicReset::FramerVisitorCapturingPublicReset() { |
| 100 | } |
| 101 | |
| 102 | FramerVisitorCapturingPublicReset::~FramerVisitorCapturingPublicReset() { |
| 103 | } |
| 104 | |
| 105 | void FramerVisitorCapturingPublicReset::OnPublicResetPacket( |
| 106 | const QuicPublicResetPacket& public_reset) { |
| 107 | public_reset_packet_ = public_reset; |
| 108 | } |
| 109 | |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 110 | MockConnectionVisitor::MockConnectionVisitor() { |
| 111 | } |
| 112 | |
| 113 | MockConnectionVisitor::~MockConnectionVisitor() { |
| 114 | } |
| 115 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 116 | MockHelper::MockHelper() { |
| 117 | } |
| 118 | |
| 119 | MockHelper::~MockHelper() { |
| 120 | } |
| 121 | |
[email protected] | 97693d1 | 2012-11-16 16:05:00 | [diff] [blame] | 122 | const QuicClock* MockHelper::GetClock() const { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 123 | return &clock_; |
| 124 | } |
| 125 | |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 126 | QuicRandom* MockHelper::GetRandomGenerator() { |
| 127 | return &random_generator_; |
| 128 | } |
| 129 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 130 | MockConnection::MockConnection(QuicGuid guid, |
| 131 | IPEndPoint address, |
| 132 | bool is_server) |
[email protected] | 6dcd1d5c | 2013-03-15 07:28:37 | [diff] [blame] | 133 | : QuicConnection(guid, address, new MockHelper(), is_server) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 134 | } |
| 135 | |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 136 | MockConnection::MockConnection(QuicGuid guid, |
| 137 | IPEndPoint address, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 138 | QuicConnectionHelperInterface* helper, |
| 139 | bool is_server) |
[email protected] | 6dcd1d5c | 2013-03-15 07:28:37 | [diff] [blame] | 140 | : QuicConnection(guid, address, helper, is_server) { |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 143 | MockConnection::~MockConnection() { |
| 144 | } |
| 145 | |
| 146 | PacketSavingConnection::PacketSavingConnection(QuicGuid guid, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 147 | IPEndPoint address, |
| 148 | bool is_server) |
| 149 | : MockConnection(guid, address, is_server) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | PacketSavingConnection::~PacketSavingConnection() { |
[email protected] | 701bc89 | 2013-01-17 04:51:54 | [diff] [blame] | 153 | STLDeleteElements(&packets_); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 154 | } |
| 155 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 156 | bool PacketSavingConnection::SendOrQueuePacket( |
| 157 | QuicPacketSequenceNumber sequence_number, |
| 158 | QuicPacket* packet, |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 159 | QuicPacketEntropyHash entropy_hash, |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame^] | 160 | HasRetransmittableData retransmittable) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 161 | packets_.push_back(packet); |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | MockSession::MockSession(QuicConnection* connection, bool is_server) |
| 166 | : QuicSession(connection, is_server) { |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 167 | ON_CALL(*this, WriteData(_, _, _, _)) |
| 168 | .WillByDefault(testing::Return(QuicConsumedData(0, false))); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | MockSession::~MockSession() { |
| 172 | } |
| 173 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 174 | MockSendAlgorithm::MockSendAlgorithm() { |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 177 | MockSendAlgorithm::~MockSendAlgorithm() { |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 178 | } |
| 179 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 180 | namespace { |
| 181 | |
| 182 | string HexDumpWithMarks(const char* data, int length, |
| 183 | const bool* marks, int mark_length) { |
| 184 | static const char kHexChars[] = "0123456789abcdef"; |
| 185 | static const int kColumns = 4; |
| 186 | |
| 187 | const int kSizeLimit = 1024; |
| 188 | if (length > kSizeLimit || mark_length > kSizeLimit) { |
| 189 | LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes."; |
| 190 | length = min(length, kSizeLimit); |
| 191 | mark_length = min(mark_length, kSizeLimit); |
| 192 | } |
| 193 | |
| 194 | string hex; |
| 195 | for (const char* row = data; length > 0; |
| 196 | row += kColumns, length -= kColumns) { |
| 197 | for (const char *p = row; p < row + 4; ++p) { |
| 198 | if (p < row + length) { |
| 199 | const bool mark = |
| 200 | (marks && (p - data) < mark_length && marks[p - data]); |
| 201 | hex += mark ? '*' : ' '; |
| 202 | hex += kHexChars[(*p & 0xf0) >> 4]; |
| 203 | hex += kHexChars[*p & 0x0f]; |
| 204 | hex += mark ? '*' : ' '; |
| 205 | } else { |
| 206 | hex += " "; |
| 207 | } |
| 208 | } |
| 209 | hex = hex + " "; |
| 210 | |
| 211 | for (const char *p = row; p < row + 4 && p < row + length; ++p) |
| 212 | hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.'; |
| 213 | |
| 214 | hex = hex + '\n'; |
| 215 | } |
| 216 | return hex; |
| 217 | } |
| 218 | |
| 219 | } // namespace |
| 220 | |
| 221 | void CompareCharArraysWithHexError( |
| 222 | const string& description, |
| 223 | const char* actual, |
| 224 | const int actual_len, |
| 225 | const char* expected, |
| 226 | const int expected_len) { |
| 227 | const int min_len = min(actual_len, expected_len); |
| 228 | const int max_len = max(actual_len, expected_len); |
| 229 | scoped_array<bool> marks(new bool[max_len]); |
| 230 | bool identical = (actual_len == expected_len); |
| 231 | for (int i = 0; i < min_len; ++i) { |
| 232 | if (actual[i] != expected[i]) { |
| 233 | marks[i] = true; |
| 234 | identical = false; |
| 235 | } else { |
| 236 | marks[i] = false; |
| 237 | } |
| 238 | } |
| 239 | for (int i = min_len; i < max_len; ++i) { |
| 240 | marks[i] = true; |
| 241 | } |
| 242 | if (identical) return; |
| 243 | ADD_FAILURE() |
| 244 | << "Description:\n" |
| 245 | << description |
| 246 | << "\n\nExpected:\n" |
| 247 | << HexDumpWithMarks(expected, expected_len, marks.get(), max_len) |
| 248 | << "\nActual:\n" |
| 249 | << HexDumpWithMarks(actual, actual_len, marks.get(), max_len); |
| 250 | } |
| 251 | |
| 252 | void CompareQuicDataWithHexError( |
| 253 | const string& description, |
| 254 | QuicData* actual, |
| 255 | QuicData* expected) { |
| 256 | CompareCharArraysWithHexError( |
| 257 | description, |
| 258 | actual->data(), actual->length(), |
| 259 | expected->data(), expected->length()); |
| 260 | } |
| 261 | |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 262 | static QuicPacket* ConstructPacketFromHandshakeMessage( |
| 263 | QuicGuid guid, |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 264 | const CryptoHandshakeMessage& message, |
| 265 | bool should_include_version) { |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 266 | CryptoFramer crypto_framer; |
[email protected] | dc2cc74 | 2012-10-21 13:56:13 | [diff] [blame] | 267 | scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message)); |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 268 | QuicFramer quic_framer(kQuicVersion1, |
| 269 | QuicDecrypter::Create(kNULL), |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 270 | QuicEncrypter::Create(kNULL), |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame^] | 271 | QuicTime::Zero(), |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 272 | false); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 273 | |
| 274 | QuicPacketHeader header; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 275 | header.public_header.guid = guid; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 276 | header.public_header.reset_flag = false; |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 277 | header.public_header.version_flag = should_include_version; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 278 | header.packet_sequence_number = 1; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 279 | header.entropy_flag = false; |
| 280 | header.entropy_hash = 0; |
| 281 | header.fec_flag = false; |
| 282 | header.fec_entropy_flag = false; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 283 | header.fec_group = 0; |
| 284 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 285 | QuicStreamFrame stream_frame(kCryptoStreamId, false, 0, |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 286 | data->AsStringPiece()); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 287 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 288 | QuicFrame frame(&stream_frame); |
| 289 | QuicFrames frames; |
| 290 | frames.push_back(frame); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 291 | return quic_framer.ConstructFrameDataPacket(header, frames).packet; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 292 | } |
| 293 | |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 294 | QuicPacket* ConstructHandshakePacket(QuicGuid guid, CryptoTag tag) { |
| 295 | CryptoHandshakeMessage message; |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 296 | message.set_tag(tag); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 297 | return ConstructPacketFromHandshakeMessage(guid, message, false); |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame] | 298 | } |
| 299 | |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 300 | size_t GetPacketLengthForOneStream(bool include_version, size_t payload) { |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 301 | // TODO(wtc): the hardcoded use of NullEncrypter here seems wrong. |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 302 | return NullEncrypter().GetCiphertextSize(payload) + |
| 303 | QuicPacketCreator::StreamFramePacketOverhead(1, include_version); |
| 304 | } |
| 305 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 306 | QuicPacketEntropyHash TestEntropyCalculator::ReceivedEntropyHash( |
| 307 | QuicPacketSequenceNumber sequence_number) const { |
| 308 | return 1u; |
| 309 | } |
| 310 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 311 | } // namespace test |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 312 | } // namespace net |