LpdMessageDispatcherTest.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #ifdef __MINGW32__
  37. recvsock->bindWithFamily(LPD_MULTICAST_PORT, AF_INET);
  38. #else // !__MINGW32__
  39. recvsock->bind(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT, AF_INET);
  40. #endif // !__MINGW32__
  41. recvsock->joinMulticastGroup(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT, "");
  42. LpdMessageDispatcher d("cd41c7fdddfd034a15a04d7ff881216e01c4ceaf", 6000,
  43. LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT);
  44. d.init("", 0, 1);
  45. CPPUNIT_ASSERT(d.sendMessage());
  46. unsigned char buf[200];
  47. std::pair<std::string, uint16_t> peer;
  48. ssize_t nbytes = recvsock->readDataFrom(buf, sizeof(buf), peer);
  49. buf[nbytes] = '\0';
  50. std::stringstream temp;
  51. temp << "BT-SEARCH * HTTP/1.1\r\n"
  52. << "Host: " << LPD_MULTICAST_ADDR << ":" << LPD_MULTICAST_PORT << "\r\n"
  53. << "Port: " << d.getPort() << "\r\n"
  54. << "Infohash: " << util::toHex(d.getInfoHash()) << "\r\n"
  55. << "\r\n\r\n";
  56. CPPUNIT_ASSERT_EQUAL(temp.str(), std::string(&buf[0], &buf[nbytes]));
  57. }
  58. } // namespace aria2