SocketCoreTest.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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(), 0, AF_INET,
  76. SOCK_STREAM, 0, 0));
  77. WSAAPI_AUTO_DELETE<struct addrinfo*> resDeleter(res, freeaddrinfo);
  78. sockaddr_in addr;
  79. memcpy(&addr, res->ai_addr, sizeof(addr));
  80. CPPUNIT_ASSERT_EQUAL(0, inetNtop(AF_INET, &addr.sin_addr,
  81. dest, sizeof(dest)));
  82. CPPUNIT_ASSERT_EQUAL(s, std::string(dest));
  83. }
  84. {
  85. std::string s = "2001:db8::2:1";
  86. addrinfo* res;
  87. CPPUNIT_ASSERT_EQUAL(0, callGetaddrinfo(&res, s.c_str(), 0, AF_INET6,
  88. SOCK_STREAM, 0, 0));
  89. WSAAPI_AUTO_DELETE<struct addrinfo*> resDeleter(res, freeaddrinfo);
  90. sockaddr_in6 addr;
  91. memcpy(&addr, res->ai_addr, sizeof(addr));
  92. CPPUNIT_ASSERT_EQUAL(0, inetNtop(AF_INET6, &addr.sin6_addr,
  93. dest, sizeof(dest)));
  94. CPPUNIT_ASSERT_EQUAL(s, std::string(dest));
  95. }
  96. }
  97. void SocketCoreTest::testInetPton()
  98. {
  99. {
  100. const char ipaddr[] = "192.168.0.1";
  101. in_addr ans;
  102. CPPUNIT_ASSERT_EQUAL((size_t)4, net::getBinAddr(&ans, ipaddr));
  103. in_addr dest;
  104. CPPUNIT_ASSERT_EQUAL(0, inetPton(AF_INET, ipaddr, &dest));
  105. CPPUNIT_ASSERT(memcmp(&ans, &dest, sizeof(ans)) == 0);
  106. }
  107. {
  108. const char ipaddr[] = "2001:db8::2:1";
  109. in6_addr ans;
  110. CPPUNIT_ASSERT_EQUAL((size_t)16, net::getBinAddr(&ans, ipaddr));
  111. in6_addr dest;
  112. CPPUNIT_ASSERT_EQUAL(0, inetPton(AF_INET6, ipaddr, &dest));
  113. CPPUNIT_ASSERT(memcmp(&ans, &dest, sizeof(ans)) == 0);
  114. }
  115. unsigned char dest[16];
  116. CPPUNIT_ASSERT_EQUAL(-1, inetPton(AF_INET, "localhost", &dest));
  117. CPPUNIT_ASSERT_EQUAL(-1, inetPton(AF_INET6, "localhost", &dest));
  118. }
  119. void SocketCoreTest::testGetBinAddr()
  120. {
  121. unsigned char dest[16];
  122. unsigned char ans1[] = { 192, 168, 0, 1 };
  123. CPPUNIT_ASSERT_EQUAL((size_t)4, net::getBinAddr(dest, "192.168.0.1"));
  124. CPPUNIT_ASSERT(std::equal(&dest[0], &dest[4], &ans1[0]));
  125. unsigned char ans2[] = { 0x20u, 0x01u, 0x0du, 0xb8u,
  126. 0x00u, 0x00u, 0x00u, 0x00u,
  127. 0x00u, 0x00u, 0x00u, 0x00u,
  128. 0x00u, 0x02u, 0x00u, 0x01u };
  129. CPPUNIT_ASSERT_EQUAL((size_t)16, net::getBinAddr(dest, "2001:db8::2:1"));
  130. CPPUNIT_ASSERT(std::equal(&dest[0], &dest[16], &ans2[0]));
  131. CPPUNIT_ASSERT_EQUAL((size_t)0, net::getBinAddr(dest, "localhost"));
  132. }
  133. void SocketCoreTest::testVerifyHostname()
  134. {
  135. {
  136. std::vector<std::string> dnsNames, ipAddrs;
  137. std::string commonName;
  138. CPPUNIT_ASSERT(!net::verifyHostname("example.org",
  139. dnsNames, ipAddrs, commonName));
  140. }
  141. {
  142. // Only commonName is provided
  143. std::vector<std::string> dnsNames, ipAddrs;
  144. std::string commonName = "example.org";
  145. CPPUNIT_ASSERT(net::verifyHostname("example.org",
  146. dnsNames, ipAddrs, commonName));
  147. }
  148. {
  149. // Match against dNSName in subjectAltName
  150. std::vector<std::string> dnsNames, ipAddrs;
  151. dnsNames.push_back("foo");
  152. dnsNames.push_back("example.org");
  153. std::string commonName = "exampleX.org";
  154. CPPUNIT_ASSERT(net::verifyHostname("example.org",
  155. dnsNames, ipAddrs, commonName));
  156. }
  157. {
  158. // If dNsName is provided, don't match with commonName
  159. std::vector<std::string> dnsNames, ipAddrs;
  160. dnsNames.push_back("foo");
  161. dnsNames.push_back("exampleX.org");
  162. ipAddrs.push_back("example.org");
  163. std::string commonName = "example.org";
  164. CPPUNIT_ASSERT(!net::verifyHostname("example.org",
  165. dnsNames, ipAddrs, commonName));
  166. }
  167. {
  168. // IPAddress in dnsName don't match.
  169. std::vector<std::string> dnsNames, ipAddrs;
  170. dnsNames.push_back("192.168.0.1");
  171. std::string commonName = "example.org";
  172. CPPUNIT_ASSERT(!net::verifyHostname("192.168.0.1",
  173. dnsNames, ipAddrs, commonName));
  174. }
  175. {
  176. // IPAddress string match with commonName
  177. std::vector<std::string> dnsNames, ipAddrs;
  178. std::string commonName = "192.168.0.1";
  179. CPPUNIT_ASSERT(net::verifyHostname("192.168.0.1",
  180. dnsNames, ipAddrs, commonName));
  181. }
  182. {
  183. // Match against iPAddress in subjectAltName
  184. std::vector<std::string> dnsNames, ipAddrs;
  185. unsigned char binAddr[16];
  186. size_t len;
  187. len = net::getBinAddr(binAddr, "192.168.0.1");
  188. ipAddrs.push_back(std::string(binAddr, binAddr+len));
  189. std::string commonName = "example.org";
  190. CPPUNIT_ASSERT(net::verifyHostname("192.168.0.1",
  191. dnsNames, ipAddrs, commonName));
  192. }
  193. {
  194. // Match against iPAddress (ipv6) in subjectAltName
  195. std::vector<std::string> dnsNames, ipAddrs;
  196. unsigned char binAddr[16];
  197. size_t len;
  198. len = net::getBinAddr(binAddr, "::1");
  199. ipAddrs.push_back(std::string(binAddr, binAddr+len));
  200. std::string commonName = "example.org";
  201. CPPUNIT_ASSERT(net::verifyHostname("::1",
  202. dnsNames, ipAddrs, commonName));
  203. }
  204. {
  205. // If iPAddress is privided, don't match with commonName
  206. std::vector<std::string> dnsNames, ipAddrs;
  207. unsigned char binAddr[16];
  208. size_t len;
  209. len = net::getBinAddr(binAddr, "192.168.0.2");
  210. ipAddrs.push_back(std::string(binAddr, binAddr+len));
  211. std::string commonName = "192.168.0.1";
  212. CPPUNIT_ASSERT(!net::verifyHostname("192.168.0.1",
  213. dnsNames, ipAddrs, commonName));
  214. }
  215. }
  216. } // namespace aria2