sockcreator_tests.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // Copyright (C) 2011-2012 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 <boost/lexical_cast.hpp>
  18. #include <gtest/gtest.h>
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22. #include <unistd.h>
  23. #include <iostream>
  24. #include <cstring>
  25. #include <cerrno>
  26. using namespace isc::socket_creator;
  27. using namespace isc::util::unittests;
  28. using namespace isc::util::io;
  29. // The tests check both TCP and UDP sockets on IPv4 and IPv6.
  30. //
  31. // Essentially we need to check all four combinations of TCP/UDP and IPv4/IPv6.
  32. // The different address families (IPv4/IPv6) require different structures to
  33. // hold the address information, and so some common code is in the form of
  34. // templates (or overloads), parameterised on the structure type.
  35. //
  36. // The protocol is determined by an integer (SOCK_STREAM or SOCK_DGRAM) so
  37. // cannot be templated in the same way. Relevant check functions are
  38. // selected manually.
  39. namespace {
  40. // Set IP-version-specific fields.
  41. void
  42. setAddressFamilyFields(sockaddr_in* address) {
  43. address->sin_family = AF_INET;
  44. address->sin_addr.s_addr = INADDR_ANY;
  45. }
  46. void
  47. setAddressFamilyFields(sockaddr_in6* address) {
  48. address->sin6_family = AF_INET6;
  49. address->sin6_addr = in6addr_loopback;
  50. }
  51. // Socket has been opened, peform a check on it. The sole argument is the
  52. // socket descriptor. The TCP check is the same regardless of the address
  53. // family. The UDP check requires that the socket address be obtained so
  54. // is parameterised on the type of structure required to hold the address.
  55. void
  56. tcpCheck(const int socknum) {
  57. // Sufficient to be able to listen on the socket.
  58. EXPECT_EQ(0, listen(socknum, 1));
  59. }
  60. template <typename ADDRTYPE>
  61. void
  62. udpCheck(const int socknum) {
  63. // UDP testing is more complicated than TCP: send a packet to ourselves and
  64. // see if it arrives.
  65. // Get details of the socket so that we can use it as the target of the
  66. // sendto().
  67. ADDRTYPE addr;
  68. memset(&addr, 0, sizeof(addr));
  69. sockaddr* addr_ptr = reinterpret_cast<sockaddr*>(&addr);
  70. socklen_t len = sizeof(addr);
  71. ASSERT_EQ(0, getsockname(socknum, addr_ptr, &len));
  72. // Send the packet to ourselves and check we receive it.
  73. ASSERT_EQ(5, sendto(socknum, "test", 5, 0, addr_ptr, sizeof(addr))) <<
  74. "Send failed with error " << strerror(errno) << " on socket " <<
  75. socknum;
  76. char buffer[5];
  77. ASSERT_EQ(5, recv(socknum, buffer, 5, 0)) <<
  78. "Recv failed with error " << strerror(errno) << " on socket " <<
  79. socknum;
  80. EXPECT_STREQ("test", buffer);
  81. }
  82. // The check function (tcpCheck/udpCheck) is passed as a parameter to the test
  83. // code, so provide a conveniet typedef.
  84. typedef void (*socket_check_t)(const int);
  85. // Address-family-specific scoket checks.
  86. //
  87. // The first argument is used to select the overloaded check function.
  88. // The other argument is the socket descriptor number.
  89. // IPv4 check
  90. void addressFamilySpecificCheck(const sockaddr_in*, const int, const int) {
  91. }
  92. // IPv6 check
  93. void addressFamilySpecificCheck(const sockaddr_in6*, const int socknum,
  94. const int socket_type)
  95. {
  96. int options;
  97. socklen_t len = sizeof(options);
  98. EXPECT_EQ(0, getsockopt(socknum, IPPROTO_IPV6, IPV6_V6ONLY, &options,
  99. &len));
  100. EXPECT_NE(0, options);
  101. if (socket_type == SOCK_DGRAM) {
  102. // Some more checks for UDP - MTU
  103. #ifdef IPV6_USE_MIN_MTU /* RFC 3542, not too common yet*/
  104. // use minimum MTU
  105. EXPECT_EQ(getsockopt(socknum, IPPROTO_IPV6, IPV6_USE_MIN_MTU, &options,
  106. &len)) << strerror(errno);
  107. EXPECT_NE(0, options);
  108. #endif
  109. #ifdef IPV6_MTU
  110. // Use minimum MTU on systems that don't have the IPV6_USE_MIN_MTU
  111. EXPECT_EQ(0, getsockopt(socknum, IPPROTO_IPV6, IPV6_MTU, &options,
  112. &len)) << strerror(errno);
  113. EXPECT_EQ(1280, options);
  114. #endif
  115. #if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DONT)
  116. // Turned off Path MTU discovery on IPv6/UDP sockets?
  117. EXPECT_EQ(0, getsockopt(socknum, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
  118. &options, &len)) << strerror(errno);
  119. EXPECT_EQ(IPV6_PMTUDISC_DONT, options);
  120. #endif
  121. }
  122. }
  123. // Generic version of the socket test. It creates the socket and checks that
  124. // it is a valid descriptor. The family-specific check functions are called
  125. // to check that the socket is valid. The function is parameterised according
  126. // to the structure used to hold the address.
  127. //
  128. // Arguments:
  129. // socket_type Type of socket to create (SOCK_DGRAM or SOCK_STREAM)
  130. // socket_check Associated check function - udpCheck() or tcpCheck()
  131. template <typename ADDRTYPE>
  132. void testAnyCreate(int socket_type, socket_check_t socket_check) {
  133. // Create the socket.
  134. ADDRTYPE addr;
  135. memset(&addr, 0, sizeof(addr));
  136. setAddressFamilyFields(&addr);
  137. sockaddr* addr_ptr = reinterpret_cast<sockaddr*>(&addr);
  138. const int socket = getSock(socket_type, addr_ptr, sizeof(addr));
  139. ASSERT_GE(socket, 0) << "Couldn't create socket: failed with " <<
  140. "return code " << socket << " and error " << strerror(errno);
  141. // Perform socket-type-specific testing.
  142. socket_check(socket);
  143. // Do address-family-independent
  144. int options;
  145. socklen_t len = sizeof(options);
  146. EXPECT_EQ(0, getsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &options, &len));
  147. EXPECT_NE(0, options);
  148. // ...and the address-family specific tests.
  149. addressFamilySpecificCheck(&addr, socket, socket_type);
  150. // Tidy up and exit.
  151. EXPECT_EQ(0, close(socket));
  152. }
  153. // Several tests to ensure we can create the sockets.
  154. TEST(get_sock, udp4_create) {
  155. testAnyCreate<sockaddr_in>(SOCK_DGRAM, udpCheck<sockaddr_in>);
  156. }
  157. TEST(get_sock, tcp4_create) {
  158. testAnyCreate<sockaddr_in>(SOCK_STREAM, tcpCheck);
  159. }
  160. TEST(get_sock, udp6_create) {
  161. testAnyCreate<sockaddr_in6>(SOCK_DGRAM, udpCheck<sockaddr_in6>);
  162. }
  163. TEST(get_sock, tcp6_create) {
  164. testAnyCreate<sockaddr_in6>(SOCK_STREAM, tcpCheck);
  165. }
  166. // Ask the get_sock function for some nonsense and test if it is able to report
  167. // an error.
  168. TEST(get_sock, fail_with_nonsense) {
  169. sockaddr addr;
  170. memset(&addr, 0, sizeof(addr));
  171. ASSERT_LT(getSock(0, &addr, sizeof addr), 0);
  172. }
  173. // The main run() function in the socket creator takes three functions to
  174. // get the socket, send information to it, and close it. These allow for
  175. // alternatives to the system functions to be used for testing.
  176. // Replacement getSock() function.
  177. // The return value indicates the result of checks and is encoded. Using LSB
  178. // bit numbering (least-significant bit is bit 0) then:
  179. //
  180. // bit 0: 1 if "type" is known, 0 otherwise
  181. // bit 1: 1 for UDP, 0 for TCP
  182. // bit 2: 1 if address family is known, 0 otherwise
  183. // bit 3: 1 for IPv6, 0 for IPv4
  184. // bit 4: 1 if port passed was valid
  185. //
  186. // Other possible return values are:
  187. //
  188. // -1: The simulated bind() call has failed
  189. // -2: The simulated socket() call has failed
  190. int
  191. getSockDummy(const int type, struct sockaddr* addr, const socklen_t) {
  192. int result = 0;
  193. int port = 0;
  194. // Validate type field
  195. switch (type) {
  196. case SOCK_STREAM:
  197. result |= 0x01;
  198. break;
  199. case SOCK_DGRAM:
  200. result |= 0x03;
  201. break;
  202. }
  203. // Validate address family
  204. switch (addr->sa_family) {
  205. case AF_INET:
  206. result |= 0x04;
  207. port = reinterpret_cast<sockaddr_in*>(addr)->sin_port;
  208. break;
  209. case AF_INET6:
  210. result |= 0x0C;
  211. port = reinterpret_cast<sockaddr_in6*>(addr)->sin6_port;
  212. break;
  213. }
  214. // The port should be 0xffff. If it's not, we change the result.
  215. // The port of 0xbbbb means bind should fail and 0xcccc means
  216. // socket should fail.
  217. if (port != 0xffff) {
  218. errno = 0;
  219. if (port == 0xbbbb) {
  220. return (-2);
  221. } else if (port == 0xcccc) {
  222. return (-1);
  223. } else {
  224. result |= 0x10;
  225. }
  226. }
  227. return (result);
  228. }
  229. // Dummy send function - return data (the result of getSock()) to the destination.
  230. int
  231. send_FdDummy(const int destination, const int what) {
  232. // Make sure it is 1 byte so we know the length. We do not use more during
  233. // the test anyway. And even with the LS bute, we can distinguish between
  234. // the different results.
  235. const char fd_data = what & 0xff;
  236. const bool status = write_data(destination, &fd_data, sizeof(fd_data));
  237. return (status ? 0 : -1);
  238. }
  239. // Just ignore the fd and pretend success. We close invalid fds in the tests.
  240. int
  241. closeIgnore(int) {
  242. return (0);
  243. }
  244. // Generic test that it works, with various inputs and outputs.
  245. // It uses different functions to create the socket and send it and pass
  246. // data to it and check it returns correct data back, to see if the run()
  247. // parses the commands correctly.
  248. void runTest(const char* input_data, const size_t input_size,
  249. const char* output_data, const size_t output_size,
  250. bool should_succeed = true,
  251. const close_t test_close = closeIgnore,
  252. const send_fd_t send_fd = send_FdDummy)
  253. {
  254. // Prepare the input feeder and output checker processes. The feeder
  255. // process sends data from the client to run() and the checker process
  256. // reads the response and checks it against the expected response.
  257. int input_fd = 0;
  258. const pid_t input = provide_input(&input_fd, input_data, input_size);
  259. ASSERT_NE(-1, input) << "Couldn't start input feeder";
  260. int output_fd = 0;
  261. const pid_t output = check_output(&output_fd, output_data, output_size);
  262. ASSERT_NE(-1, output) << "Couldn't start output checker";
  263. // Run the body
  264. if (should_succeed) {
  265. EXPECT_NO_THROW(run(input_fd, output_fd, getSockDummy, send_fd,
  266. test_close));
  267. } else {
  268. EXPECT_THROW(run(input_fd, output_fd, getSockDummy, send_fd,
  269. test_close), isc::socket_creator::SocketCreatorError);
  270. }
  271. // Close the pipes
  272. close(input_fd);
  273. close(output_fd);
  274. // Check the subprocesses say everything is OK too
  275. EXPECT_TRUE(process_ok(input));
  276. EXPECT_TRUE(process_ok(output));
  277. }
  278. // Check it terminates successfully when asked to.
  279. TEST(run, terminate) {
  280. runTest("T", 1, NULL, 0);
  281. }
  282. // Check it rejects incorrect input.
  283. TEST(run, bad_input) {
  284. runTest("XXX", 3, "FI", 2, false);
  285. }
  286. // Check it correctly parses query stream to create sockets.
  287. TEST(run, sockets) {
  288. runTest(
  289. // Commands:
  290. "SU4\xff\xff\0\0\0\0" // IPv4 UDP socket, port 0xffffff, address 0.0.0.0
  291. "ST4\xff\xff\0\0\0\0" // IPv4 TCP socket, port 0xffffff, address 0.0.0.0
  292. "ST6\xff\xff\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  293. // IPv6 UDP socket, port 0xffffff, address ::
  294. "SU6\xff\xff\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  295. // IPv6 TCP socket, port 0xffffff, address ::
  296. "T", // ... and terminate
  297. 9 + 9 + 21 + 21 + 1, // Length of command string
  298. "S\x07S\x05S\x0dS\x0f", // Response ("S" + LS byte of getSock() return)
  299. 8); // Length of response
  300. }
  301. // Check if failures of get_socket are handled correctly.
  302. TEST(run, bad_sockets) {
  303. // We need to construct the answer, but it depends on int length. We expect
  304. // two failure answers in this test, each answer comprising two characters
  305. // followed by the (int) errno value.
  306. char result[2 * (2 + sizeof(int))];
  307. // We expect the errno parts to be zero but the characters to depend on the
  308. // exact failure.
  309. memset(result, 0, sizeof(result));
  310. strcpy(result, "EB");
  311. strcpy(result + 2 + sizeof(int), "ES");
  312. // Run the test
  313. runTest(
  314. "SU4\xbb\xbb\0\0\0\0" // Port number will trigger simulated bind() fail
  315. "SU4\xcc\xcc\0\0\0\0" // Port number will trigger simulated socket() fail
  316. "T", // Terminate
  317. 19, // Length of command string
  318. result, sizeof(result));
  319. }
  320. // A close that fails. (This causes an abort.)
  321. int
  322. closeFail(int) {
  323. return (-1);
  324. }
  325. TEST(run, cant_close) {
  326. runTest("SU4\xff\xff\0\0\0\0", 9,
  327. "S\x07", 2,
  328. false, closeFail);
  329. }
  330. // A send of the file descriptor that fails. In this case we expect the client
  331. // to receive the "S" indicating that the descriptor is being sent and nothing
  332. // else. This causes an abort.
  333. int
  334. sendFDFail(const int, const int) {
  335. return (FD_SYSTEM_ERROR);
  336. }
  337. TEST(run, cant_send_fd) {
  338. runTest("SU4\xff\xff\0\0\0\0", 9,
  339. "S", 1,
  340. false, closeIgnore, sendFDFail);
  341. }
  342. } // Anonymous namespace