SocketCoreTest.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. } catch(Exception& e) {
  58. std::cerr << e.stackTrace() << std::endl;
  59. CPPUNIT_FAIL("exception thrown");
  60. }
  61. }
  62. void SocketCoreTest::testGetSocketError()
  63. {
  64. SocketCore s;
  65. s.bind(0);
  66. // See there is no error at this point
  67. CPPUNIT_ASSERT_EQUAL(std::string(""), s.getSocketError());
  68. }
  69. void SocketCoreTest::testInetNtop()
  70. {
  71. char dest[NI_MAXHOST];
  72. {
  73. std::string s = "192.168.0.1";
  74. addrinfo* res;
  75. CPPUNIT_ASSERT_EQUAL(0, callGetaddrinfo(&res, s.c_str(), nullptr, AF_INET,
  76. SOCK_STREAM, 0, 0));
  77. std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> resDeleter
  78. (res, freeaddrinfo);
  79. sockaddr_in addr;
  80. memcpy(&addr, res->ai_addr, sizeof(addr));
  81. CPPUNIT_ASSERT_EQUAL(0, inetNtop(AF_INET, &addr.sin_addr,
  82. dest, sizeof(dest)));
  83. CPPUNIT_ASSERT_EQUAL(s, std::string(dest));
  84. }
  85. {
  86. std::string s = "2001:db8::2:1";
  87. addrinfo* res;
  88. CPPUNIT_ASSERT_EQUAL(0, callGetaddrinfo(&res, s.c_str(), nullptr, AF_INET6,
  89. SOCK_STREAM, 0, 0));
  90. std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> resDeleter
  91. (res, freeaddrinfo);
  92. sockaddr_in6 addr;
  93. memcpy(&addr, res->ai_addr, sizeof(addr));
  94. CPPUNIT_ASSERT_EQUAL(0, inetNtop(AF_INET6, &addr.sin6_addr,
  95. dest, sizeof(dest)));
  96. CPPUNIT_ASSERT_EQUAL(s, std::string(dest));
  97. }
  98. }
  99. void SocketCoreTest::testInetPton()
  100. {
  101. {
  102. const char ipaddr[] = "192.168.0.1";
  103. in_addr ans;
  104. CPPUNIT_ASSERT_EQUAL((size_t)4, net::getBinAddr(&ans, ipaddr));
  105. in_addr dest;
  106. CPPUNIT_ASSERT_EQUAL(0, inetPton(AF_INET, ipaddr, &dest));
  107. CPPUNIT_ASSERT(memcmp(&ans, &dest, sizeof(ans)) == 0);
  108. }
  109. {
  110. const char ipaddr[] = "2001:db8::2:1";
  111. in6_addr ans;
  112. CPPUNIT_ASSERT_EQUAL((size_t)16, net::getBinAddr(&ans, ipaddr));
  113. in6_addr dest;
  114. CPPUNIT_ASSERT_EQUAL(0, inetPton(AF_INET6, ipaddr, &dest));
  115. CPPUNIT_ASSERT(memcmp(&ans, &dest, sizeof(ans)) == 0);
  116. }
  117. unsigned char dest[16];
  118. CPPUNIT_ASSERT_EQUAL(-1, inetPton(AF_INET, "localhost", &dest));
  119. CPPUNIT_ASSERT_EQUAL(-1, inetPton(AF_INET6, "localhost", &dest));
  120. }
  121. void SocketCoreTest::testGetBinAddr()
  122. {
  123. unsigned char dest[16];
  124. unsigned char ans1[] = { 192, 168, 0, 1 };
  125. CPPUNIT_ASSERT_EQUAL((size_t)4, net::getBinAddr(dest, "192.168.0.1"));
  126. CPPUNIT_ASSERT(std::equal(&dest[0], &dest[4], &ans1[0]));
  127. unsigned char ans2[] = { 0x20u, 0x01u, 0x0du, 0xb8u,
  128. 0x00u, 0x00u, 0x00u, 0x00u,
  129. 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(!net::verifyHostname("example.org",
  141. 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(net::verifyHostname("example.org",
  148. 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(net::verifyHostname("example.org",
  157. 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(!net::verifyHostname("example.org",
  167. 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(!net::verifyHostname("192.168.0.1",
  175. 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(net::verifyHostname("192.168.0.1",
  182. 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(net::verifyHostname("192.168.0.1",
  193. 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",
  204. dnsNames, ipAddrs, commonName));
  205. }
  206. {
  207. // If iPAddress is privided, don't match with commonName
  208. std::vector<std::string> dnsNames, ipAddrs;
  209. unsigned char binAddr[16];
  210. size_t len;
  211. len = net::getBinAddr(binAddr, "192.168.0.2");
  212. ipAddrs.push_back(std::string(binAddr, binAddr+len));
  213. std::string commonName = "192.168.0.1";
  214. CPPUNIT_ASSERT(!net::verifyHostname("192.168.0.1",
  215. dnsNames, ipAddrs, commonName));
  216. }
  217. }
  218. } // namespace aria2