[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" |
| 8 | |
| 9 | using std::max; |
| 10 | using std::min; |
| 11 | using std::string; |
| 12 | |
| 13 | namespace net { |
| 14 | namespace test { |
| 15 | |
| 16 | MockFramerVisitor::MockFramerVisitor() { |
| 17 | // By default, we want to accept packets. |
| 18 | ON_CALL(*this, OnPacketHeader(testing::_)) |
| 19 | .WillByDefault(testing::Return(true)); |
| 20 | } |
| 21 | |
| 22 | MockFramerVisitor::~MockFramerVisitor() {} |
| 23 | |
| 24 | bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) { |
| 25 | return true; |
| 26 | } |
| 27 | |
[email protected] | 4e6f0ed | 2012-11-02 22:15:38 | [diff] [blame] | 28 | bool FramerVisitorCapturingAcks::OnPacketHeader( |
| 29 | const QuicPacketHeader& header) { |
| 30 | header_ = header; |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | void FramerVisitorCapturingAcks::OnAckFrame(const QuicAckFrame& frame) { |
| 35 | frame_ = frame; |
| 36 | } |
| 37 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame^] | 38 | MockHelper::MockHelper() { |
| 39 | } |
| 40 | |
| 41 | MockHelper::~MockHelper() { |
| 42 | } |
| 43 | |
| 44 | QuicClock* MockHelper::GetClock() { |
| 45 | return &clock_; |
| 46 | } |
| 47 | |
[email protected] | 4e6f0ed | 2012-11-02 22:15:38 | [diff] [blame] | 48 | MockConnectionVisitor::MockConnectionVisitor() { |
| 49 | } |
| 50 | |
| 51 | MockConnectionVisitor::~MockConnectionVisitor() { |
| 52 | } |
| 53 | |
| 54 | MockScheduler::MockScheduler() |
| 55 | : QuicSendScheduler(NULL, kFixRate) { |
| 56 | } |
| 57 | |
| 58 | MockScheduler::~MockScheduler() { |
| 59 | } |
| 60 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 61 | namespace { |
| 62 | |
| 63 | string HexDumpWithMarks(const char* data, int length, |
| 64 | const bool* marks, int mark_length) { |
| 65 | static const char kHexChars[] = "0123456789abcdef"; |
| 66 | static const int kColumns = 4; |
| 67 | |
| 68 | const int kSizeLimit = 1024; |
| 69 | if (length > kSizeLimit || mark_length > kSizeLimit) { |
| 70 | LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes."; |
| 71 | length = min(length, kSizeLimit); |
| 72 | mark_length = min(mark_length, kSizeLimit); |
| 73 | } |
| 74 | |
| 75 | string hex; |
| 76 | for (const char* row = data; length > 0; |
| 77 | row += kColumns, length -= kColumns) { |
| 78 | for (const char *p = row; p < row + 4; ++p) { |
| 79 | if (p < row + length) { |
| 80 | const bool mark = |
| 81 | (marks && (p - data) < mark_length && marks[p - data]); |
| 82 | hex += mark ? '*' : ' '; |
| 83 | hex += kHexChars[(*p & 0xf0) >> 4]; |
| 84 | hex += kHexChars[*p & 0x0f]; |
| 85 | hex += mark ? '*' : ' '; |
| 86 | } else { |
| 87 | hex += " "; |
| 88 | } |
| 89 | } |
| 90 | hex = hex + " "; |
| 91 | |
| 92 | for (const char *p = row; p < row + 4 && p < row + length; ++p) |
| 93 | hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.'; |
| 94 | |
| 95 | hex = hex + '\n'; |
| 96 | } |
| 97 | return hex; |
| 98 | } |
| 99 | |
| 100 | } // namespace |
| 101 | |
| 102 | void CompareCharArraysWithHexError( |
| 103 | const string& description, |
| 104 | const char* actual, |
| 105 | const int actual_len, |
| 106 | const char* expected, |
| 107 | const int expected_len) { |
| 108 | const int min_len = min(actual_len, expected_len); |
| 109 | const int max_len = max(actual_len, expected_len); |
| 110 | scoped_array<bool> marks(new bool[max_len]); |
| 111 | bool identical = (actual_len == expected_len); |
| 112 | for (int i = 0; i < min_len; ++i) { |
| 113 | if (actual[i] != expected[i]) { |
| 114 | marks[i] = true; |
| 115 | identical = false; |
| 116 | } else { |
| 117 | marks[i] = false; |
| 118 | } |
| 119 | } |
| 120 | for (int i = min_len; i < max_len; ++i) { |
| 121 | marks[i] = true; |
| 122 | } |
| 123 | if (identical) return; |
| 124 | ADD_FAILURE() |
| 125 | << "Description:\n" |
| 126 | << description |
| 127 | << "\n\nExpected:\n" |
| 128 | << HexDumpWithMarks(expected, expected_len, marks.get(), max_len) |
| 129 | << "\nActual:\n" |
| 130 | << HexDumpWithMarks(actual, actual_len, marks.get(), max_len); |
| 131 | } |
| 132 | |
| 133 | void CompareQuicDataWithHexError( |
| 134 | const string& description, |
| 135 | QuicData* actual, |
| 136 | QuicData* expected) { |
| 137 | CompareCharArraysWithHexError( |
| 138 | description, |
| 139 | actual->data(), actual->length(), |
| 140 | expected->data(), expected->length()); |
| 141 | } |
| 142 | |
| 143 | QuicPacket* ConstructHandshakePacket(QuicGuid guid, CryptoTag tag) { |
| 144 | CryptoHandshakeMessage message; |
| 145 | message.tag = tag; |
| 146 | CryptoFramer crypto_framer; |
[email protected] | dc2cc74 | 2012-10-21 13:56:13 | [diff] [blame] | 147 | scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message)); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 148 | QuicFramer quic_framer(QuicDecrypter::Create(kNULL), |
| 149 | QuicEncrypter::Create(kNULL)); |
| 150 | |
| 151 | QuicPacketHeader header; |
| 152 | header.guid = guid; |
| 153 | header.retransmission_count = 0; |
| 154 | header.packet_sequence_number = 1; |
| 155 | header.transmission_time = 0; |
| 156 | header.flags = PACKET_FLAGS_NONE; |
| 157 | header.fec_group = 0; |
| 158 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 159 | QuicStreamFrame stream_frame(kCryptoStreamId, false, 0, |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 160 | data->AsStringPiece()); |
| 161 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 162 | QuicFrame frame(&stream_frame); |
| 163 | QuicFrames frames; |
| 164 | frames.push_back(frame); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 165 | QuicPacket* packet; |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 166 | quic_framer.ConstructFragementDataPacket(header, frames, &packet); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 167 | return packet; |
| 168 | } |
| 169 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame^] | 170 | MockConnection::MockConnection(QuicGuid guid, IPEndPoint address) |
| 171 | : QuicConnection(guid, address, new MockHelper()) { |
| 172 | } |
| 173 | |
| 174 | MockConnection::~MockConnection() { |
| 175 | } |
| 176 | |
| 177 | PacketSavingConnection::PacketSavingConnection(QuicGuid guid, |
| 178 | IPEndPoint address) |
| 179 | : MockConnection(guid, address) { |
| 180 | } |
| 181 | |
| 182 | PacketSavingConnection::~PacketSavingConnection() { |
| 183 | } |
| 184 | |
| 185 | bool PacketSavingConnection::SendPacket(QuicPacketSequenceNumber number, |
| 186 | QuicPacket* packet, |
| 187 | bool resend, |
| 188 | bool force) { |
| 189 | packets_.push_back(packet); |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | MockSession::MockSession(QuicConnection* connection, bool is_server) |
| 194 | : QuicSession(connection, is_server) { |
| 195 | } |
| 196 | |
| 197 | MockSession::~MockSession() { |
| 198 | } |
| 199 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 200 | } // namespace test |
| 201 | |
| 202 | } // namespace net |