OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/tools/quic/test_tools/quic_test_utils.h" | 5 #include "net/tools/quic/test_tools/quic_test_utils.h" |
6 | 6 |
| 7 #include "base/memory/singleton.h" |
7 #include "net/quic/quic_connection.h" | 8 #include "net/quic/quic_connection.h" |
8 #include "net/quic/test_tools/quic_connection_peer.h" | 9 #include "net/quic/test_tools/quic_connection_peer.h" |
9 #include "net/quic/test_tools/quic_test_utils.h" | 10 #include "net/quic/test_tools/quic_test_utils.h" |
10 #include "net/tools/quic/quic_epoll_connection_helper.h" | 11 #include "net/tools/quic/quic_epoll_connection_helper.h" |
11 | 12 |
12 using base::StringPiece; | 13 using base::StringPiece; |
13 using net::test::MakeAckFrame; | 14 using net::test::MakeAckFrame; |
14 using net::test::MockHelper; | 15 using net::test::MockHelper; |
15 using net::test::QuicConnectionPeer; | 16 using net::test::QuicConnectionPeer; |
16 | 17 |
17 namespace net { | 18 namespace net { |
18 namespace tools { | 19 namespace tools { |
19 namespace test { | 20 namespace test { |
20 | 21 |
| 22 namespace { |
| 23 class NiceMockPacketWriterFactory |
| 24 : public QuicConnection::PacketWriterFactory { |
| 25 public: |
| 26 virtual ~NiceMockPacketWriterFactory() {} |
| 27 |
| 28 static NiceMockPacketWriterFactory* GetInstance() { |
| 29 return Singleton<NiceMockPacketWriterFactory>::get(); |
| 30 } |
| 31 |
| 32 virtual QuicPacketWriter* Create( |
| 33 QuicConnection* /*connection*/) const override { |
| 34 return new testing::NiceMock<MockPacketWriter>(); |
| 35 } |
| 36 |
| 37 private: |
| 38 friend struct DefaultSingletonTraits<NiceMockPacketWriterFactory>; |
| 39 NiceMockPacketWriterFactory() {} |
| 40 DISALLOW_COPY_AND_ASSIGN(NiceMockPacketWriterFactory); |
| 41 }; |
| 42 } // namespace |
| 43 |
21 MockConnection::MockConnection(bool is_server) | 44 MockConnection::MockConnection(bool is_server) |
22 : QuicConnection(kTestConnectionId, | 45 : QuicConnection(kTestConnectionId, |
23 IPEndPoint(net::test::Loopback4(), kTestPort), | 46 IPEndPoint(net::test::Loopback4(), kTestPort), |
24 new testing::NiceMock<MockHelper>(), | 47 new testing::NiceMock<MockHelper>(), |
25 new testing::NiceMock<MockPacketWriter>(), | 48 *NiceMockPacketWriterFactory::GetInstance(), |
26 true /* owns_writer */, | 49 /* owns_writer= */ true, |
27 is_server, QuicSupportedVersions()), | 50 is_server, QuicSupportedVersions()), |
28 helper_(helper()) { | 51 helper_(helper()) { |
29 } | 52 } |
30 | 53 |
31 MockConnection::MockConnection(IPEndPoint address, | 54 MockConnection::MockConnection(IPEndPoint address, |
32 bool is_server) | 55 bool is_server) |
33 : QuicConnection(kTestConnectionId, address, | 56 : QuicConnection(kTestConnectionId, address, |
34 new testing::NiceMock<MockHelper>(), | 57 new testing::NiceMock<MockHelper>(), |
35 new testing::NiceMock<MockPacketWriter>(), | 58 *NiceMockPacketWriterFactory::GetInstance(), |
36 true /* owns_writer */, | 59 /* owns_writer= */ true, |
37 is_server, QuicSupportedVersions()), | 60 is_server, QuicSupportedVersions()), |
38 helper_(helper()) { | 61 helper_(helper()) { |
39 } | 62 } |
40 | 63 |
41 MockConnection::MockConnection(QuicConnectionId connection_id, | 64 MockConnection::MockConnection(QuicConnectionId connection_id, |
42 bool is_server) | 65 bool is_server) |
43 : QuicConnection(connection_id, | 66 : QuicConnection(connection_id, |
44 IPEndPoint(net::test::Loopback4(), kTestPort), | 67 IPEndPoint(net::test::Loopback4(), kTestPort), |
45 new testing::NiceMock<MockHelper>(), | 68 new testing::NiceMock<MockHelper>(), |
46 new testing::NiceMock<MockPacketWriter>(), | 69 *NiceMockPacketWriterFactory::GetInstance(), |
47 true /* owns_writer */, | 70 /* owns_writer= */ true, |
48 is_server, QuicSupportedVersions()), | 71 is_server, QuicSupportedVersions()), |
49 helper_(helper()) { | 72 helper_(helper()) { |
50 } | 73 } |
51 | 74 |
52 MockConnection::MockConnection(bool is_server, | 75 MockConnection::MockConnection(bool is_server, |
53 const QuicVersionVector& supported_versions) | 76 const QuicVersionVector& supported_versions) |
54 : QuicConnection(kTestConnectionId, | 77 : QuicConnection(kTestConnectionId, |
55 IPEndPoint(net::test::Loopback4(), kTestPort), | 78 IPEndPoint(net::test::Loopback4(), kTestPort), |
56 new testing::NiceMock<MockHelper>(), | 79 new testing::NiceMock<MockHelper>(), |
57 new testing::NiceMock<MockPacketWriter>(), | 80 *NiceMockPacketWriterFactory::GetInstance(), |
58 true /* owns_writer */, | 81 /* owns_writer= */ true, |
59 is_server, QuicSupportedVersions()), | 82 is_server, QuicSupportedVersions()), |
60 helper_(helper()) { | 83 helper_(helper()) { |
61 } | 84 } |
62 | 85 |
63 MockConnection::~MockConnection() { | 86 MockConnection::~MockConnection() { |
64 } | 87 } |
65 | 88 |
66 void MockConnection::AdvanceTime(QuicTime::Delta delta) { | 89 void MockConnection::AdvanceTime(QuicTime::Delta delta) { |
67 static_cast<MockHelper*>(helper())->AdvanceTime(delta); | 90 static_cast<MockHelper*>(helper())->AdvanceTime(delta); |
68 } | 91 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 | 128 |
106 MockQuicServerSessionVisitor::~MockQuicServerSessionVisitor() { | 129 MockQuicServerSessionVisitor::~MockQuicServerSessionVisitor() { |
107 } | 130 } |
108 | 131 |
109 MockAckNotifierDelegate::MockAckNotifierDelegate() { | 132 MockAckNotifierDelegate::MockAckNotifierDelegate() { |
110 } | 133 } |
111 | 134 |
112 MockAckNotifierDelegate::~MockAckNotifierDelegate() { | 135 MockAckNotifierDelegate::~MockAckNotifierDelegate() { |
113 } | 136 } |
114 | 137 |
| 138 TestWriterFactory::TestWriterFactory() : current_writer_(NULL) {} |
| 139 TestWriterFactory::~TestWriterFactory() {} |
| 140 |
| 141 QuicPacketWriter* TestWriterFactory::Create(QuicPacketWriter* writer, |
| 142 QuicConnection* connection) { |
| 143 return new PerConnectionPacketWriter(this, writer, connection); |
| 144 } |
| 145 |
| 146 void TestWriterFactory::OnPacketSent(WriteResult result) { |
| 147 if (current_writer_ != NULL) { |
| 148 current_writer_->connection()->OnPacketSent(result); |
| 149 current_writer_ = NULL; |
| 150 } |
| 151 } |
| 152 |
| 153 void TestWriterFactory::Unregister(PerConnectionPacketWriter* writer) { |
| 154 if (current_writer_ == writer) { |
| 155 current_writer_ = NULL; |
| 156 } |
| 157 } |
| 158 |
| 159 TestWriterFactory::PerConnectionPacketWriter::PerConnectionPacketWriter( |
| 160 TestWriterFactory* factory, |
| 161 QuicPacketWriter* writer, |
| 162 QuicConnection* connection) |
| 163 : QuicPerConnectionPacketWriter(writer, connection), |
| 164 factory_(factory) { |
| 165 } |
| 166 |
| 167 TestWriterFactory::PerConnectionPacketWriter::~PerConnectionPacketWriter() { |
| 168 factory_->Unregister(this); |
| 169 } |
| 170 |
| 171 WriteResult TestWriterFactory::PerConnectionPacketWriter::WritePacket( |
| 172 const char* buffer, |
| 173 size_t buf_len, |
| 174 const IPAddressNumber& self_address, |
| 175 const IPEndPoint& peer_address) { |
| 176 // A DCHECK(factory_current_writer_ == NULL) would be wrong here -- this class |
| 177 // may be used in a setting where connection()->OnPacketSent() is called in a |
| 178 // different way, so TestWriterFactory::OnPacketSent might never be called. |
| 179 factory_->current_writer_ = this; |
| 180 return QuicPerConnectionPacketWriter::WritePacket(buffer, |
| 181 buf_len, |
| 182 self_address, |
| 183 peer_address); |
| 184 } |
| 185 |
115 } // namespace test | 186 } // namespace test |
116 } // namespace tools | 187 } // namespace tools |
117 } // namespace net | 188 } // namespace net |
OLD | NEW |