LpdMessageDispatcherTest.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "LpdMessageDispatcher.h"
  2. #include <cstring>
  3. #include <sstream>
  4. #include <cppunit/extensions/HelperMacros.h>
  5. #include "Exception.h"
  6. #include "util.h"
  7. #include "LpdMessageDispatcher.h"
  8. #include "SocketCore.h"
  9. #include "BtConstants.h"
  10. namespace aria2 {
  11. class LpdMessageDispatcherTest:public CppUnit::TestFixture {
  12. CPPUNIT_TEST_SUITE(LpdMessageDispatcherTest);
  13. CPPUNIT_TEST(testCreateLpdRequest);
  14. CPPUNIT_TEST(testSendMessage);
  15. CPPUNIT_TEST_SUITE_END();
  16. public:
  17. void testCreateLpdRequest();
  18. void testSendMessage();
  19. };
  20. CPPUNIT_TEST_SUITE_REGISTRATION(LpdMessageDispatcherTest);
  21. void LpdMessageDispatcherTest::testCreateLpdRequest()
  22. {
  23. std::string infoHashString = "cd41c7fdddfd034a15a04d7ff881216e01c4ceaf";
  24. CPPUNIT_ASSERT_EQUAL
  25. (std::string("BT-SEARCH * HTTP/1.1\r\n"
  26. "Host: 239.192.152.143:6771\r\n"
  27. "Port: 6000\r\n"
  28. "Infohash: cd41c7fdddfd034a15a04d7ff881216e01c4ceaf\r\n"
  29. "\r\n\r\n"),
  30. bittorrent::createLpdRequest("239.192.152.143", 6771,
  31. util::fromHex(infoHashString), 6000));
  32. }
  33. void LpdMessageDispatcherTest::testSendMessage()
  34. {
  35. SharedHandle<SocketCore> recvsock(new SocketCore(SOCK_DGRAM));
  36. recvsock->bind(LPD_MULTICAST_PORT);
  37. recvsock->joinMulticastGroup(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT);
  38. recvsock->setMulticastTtl(0);
  39. LpdMessageDispatcher d("cd41c7fdddfd034a15a04d7ff881216e01c4ceaf", 6000,
  40. LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
  41. recvsock);
  42. CPPUNIT_ASSERT(d.sendMessage());
  43. unsigned char buf[200];
  44. std::pair<std::string, uint16_t> peer;
  45. ssize_t nbytes = recvsock->readDataFrom(buf, sizeof(buf), peer);
  46. buf[nbytes] = '\0';
  47. std::stringstream temp;
  48. temp << "BT-SEARCH * HTTP/1.1\r\n"
  49. << "Host: " << LPD_MULTICAST_ADDR << ":" << LPD_MULTICAST_PORT << "\r\n"
  50. << "Port: " << d.getPort() << "\r\n"
  51. << "Infohash: " << util::toHex(d.getInfoHash()) << "\r\n"
  52. << "\r\n\r\n";
  53. CPPUNIT_ASSERT_EQUAL(temp.str(), std::string(&buf[0], &buf[nbytes]));
  54. }
  55. } // namespace aria2