SocketCoreTest.cc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "SocketCore.h"
  2. #include "Exception.h"
  3. #include <cppunit/extensions/HelperMacros.h>
  4. namespace aria2 {
  5. class SocketCoreTest:public CppUnit::TestFixture {
  6. CPPUNIT_TEST_SUITE(SocketCoreTest);
  7. CPPUNIT_TEST(testWriteAndReadDatagram);
  8. CPPUNIT_TEST_SUITE_END();
  9. public:
  10. void setUp() {}
  11. void tearDown() {}
  12. void testWriteAndReadDatagram();
  13. };
  14. CPPUNIT_TEST_SUITE_REGISTRATION(SocketCoreTest);
  15. void SocketCoreTest::testWriteAndReadDatagram()
  16. {
  17. try {
  18. SocketCore s(SOCK_DGRAM);
  19. s.bind(0);
  20. SocketCore c(SOCK_DGRAM);
  21. c.bind(0);
  22. std::pair<std::string, int32_t> svaddr;
  23. s.getAddrInfo(svaddr);
  24. std::string message1 = "hello world.";
  25. c.writeData(message1.c_str(), message1.size(), "localhost", svaddr.second);
  26. std::string message2 = "chocolate coated pie";
  27. c.writeData(message2.c_str(), message2.size(), "localhost", svaddr.second);
  28. char readbuffer[100];
  29. std::pair<std::string, uint16_t> peer;
  30. {
  31. ssize_t rlength = s.readDataFrom(readbuffer, sizeof(readbuffer), peer);
  32. // commented out because ip address may vary
  33. //CPPUNIT_ASSERT_EQUAL(std::std::string("127.0.0.1"), peer.first);
  34. CPPUNIT_ASSERT_EQUAL((ssize_t)message1.size(), rlength);
  35. readbuffer[rlength] = '\0';
  36. CPPUNIT_ASSERT_EQUAL(message1, std::string(readbuffer));
  37. }
  38. {
  39. ssize_t rlength = s.readDataFrom(readbuffer, sizeof(readbuffer));
  40. CPPUNIT_ASSERT_EQUAL((ssize_t)message2.size(), rlength);
  41. readbuffer[rlength] = '\0';
  42. CPPUNIT_ASSERT_EQUAL(message2, std::string(readbuffer));
  43. }
  44. } catch(Exception* e) {
  45. std::cerr << *e << std::endl;
  46. delete e;
  47. CPPUNIT_FAIL("exception thrown");
  48. }
  49. }
  50. } // namespace aria2