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.
5 #include "net/tools/quic/test_tools/quic_test_utils.h"
7 #include "net/quic/quic_connection.h"
8 #include "net/quic/test_tools/quic_connection_peer.h"
9 #include "net/quic/test_tools/quic_test_utils.h"
10 #include "net/tools/quic/quic_epoll_connection_helper.h"
12 using base::StringPiece
;
13 using net::test::MakeAckFrame
;
14 using net::test::MockHelper
;
15 using net::test::QuicConnectionPeer
;
22 class NiceMockPacketWriterFactory
23 : public QuicConnection::PacketWriterFactory
{
25 NiceMockPacketWriterFactory() {}
26 virtual ~NiceMockPacketWriterFactory() {}
28 virtual QuicPacketWriter
* Create(
29 QuicConnection
* /*connection*/) const override
{
30 return new testing::NiceMock
<MockPacketWriter
>();
34 DISALLOW_COPY_AND_ASSIGN(NiceMockPacketWriterFactory
);
38 MockConnection::MockConnection(bool is_server
)
39 : QuicConnection(kTestConnectionId
,
40 IPEndPoint(net::test::Loopback4(), kTestPort
),
41 new testing::NiceMock
<MockHelper
>(),
42 NiceMockPacketWriterFactory(),
43 /* owns_writer= */ true,
44 is_server
, QuicSupportedVersions()),
48 MockConnection::MockConnection(IPEndPoint address
,
50 : QuicConnection(kTestConnectionId
, address
,
51 new testing::NiceMock
<MockHelper
>(),
52 NiceMockPacketWriterFactory(),
53 /* owns_writer= */ true,
54 is_server
, QuicSupportedVersions()),
58 MockConnection::MockConnection(QuicConnectionId connection_id
,
60 : QuicConnection(connection_id
,
61 IPEndPoint(net::test::Loopback4(), kTestPort
),
62 new testing::NiceMock
<MockHelper
>(),
63 NiceMockPacketWriterFactory(),
64 /* owns_writer= */ true,
65 is_server
, QuicSupportedVersions()),
69 MockConnection::MockConnection(bool is_server
,
70 const QuicVersionVector
& supported_versions
)
71 : QuicConnection(kTestConnectionId
,
72 IPEndPoint(net::test::Loopback4(), kTestPort
),
73 new testing::NiceMock
<MockHelper
>(),
74 NiceMockPacketWriterFactory(),
75 /* owns_writer= */ true,
76 is_server
, QuicSupportedVersions()),
80 MockConnection::~MockConnection() {
83 void MockConnection::AdvanceTime(QuicTime::Delta delta
) {
84 static_cast<MockHelper
*>(helper())->AdvanceTime(delta
);
87 QuicAckFrame
MakeAckFrameWithNackRanges(
88 size_t num_nack_ranges
, QuicPacketSequenceNumber least_unacked
) {
89 QuicAckFrame ack
= MakeAckFrame(2 * num_nack_ranges
+ least_unacked
);
90 // Add enough missing packets to get num_nack_ranges nack ranges.
91 for (QuicPacketSequenceNumber i
= 1; i
< 2 * num_nack_ranges
; i
+= 2) {
92 ack
.missing_packets
.insert(least_unacked
+ i
);
97 TestSession::TestSession(QuicConnection
* connection
,
98 const QuicConfig
& config
)
99 : QuicSession(connection
, config
),
100 crypto_stream_(NULL
) {
104 TestSession::~TestSession() {}
106 void TestSession::SetCryptoStream(QuicCryptoStream
* stream
) {
107 crypto_stream_
= stream
;
110 QuicCryptoStream
* TestSession::GetCryptoStream() {
111 return crypto_stream_
;
114 MockPacketWriter::MockPacketWriter() {
117 MockPacketWriter::~MockPacketWriter() {
120 MockQuicServerSessionVisitor::MockQuicServerSessionVisitor() {
123 MockQuicServerSessionVisitor::~MockQuicServerSessionVisitor() {
126 MockAckNotifierDelegate::MockAckNotifierDelegate() {
129 MockAckNotifierDelegate::~MockAckNotifierDelegate() {
132 TestWriterFactory::TestWriterFactory() : current_writer_(NULL
) {}
133 TestWriterFactory::~TestWriterFactory() {}
135 QuicPacketWriter
* TestWriterFactory::Create(QuicPacketWriter
* writer
,
136 QuicConnection
* connection
) {
137 return new PerConnectionPacketWriter(this, writer
, connection
);
140 void TestWriterFactory::OnPacketSent(WriteResult result
) {
141 if (current_writer_
!= NULL
&& result
.status
== WRITE_STATUS_ERROR
) {
142 current_writer_
->connection()->OnWriteError(result
.error_code
);
143 current_writer_
= NULL
;
147 void TestWriterFactory::Unregister(PerConnectionPacketWriter
* writer
) {
148 if (current_writer_
== writer
) {
149 current_writer_
= NULL
;
153 TestWriterFactory::PerConnectionPacketWriter::PerConnectionPacketWriter(
154 TestWriterFactory
* factory
,
155 QuicPacketWriter
* writer
,
156 QuicConnection
* connection
)
157 : QuicPerConnectionPacketWriter(writer
, connection
),
161 TestWriterFactory::PerConnectionPacketWriter::~PerConnectionPacketWriter() {
162 factory_
->Unregister(this);
165 WriteResult
TestWriterFactory::PerConnectionPacketWriter::WritePacket(
168 const IPAddressNumber
& self_address
,
169 const IPEndPoint
& peer_address
) {
170 // A DCHECK(factory_current_writer_ == NULL) would be wrong here -- this class
171 // may be used in a setting where connection()->OnPacketSent() is called in a
172 // different way, so TestWriterFactory::OnPacketSent might never be called.
173 factory_
->current_writer_
= this;
174 return QuicPerConnectionPacketWriter::WritePacket(buffer
,