OLD | NEW |
| (Empty) |
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/tools/quic/test_tools/quic_test_utils.h" | |
6 | |
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" | |
11 | |
12 using base::StringPiece; | |
13 using net::test::MakeAckFrame; | |
14 using net::test::MockHelper; | |
15 using net::test::QuicConnectionPeer; | |
16 | |
17 namespace net { | |
18 namespace tools { | |
19 namespace test { | |
20 | |
21 QuicPacketWriter* NiceMockPacketWriterFactory::Create( | |
22 QuicConnection* /*connection*/) const { | |
23 return new testing::NiceMock<MockPacketWriter>(); | |
24 } | |
25 | |
26 MockConnection::MockConnection(bool is_server) | |
27 : QuicConnection(kTestConnectionId, | |
28 IPEndPoint(net::test::Loopback4(), kTestPort), | |
29 new testing::NiceMock<MockHelper>(), | |
30 NiceMockPacketWriterFactory(), | |
31 /* owns_writer= */ true, | |
32 is_server, | |
33 /* is_secure= */ false, | |
34 QuicSupportedVersions()), | |
35 helper_(helper()) { | |
36 } | |
37 | |
38 MockConnection::MockConnection(bool is_server, bool is_secure) | |
39 : QuicConnection(kTestConnectionId, | |
40 IPEndPoint(net::test::Loopback4(), kTestPort), | |
41 new testing::NiceMock<MockHelper>(), | |
42 NiceMockPacketWriterFactory(), | |
43 /* owns_writer= */ true, | |
44 is_server, | |
45 is_secure, | |
46 QuicSupportedVersions()), | |
47 helper_(helper()) { | |
48 } | |
49 | |
50 MockConnection::MockConnection(IPEndPoint address, | |
51 bool is_server) | |
52 : QuicConnection(kTestConnectionId, address, | |
53 new testing::NiceMock<MockHelper>(), | |
54 NiceMockPacketWriterFactory(), | |
55 /* owns_writer= */ true, | |
56 is_server, | |
57 /* is_secure= */ false, | |
58 QuicSupportedVersions()), | |
59 helper_(helper()) { | |
60 } | |
61 | |
62 MockConnection::MockConnection(QuicConnectionId connection_id, | |
63 bool is_server) | |
64 : QuicConnection(connection_id, | |
65 IPEndPoint(net::test::Loopback4(), kTestPort), | |
66 new testing::NiceMock<MockHelper>(), | |
67 NiceMockPacketWriterFactory(), | |
68 /* owns_writer= */ true, | |
69 is_server, | |
70 /* is_secure= */ false, | |
71 QuicSupportedVersions()), | |
72 helper_(helper()) { | |
73 } | |
74 | |
75 MockConnection::MockConnection(bool is_server, | |
76 const QuicVersionVector& supported_versions) | |
77 : QuicConnection(kTestConnectionId, | |
78 IPEndPoint(net::test::Loopback4(), kTestPort), | |
79 new testing::NiceMock<MockHelper>(), | |
80 NiceMockPacketWriterFactory(), | |
81 /* owns_writer= */ true, | |
82 is_server, | |
83 /* is_secure= */ false, | |
84 supported_versions), | |
85 helper_(helper()) { | |
86 } | |
87 | |
88 MockConnection::~MockConnection() { | |
89 } | |
90 | |
91 void MockConnection::AdvanceTime(QuicTime::Delta delta) { | |
92 static_cast<MockHelper*>(helper())->AdvanceTime(delta); | |
93 } | |
94 | |
95 QuicAckFrame MakeAckFrameWithNackRanges( | |
96 size_t num_nack_ranges, QuicPacketSequenceNumber least_unacked) { | |
97 QuicAckFrame ack = MakeAckFrame(2 * num_nack_ranges + least_unacked); | |
98 // Add enough missing packets to get num_nack_ranges nack ranges. | |
99 for (QuicPacketSequenceNumber i = 1; i < 2 * num_nack_ranges; i += 2) { | |
100 ack.missing_packets.insert(least_unacked + i); | |
101 } | |
102 return ack; | |
103 } | |
104 | |
105 TestSession::TestSession(QuicConnection* connection, const QuicConfig& config) | |
106 : QuicSession(connection, config), | |
107 crypto_stream_(nullptr) { | |
108 InitializeSession(); | |
109 } | |
110 | |
111 TestSession::~TestSession() {} | |
112 | |
113 void TestSession::SetCryptoStream(QuicCryptoStream* stream) { | |
114 crypto_stream_ = stream; | |
115 } | |
116 | |
117 QuicCryptoStream* TestSession::GetCryptoStream() { | |
118 return crypto_stream_; | |
119 } | |
120 | |
121 MockPacketWriter::MockPacketWriter() { | |
122 } | |
123 | |
124 MockPacketWriter::~MockPacketWriter() { | |
125 } | |
126 | |
127 MockQuicServerSessionVisitor::MockQuicServerSessionVisitor() { | |
128 } | |
129 | |
130 MockQuicServerSessionVisitor::~MockQuicServerSessionVisitor() { | |
131 } | |
132 | |
133 MockAckNotifierDelegate::MockAckNotifierDelegate() { | |
134 } | |
135 | |
136 MockAckNotifierDelegate::~MockAckNotifierDelegate() { | |
137 } | |
138 | |
139 TestWriterFactory::TestWriterFactory() : current_writer_(nullptr) {} | |
140 TestWriterFactory::~TestWriterFactory() {} | |
141 | |
142 QuicPacketWriter* TestWriterFactory::Create(QuicPacketWriter* writer, | |
143 QuicConnection* connection) { | |
144 return new PerConnectionPacketWriter(this, writer, connection); | |
145 } | |
146 | |
147 void TestWriterFactory::OnPacketSent(WriteResult result) { | |
148 if (current_writer_ != nullptr && result.status == WRITE_STATUS_ERROR) { | |
149 current_writer_->connection()->OnWriteError(result.error_code); | |
150 current_writer_ = nullptr; | |
151 } | |
152 } | |
153 | |
154 void TestWriterFactory::Unregister(PerConnectionPacketWriter* writer) { | |
155 if (current_writer_ == writer) { | |
156 current_writer_ = nullptr; | |
157 } | |
158 } | |
159 | |
160 TestWriterFactory::PerConnectionPacketWriter::PerConnectionPacketWriter( | |
161 TestWriterFactory* factory, | |
162 QuicPacketWriter* writer, | |
163 QuicConnection* connection) | |
164 : QuicPerConnectionPacketWriter(writer, connection), | |
165 factory_(factory) { | |
166 } | |
167 | |
168 TestWriterFactory::PerConnectionPacketWriter::~PerConnectionPacketWriter() { | |
169 factory_->Unregister(this); | |
170 } | |
171 | |
172 WriteResult TestWriterFactory::PerConnectionPacketWriter::WritePacket( | |
173 const char* buffer, | |
174 size_t buf_len, | |
175 const IPAddressNumber& self_address, | |
176 const IPEndPoint& peer_address) { | |
177 // A DCHECK(factory_current_writer_ == nullptr) would be wrong here -- this | |
178 // class may be used in a setting where connection()->OnPacketSent() is called | |
179 // in a different way, so TestWriterFactory::OnPacketSent might never be | |
180 // called. | |
181 factory_->current_writer_ = this; | |
182 return QuicPerConnectionPacketWriter::WritePacket(buffer, | |
183 buf_len, | |
184 self_address, | |
185 peer_address); | |
186 } | |
187 | |
188 } // namespace test | |
189 } // namespace tools | |
190 } // namespace net | |
OLD | NEW |