utilunix__my_system-common.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. lib - common code for testing lib/utilinux:my_system() function
  3. Copyright (C) 2013-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2013
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <signal.h>
  20. #include <unistd.h>
  21. #include "lib/vfs/vfs.h"
  22. /* sighandler_t is GNU extension */
  23. #ifndef HAVE_SIGHANDLER_T
  24. typedef void (*sighandler_t) (int);
  25. #endif
  26. /* --------------------------------------------------------------------------------------------- */
  27. /* @CapturedValue */
  28. static sigset_t *sigemptyset_set__captured;
  29. /* @ThenReturnValue */
  30. static int sigemptyset__return_value = 0;
  31. /* @Mock */
  32. int
  33. sigemptyset (sigset_t *set)
  34. {
  35. sigemptyset_set__captured = set;
  36. return sigemptyset__return_value;
  37. }
  38. /* --------------------------------------------------------------------------------------------- */
  39. /* @CapturedValue */
  40. static GPtrArray *sigaction_signum__captured = NULL;
  41. /* @CapturedValue */
  42. static GPtrArray *sigaction_act__captured = NULL;
  43. /* @CapturedValue */
  44. static GPtrArray *sigaction_oldact__captured = NULL;
  45. /* @ThenReturnValue */
  46. static int sigaction__return_value = 0;
  47. /* @Mock */
  48. int
  49. sigaction (int signum, const struct sigaction *act, struct sigaction *oldact)
  50. {
  51. int *tmp_signum;
  52. struct sigaction *tmp_act;
  53. /* store signum */
  54. tmp_signum = g_new (int, 1);
  55. memcpy (tmp_signum, &signum, sizeof (*tmp_signum));
  56. if (sigaction_signum__captured != NULL)
  57. g_ptr_array_add (sigaction_signum__captured, tmp_signum);
  58. /* store act */
  59. if (act != NULL)
  60. {
  61. tmp_act = g_new (struct sigaction, 1);
  62. memcpy (tmp_act, act, sizeof (*tmp_act));
  63. }
  64. else
  65. tmp_act = NULL;
  66. if (sigaction_act__captured != NULL)
  67. g_ptr_array_add (sigaction_act__captured, tmp_act);
  68. /* store oldact */
  69. if (oldact != NULL)
  70. {
  71. tmp_act = g_new (struct sigaction, 1);
  72. memcpy (tmp_act, oldact, sizeof (*tmp_act));
  73. }
  74. else
  75. tmp_act = NULL;
  76. if (sigaction_oldact__captured != NULL)
  77. g_ptr_array_add (sigaction_oldact__captured, tmp_act);
  78. return sigaction__return_value;
  79. }
  80. static void
  81. sigaction__init (void)
  82. {
  83. sigaction_signum__captured = g_ptr_array_new_with_free_func (g_free);
  84. sigaction_act__captured = g_ptr_array_new_with_free_func (g_free);
  85. sigaction_oldact__captured = g_ptr_array_new_with_free_func (g_free);
  86. }
  87. static void
  88. sigaction__deinit (void)
  89. {
  90. g_ptr_array_free (sigaction_signum__captured, TRUE);
  91. sigaction_signum__captured = NULL;
  92. g_ptr_array_free (sigaction_act__captured, TRUE);
  93. sigaction_act__captured = NULL;
  94. g_ptr_array_free (sigaction_oldact__captured, TRUE);
  95. sigaction_oldact__captured = NULL;
  96. }
  97. /* --------------------------------------------------------------------------------------------- */
  98. /* @CapturedValue */
  99. static GPtrArray *signal_signum__captured;
  100. /* @CapturedValue */
  101. static GPtrArray *signal_handler__captured;
  102. /* @ThenReturnValue */
  103. static sighandler_t signal__return_value = NULL;
  104. /* @Mock */
  105. sighandler_t
  106. signal (int signum, sighandler_t handler)
  107. {
  108. int *tmp_signum;
  109. sighandler_t *tmp_handler;
  110. /* store signum */
  111. tmp_signum = g_new (int, 1);
  112. memcpy (tmp_signum, &signum, sizeof (*tmp_signum));
  113. g_ptr_array_add (signal_signum__captured, tmp_signum);
  114. /* store handler */
  115. if (handler != SIG_DFL)
  116. {
  117. tmp_handler = g_new (sighandler_t, 1);
  118. memcpy (tmp_handler, handler, sizeof (*tmp_handler));
  119. }
  120. else
  121. tmp_handler = (void *) SIG_DFL;
  122. g_ptr_array_add (signal_handler__captured, tmp_handler);
  123. return signal__return_value;
  124. }
  125. static void
  126. signal__init (void)
  127. {
  128. signal_signum__captured = g_ptr_array_new_with_free_func (g_free);
  129. signal_handler__captured = g_ptr_array_new_with_free_func (g_free);
  130. }
  131. static void
  132. signal__deinit (void)
  133. {
  134. g_ptr_array_free (signal_signum__captured, TRUE);
  135. signal_signum__captured = NULL;
  136. g_ptr_array_free (signal_handler__captured, TRUE);
  137. signal_handler__captured = NULL;
  138. }
  139. /* --------------------------------------------------------------------------------------------- */
  140. /* @ThenReturnValue */
  141. static pid_t fork__return_value;
  142. /* @Mock */
  143. pid_t
  144. fork (void)
  145. {
  146. return fork__return_value;
  147. }
  148. /* --------------------------------------------------------------------------------------------- */
  149. /* @CapturedValue */
  150. static int my_exit__status__captured;
  151. /* @Mock */
  152. void
  153. my_exit (int status)
  154. {
  155. my_exit__status__captured = status;
  156. }
  157. /* --------------------------------------------------------------------------------------------- */
  158. /* @CapturedValue */
  159. static char *execvp__file__captured = NULL;
  160. /* @CapturedValue */
  161. static GPtrArray *execvp__args__captured;
  162. /* @ThenReturnValue */
  163. static int execvp__return_value = 0;
  164. /* @Mock */
  165. int
  166. execvp (const char *file, char *const argv[])
  167. {
  168. char **one_arg;
  169. execvp__file__captured = g_strdup (file);
  170. for (one_arg = (char **) argv; *one_arg != NULL; one_arg++)
  171. g_ptr_array_add (execvp__args__captured, g_strdup (*one_arg));
  172. return execvp__return_value;
  173. }
  174. static void
  175. execvp__init (void)
  176. {
  177. execvp__args__captured = g_ptr_array_new_with_free_func (g_free);
  178. }
  179. static void
  180. execvp__deinit (void)
  181. {
  182. g_ptr_array_free (execvp__args__captured, TRUE);
  183. execvp__args__captured = NULL;
  184. MC_PTR_FREE (execvp__file__captured);
  185. }
  186. /* --------------------------------------------------------------------------------------------- */
  187. #define VERIFY_SIGACTION__ACT_IGNORED(_pntr) { \
  188. struct sigaction *_act = (struct sigaction *) _pntr; \
  189. mctest_assert_ptr_eq (_act->sa_handler, SIG_IGN); \
  190. ck_assert_int_eq (_act->sa_flags, 0); \
  191. }
  192. #define VERIFY_SIGACTION__IS_RESTORED(oldact_idx, act_idx) { \
  193. struct sigaction *_oldact = (struct sigaction *) g_ptr_array_index(sigaction_oldact__captured, oldact_idx); \
  194. struct sigaction *_act = (struct sigaction *) g_ptr_array_index(sigaction_act__captured, act_idx); \
  195. ck_assert_msg (memcmp(_oldact, _act, sizeof(struct sigaction)) == 0, \
  196. "sigaction(): oldact[%d] should be equals to act[%d]", oldact_idx, act_idx); \
  197. }
  198. /* @Verify */
  199. #define VERIFY_SIGACTION_CALLS() { \
  200. ck_assert_int_eq (sigaction_signum__captured->len, 6); \
  201. \
  202. ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 0)), SIGINT); \
  203. ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 1)), SIGQUIT); \
  204. ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 2)), SIGTSTP); \
  205. ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 3)), SIGINT); \
  206. ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 4)), SIGQUIT); \
  207. ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 5)), SIGTSTP); \
  208. \
  209. VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 0)); \
  210. VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 1)); \
  211. { \
  212. struct sigaction *_act = g_ptr_array_index(sigaction_act__captured, 2); \
  213. ck_assert_msg (memcmp (_act, &startup_handler, sizeof(struct sigaction)) == 0, \
  214. "The 'act' in third call to sigaction() should be equals to startup_handler"); \
  215. } \
  216. \
  217. VERIFY_SIGACTION__IS_RESTORED (0, 3); \
  218. VERIFY_SIGACTION__IS_RESTORED (1, 4); \
  219. VERIFY_SIGACTION__IS_RESTORED (2, 5); \
  220. \
  221. ck_assert_msg (g_ptr_array_index(sigaction_oldact__captured, 3) == NULL, \
  222. "oldact in fourth call to sigaction() should be NULL"); \
  223. ck_assert_msg (g_ptr_array_index(sigaction_oldact__captured, 4) == NULL, \
  224. "oldact in fifth call to sigaction() should be NULL"); \
  225. ck_assert_msg (g_ptr_array_index(sigaction_oldact__captured, 5) == NULL, \
  226. "oldact in sixth call to sigaction() should be NULL"); \
  227. }
  228. /* --------------------------------------------------------------------------------------------- */
  229. #define VERIFY_SIGNAL_HANDLER_IS_SIG_DFL(_idx) { \
  230. sighandler_t *tmp_handler = (sighandler_t *) g_ptr_array_index(signal_handler__captured, _idx);\
  231. mctest_assert_ptr_eq (tmp_handler, (sighandler_t *) SIG_DFL); \
  232. }
  233. /* @Verify */
  234. #define VERIFY_SIGNAL_CALLS() { \
  235. ck_assert_int_eq (signal_signum__captured->len, 4); \
  236. ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 0)), SIGINT); \
  237. ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 1)), SIGQUIT); \
  238. ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 2)), SIGTSTP); \
  239. ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 3)), SIGCHLD); \
  240. \
  241. VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (0); \
  242. VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (1); \
  243. VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (2); \
  244. VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (3); \
  245. }
  246. /* --------------------------------------------------------------------------------------------- */
  247. /* @Before */
  248. static void
  249. setup (void)
  250. {
  251. signal__return_value = NULL;
  252. sigaction__init ();
  253. signal__init ();
  254. execvp__init ();
  255. }
  256. /* --------------------------------------------------------------------------------------------- */
  257. /* @After */
  258. static void
  259. teardown (void)
  260. {
  261. execvp__deinit ();
  262. signal__deinit ();
  263. sigaction__deinit ();
  264. }
  265. /* --------------------------------------------------------------------------------------------- */