SocketCoreTest.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "SocketCore.h"
  2. #include <cstring>
  3. #include <iostream>
  4. #include <cppunit/extensions/HelperMacros.h>
  5. #include "a2functional.h"
  6. #include "Exception.h"
  7. namespace aria2 {
  8. class SocketCoreTest:public CppUnit::TestFixture {
  9. CPPUNIT_TEST_SUITE(SocketCoreTest);
  10. CPPUNIT_TEST(testWriteAndReadDatagram);
  11. CPPUNIT_TEST(testGetSocketError);
  12. CPPUNIT_TEST(testInetNtop);
  13. CPPUNIT_TEST(testGetBinAddr);
  14. CPPUNIT_TEST(testVerifyHostname);
  15. CPPUNIT_TEST_SUITE_END();
  16. public:
  17. void setUp() {}
  18. void tearDown() {}
  19. void testWriteAndReadDatagram();
  20. void testGetSocketError();
  21. void testInetNtop();
  22. void testGetBinAddr();
  23. void testVerifyHostname();
  24. };
  25. CPPUNIT_TEST_SUITE_REGISTRATION(SocketCoreTest);
  26. void SocketCoreTest::testWriteAndReadDatagram()
  27. {
  28. try {
  29. SocketCore s(SOCK_DGRAM);
  30. s.bind(0);
  31. SocketCore c(SOCK_DGRAM);
  32. c.bind(0);
  33. std::pair<std::string, uint16_t> svaddr;
  34. s.getAddrInfo(svaddr);
  35. std::string message1 = "hello world.";
  36. c.writeData(message1.c_str(), message1.size(), "localhost", svaddr.second);
  37. std::string message2 = "chocolate coated pie";
  38. c.writeData(message2.c_str(), message2.size(), "localhost", svaddr.second);
  39. char readbuffer[100];
  40. std::pair<std::string, uint16_t> peer;
  41. {
  42. ssize_t rlength = s.readDataFrom(readbuffer, sizeof(readbuffer), peer);
  43. // commented out because ip address may vary
  44. //CPPUNIT_ASSERT_EQUAL(std::std::string("127.0.0.1"), peer.first);
  45. CPPUNIT_ASSERT_EQUAL((ssize_t)message1.size(), rlength);
  46. readbuffer[rlength] = '\0';
  47. CPPUNIT_ASSERT_EQUAL(message1, std::string(readbuffer));
  48. }
  49. {
  50. ssize_t rlength = s.readDataFrom(readbuffer, sizeof(readbuffer), peer);
  51. CPPUNIT_ASSERT_EQUAL((ssize_t)message2.size(), rlength);
  52. readbuffer[rlength] = '\0';
  53. CPPUNIT_ASSERT_EQUAL(message2, std::string(readbuffer));
  54. }
  55. } catch(Exception& e) {
  56. std::cerr << e.stackTrace() << std::endl;
  57. CPPUNIT_FAIL("exception thrown");
  58. }
  59. }
  60. void SocketCoreTest::testGetSocketError()
  61. {
  62. SocketCore s;
  63. s.bind(0);
  64. // See there is no error at this point
  65. CPPUNIT_ASSERT_EQUAL(std::string(""), s.getSocketError());
  66. }
  67. void SocketCoreTest::testInetNtop()
  68. {
  69. char dest[NI_MAXHOST];
  70. {
  71. std::string s = "192.168.0.1";
  72. addrinfo* res;
  73. CPPUNIT_ASSERT_EQUAL(0, callGetaddrinfo(&res, s.c_str(), 0, AF_INET,
  74. SOCK_STREAM, 0, 0));
  75. WSAAPI_AUTO_DELETE<struct addrinfo*> resDeleter(res, freeaddrinfo);
  76. sockaddr_in addr;
  77. memcpy(&addr, res->ai_addr, sizeof(addr));
  78. CPPUNIT_ASSERT_EQUAL(0, inetNtop(AF_INET, &addr.sin_addr,
  79. dest, sizeof(dest)));
  80. CPPUNIT_ASSERT_EQUAL(s, std::string(dest));
  81. }
  82. {
  83. std::string s = "2001:db8::2:1";
  84. addrinfo* res;
  85. CPPUNIT_ASSERT_EQUAL(0, callGetaddrinfo(&res, s.c_str(), 0, AF_INET6,
  86. SOCK_STREAM, 0, 0));
  87. WSAAPI_AUTO_DELETE<struct addrinfo*> resDeleter(res, freeaddrinfo);
  88. sockaddr_in6 addr;
  89. memcpy(&addr, res->ai_addr, sizeof(addr));
  90. CPPUNIT_ASSERT_EQUAL(0, inetNtop(AF_INET6, &addr.sin6_addr,
  91. dest, sizeof(dest)));
  92. CPPUNIT_ASSERT_EQUAL(s, std::string(dest));
  93. }
  94. }
  95. void SocketCoreTest::testGetBinAddr()
  96. {
  97. unsigned char dest[16];
  98. unsigned char ans1[] = { 192, 168, 0, 1 };
  99. CPPUNIT_ASSERT_EQUAL((size_t)4, net::getBinAddr(dest, "192.168.0.1"));
  100. CPPUNIT_ASSERT(std::equal(&dest[0], &dest[4], &ans1[0]));
  101. unsigned char ans2[] = { 0x20u, 0x01u, 0x0du, 0xb8u,
  102. 0x00u, 0x00u, 0x00u, 0x00u,
  103. 0x00u, 0x00u, 0x00u, 0x00u,
  104. 0x00u, 0x02u, 0x00u, 0x01u };
  105. CPPUNIT_ASSERT_EQUAL((size_t)16, net::getBinAddr(dest, "2001:db8::2:1"));
  106. CPPUNIT_ASSERT(std::equal(&dest[0], &dest[16], &ans2[0]));
  107. CPPUNIT_ASSERT_EQUAL((size_t)0, net::getBinAddr(dest, "localhost"));
  108. }
  109. void SocketCoreTest::testVerifyHostname()
  110. {
  111. {
  112. std::vector<std::string> dnsNames, ipAddrs;
  113. std::string commonName;
  114. CPPUNIT_ASSERT(!net::verifyHostname("example.org",
  115. dnsNames, ipAddrs, commonName));
  116. }
  117. {
  118. // Only commonName is provided
  119. std::vector<std::string> dnsNames, ipAddrs;
  120. std::string commonName = "example.org";
  121. CPPUNIT_ASSERT(net::verifyHostname("example.org",
  122. dnsNames, ipAddrs, commonName));
  123. }
  124. {
  125. // Match against dNSName in subjectAltName
  126. std::vector<std::string> dnsNames, ipAddrs;
  127. dnsNames.push_back("foo");
  128. dnsNames.push_back("example.org");
  129. std::string commonName = "exampleX.org";
  130. CPPUNIT_ASSERT(net::verifyHostname("example.org",
  131. dnsNames, ipAddrs, commonName));
  132. }
  133. {
  134. // If dNsName is provided, don't match with commonName
  135. std::vector<std::string> dnsNames, ipAddrs;
  136. dnsNames.push_back("foo");
  137. dnsNames.push_back("exampleX.org");
  138. ipAddrs.push_back("example.org");
  139. std::string commonName = "example.org";
  140. CPPUNIT_ASSERT(!net::verifyHostname("example.org",
  141. dnsNames, ipAddrs, commonName));
  142. }
  143. {
  144. // IPAddress in dnsName don't match.
  145. std::vector<std::string> dnsNames, ipAddrs;
  146. dnsNames.push_back("192.168.0.1");
  147. std::string commonName = "example.org";
  148. CPPUNIT_ASSERT(!net::verifyHostname("192.168.0.1",
  149. dnsNames, ipAddrs, commonName));
  150. }
  151. {
  152. // IPAddress string match with commonName
  153. std::vector<std::string> dnsNames, ipAddrs;
  154. std::string commonName = "192.168.0.1";
  155. CPPUNIT_ASSERT(net::verifyHostname("192.168.0.1",
  156. dnsNames, ipAddrs, commonName));
  157. }
  158. {
  159. // Match against iPAddress in subjectAltName
  160. std::vector<std::string> dnsNames, ipAddrs;
  161. unsigned char binAddr[16];
  162. size_t len;
  163. len = net::getBinAddr(binAddr, "192.168.0.1");
  164. ipAddrs.push_back(std::string(binAddr, binAddr+len));
  165. std::string commonName = "example.org";
  166. CPPUNIT_ASSERT(net::verifyHostname("192.168.0.1",
  167. dnsNames, ipAddrs, commonName));
  168. }
  169. {
  170. // Match against iPAddress (ipv6) in subjectAltName
  171. std::vector<std::string> dnsNames, ipAddrs;
  172. unsigned char binAddr[16];
  173. size_t len;
  174. len = net::getBinAddr(binAddr, "::1");
  175. ipAddrs.push_back(std::string(binAddr, binAddr+len));
  176. std::string commonName = "example.org";
  177. CPPUNIT_ASSERT(net::verifyHostname("::1",
  178. dnsNames, ipAddrs, commonName));
  179. }
  180. {
  181. // If iPAddress is privided, don't match with commonName
  182. std::vector<std::string> dnsNames, ipAddrs;
  183. unsigned char binAddr[16];
  184. size_t len;
  185. len = net::getBinAddr(binAddr, "192.168.0.2");
  186. ipAddrs.push_back(std::string(binAddr, binAddr+len));
  187. std::string commonName = "192.168.0.1";
  188. CPPUNIT_ASSERT(!net::verifyHostname("192.168.0.1",
  189. dnsNames, ipAddrs, commonName));
  190. }
  191. }
  192. } // namespace aria2