[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] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 9 | #include "net/quic/crypto/crypto_utils.h" |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 10 | |
| 11 | using std::max; |
| 12 | using std::min; |
| 13 | using std::string; |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 14 | using testing::_; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 15 | |
| 16 | namespace net { |
| 17 | namespace test { |
| 18 | |
| 19 | MockFramerVisitor::MockFramerVisitor() { |
| 20 | // By default, we want to accept packets. |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 21 | ON_CALL(*this, OnPacketHeader(_)) |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 22 | .WillByDefault(testing::Return(true)); |
| 23 | } |
| 24 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 25 | MockFramerVisitor::~MockFramerVisitor() { |
| 26 | } |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 27 | |
| 28 | bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) { |
| 29 | return true; |
| 30 | } |
| 31 | |
[email protected] | 134e5c3 | 2012-12-12 19:20:36 | [diff] [blame] | 32 | FramerVisitorCapturingAcks::FramerVisitorCapturingAcks() { |
| 33 | } |
| 34 | |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 35 | FramerVisitorCapturingAcks::~FramerVisitorCapturingAcks() { |
| 36 | } |
| 37 | |
[email protected] | 4e6f0ed | 2012-11-02 22:15:38 | [diff] [blame] | 38 | bool FramerVisitorCapturingAcks::OnPacketHeader( |
| 39 | const QuicPacketHeader& header) { |
| 40 | header_ = header; |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | void FramerVisitorCapturingAcks::OnAckFrame(const QuicAckFrame& frame) { |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 45 | ack_.reset(new QuicAckFrame(frame)); |
| 46 | } |
| 47 | |
| 48 | void FramerVisitorCapturingAcks::OnCongestionFeedbackFrame( |
| 49 | const QuicCongestionFeedbackFrame& frame) { |
| 50 | feedback_.reset(new QuicCongestionFeedbackFrame(frame)); |
[email protected] | 4e6f0ed | 2012-11-02 22:15:38 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 53 | MockConnectionVisitor::MockConnectionVisitor() { |
| 54 | } |
| 55 | |
| 56 | MockConnectionVisitor::~MockConnectionVisitor() { |
| 57 | } |
| 58 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 59 | MockHelper::MockHelper() { |
| 60 | } |
| 61 | |
| 62 | MockHelper::~MockHelper() { |
| 63 | } |
| 64 | |
[email protected] | 97693d1 | 2012-11-16 16:05:00 | [diff] [blame] | 65 | const QuicClock* MockHelper::GetClock() const { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 66 | return &clock_; |
| 67 | } |
| 68 | |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 69 | QuicRandom* MockHelper::GetRandomGenerator() { |
| 70 | return &random_generator_; |
| 71 | } |
| 72 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 73 | MockConnection::MockConnection(QuicGuid guid, IPEndPoint address) |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 74 | : QuicConnection(guid, address, new MockHelper()), |
| 75 | helper_(helper()) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 76 | } |
| 77 | |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 78 | MockConnection::MockConnection(QuicGuid guid, |
| 79 | IPEndPoint address, |
| 80 | QuicConnectionHelperInterface* helper) |
| 81 | : QuicConnection(guid, address, helper), |
| 82 | helper_(helper) { |
| 83 | } |
| 84 | |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 85 | MockConnection::~MockConnection() { |
| 86 | } |
| 87 | |
| 88 | PacketSavingConnection::PacketSavingConnection(QuicGuid guid, |
| 89 | IPEndPoint address) |
| 90 | : MockConnection(guid, address) { |
| 91 | } |
| 92 | |
| 93 | PacketSavingConnection::~PacketSavingConnection() { |
[email protected] | 701bc89 | 2013-01-17 04:51:54 | [diff] [blame] | 94 | STLDeleteElements(&packets_); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | bool PacketSavingConnection::SendPacket(QuicPacketSequenceNumber number, |
| 98 | QuicPacket* packet, |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 99 | bool should_retransmit, |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 100 | bool force, |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 101 | bool is_retransmission) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 102 | packets_.push_back(packet); |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | MockSession::MockSession(QuicConnection* connection, bool is_server) |
| 107 | : QuicSession(connection, is_server) { |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 108 | ON_CALL(*this, WriteData(_, _, _, _)) |
| 109 | .WillByDefault(testing::Return(QuicConsumedData(0, false))); |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | MockSession::~MockSession() { |
| 113 | } |
| 114 | |
[email protected] | 8d659e2 | 2013-01-19 04:26:10 | [diff] [blame] | 115 | MockScheduler::MockScheduler() |
| 116 | : QuicSendScheduler(NULL, kFixRate) { |
| 117 | } |
| 118 | |
| 119 | MockScheduler::~MockScheduler() { |
| 120 | } |
| 121 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 122 | namespace { |
| 123 | |
| 124 | string HexDumpWithMarks(const char* data, int length, |
| 125 | const bool* marks, int mark_length) { |
| 126 | static const char kHexChars[] = "0123456789abcdef"; |
| 127 | static const int kColumns = 4; |
| 128 | |
| 129 | const int kSizeLimit = 1024; |
| 130 | if (length > kSizeLimit || mark_length > kSizeLimit) { |
| 131 | LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes."; |
| 132 | length = min(length, kSizeLimit); |
| 133 | mark_length = min(mark_length, kSizeLimit); |
| 134 | } |
| 135 | |
| 136 | string hex; |
| 137 | for (const char* row = data; length > 0; |
| 138 | row += kColumns, length -= kColumns) { |
| 139 | for (const char *p = row; p < row + 4; ++p) { |
| 140 | if (p < row + length) { |
| 141 | const bool mark = |
| 142 | (marks && (p - data) < mark_length && marks[p - data]); |
| 143 | hex += mark ? '*' : ' '; |
| 144 | hex += kHexChars[(*p & 0xf0) >> 4]; |
| 145 | hex += kHexChars[*p & 0x0f]; |
| 146 | hex += mark ? '*' : ' '; |
| 147 | } else { |
| 148 | hex += " "; |
| 149 | } |
| 150 | } |
| 151 | hex = hex + " "; |
| 152 | |
| 153 | for (const char *p = row; p < row + 4 && p < row + length; ++p) |
| 154 | hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.'; |
| 155 | |
| 156 | hex = hex + '\n'; |
| 157 | } |
| 158 | return hex; |
| 159 | } |
| 160 | |
| 161 | } // namespace |
| 162 | |
| 163 | void CompareCharArraysWithHexError( |
| 164 | const string& description, |
| 165 | const char* actual, |
| 166 | const int actual_len, |
| 167 | const char* expected, |
| 168 | const int expected_len) { |
| 169 | const int min_len = min(actual_len, expected_len); |
| 170 | const int max_len = max(actual_len, expected_len); |
| 171 | scoped_array<bool> marks(new bool[max_len]); |
| 172 | bool identical = (actual_len == expected_len); |
| 173 | for (int i = 0; i < min_len; ++i) { |
| 174 | if (actual[i] != expected[i]) { |
| 175 | marks[i] = true; |
| 176 | identical = false; |
| 177 | } else { |
| 178 | marks[i] = false; |
| 179 | } |
| 180 | } |
| 181 | for (int i = min_len; i < max_len; ++i) { |
| 182 | marks[i] = true; |
| 183 | } |
| 184 | if (identical) return; |
| 185 | ADD_FAILURE() |
| 186 | << "Description:\n" |
| 187 | << description |
| 188 | << "\n\nExpected:\n" |
| 189 | << HexDumpWithMarks(expected, expected_len, marks.get(), max_len) |
| 190 | << "\nActual:\n" |
| 191 | << HexDumpWithMarks(actual, actual_len, marks.get(), max_len); |
| 192 | } |
| 193 | |
| 194 | void CompareQuicDataWithHexError( |
| 195 | const string& description, |
| 196 | QuicData* actual, |
| 197 | QuicData* expected) { |
| 198 | CompareCharArraysWithHexError( |
| 199 | description, |
| 200 | actual->data(), actual->length(), |
| 201 | expected->data(), expected->length()); |
| 202 | } |
| 203 | |
| 204 | QuicPacket* ConstructHandshakePacket(QuicGuid guid, CryptoTag tag) { |
| 205 | CryptoHandshakeMessage message; |
| 206 | message.tag = tag; |
| 207 | CryptoFramer crypto_framer; |
[email protected] | dc2cc74 | 2012-10-21 13:56:13 | [diff] [blame] | 208 | scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message)); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 209 | QuicFramer quic_framer(QuicDecrypter::Create(kNULL), |
| 210 | QuicEncrypter::Create(kNULL)); |
| 211 | |
| 212 | QuicPacketHeader header; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 213 | header.public_header.guid = guid; |
| 214 | header.public_header.flags = PACKET_PUBLIC_FLAGS_NONE; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 215 | header.packet_sequence_number = 1; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 216 | header.private_flags = PACKET_PRIVATE_FLAGS_NONE; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 217 | header.fec_group = 0; |
| 218 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 219 | QuicStreamFrame stream_frame(kCryptoStreamId, false, 0, |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 220 | data->AsStringPiece()); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 221 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 222 | QuicFrame frame(&stream_frame); |
| 223 | QuicFrames frames; |
| 224 | frames.push_back(frame); |
[email protected] | 610a7e94 | 2012-12-18 00:21:39 | [diff] [blame] | 225 | return quic_framer.ConstructFrameDataPacket(header, frames); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 226 | } |
| 227 | |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 228 | QuicPacket* ConstructClientHelloPacket(QuicGuid guid, |
| 229 | const QuicClock* clock, |
[email protected] | 41d6b17 | 2013-01-29 16:10:57 | [diff] [blame^] | 230 | QuicRandom* random_generator, |
| 231 | const string& server_hostname) { |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 232 | QuicClientCryptoConfig config; |
| 233 | config.SetDefaults(); |
| 234 | string nonce; |
| 235 | CryptoUtils::GenerateNonce(clock, random_generator, &nonce); |
| 236 | |
| 237 | CryptoHandshakeMessage message; |
[email protected] | 41d6b17 | 2013-01-29 16:10:57 | [diff] [blame^] | 238 | CryptoUtils::FillClientHelloMessage(config, nonce, server_hostname, |
| 239 | &message); |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 240 | CryptoFramer crypto_framer; |
| 241 | scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message)); |
| 242 | QuicFramer quic_framer(QuicDecrypter::Create(kNULL), |
| 243 | QuicEncrypter::Create(kNULL)); |
| 244 | |
| 245 | QuicPacketHeader header; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 246 | header.public_header.guid = guid; |
| 247 | header.public_header.flags = PACKET_PUBLIC_FLAGS_NONE; |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 248 | header.packet_sequence_number = 1; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 249 | header.private_flags = PACKET_PRIVATE_FLAGS_NONE; |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 250 | header.fec_group = 0; |
| 251 | |
| 252 | QuicStreamFrame stream_frame(kCryptoStreamId, false, 0, |
| 253 | data->AsStringPiece()); |
| 254 | |
| 255 | QuicFrame frame(&stream_frame); |
| 256 | QuicFrames frames; |
| 257 | frames.push_back(frame); |
| 258 | return quic_framer.ConstructFrameDataPacket(header, frames); |
| 259 | } |
| 260 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 261 | } // namespace test |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 262 | } // namespace net |