SocketCoreTest.cc 7.6 KB

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