mirror of
https://github.com/aria2/aria2.git
synced 2025-02-26 08:22:11 +00:00
Made BtRegistry non-static object. Now DownloadEngine has a reference to its object. * src/AbstractBtMessage.cc * src/ActivePeerConnectionCommand.cc * src/ActivePeerConnectionCommand.h * src/BtContextAwareCommand.cc * src/BtContextAwareCommand.h * src/BtExtendedMessage.cc * src/BtExtendedMessage.h * src/BtInterestedMessage.cc * src/BtInterestedMessage.h * src/BtNotInterestedMessage.cc * src/BtNotInterestedMessage.h * src/BtRegistry.cc * src/BtRegistry.h * src/BtSeederStateChoke.cc * src/BtSetup.cc * src/DHTGetPeersCommand.cc * src/DHTGetPeersCommand.h * src/DHTPeerAnnounceEntry.cc * src/DHTPeerAnnounceEntry.h * src/DHTPeerAnnounceStorage.cc * src/DHTPeerAnnounceStorage.h * src/DHTPeerLookupTask.cc * src/DHTPeerLookupTask.h * src/DHTSetup.cc * src/DHTSetup.h * src/DHTTaskFactory.h * src/DHTTaskFactoryImpl.cc * src/DHTTaskFactoryImpl.h * src/DefaultBtAnnounce.cc * src/DefaultBtInteractive.cc * src/DefaultBtInteractive.h * src/DefaultBtMessageDispatcher.cc * src/DefaultBtMessageDispatcher.h * src/DefaultBtMessageFactory.cc * src/DefaultBtMessageFactory.h * src/DefaultBtMessageReceiver.cc * src/DefaultBtProgressInfoFile.cc * src/DefaultBtProgressInfoFile.h * src/DefaultBtRequestFactory.cc * src/DefaultBtRequestFactory.h * src/DefaultExtensionMessageFactory.cc * src/DefaultExtensionMessageFactory.h * src/DefaultPeerStorage.cc * src/DefaultPeerStorage.h * src/DownloadEngine.cc * src/DownloadEngine.h * src/InitiatorMSEHandshakeCommand.cc * src/InitiatorMSEHandshakeCommand.h * src/MSEHandshake.cc * src/MSEHandshake.h * src/Makefile.am * src/Makefile.in * src/PeerChokeCommand.cc * src/PeerChokeCommand.h * src/PeerInitiateConnectionCommand.cc * src/PeerInitiateConnectionCommand.h * src/PeerInteractionCommand.cc * src/PeerInteractionCommand.h * src/PeerObject.h * src/PeerReceiveHandshakeCommand.cc * src/PeerReceiveHandshakeCommand.h * src/ReceiverMSEHandshakeCommand.cc * src/RequestGroup.cc * src/RequestGroup.h * src/RequestGroupMan.cc * src/RequestGroupMan.h * src/SeedCheckCommand.cc * src/SeedCheckCommand.h * src/ShareRatioSeedCriteria.h * src/SimpleBtMessage.cc * src/TrackerWatcherCommand.cc * src/TrackerWatcherCommand.h * src/UTPexExtensionMessage.cc * src/UTPexExtensionMessage.h * test/BtCancelMessageTest.cc * test/BtChokeMessageTest.cc * test/BtExtendedMessageTest.cc * test/BtInterestedMessageTest.cc * test/BtNotInterestedMessageTest.cc * test/BtPieceMessageTest.cc * test/BtPortMessageTest.cc * test/BtRegistryTest.cc * test/BtRejectMessageTest.cc * test/BtRequestMessageTest.cc * test/DHTPeerAnnounceEntryTest.cc * test/DHTPeerAnnounceStorageTest.cc * test/DefaultBtMessageDispatcherTest.cc * test/DefaultBtMessageFactoryTest.cc * test/DefaultBtProgressInfoFileTest.cc * test/DefaultBtRequestFactoryTest.cc * test/DefaultExtensionMessageFactoryTest.cc * test/MSEHandshakeTest.cc * test/MockDHTTaskFactory.h * test/ShareRatioSeedCriteriaTest.cc * test/UTPexExtensionMessageTest.cc
106 lines
3.0 KiB
C++
106 lines
3.0 KiB
C++
#include "DefaultBtMessageFactory.h"
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
#include "Peer.h"
|
|
#include "PeerMessageUtil.h"
|
|
#include "MockBtContext.h"
|
|
#include "MockExtensionMessageFactory.h"
|
|
#include "BtExtendedMessage.h"
|
|
#include "BtPortMessage.h"
|
|
#include "Exception.h"
|
|
#include "FileEntry.h"
|
|
|
|
namespace aria2 {
|
|
|
|
class DefaultBtMessageFactoryTest:public CppUnit::TestFixture {
|
|
|
|
CPPUNIT_TEST_SUITE(DefaultBtMessageFactoryTest);
|
|
CPPUNIT_TEST(testCreateBtMessage_BtExtendedMessage);
|
|
CPPUNIT_TEST(testCreatePortMessage);
|
|
CPPUNIT_TEST_SUITE_END();
|
|
private:
|
|
SharedHandle<MockBtContext> _btContext;
|
|
SharedHandle<Peer> _peer;
|
|
SharedHandle<MockExtensionMessageFactory> _exmsgFactory;
|
|
SharedHandle<DefaultBtMessageFactory> _factory;
|
|
public:
|
|
void setUp()
|
|
{
|
|
_btContext.reset(new MockBtContext());
|
|
unsigned char infohash[20];
|
|
memset(infohash, 0, sizeof(infohash));
|
|
_btContext->setInfoHash(infohash);
|
|
|
|
_peer.reset(new Peer("192.168.0.1", 6969));
|
|
_peer->allocateSessionResource(1024, 1024*1024);
|
|
_peer->setExtendedMessagingEnabled(true);
|
|
|
|
_exmsgFactory.reset(new MockExtensionMessageFactory());
|
|
|
|
_factory.reset(new DefaultBtMessageFactory());
|
|
_factory->setBtContext(_btContext);
|
|
_factory->setPeer(_peer);
|
|
_factory->setExtensionMessageFactory(_exmsgFactory);
|
|
}
|
|
|
|
void testCreateBtMessage_BtExtendedMessage();
|
|
void testCreatePortMessage();
|
|
};
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(DefaultBtMessageFactoryTest);
|
|
|
|
void DefaultBtMessageFactoryTest::testCreateBtMessage_BtExtendedMessage()
|
|
{
|
|
// payload:{4:name3:foo}->11bytes
|
|
std::string payload = "4:name3:foo";
|
|
char msg[17];// 6+11bytes
|
|
PeerMessageUtil::createPeerMessageString((unsigned char*)msg, sizeof(msg),
|
|
13, 20);
|
|
msg[5] = 1; // Set dummy extended message ID 1
|
|
memcpy(msg+6, payload.c_str(), payload.size());
|
|
|
|
SharedHandle<BtExtendedMessage> m
|
|
(dynamic_pointer_cast<BtExtendedMessage>
|
|
(_factory->createBtMessage((const unsigned char*)msg+4, sizeof(msg))));
|
|
|
|
try {
|
|
// disable extended messaging
|
|
_peer->setExtendedMessagingEnabled(false);
|
|
_factory->createBtMessage((const unsigned char*)msg+4, sizeof(msg));
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
|
} catch(Exception& e) {
|
|
std::cerr << e.stackTrace() << std::endl;
|
|
}
|
|
}
|
|
|
|
void DefaultBtMessageFactoryTest::testCreatePortMessage()
|
|
{
|
|
{
|
|
unsigned char data[7];
|
|
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 3, 9);
|
|
PeerMessageUtil::setShortIntParam(&data[5], 6881);
|
|
try {
|
|
SharedHandle<BtPortMessage> m
|
|
(dynamic_pointer_cast<BtPortMessage>
|
|
(_factory->createBtMessage(&data[4], sizeof(data)-4)));
|
|
CPPUNIT_ASSERT(!m.isNull());
|
|
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, m->getPort());
|
|
} catch(Exception& e) {
|
|
CPPUNIT_FAIL(e.stackTrace());
|
|
}
|
|
}
|
|
{
|
|
SharedHandle<BtPortMessage> m
|
|
(dynamic_pointer_cast<BtPortMessage>(_factory->createPortMessage(6881)));
|
|
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, m->getPort());
|
|
}
|
|
}
|
|
|
|
} // namespace aria2
|