summaryrefslogtreecommitdiff
path: root/test/LpdMessageDispatcherTest.cc
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2010-02-20 14:23:25 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2010-02-20 14:23:25 +0000
commit9281f1126471fa90e2d6fe094c4c6eba00073ea6 (patch)
treedb2dc360e785cf78fd67f565d061cc24dc68f388 /test/LpdMessageDispatcherTest.cc
parentaee471e52c6f7ac06817600e2c21b400ef375ecb (diff)
2010-02-20 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added Local Peer Discovery. It is disabled by default. Use --bt-enable-lpd to enable the function. * src/BtConstants.h * src/BtSetup.cc * src/LpdDispatchMessageCommand.cc * src/LpdDispatchMessageCommand.h * src/LpdMessage.h * src/LpdMessageDispatcher.cc * src/LpdMessageDispatcher.h * src/LpdMessageReceiver.cc * src/LpdMessageReceiver.h * src/LpdReceiveMessageCommand.cc * src/LpdReceiveMessageCommand.h * src/Makefile.am * src/OptionHandlerFactory.cc * src/Peer.cc * src/Peer.h * src/PeerInteractionCommand.cc * src/SocketCore.cc * src/SocketCore.h * src/prefs.cc * src/prefs.h * src/usage_text.h * src/util.cc * src/util.h * test/LpdMessageDispatcherTest.cc * test/LpdMessageReceiverTest.cc * test/Makefile.am
Diffstat (limited to 'test/LpdMessageDispatcherTest.cc')
-rw-r--r--test/LpdMessageDispatcherTest.cc69
1 files changed, 69 insertions, 0 deletions
diff --git a/test/LpdMessageDispatcherTest.cc b/test/LpdMessageDispatcherTest.cc
new file mode 100644
index 00000000..79272996
--- /dev/null
+++ b/test/LpdMessageDispatcherTest.cc
@@ -0,0 +1,69 @@
+#include "LpdMessageDispatcher.h"
+
+#include <cstring>
+#include <sstream>
+
+#include <cppunit/extensions/HelperMacros.h>
+
+#include "Exception.h"
+#include "util.h"
+#include "LpdMessageDispatcher.h"
+#include "SocketCore.h"
+#include "BtConstants.h"
+
+namespace aria2 {
+
+class LpdMessageDispatcherTest:public CppUnit::TestFixture {
+
+ CPPUNIT_TEST_SUITE(LpdMessageDispatcherTest);
+ CPPUNIT_TEST(testCreateLpdRequest);
+ CPPUNIT_TEST(testSendMessage);
+ CPPUNIT_TEST_SUITE_END();
+public:
+ void testCreateLpdRequest();
+ void testSendMessage();
+};
+
+
+CPPUNIT_TEST_SUITE_REGISTRATION(LpdMessageDispatcherTest);
+
+void LpdMessageDispatcherTest::testCreateLpdRequest()
+{
+ std::string infoHashString = "cd41c7fdddfd034a15a04d7ff881216e01c4ceaf";
+ CPPUNIT_ASSERT_EQUAL
+ (std::string("BT-SEARCH * HTTP/1.1\r\n"
+ "Host: 239.192.152.143:6771\r\n"
+ "Port: 6000\r\n"
+ "Infohash: cd41c7fdddfd034a15a04d7ff881216e01c4ceaf\r\n"
+ "\r\n\r\n"),
+ bittorrent::createLpdRequest("239.192.152.143", 6771,
+ util::fromHex(infoHashString), 6000));
+}
+
+void LpdMessageDispatcherTest::testSendMessage()
+{
+ SharedHandle<SocketCore> recvsock(new SocketCore(SOCK_DGRAM));
+ recvsock->bind(LPD_MULTICAST_PORT);
+ recvsock->joinMulticastGroup(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT);
+ recvsock->setMulticastTtl(0);
+ LpdMessageDispatcher d("cd41c7fdddfd034a15a04d7ff881216e01c4ceaf", 6000,
+ LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
+ recvsock);
+
+ CPPUNIT_ASSERT(d.sendMessage());
+
+ unsigned char buf[200];
+
+ std::pair<std::string, uint16_t> peer;
+ ssize_t nbytes = recvsock->readDataFrom(buf, sizeof(buf), peer);
+ buf[nbytes] = '\0';
+ std::stringstream temp;
+ temp << "BT-SEARCH * HTTP/1.1\r\n"
+ << "Host: " << LPD_MULTICAST_ADDR << ":" << LPD_MULTICAST_PORT << "\r\n"
+ << "Port: " << d.getPort() << "\r\n"
+ << "Infohash: " << util::toHex(d.getInfoHash()) << "\r\n"
+ << "\r\n\r\n";
+ CPPUNIT_ASSERT_EQUAL(temp.str(), std::string(&buf[0], &buf[nbytes]));
+}
+
+} // namespace aria2