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