blob: 3e3171f6839d323cef2f97890da7589fec9f30d2 [file] [log] [blame]
[email protected]8b37a092012-10-18 21:53:491// 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]9c0b1352012-11-04 00:03:276
[email protected]8b37a092012-10-18 21:53:497#include "net/quic/crypto/crypto_framer.h"
8
9using std::max;
10using std::min;
11using std::string;
12
13namespace net {
14namespace test {
15
16MockFramerVisitor::MockFramerVisitor() {
17 // By default, we want to accept packets.
18 ON_CALL(*this, OnPacketHeader(testing::_))
19 .WillByDefault(testing::Return(true));
20}
21
22MockFramerVisitor::~MockFramerVisitor() {}
23
24bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
25 return true;
26}
27
[email protected]4e6f0ed2012-11-02 22:15:3828bool FramerVisitorCapturingAcks::OnPacketHeader(
29 const QuicPacketHeader& header) {
30 header_ = header;
31 return true;
32}
33
34void FramerVisitorCapturingAcks::OnAckFrame(const QuicAckFrame& frame) {
35 frame_ = frame;
36}
37
[email protected]9c0b1352012-11-04 00:03:2738MockHelper::MockHelper() {
39}
40
41MockHelper::~MockHelper() {
42}
43
44QuicClock* MockHelper::GetClock() {
45 return &clock_;
46}
47
[email protected]4e6f0ed2012-11-02 22:15:3848MockConnectionVisitor::MockConnectionVisitor() {
49}
50
51MockConnectionVisitor::~MockConnectionVisitor() {
52}
53
54MockScheduler::MockScheduler()
55 : QuicSendScheduler(NULL, kFixRate) {
56}
57
58MockScheduler::~MockScheduler() {
59}
60
[email protected]8b37a092012-10-18 21:53:4961namespace {
62
63string 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
102void 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
133void 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
143QuicPacket* ConstructHandshakePacket(QuicGuid guid, CryptoTag tag) {
144 CryptoHandshakeMessage message;
145 message.tag = tag;
146 CryptoFramer crypto_framer;
[email protected]dc2cc742012-10-21 13:56:13147 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
[email protected]8b37a092012-10-18 21:53:49148 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]be24ab22012-10-22 03:01:52159 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
[email protected]8b37a092012-10-18 21:53:49160 data->AsStringPiece());
161
[email protected]be24ab22012-10-22 03:01:52162 QuicFrame frame(&stream_frame);
163 QuicFrames frames;
164 frames.push_back(frame);
[email protected]8b37a092012-10-18 21:53:49165 QuicPacket* packet;
[email protected]be24ab22012-10-22 03:01:52166 quic_framer.ConstructFragementDataPacket(header, frames, &packet);
[email protected]8b37a092012-10-18 21:53:49167 return packet;
168}
169
[email protected]9c0b1352012-11-04 00:03:27170MockConnection::MockConnection(QuicGuid guid, IPEndPoint address)
171 : QuicConnection(guid, address, new MockHelper()) {
172}
173
174MockConnection::~MockConnection() {
175}
176
177PacketSavingConnection::PacketSavingConnection(QuicGuid guid,
178 IPEndPoint address)
179 : MockConnection(guid, address) {
180}
181
182PacketSavingConnection::~PacketSavingConnection() {
183}
184
185bool PacketSavingConnection::SendPacket(QuicPacketSequenceNumber number,
186 QuicPacket* packet,
187 bool resend,
188 bool force) {
189 packets_.push_back(packet);
190 return true;
191}
192
193MockSession::MockSession(QuicConnection* connection, bool is_server)
194 : QuicSession(connection, is_server) {
195}
196
197MockSession::~MockSession() {
198}
199
[email protected]8b37a092012-10-18 21:53:49200} // namespace test
201
202} // namespace net