blob: 0971e06dface238b94f2df8bcfe92bc73f5a42a0 [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 {
[email protected]8b37a092012-10-18 21:53:4914namespace 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
[email protected]044ac2b2012-11-13 21:41:0622MockFramerVisitor::~MockFramerVisitor() {
23}
[email protected]8b37a092012-10-18 21:53:4924
25bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
26 return true;
27}
28
[email protected]134e5c32012-12-12 19:20:3629FramerVisitorCapturingAcks::FramerVisitorCapturingAcks() {
30}
31
[email protected]26f3f8e2012-12-13 21:07:1932FramerVisitorCapturingAcks::~FramerVisitorCapturingAcks() {
33}
34
[email protected]4e6f0ed2012-11-02 22:15:3835bool FramerVisitorCapturingAcks::OnPacketHeader(
36 const QuicPacketHeader& header) {
37 header_ = header;
38 return true;
39}
40
41void FramerVisitorCapturingAcks::OnAckFrame(const QuicAckFrame& frame) {
[email protected]26f3f8e2012-12-13 21:07:1942 ack_.reset(new QuicAckFrame(frame));
43}
44
45void FramerVisitorCapturingAcks::OnCongestionFeedbackFrame(
46 const QuicCongestionFeedbackFrame& frame) {
47 feedback_.reset(new QuicCongestionFeedbackFrame(frame));
[email protected]4e6f0ed2012-11-02 22:15:3848}
49
[email protected]9c0b1352012-11-04 00:03:2750MockHelper::MockHelper() {
51}
52
53MockHelper::~MockHelper() {
54}
55
[email protected]97693d12012-11-16 16:05:0056const QuicClock* MockHelper::GetClock() const {
[email protected]9c0b1352012-11-04 00:03:2757 return &clock_;
58}
59
[email protected]4e6f0ed2012-11-02 22:15:3860MockConnectionVisitor::MockConnectionVisitor() {
61}
62
63MockConnectionVisitor::~MockConnectionVisitor() {
64}
65
66MockScheduler::MockScheduler()
67 : QuicSendScheduler(NULL, kFixRate) {
68}
69
70MockScheduler::~MockScheduler() {
71}
72
[email protected]044ac2b2012-11-13 21:41:0673MockConnection::MockConnection(QuicGuid guid, IPEndPoint address)
[email protected]e13201d82012-12-12 05:00:3274 : QuicConnection(guid, address, new MockHelper()),
75 helper_(helper()) {
[email protected]044ac2b2012-11-13 21:41:0676}
77
78MockConnection::~MockConnection() {
79}
80
81PacketSavingConnection::PacketSavingConnection(QuicGuid guid,
82 IPEndPoint address)
83 : MockConnection(guid, address) {
84}
85
86PacketSavingConnection::~PacketSavingConnection() {
87}
88
89bool PacketSavingConnection::SendPacket(QuicPacketSequenceNumber number,
90 QuicPacket* packet,
91 bool should_resend,
92 bool force,
93 bool is_retransmit) {
94 packets_.push_back(packet);
95 return true;
96}
97
98MockSession::MockSession(QuicConnection* connection, bool is_server)
99 : QuicSession(connection, is_server) {
100}
101
102MockSession::~MockSession() {
103}
104
[email protected]8b37a092012-10-18 21:53:49105namespace {
106
107string HexDumpWithMarks(const char* data, int length,
108 const bool* marks, int mark_length) {
109 static const char kHexChars[] = "0123456789abcdef";
110 static const int kColumns = 4;
111
112 const int kSizeLimit = 1024;
113 if (length > kSizeLimit || mark_length > kSizeLimit) {
114 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
115 length = min(length, kSizeLimit);
116 mark_length = min(mark_length, kSizeLimit);
117 }
118
119 string hex;
120 for (const char* row = data; length > 0;
121 row += kColumns, length -= kColumns) {
122 for (const char *p = row; p < row + 4; ++p) {
123 if (p < row + length) {
124 const bool mark =
125 (marks && (p - data) < mark_length && marks[p - data]);
126 hex += mark ? '*' : ' ';
127 hex += kHexChars[(*p & 0xf0) >> 4];
128 hex += kHexChars[*p & 0x0f];
129 hex += mark ? '*' : ' ';
130 } else {
131 hex += " ";
132 }
133 }
134 hex = hex + " ";
135
136 for (const char *p = row; p < row + 4 && p < row + length; ++p)
137 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
138
139 hex = hex + '\n';
140 }
141 return hex;
142}
143
144} // namespace
145
146void CompareCharArraysWithHexError(
147 const string& description,
148 const char* actual,
149 const int actual_len,
150 const char* expected,
151 const int expected_len) {
152 const int min_len = min(actual_len, expected_len);
153 const int max_len = max(actual_len, expected_len);
154 scoped_array<bool> marks(new bool[max_len]);
155 bool identical = (actual_len == expected_len);
156 for (int i = 0; i < min_len; ++i) {
157 if (actual[i] != expected[i]) {
158 marks[i] = true;
159 identical = false;
160 } else {
161 marks[i] = false;
162 }
163 }
164 for (int i = min_len; i < max_len; ++i) {
165 marks[i] = true;
166 }
167 if (identical) return;
168 ADD_FAILURE()
169 << "Description:\n"
170 << description
171 << "\n\nExpected:\n"
172 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
173 << "\nActual:\n"
174 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
175}
176
177void CompareQuicDataWithHexError(
178 const string& description,
179 QuicData* actual,
180 QuicData* expected) {
181 CompareCharArraysWithHexError(
182 description,
183 actual->data(), actual->length(),
184 expected->data(), expected->length());
185}
186
187QuicPacket* ConstructHandshakePacket(QuicGuid guid, CryptoTag tag) {
188 CryptoHandshakeMessage message;
189 message.tag = tag;
190 CryptoFramer crypto_framer;
[email protected]dc2cc742012-10-21 13:56:13191 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
[email protected]8b37a092012-10-18 21:53:49192 QuicFramer quic_framer(QuicDecrypter::Create(kNULL),
193 QuicEncrypter::Create(kNULL));
194
195 QuicPacketHeader header;
196 header.guid = guid;
[email protected]8b37a092012-10-18 21:53:49197 header.packet_sequence_number = 1;
[email protected]8b37a092012-10-18 21:53:49198 header.flags = PACKET_FLAGS_NONE;
199 header.fec_group = 0;
200
[email protected]be24ab22012-10-22 03:01:52201 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
[email protected]044ac2b2012-11-13 21:41:06202 data->AsStringPiece());
[email protected]8b37a092012-10-18 21:53:49203
[email protected]be24ab22012-10-22 03:01:52204 QuicFrame frame(&stream_frame);
205 QuicFrames frames;
206 frames.push_back(frame);
[email protected]610a7e942012-12-18 00:21:39207 return quic_framer.ConstructFrameDataPacket(header, frames);
[email protected]8b37a092012-10-18 21:53:49208}
209
[email protected]8b37a092012-10-18 21:53:49210} // namespace test
[email protected]8b37a092012-10-18 21:53:49211} // namespace net