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