aria2/test/DefaultBtMessageFactoryTest.cc
Tatsuhiro Tsujikawa 99654e4160 2009-09-29 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Move all functions in PeerMessageUtil to bittorrent_helper.
	* src/BtBitfieldMessage.cc
	* src/BtBitfieldMessageValidator.h
	* src/BtExtendedMessage.cc
	* src/BtHandshakeMessage.cc
	* src/BtHandshakeMessageValidator.h
	* src/BtPieceMessage.cc
	* src/BtPieceMessageValidator.h
	* src/BtPortMessage.cc
	* src/DHTFindNodeReplyMessage.cc
	* src/DHTGetPeersReplyMessage.cc
	* src/DHTMessageFactoryImpl.cc
	* src/DHTRoutingTableDeserializer.cc
	* src/DHTRoutingTableSerializer.cc
	* src/DHTTokenTracker.cc
	* src/DefaultBtMessageFactory.cc
	* src/IndexBtMessage.cc
	* src/IndexBtMessage.h
	* src/IndexBtMessageValidator.h
	* src/Makefile.am
	* src/PeerMessageUtil.cc: Removed.
	* src/PeerMessageUtil.h: Removed.
	* src/RangeBtMessage.cc
	* src/RangeBtMessage.h
	* src/RangeBtMessageValidator.h
	* src/UTPexExtensionMessage.cc
	* src/ZeroBtMessage.cc
	* src/ZeroBtMessage.h
	* src/bittorrent_helper.cc
	* src/bittorrent_helper.h
	* test/BittorrentHelperTest.cc
	* test/BtAllowedFastMessageTest.cc
	* test/BtBitfieldMessageTest.cc
	* test/BtCancelMessageTest.cc
	* test/BtChokeMessageTest.cc
	* test/BtExtendedMessageTest.cc
	* test/BtHandshakeMessageTest.cc
	* test/BtHaveAllMessageTest.cc
	* test/BtHaveMessageTest.cc
	* test/BtHaveNoneMessageTest.cc
	* test/BtInterestedMessageTest.cc
	* test/BtNotInterestedMessageTest.cc
	* test/BtPieceMessageTest.cc
	* test/BtPortMessageTest.cc
	* test/BtRejectMessageTest.cc
	* test/BtRequestMessageTest.cc
	* test/BtSuggestPieceMessageTest.cc
	* test/BtUnchokeMessageTest.cc
	* test/DHTFindNodeReplyMessageTest.cc
	* test/DHTGetPeersReplyMessageTest.cc
	* test/DHTMessageFactoryImplTest.cc
	* test/DHTRoutingTableDeserializerTest.cc
	* test/DHTRoutingTableSerializerTest.cc
	* test/DefaultBtMessageFactoryTest.cc
	* test/DefaultExtensionMessageFactoryTest.cc
	* test/Makefile.am
	* test/PeerListProcessorTest.cc
	* test/PeerMessageUtilTest.cc: Removed.
	* test/UTPexExtensionMessageTest.cc
2009-09-29 14:52:42 +00:00

102 lines
2.9 KiB
C++

#include "DefaultBtMessageFactory.h"
#include <cstring>
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
#include "Peer.h"
#include "bittorrent_helper.h"
#include "DownloadContext.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<DownloadContext> _dctx;
SharedHandle<Peer> _peer;
SharedHandle<MockExtensionMessageFactory> _exmsgFactory;
SharedHandle<DefaultBtMessageFactory> _factory;
public:
void setUp()
{
_dctx.reset(new DownloadContext());
_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->setDownloadContext(_dctx);
_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
bittorrent::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];
bittorrent::createPeerMessageString(data, sizeof(data), 3, 9);
bittorrent::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