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/epoll_server/epoll_server.h"
11 #include "net/tools/quic/quic_epoll_connection_helper.h"
13 using base::StringPiece
;
14 using net::test::MakeAckFrame
;
15 using net::test::MockHelper
;
16 using net::test::QuicConnectionPeer
;
18 using testing::AnyNumber
;
19 using testing::Invoke
;
25 QuicAckFrame
MakeAckFrameWithNackRanges(
26 size_t num_nack_ranges
, QuicPacketSequenceNumber least_unacked
) {
27 QuicAckFrame ack
= MakeAckFrame(2 * num_nack_ranges
+ least_unacked
);
28 // Add enough missing packets to get num_nack_ranges nack ranges.
29 for (QuicPacketSequenceNumber i
= 1; i
< 2 * num_nack_ranges
; i
+= 2) {
30 ack
.missing_packets
.insert(least_unacked
+ i
);
35 TestSession::TestSession(QuicConnection
* connection
, const QuicConfig
& config
)
36 : QuicSession(connection
, config
),
37 crypto_stream_(nullptr) {
41 TestSession::~TestSession() {}
43 void TestSession::SetCryptoStream(QuicCryptoStream
* stream
) {
44 crypto_stream_
= stream
;
47 QuicCryptoStream
* TestSession::GetCryptoStream() {
48 return crypto_stream_
;
51 MockPacketWriter::MockPacketWriter() {
54 MockPacketWriter::~MockPacketWriter() {
57 MockQuicServerSessionVisitor::MockQuicServerSessionVisitor() {
60 MockQuicServerSessionVisitor::~MockQuicServerSessionVisitor() {
63 MockAckNotifierDelegate::MockAckNotifierDelegate() {
66 MockAckNotifierDelegate::~MockAckNotifierDelegate() {
69 TestWriterFactory::TestWriterFactory() : current_writer_(nullptr) {}
70 TestWriterFactory::~TestWriterFactory() {}
72 QuicPacketWriter
* TestWriterFactory::Create(QuicPacketWriter
* writer
,
73 QuicConnection
* connection
) {
74 return new PerConnectionPacketWriter(this, writer
, connection
);
77 void TestWriterFactory::OnPacketSent(WriteResult result
) {
78 if (current_writer_
!= nullptr && result
.status
== WRITE_STATUS_ERROR
) {
79 current_writer_
->connection()->OnWriteError(result
.error_code
);
80 current_writer_
= nullptr;
84 void TestWriterFactory::Unregister(PerConnectionPacketWriter
* writer
) {
85 if (current_writer_
== writer
) {
86 current_writer_
= nullptr;
90 TestWriterFactory::PerConnectionPacketWriter::PerConnectionPacketWriter(
91 TestWriterFactory
* factory
,
92 QuicPacketWriter
* writer
,
93 QuicConnection
* connection
)
94 : QuicPerConnectionPacketWriter(writer
, connection
),
98 TestWriterFactory::PerConnectionPacketWriter::~PerConnectionPacketWriter() {
99 factory_
->Unregister(this);
102 WriteResult
TestWriterFactory::PerConnectionPacketWriter::WritePacket(
105 const IPAddressNumber
& self_address
,
106 const IPEndPoint
& peer_address
) {
107 // A DCHECK(factory_current_writer_ == nullptr) would be wrong here -- this
108 // class may be used in a setting where connection()->OnPacketSent() is called
109 // in a different way, so TestWriterFactory::OnPacketSent might never be
111 factory_
->current_writer_
= this;
112 return QuicPerConnectionPacketWriter::WritePacket(buffer
,
118 MockTimeWaitListManager::MockTimeWaitListManager(
119 QuicPacketWriter
* writer
,
120 QuicServerSessionVisitor
* visitor
,
121 QuicConnectionHelperInterface
* helper
)
122 : QuicTimeWaitListManager(writer
, visitor
, helper
,
123 QuicSupportedVersions()) {
124 // Though AddConnectionIdToTimeWait is mocked, we want to retain its
126 EXPECT_CALL(*this, AddConnectionIdToTimeWait(_
, _
, _
)).Times(AnyNumber());
127 ON_CALL(*this, AddConnectionIdToTimeWait(_
, _
, _
))
129 Invoke(this, &MockTimeWaitListManager::
130 QuicTimeWaitListManager_AddConnectionIdToTimeWait
));
133 MockTimeWaitListManager::~MockTimeWaitListManager() {