testutil_unity.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. Copyright (c) 2007-2019 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "testutil_unity.hpp"
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #ifdef _WIN32
  28. #include <direct.h>
  29. #else
  30. #include <unistd.h>
  31. #endif
  32. int test_assert_success_message_errno_helper (int rc_,
  33. const char *msg_,
  34. const char *expr_,
  35. int line_)
  36. {
  37. if (rc_ == -1) {
  38. char buffer[512];
  39. buffer[sizeof (buffer) - 1] =
  40. 0; // to ensure defined behavior with VC++ <= 2013
  41. snprintf (buffer, sizeof (buffer) - 1,
  42. "%s failed%s%s%s, errno = %i (%s)", expr_,
  43. msg_ ? " (additional info: " : "", msg_ ? msg_ : "",
  44. msg_ ? ")" : "", zmq_errno (), zmq_strerror (zmq_errno ()));
  45. UNITY_TEST_FAIL (line_, buffer);
  46. }
  47. return rc_;
  48. }
  49. int test_assert_success_message_raw_errno_helper (
  50. int rc_, const char *msg_, const char *expr_, int line_, bool zero)
  51. {
  52. if (rc_ == -1 || (zero && rc_ != 0)) {
  53. #if defined ZMQ_HAVE_WINDOWS
  54. int current_errno = WSAGetLastError ();
  55. #else
  56. int current_errno = errno;
  57. #endif
  58. char buffer[512];
  59. buffer[sizeof (buffer) - 1] =
  60. 0; // to ensure defined behavior with VC++ <= 2013
  61. snprintf (
  62. buffer, sizeof (buffer) - 1, "%s failed%s%s%s with %d, errno = %i/%s",
  63. expr_, msg_ ? " (additional info: " : "", msg_ ? msg_ : "",
  64. msg_ ? ")" : "", rc_, current_errno, strerror (current_errno));
  65. UNITY_TEST_FAIL (line_, buffer);
  66. }
  67. return rc_;
  68. }
  69. int test_assert_success_message_raw_zero_errno_helper (int rc_,
  70. const char *msg_,
  71. const char *expr_,
  72. int line_)
  73. {
  74. return test_assert_success_message_raw_errno_helper (rc_, msg_, expr_,
  75. line_, true);
  76. }
  77. int test_assert_failure_message_raw_errno_helper (
  78. int rc_, int expected_errno_, const char *msg_, const char *expr_, int line_)
  79. {
  80. char buffer[512];
  81. buffer[sizeof (buffer) - 1] =
  82. 0; // to ensure defined behavior with VC++ <= 2013
  83. if (rc_ != -1) {
  84. snprintf (buffer, sizeof (buffer) - 1,
  85. "%s was unexpectedly successful%s%s%s, expected "
  86. "errno = %i, actual return value = %i",
  87. expr_, msg_ ? " (additional info: " : "", msg_ ? msg_ : "",
  88. msg_ ? ")" : "", expected_errno_, rc_);
  89. UNITY_TEST_FAIL (line_, buffer);
  90. } else {
  91. #if defined ZMQ_HAVE_WINDOWS
  92. int current_errno = WSAGetLastError ();
  93. #else
  94. int current_errno = errno;
  95. #endif
  96. if (current_errno != expected_errno_) {
  97. snprintf (buffer, sizeof (buffer) - 1,
  98. "%s failed with an unexpected error%s%s%s, expected "
  99. "errno = %i, actual errno = %i",
  100. expr_, msg_ ? " (additional info: " : "",
  101. msg_ ? msg_ : "", msg_ ? ")" : "", expected_errno_,
  102. current_errno);
  103. UNITY_TEST_FAIL (line_, buffer);
  104. }
  105. }
  106. return rc_;
  107. }
  108. void send_string_expect_success (void *socket_, const char *str_, int flags_)
  109. {
  110. const size_t len = str_ ? strlen (str_) : 0;
  111. const int rc = zmq_send (socket_, str_, len, flags_);
  112. TEST_ASSERT_EQUAL_INT ((int) len, rc);
  113. }
  114. void recv_string_expect_success (void *socket_, const char *str_, int flags_)
  115. {
  116. const size_t len = str_ ? strlen (str_) : 0;
  117. char buffer[255];
  118. TEST_ASSERT_LESS_OR_EQUAL_MESSAGE (sizeof (buffer), len,
  119. "recv_string_expect_success cannot be "
  120. "used for strings longer than 255 "
  121. "characters");
  122. const int rc = TEST_ASSERT_SUCCESS_ERRNO (
  123. zmq_recv (socket_, buffer, sizeof (buffer), flags_));
  124. TEST_ASSERT_EQUAL_INT ((int) len, rc);
  125. if (str_)
  126. TEST_ASSERT_EQUAL_STRING_LEN (str_, buffer, len);
  127. }
  128. static void *internal_manage_test_context (bool init_, bool clear_)
  129. {
  130. static void *test_context = NULL;
  131. if (clear_) {
  132. TEST_ASSERT_NOT_NULL (test_context);
  133. TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_term (test_context));
  134. test_context = NULL;
  135. } else {
  136. if (init_) {
  137. TEST_ASSERT_NULL (test_context);
  138. test_context = zmq_ctx_new ();
  139. TEST_ASSERT_NOT_NULL (test_context);
  140. }
  141. }
  142. return test_context;
  143. }
  144. static void internal_manage_test_sockets (void *socket_, bool add_)
  145. {
  146. static void *test_sockets[MAX_TEST_SOCKETS];
  147. static size_t test_socket_count = 0;
  148. if (!socket_) {
  149. TEST_ASSERT_FALSE (add_);
  150. // force-close all sockets
  151. if (test_socket_count) {
  152. for (size_t i = 0; i < test_socket_count; ++i) {
  153. close_zero_linger (test_sockets[i]);
  154. }
  155. fprintf (stderr,
  156. "WARNING: Forced closure of %i sockets, this is an "
  157. "implementation error unless the test case failed\n",
  158. static_cast<int> (test_socket_count));
  159. test_socket_count = 0;
  160. }
  161. } else {
  162. if (add_) {
  163. ++test_socket_count;
  164. TEST_ASSERT_LESS_THAN_MESSAGE (MAX_TEST_SOCKETS, test_socket_count,
  165. "MAX_TEST_SOCKETS must be "
  166. "increased, or you cannot use the "
  167. "test context");
  168. test_sockets[test_socket_count - 1] = socket_;
  169. } else {
  170. bool found = false;
  171. for (size_t i = 0; i < test_socket_count; ++i) {
  172. if (test_sockets[i] == socket_) {
  173. found = true;
  174. }
  175. if (found) {
  176. if (i < test_socket_count)
  177. test_sockets[i] = test_sockets[i + 1];
  178. }
  179. }
  180. TEST_ASSERT_TRUE_MESSAGE (found,
  181. "Attempted to close a socket that was "
  182. "not created by test_context_socket");
  183. --test_socket_count;
  184. }
  185. }
  186. }
  187. void setup_test_context ()
  188. {
  189. internal_manage_test_context (true, false);
  190. }
  191. void *get_test_context ()
  192. {
  193. return internal_manage_test_context (false, false);
  194. }
  195. void teardown_test_context ()
  196. {
  197. // this condition allows an explicit call to teardown_test_context from a
  198. // test. if this is never used, it should probably be removed, to detect
  199. // misuses
  200. if (get_test_context ()) {
  201. internal_manage_test_sockets (NULL, false);
  202. internal_manage_test_context (false, true);
  203. }
  204. }
  205. void *test_context_socket (int type_)
  206. {
  207. void *const socket = zmq_socket (get_test_context (), type_);
  208. TEST_ASSERT_NOT_NULL (socket);
  209. internal_manage_test_sockets (socket, true);
  210. return socket;
  211. }
  212. void *test_context_socket_close (void *socket_)
  213. {
  214. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (socket_));
  215. internal_manage_test_sockets (socket_, false);
  216. return socket_;
  217. }
  218. void *test_context_socket_close_zero_linger (void *socket_)
  219. {
  220. const int linger = 0;
  221. int rc = zmq_setsockopt (socket_, ZMQ_LINGER, &linger, sizeof (linger));
  222. TEST_ASSERT_TRUE (rc == 0 || zmq_errno () == ETERM);
  223. return test_context_socket_close (socket_);
  224. }
  225. void test_bind (void *socket_,
  226. const char *bind_address_,
  227. char *my_endpoint_,
  228. size_t len_)
  229. {
  230. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (socket_, bind_address_));
  231. TEST_ASSERT_SUCCESS_ERRNO (
  232. zmq_getsockopt (socket_, ZMQ_LAST_ENDPOINT, my_endpoint_, &len_));
  233. }
  234. void bind_loopback (void *socket_, int ipv6_, char *my_endpoint_, size_t len_)
  235. {
  236. if (ipv6_ && !is_ipv6_available ()) {
  237. TEST_IGNORE_MESSAGE ("ipv6 is not available");
  238. }
  239. TEST_ASSERT_SUCCESS_ERRNO (
  240. zmq_setsockopt (socket_, ZMQ_IPV6, &ipv6_, sizeof (int)));
  241. test_bind (socket_, ipv6_ ? "tcp://[::1]:*" : "tcp://127.0.0.1:*",
  242. my_endpoint_, len_);
  243. }
  244. void bind_loopback_ipv4 (void *socket_, char *my_endpoint_, size_t len_)
  245. {
  246. bind_loopback (socket_, false, my_endpoint_, len_);
  247. }
  248. void bind_loopback_ipv6 (void *socket_, char *my_endpoint_, size_t len_)
  249. {
  250. bind_loopback (socket_, true, my_endpoint_, len_);
  251. }
  252. void bind_loopback_ipc (void *socket_, char *my_endpoint_, size_t len_)
  253. {
  254. if (!zmq_has ("ipc")) {
  255. TEST_IGNORE_MESSAGE ("ipc is not available");
  256. }
  257. test_bind (socket_, "ipc://*", my_endpoint_, len_);
  258. }
  259. void bind_loopback_tipc (void *socket_, char *my_endpoint_, size_t len_)
  260. {
  261. if (!is_tipc_available ()) {
  262. TEST_IGNORE_MESSAGE ("tipc is not available");
  263. }
  264. test_bind (socket_, "tipc://<*>", my_endpoint_, len_);
  265. }
  266. #if defined(ZMQ_HAVE_IPC) && !defined(ZMQ_HAVE_GNU)
  267. void make_random_ipc_endpoint (char *out_endpoint_)
  268. {
  269. #ifdef ZMQ_HAVE_WINDOWS
  270. char random_file[MAX_PATH];
  271. {
  272. const errno_t rc = tmpnam_s (random_file);
  273. TEST_ASSERT_EQUAL (0, rc);
  274. }
  275. // TODO or use CreateDirectoryA and specify permissions?
  276. const int rc = _mkdir (random_file);
  277. TEST_ASSERT_EQUAL (0, rc);
  278. strcat (random_file, "/ipc");
  279. #else
  280. char random_file[16];
  281. strcpy (random_file, "tmpXXXXXX");
  282. #ifdef HAVE_MKDTEMP
  283. TEST_ASSERT_TRUE (mkdtemp (random_file));
  284. strcat (random_file, "/ipc");
  285. #else
  286. int fd = mkstemp (random_file);
  287. TEST_ASSERT_TRUE (fd != -1);
  288. close (fd);
  289. #endif
  290. #endif
  291. strcpy (out_endpoint_, "ipc://");
  292. strcat (out_endpoint_, random_file);
  293. }
  294. #endif