sockcreator_tests.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include "../sockcreator.h"
  15. #include <util/unittests/fork.h>
  16. #include <util/io/fd.h>
  17. #include <gtest/gtest.h>
  18. #include <sys/types.h>
  19. #include <sys/socket.h>
  20. #include <netinet/in.h>
  21. #include <unistd.h>
  22. #include <cstring>
  23. #include <cerrno>
  24. using namespace isc::socket_creator;
  25. using namespace isc::util::unittests;
  26. using namespace isc::util::io;
  27. namespace {
  28. /*
  29. * Generic version of the creation of socket test. It just tries to
  30. * create the socket and checks the result is not negative (eg.
  31. * it is valid descriptor) and that it can listen.
  32. *
  33. * This is a macro so ASSERT_* does abort the TEST, not just the
  34. * function inside.
  35. */
  36. #define TEST_ANY_CREATE(SOCK_TYPE, ADDR_TYPE, ADDR_FAMILY, FAMILY_FIELD, \
  37. ADDR_SET, CHECK_SOCK) \
  38. do { \
  39. /*
  40. * This should create an address that binds on all interfaces
  41. * and lets the OS choose a free port.
  42. */ \
  43. struct ADDR_TYPE addr; \
  44. memset(&addr, 0, sizeof addr); \
  45. ADDR_SET(addr); \
  46. addr.FAMILY_FIELD = ADDR_FAMILY; \
  47. struct sockaddr *addr_ptr = static_cast<struct sockaddr *>( \
  48. static_cast<void *>(&addr)); \
  49. \
  50. int socket = get_sock(SOCK_TYPE, addr_ptr, sizeof addr); \
  51. /* Provide even nice error message. */ \
  52. ASSERT_GE(socket, 0) << "Couldn't create a socket of type " \
  53. #SOCK_TYPE " and family " #ADDR_FAMILY ", failed with " \
  54. << socket << " and error " << strerror(errno); \
  55. CHECK_SOCK(ADDR_TYPE, socket); \
  56. int on; \
  57. socklen_t len(sizeof(on)); \
  58. ASSERT_EQ(0, getsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &on, &len));\
  59. ASSERT_EQ(1, on); \
  60. if (ADDR_FAMILY == AF_INET6) { \
  61. ASSERT_EQ(0, getsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY, &on, \
  62. &len)); \
  63. ASSERT_EQ(1, on); \
  64. } \
  65. EXPECT_EQ(0, close(socket)); \
  66. } while (0)
  67. // Just helper macros
  68. #define INADDR_SET(WHAT) do { WHAT.sin_addr.s_addr = INADDR_ANY; } while (0)
  69. #define IN6ADDR_SET(WHAT) do { WHAT.sin6_addr = in6addr_loopback; } while (0)
  70. // If the get_sock returned something useful, listen must work
  71. #define TCP_CHECK(UNUSED, SOCKET) do { \
  72. EXPECT_EQ(0, listen(SOCKET, 1)); \
  73. } while (0)
  74. // More complicated with UDP, so we send a packet to ourselfs and se if it
  75. // arrives
  76. #define UDP_CHECK(ADDR_TYPE, SOCKET) do { \
  77. struct ADDR_TYPE addr; \
  78. memset(&addr, 0, sizeof addr); \
  79. struct sockaddr *addr_ptr = static_cast<struct sockaddr *>( \
  80. static_cast<void *>(&addr)); \
  81. \
  82. socklen_t len = sizeof addr; \
  83. ASSERT_EQ(0, getsockname(SOCKET, addr_ptr, &len)); \
  84. ASSERT_EQ(5, sendto(SOCKET, "test", 5, 0, addr_ptr, sizeof addr)) << \
  85. "Send failed with error " << strerror(errno) << " on socket " << \
  86. SOCKET; \
  87. char buffer[5]; \
  88. ASSERT_EQ(5, recv(SOCKET, buffer, 5, 0)) << \
  89. "Recv failed with error " << strerror(errno) << " on socket " << \
  90. SOCKET; \
  91. EXPECT_STREQ("test", buffer); \
  92. } while (0)
  93. /*
  94. * Several tests to ensure we can create the sockets.
  95. */
  96. TEST(get_sock, udp4_create) {
  97. TEST_ANY_CREATE(SOCK_DGRAM, sockaddr_in, AF_INET, sin_family, INADDR_SET,
  98. UDP_CHECK);
  99. }
  100. TEST(get_sock, tcp4_create) {
  101. TEST_ANY_CREATE(SOCK_STREAM, sockaddr_in, AF_INET, sin_family, INADDR_SET,
  102. TCP_CHECK);
  103. }
  104. TEST(get_sock, udp6_create) {
  105. TEST_ANY_CREATE(SOCK_DGRAM, sockaddr_in6, AF_INET6, sin6_family,
  106. IN6ADDR_SET, UDP_CHECK);
  107. }
  108. TEST(get_sock, tcp6_create) {
  109. TEST_ANY_CREATE(SOCK_STREAM, sockaddr_in6, AF_INET6, sin6_family,
  110. IN6ADDR_SET, TCP_CHECK);
  111. }
  112. /*
  113. * Try to ask the get_sock function some nonsense and test if it
  114. * is able to report error.
  115. */
  116. TEST(get_sock, fail_with_nonsense) {
  117. struct sockaddr addr;
  118. memset(&addr, 0, sizeof addr);
  119. ASSERT_LT(get_sock(0, &addr, sizeof addr), 0);
  120. }
  121. /*
  122. * Helper functions to pass to run during testing.
  123. */
  124. int
  125. get_sock_dummy(const int type, struct sockaddr *addr, const socklen_t)
  126. {
  127. int result(0);
  128. int port(0);
  129. /*
  130. * We encode the type and address family into the int and return it.
  131. * Lets ignore the port and address for now
  132. * First bit is 1 if it is known type. Second tells if TCP or UDP.
  133. * The familly is similar - third bit is known address family,
  134. * the fourth is the family.
  135. */
  136. switch (type) {
  137. case SOCK_STREAM:
  138. result += 1;
  139. break;
  140. case SOCK_DGRAM:
  141. result += 3;
  142. break;
  143. }
  144. switch (addr->sa_family) {
  145. case AF_INET:
  146. result += 4;
  147. port = static_cast<struct sockaddr_in *>(
  148. static_cast<void *>(addr))->sin_port;
  149. break;
  150. case AF_INET6:
  151. result += 12;
  152. port = static_cast<struct sockaddr_in6 *>(
  153. static_cast<void *>(addr))->sin6_port;
  154. break;
  155. }
  156. /*
  157. * The port should be 0xffff. If it's not, we change the result.
  158. * The port of 0xbbbb means bind should fail and 0xcccc means
  159. * socket should fail.
  160. */
  161. if (port != 0xffff) {
  162. errno = 0;
  163. if (port == 0xbbbb) {
  164. return -2;
  165. } else if (port == 0xcccc) {
  166. return -1;
  167. } else {
  168. result += 16;
  169. }
  170. }
  171. return result;
  172. }
  173. int
  174. send_fd_dummy(const int destination, const int what)
  175. {
  176. /*
  177. * Make sure it is 1 byte so we know the length. We do not use more during
  178. * the test anyway.
  179. */
  180. char fd_data(what);
  181. if (!write_data(destination, &fd_data, 1)) {
  182. return -1;
  183. } else {
  184. return 0;
  185. }
  186. }
  187. /*
  188. * Generic test that it works, with various inputs and outputs.
  189. * It uses different functions to create the socket and send it and pass
  190. * data to it and check it returns correct data back, to see if the run()
  191. * parses the commands correctly.
  192. */
  193. void run_test(const char *input_data, const size_t input_size,
  194. const char *output_data, const size_t output_size,
  195. bool should_succeed = true)
  196. {
  197. // Prepare the input feeder and output checker processes
  198. int input_fd(0), output_fd(0);
  199. pid_t input(provide_input(&input_fd, input_data, input_size)),
  200. output(check_output(&output_fd, output_data, output_size));
  201. ASSERT_NE(-1, input) << "Couldn't start input feeder";
  202. ASSERT_NE(-1, output) << "Couldn't start output checker";
  203. // Run the body
  204. int result(run(input_fd, output_fd, get_sock_dummy, send_fd_dummy));
  205. // Close the pipes
  206. close(input_fd);
  207. close(output_fd);
  208. // Did it run well?
  209. if (should_succeed) {
  210. EXPECT_EQ(0, result);
  211. } else {
  212. EXPECT_NE(0, result);
  213. }
  214. // Check the subprocesses say everything is OK too
  215. EXPECT_TRUE(process_ok(input));
  216. EXPECT_TRUE(process_ok(output));
  217. }
  218. /*
  219. * Check it terminates successfully when asked to.
  220. */
  221. TEST(run, terminate) {
  222. run_test("T", 1, NULL, 0);
  223. }
  224. /*
  225. * Check it rejects incorrect input.
  226. */
  227. TEST(run, bad_input) {
  228. run_test("XXX", 3, "FI", 2, false);
  229. }
  230. /*
  231. * Check it correctly parses queries to create sockets.
  232. */
  233. TEST(run, sockets) {
  234. run_test(
  235. "SU4\xff\xff\0\0\0\0" // This has 9 bytes
  236. "ST4\xff\xff\0\0\0\0" // This has 9 bytes
  237. "ST6\xff\xff\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" // This has 21 bytes
  238. "SU6\xff\xff\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" // This has 21 bytes
  239. "T", 61,
  240. "S\x07S\x05S\x0dS\x0f", 8);
  241. }
  242. /*
  243. * Check if failures of get_socket are handled correctly.
  244. */
  245. TEST(run, bad_sockets) {
  246. // We need to construct the answer, but it depends on int length.
  247. size_t int_len(sizeof(int));
  248. size_t result_len(4 + 2 * int_len);
  249. char result[4 + sizeof(int) * 2];
  250. // Both errno parts should be 0
  251. memset(result, 0, result_len);
  252. // Fill the 2 control parts
  253. strcpy(result, "EB");
  254. strcpy(result + 2 + int_len, "ES");
  255. // Run the test
  256. run_test(
  257. "SU4\xbb\xbb\0\0\0\0"
  258. "SU4\xcc\xcc\0\0\0\0"
  259. "T", 19,
  260. result, result_len);
  261. }
  262. }