1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#include "LpdMessageReceiver.h"
#include <cstring>
#include <cppunit/extensions/HelperMacros.h>
#include "Exception.h"
#include "util.h"
#include "LpdMessageReceiver.h"
#include "SocketCore.h"
#include "BtConstants.h"
#include "LpdMessage.h"
#include "LpdMessageDispatcher.h"
namespace aria2 {
class LpdMessageReceiverTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(LpdMessageReceiverTest);
CPPUNIT_TEST(testReceiveMessage);
CPPUNIT_TEST_SUITE_END();
public:
void testReceiveMessage();
};
CPPUNIT_TEST_SUITE_REGISTRATION(LpdMessageReceiverTest);
void LpdMessageReceiverTest::testReceiveMessage()
{
LpdMessageReceiver rcv(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT);
CPPUNIT_ASSERT(rcv.init(""));
SharedHandle<SocketCore> sendsock(new SocketCore(SOCK_DGRAM));
sendsock->create(AF_INET);
// Mingw32 build needs to set interface explicitly.
sendsock->setMulticastInterface("");
sendsock->setMulticastTtl(0);
std::string infoHashString = "cd41c7fdddfd034a15a04d7ff881216e01c4ceaf";
std::string infoHash = util::fromHex(infoHashString);
std::string request =
bittorrent::createLpdRequest(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
infoHash,
6000);
sendsock->writeData(request.c_str(), request.size(),
LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT);
rcv.getSocket()->isReadable(5);
SharedHandle<LpdMessage> msg = rcv.receiveMessage();
CPPUNIT_ASSERT(!msg.isNull());
CPPUNIT_ASSERT_EQUAL(std::string("cd41c7fdddfd034a15a04d7ff881216e01c4ceaf"),
util::toHex(msg->getInfoHash()));
CPPUNIT_ASSERT_EQUAL((uint16_t)6000, msg->getPeer()->port);
// Bad infohash
std::string badInfoHashString = "cd41c7fdddfd034a15a04d7ff881216e01c4ce";
request =
bittorrent::createLpdRequest(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
util::fromHex(badInfoHashString),
6000);
sendsock->writeData(request.c_str(), request.size(),
LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT);
rcv.getSocket()->isReadable(5);
msg = rcv.receiveMessage();
CPPUNIT_ASSERT(!msg.isNull());
CPPUNIT_ASSERT(msg->getPeer().isNull());
CPPUNIT_ASSERT(msg->getInfoHash().empty());
// Bad port
request =
bittorrent::createLpdRequest(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
infoHash,
0);
sendsock->writeData(request.c_str(), request.size(),
LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT);
rcv.getSocket()->isReadable(5);
msg = rcv.receiveMessage();
CPPUNIT_ASSERT(!msg.isNull());
CPPUNIT_ASSERT(msg->getPeer().isNull());
CPPUNIT_ASSERT(msg->getInfoHash().empty());
// No data available
msg = rcv.receiveMessage();
CPPUNIT_ASSERT(msg.isNull());
}
} // namespace aria2
|