SocketCoreTest.cc 7.6 KB

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