2010-02-20 14:23:25 +00:00
|
|
|
#include "LpdMessageReceiver.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2011-11-05 09:05:23 +00:00
|
|
|
#include "TestUtil.h"
|
2010-02-20 14:23:25 +00:00
|
|
|
#include "Exception.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "LpdMessageReceiver.h"
|
|
|
|
#include "SocketCore.h"
|
|
|
|
#include "BtConstants.h"
|
|
|
|
#include "LpdMessage.h"
|
|
|
|
#include "LpdMessageDispatcher.h"
|
2010-11-14 07:17:55 +00:00
|
|
|
#include "Peer.h"
|
2010-02-20 14:23:25 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
class LpdMessageReceiverTest : public CppUnit::TestFixture {
|
2010-02-20 14:23:25 +00:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(LpdMessageReceiverTest);
|
|
|
|
CPPUNIT_TEST(testReceiveMessage);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
2015-12-27 09:39:47 +00:00
|
|
|
|
2010-02-20 14:23:25 +00:00
|
|
|
public:
|
|
|
|
void testReceiveMessage();
|
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(LpdMessageReceiverTest);
|
|
|
|
|
|
|
|
void LpdMessageReceiverTest::testReceiveMessage()
|
|
|
|
{
|
|
|
|
LpdMessageReceiver rcv(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT);
|
2010-02-22 15:58:05 +00:00
|
|
|
CPPUNIT_ASSERT(rcv.init(""));
|
2010-02-20 14:23:25 +00:00
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<SocketCore> sendsock(new SocketCore(SOCK_DGRAM));
|
2010-02-22 15:58:05 +00:00
|
|
|
sendsock->create(AF_INET);
|
2010-03-08 13:47:17 +00:00
|
|
|
// Mingw32 build needs to set interface explicitly.
|
|
|
|
sendsock->setMulticastInterface("");
|
2010-02-20 14:23:25 +00:00
|
|
|
sendsock->setMulticastTtl(0);
|
|
|
|
|
|
|
|
std::string infoHashString = "cd41c7fdddfd034a15a04d7ff881216e01c4ceaf";
|
2011-11-05 09:05:23 +00:00
|
|
|
std::string infoHash = fromHex(infoHashString);
|
2015-12-27 09:39:47 +00:00
|
|
|
std::string request = bittorrent::createLpdRequest(
|
|
|
|
LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT, infoHash, 6000);
|
2010-02-20 14:23:25 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
sendsock->writeData(request.c_str(), request.size(), LPD_MULTICAST_ADDR,
|
|
|
|
LPD_MULTICAST_PORT);
|
2010-02-20 14:23:25 +00:00
|
|
|
|
2010-03-08 11:59:12 +00:00
|
|
|
rcv.getSocket()->isReadable(5);
|
2013-07-03 15:56:29 +00:00
|
|
|
auto msg = rcv.receiveMessage();
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(msg);
|
2010-02-20 14:23:25 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("cd41c7fdddfd034a15a04d7ff881216e01c4ceaf"),
|
2011-10-17 15:47:14 +00:00
|
|
|
util::toHex(msg->infoHash));
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint16_t)6000, msg->peer->getPort());
|
2010-02-20 14:23:25 +00:00
|
|
|
|
|
|
|
// Bad infohash
|
|
|
|
std::string badInfoHashString = "cd41c7fdddfd034a15a04d7ff881216e01c4ce";
|
2015-12-27 09:39:47 +00:00
|
|
|
request = bittorrent::createLpdRequest(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
|
|
|
|
fromHex(badInfoHashString), 6000);
|
|
|
|
sendsock->writeData(request.c_str(), request.size(), LPD_MULTICAST_ADDR,
|
|
|
|
LPD_MULTICAST_PORT);
|
2010-02-20 14:23:25 +00:00
|
|
|
|
2010-03-02 15:34:10 +00:00
|
|
|
rcv.getSocket()->isReadable(5);
|
2010-02-22 16:11:33 +00:00
|
|
|
msg = rcv.receiveMessage();
|
2012-09-26 14:45:58 +00:00
|
|
|
CPPUNIT_ASSERT(!msg);
|
2010-02-20 14:23:25 +00:00
|
|
|
|
|
|
|
// Bad port
|
2015-12-27 09:39:47 +00:00
|
|
|
request = bittorrent::createLpdRequest(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
|
|
|
|
infoHash, 0);
|
|
|
|
sendsock->writeData(request.c_str(), request.size(), LPD_MULTICAST_ADDR,
|
|
|
|
LPD_MULTICAST_PORT);
|
2010-02-20 14:23:25 +00:00
|
|
|
|
2010-03-02 15:34:10 +00:00
|
|
|
rcv.getSocket()->isReadable(5);
|
2010-02-22 16:11:33 +00:00
|
|
|
msg = rcv.receiveMessage();
|
2012-09-26 14:45:58 +00:00
|
|
|
CPPUNIT_ASSERT(!msg);
|
2010-02-22 16:11:33 +00:00
|
|
|
|
|
|
|
// No data available
|
|
|
|
msg = rcv.receiveMessage();
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(!msg);
|
2010-02-20 14:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aria2
|