#include "DefaultExtensionMessageFactory.h" #include #include #include "Peer.h" #include "MockPeerStorage.h" #include "bittorrent_helper.h" #include "HandshakeExtensionMessage.h" #include "UTPexExtensionMessage.h" #include "Exception.h" #include "FileEntry.h" #include "ExtensionMessageRegistry.h" namespace aria2 { class DefaultExtensionMessageFactoryTest:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(DefaultExtensionMessageFactoryTest); CPPUNIT_TEST(testCreateMessage_unknown); CPPUNIT_TEST(testCreateMessage_Handshake); CPPUNIT_TEST(testCreateMessage_UTPex); CPPUNIT_TEST_SUITE_END(); private: SharedHandle _peerStorage; SharedHandle _peer; SharedHandle _factory; SharedHandle _registry; public: void setUp() { _peerStorage.reset(new MockPeerStorage()); _peer.reset(new Peer("192.168.0.1", 6969)); _peer->allocateSessionResource(1024, 1024*1024); _peer->setExtension("ut_pex", 1); _registry.reset(new ExtensionMessageRegistry()); _factory.reset(new DefaultExtensionMessageFactory()); _factory->setPeerStorage(_peerStorage); _factory->setPeer(_peer); _factory->setExtensionMessageRegistry(_registry); } void testCreateMessage_unknown(); void testCreateMessage_Handshake(); void testCreateMessage_UTPex(); }; CPPUNIT_TEST_SUITE_REGISTRATION(DefaultExtensionMessageFactoryTest); void DefaultExtensionMessageFactoryTest::testCreateMessage_unknown() { _peer->setExtension("foo", 255); char id[1] = { 255 }; std::string data = std::string(&id[0], &id[1]); try { // this test fails because localhost doesn't have extension id = 255. _factory->createMessage (reinterpret_cast(data.c_str()), data.size()); CPPUNIT_FAIL("exception must be thrown."); } catch(Exception& e) { std::cerr << e.stackTrace() << std::endl; } } void DefaultExtensionMessageFactoryTest::testCreateMessage_Handshake() { char id[1] = { 0 }; std::string data = std::string(&id[0], &id[1])+"d1:v5:aria2e"; SharedHandle m (dynamic_pointer_cast (_factory->createMessage (reinterpret_cast(data.c_str()), data.size()))); CPPUNIT_ASSERT_EQUAL(std::string("aria2"), m->getClientVersion()); } void DefaultExtensionMessageFactoryTest::testCreateMessage_UTPex() { unsigned char c1[6]; unsigned char c2[6]; unsigned char c3[6]; unsigned char c4[6]; bittorrent::createcompact(c1, "192.168.0.1", 6881); bittorrent::createcompact(c2, "10.1.1.2", 9999); bittorrent::createcompact(c3, "192.168.0.2", 6882); bittorrent::createcompact(c4, "10.1.1.3",10000); char id[1] = { _registry->getExtensionMessageID("ut_pex") }; std::string data = std::string(&id[0], &id[1])+"d5:added12:"+ std::string(&c1[0], &c1[6])+std::string(&c2[0], &c2[6])+ "7:added.f2:207:dropped12:"+ std::string(&c3[0], &c3[6])+std::string(&c4[0], &c4[6])+ "e"; SharedHandle m (dynamic_pointer_cast (_factory->createMessage (reinterpret_cast(data.c_str()), data.size()))); CPPUNIT_ASSERT_EQUAL(_registry->getExtensionMessageID("ut_pex"), m->getExtensionMessageID()); } } // namespace aria2