Chromium Code Reviews| 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/quic/test_tools/quic_test_utils.h" | 5 #include "net/quic/test_tools/quic_test_utils.h" |
| 6 | 6 |
| 7 #include "base/memory/singleton.h" | |
| 7 #include "base/sha1.h" | 8 #include "base/sha1.h" |
| 8 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 9 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 10 #include "net/quic/crypto/crypto_framer.h" | 11 #include "net/quic/crypto/crypto_framer.h" |
| 11 #include "net/quic/crypto/crypto_handshake.h" | 12 #include "net/quic/crypto/crypto_handshake.h" |
| 12 #include "net/quic/crypto/crypto_utils.h" | 13 #include "net/quic/crypto/crypto_utils.h" |
| 13 #include "net/quic/crypto/null_encrypter.h" | 14 #include "net/quic/crypto/null_encrypter.h" |
| 14 #include "net/quic/crypto/quic_decrypter.h" | 15 #include "net/quic/crypto/quic_decrypter.h" |
| 15 #include "net/quic/crypto/quic_encrypter.h" | 16 #include "net/quic/crypto/quic_encrypter.h" |
| 16 #include "net/quic/quic_framer.h" | 17 #include "net/quic/quic_framer.h" |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 } | 215 } |
| 215 | 216 |
| 216 QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) { | 217 QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) { |
| 217 return new TestAlarm(delegate); | 218 return new TestAlarm(delegate); |
| 218 } | 219 } |
| 219 | 220 |
| 220 void MockHelper::AdvanceTime(QuicTime::Delta delta) { | 221 void MockHelper::AdvanceTime(QuicTime::Delta delta) { |
| 221 clock_.AdvanceTime(delta); | 222 clock_.AdvanceTime(delta); |
| 222 } | 223 } |
| 223 | 224 |
| 225 namespace { | |
| 226 class NiceMockPacketWriterFactory | |
| 227 : public QuicConnection::PacketWriterFactory { | |
| 228 public: | |
| 229 virtual ~NiceMockPacketWriterFactory() {} | |
| 230 | |
| 231 static NiceMockPacketWriterFactory* GetInstance() { | |
| 232 return Singleton<NiceMockPacketWriterFactory>::get(); | |
| 233 } | |
| 234 | |
| 235 virtual QuicPacketWriter* Create( | |
| 236 QuicConnection* /*connection*/) const override { | |
| 237 return new testing::NiceMock<MockPacketWriter>(); | |
| 238 } | |
| 239 | |
| 240 private: | |
| 241 friend struct DefaultSingletonTraits<NiceMockPacketWriterFactory>; | |
| 242 NiceMockPacketWriterFactory() {} | |
| 243 DISALLOW_COPY_AND_ASSIGN(NiceMockPacketWriterFactory); | |
| 244 }; | |
| 245 } // namespace | |
| 246 | |
| 224 MockConnection::MockConnection(bool is_server) | 247 MockConnection::MockConnection(bool is_server) |
| 225 : QuicConnection(kTestConnectionId, | 248 : QuicConnection(kTestConnectionId, |
| 226 IPEndPoint(TestPeerIPAddress(), kTestPort), | 249 IPEndPoint(TestPeerIPAddress(), kTestPort), |
| 227 new testing::NiceMock<MockHelper>(), | 250 new testing::NiceMock<MockHelper>(), |
| 228 new testing::NiceMock<MockPacketWriter>(), | 251 *NiceMockPacketWriterFactory::GetInstance(), |
|
wtc
2014/08/13 21:01:34
IMPORTANT: if we can rely on the API contract that
dmz
2014/08/14 17:22:42
Good point, that would be simpler. I'll put this i
| |
| 229 true /* owns_writer */, | 252 /* owns_writer= */ true, |
| 230 is_server, QuicSupportedVersions()), | 253 is_server, QuicSupportedVersions()), |
| 231 helper_(helper()) { | 254 helper_(helper()) { |
| 232 } | 255 } |
| 233 | 256 |
| 234 MockConnection::MockConnection(IPEndPoint address, | 257 MockConnection::MockConnection(IPEndPoint address, |
| 235 bool is_server) | 258 bool is_server) |
| 236 : QuicConnection(kTestConnectionId, address, | 259 : QuicConnection(kTestConnectionId, address, |
| 237 new testing::NiceMock<MockHelper>(), | 260 new testing::NiceMock<MockHelper>(), |
| 238 new testing::NiceMock<MockPacketWriter>(), | 261 *NiceMockPacketWriterFactory::GetInstance(), |
| 239 true /* owns_writer */, | 262 /* owns_writer= */ true, |
| 240 is_server, QuicSupportedVersions()), | 263 is_server, QuicSupportedVersions()), |
| 241 helper_(helper()) { | 264 helper_(helper()) { |
| 242 } | 265 } |
| 243 | 266 |
| 244 MockConnection::MockConnection(QuicConnectionId connection_id, | 267 MockConnection::MockConnection(QuicConnectionId connection_id, |
| 245 bool is_server) | 268 bool is_server) |
| 246 : QuicConnection(connection_id, | 269 : QuicConnection(connection_id, |
| 247 IPEndPoint(TestPeerIPAddress(), kTestPort), | 270 IPEndPoint(TestPeerIPAddress(), kTestPort), |
| 248 new testing::NiceMock<MockHelper>(), | 271 new testing::NiceMock<MockHelper>(), |
| 249 new testing::NiceMock<MockPacketWriter>(), | 272 *NiceMockPacketWriterFactory::GetInstance(), |
| 250 true /* owns_writer */, | 273 /* owns_writer= */ true, |
| 251 is_server, QuicSupportedVersions()), | 274 is_server, QuicSupportedVersions()), |
| 252 helper_(helper()) { | 275 helper_(helper()) { |
| 253 } | 276 } |
| 254 | 277 |
| 255 MockConnection::MockConnection(bool is_server, | 278 MockConnection::MockConnection(bool is_server, |
| 256 const QuicVersionVector& supported_versions) | 279 const QuicVersionVector& supported_versions) |
| 257 : QuicConnection(kTestConnectionId, | 280 : QuicConnection(kTestConnectionId, |
| 258 IPEndPoint(TestPeerIPAddress(), kTestPort), | 281 IPEndPoint(TestPeerIPAddress(), kTestPort), |
| 259 new testing::NiceMock<MockHelper>(), | 282 new testing::NiceMock<MockHelper>(), |
| 260 new testing::NiceMock<MockPacketWriter>(), | 283 *NiceMockPacketWriterFactory::GetInstance(), |
| 261 true /* owns_writer */, | 284 /* owns_writer= */ true, |
| 262 is_server, supported_versions), | 285 is_server, supported_versions), |
| 263 helper_(helper()) { | 286 helper_(helper()) { |
| 264 } | 287 } |
| 265 | 288 |
| 266 MockConnection::~MockConnection() { | 289 MockConnection::~MockConnection() { |
| 267 } | 290 } |
| 268 | 291 |
| 269 void MockConnection::AdvanceTime(QuicTime::Delta delta) { | 292 void MockConnection::AdvanceTime(QuicTime::Delta delta) { |
| 270 static_cast<MockHelper*>(helper())->AdvanceTime(delta); | 293 static_cast<MockHelper*>(helper())->AdvanceTime(delta); |
| 271 } | 294 } |
| (...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 599 kInitialSessionFlowControlWindowForTest); | 622 kInitialSessionFlowControlWindowForTest); |
| 600 return config; | 623 return config; |
| 601 } | 624 } |
| 602 | 625 |
| 603 QuicVersionVector SupportedVersions(QuicVersion version) { | 626 QuicVersionVector SupportedVersions(QuicVersion version) { |
| 604 QuicVersionVector versions; | 627 QuicVersionVector versions; |
| 605 versions.push_back(version); | 628 versions.push_back(version); |
| 606 return versions; | 629 return versions; |
| 607 } | 630 } |
| 608 | 631 |
| 632 TestWriterFactory::TestWriterFactory() : current_writer_(NULL) {} | |
| 633 TestWriterFactory::~TestWriterFactory() {} | |
| 634 | |
| 635 QuicPacketWriter* TestWriterFactory::Create(QuicServerPacketWriter* writer, | |
| 636 QuicConnection* connection) { | |
| 637 return new PerConnectionPacketWriter(this, writer, connection); | |
| 638 } | |
| 639 | |
| 640 void TestWriterFactory::OnPacketSent(WriteResult result) { | |
| 641 if (current_writer_ != NULL) { | |
| 642 current_writer_->connection()->OnPacketSent(result); | |
| 643 current_writer_ = NULL; | |
| 644 } | |
| 645 } | |
| 646 | |
| 647 void TestWriterFactory::Unregister(PerConnectionPacketWriter* writer) { | |
| 648 if (current_writer_ == writer) { | |
| 649 current_writer_ = NULL; | |
| 650 } | |
| 651 } | |
| 652 | |
| 653 TestWriterFactory::PerConnectionPacketWriter::PerConnectionPacketWriter( | |
| 654 TestWriterFactory* factory, | |
| 655 QuicServerPacketWriter* writer, | |
| 656 QuicConnection* connection) | |
| 657 : QuicPerConnectionPacketWriter(writer, connection), | |
| 658 factory_(factory) { | |
| 659 } | |
| 660 | |
| 661 TestWriterFactory::PerConnectionPacketWriter::~PerConnectionPacketWriter() { | |
| 662 factory_->Unregister(this); | |
| 663 } | |
| 664 | |
| 665 WriteResult TestWriterFactory::PerConnectionPacketWriter::WritePacket( | |
| 666 const char* buffer, | |
| 667 size_t buf_len, | |
| 668 const IPAddressNumber& self_address, | |
| 669 const IPEndPoint& peer_address) { | |
| 670 // A DCHECK(factory_current_writer_ == NULL) would be wrong here -- this class | |
| 671 // may be used in a setting where connection()->OnPacketSent() is called in a | |
| 672 // different way, so TestWriterFactory::OnPacketSent might never be called. | |
| 673 factory_->current_writer_ = this; | |
| 674 return QuicPerConnectionPacketWriter::WritePacket(buffer, | |
| 675 buf_len, | |
| 676 self_address, | |
| 677 peer_address); | |
| 678 } | |
| 679 | |
| 609 } // namespace test | 680 } // namespace test |
| 610 } // namespace net | 681 } // namespace net |
| OLD | NEW |