DHTConnectionImplTest.cc 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "DHTConnectionImpl.h"
  2. #include "Exception.h"
  3. #include <cppunit/extensions/HelperMacros.h>
  4. namespace aria2 {
  5. class DHTConnectionImplTest:public CppUnit::TestFixture {
  6. CPPUNIT_TEST_SUITE(DHTConnectionImplTest);
  7. CPPUNIT_TEST(testWriteAndReadData);
  8. CPPUNIT_TEST_SUITE_END();
  9. public:
  10. void setUp() {}
  11. void tearDown() {}
  12. void testWriteAndReadData();
  13. };
  14. CPPUNIT_TEST_SUITE_REGISTRATION(DHTConnectionImplTest);
  15. void DHTConnectionImplTest::testWriteAndReadData()
  16. {
  17. try {
  18. DHTConnectionImpl con1;
  19. /*uint16_t con1port =*/ con1.bind(0);
  20. DHTConnectionImpl con2;
  21. uint16_t con2port = con2.bind(0);
  22. std::string message1 = "hello world.";
  23. con1.sendMessage(message1.c_str(), message1.size(), "127.0.0.1", con2port);
  24. char readbuffer[100];
  25. std::string remoteHost;
  26. uint16_t remotePort;
  27. {
  28. ssize_t rlength = con2.receiveMessage(readbuffer, sizeof(readbuffer), remoteHost, remotePort);
  29. CPPUNIT_ASSERT_EQUAL((ssize_t)message1.size(), rlength);
  30. readbuffer[rlength] = '\0';
  31. CPPUNIT_ASSERT_EQUAL(message1, std::string(readbuffer));
  32. }
  33. } catch(Exception* e) {
  34. std::cerr << *e << std::endl;
  35. delete e;
  36. CPPUNIT_FAIL("exception thrown");
  37. }
  38. }
  39. } // namespace aria2