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