utilunix__my_system-common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. lib - common code for testing lib/utilinux:my_system() function
  3. Copyright (C) 2013-2019
  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 ();
  84. sigaction_act__captured = g_ptr_array_new ();
  85. sigaction_oldact__captured = g_ptr_array_new ();
  86. }
  87. static void
  88. sigaction__deinit (void)
  89. {
  90. g_ptr_array_foreach (sigaction_signum__captured, (GFunc) g_free, NULL);
  91. g_ptr_array_free (sigaction_signum__captured, TRUE);
  92. sigaction_signum__captured = NULL;
  93. g_ptr_array_foreach (sigaction_act__captured, (GFunc) g_free, NULL);
  94. g_ptr_array_free (sigaction_act__captured, TRUE);
  95. sigaction_act__captured = NULL;
  96. g_ptr_array_foreach (sigaction_oldact__captured, (GFunc) g_free, NULL);
  97. g_ptr_array_free (sigaction_oldact__captured, TRUE);
  98. sigaction_oldact__captured = NULL;
  99. }
  100. /* --------------------------------------------------------------------------------------------- */
  101. /* @CapturedValue */
  102. static GPtrArray *signal_signum__captured;
  103. /* @CapturedValue */
  104. static GPtrArray *signal_handler__captured;
  105. /* @ThenReturnValue */
  106. static sighandler_t signal__return_value = NULL;
  107. /* @Mock */
  108. sighandler_t
  109. signal (int signum, sighandler_t handler)
  110. {
  111. int *tmp_signum;
  112. sighandler_t *tmp_handler;
  113. /* store signum */
  114. tmp_signum = g_new (int, 1);
  115. memcpy (tmp_signum, &signum, sizeof (*tmp_signum));
  116. g_ptr_array_add (signal_signum__captured, tmp_signum);
  117. /* store handler */
  118. if (handler != SIG_DFL)
  119. {
  120. tmp_handler = g_new (sighandler_t, 1);
  121. memcpy (tmp_handler, handler, sizeof (*tmp_handler));
  122. }
  123. else
  124. tmp_handler = (void *) SIG_DFL;
  125. g_ptr_array_add (signal_handler__captured, tmp_handler);
  126. return signal__return_value;
  127. }
  128. static void
  129. signal__init (void)
  130. {
  131. signal_signum__captured = g_ptr_array_new ();
  132. signal_handler__captured = g_ptr_array_new ();
  133. }
  134. static void
  135. signal__deinit (void)
  136. {
  137. g_ptr_array_foreach (signal_signum__captured, (GFunc) g_free, NULL);
  138. g_ptr_array_free (signal_signum__captured, TRUE);
  139. signal_signum__captured = NULL;
  140. g_ptr_array_foreach (signal_handler__captured, (GFunc) g_free, NULL);
  141. g_ptr_array_free (signal_handler__captured, TRUE);
  142. signal_handler__captured = NULL;
  143. }
  144. /* --------------------------------------------------------------------------------------------- */
  145. /* @ThenReturnValue */
  146. static pid_t fork__return_value;
  147. /* @Mock */
  148. pid_t
  149. fork (void)
  150. {
  151. return fork__return_value;
  152. }
  153. /* --------------------------------------------------------------------------------------------- */
  154. /* @CapturedValue */
  155. static int my_exit__status__captured;
  156. /* @Mock */
  157. void
  158. my_exit (int status)
  159. {
  160. my_exit__status__captured = status;
  161. }
  162. /* --------------------------------------------------------------------------------------------- */
  163. /* @CapturedValue */
  164. static char *execvp__file__captured = NULL;
  165. /* @CapturedValue */
  166. static GPtrArray *execvp__args__captured;
  167. /* @ThenReturnValue */
  168. static int execvp__return_value = 0;
  169. /* @Mock */
  170. int
  171. execvp (const char *file, char *const argv[])
  172. {
  173. char **one_arg;
  174. execvp__file__captured = g_strdup (file);
  175. for (one_arg = (char **) argv; *one_arg != NULL; one_arg++)
  176. g_ptr_array_add (execvp__args__captured, g_strdup (*one_arg));
  177. return execvp__return_value;
  178. }
  179. static void
  180. execvp__init (void)
  181. {
  182. execvp__args__captured = g_ptr_array_new ();
  183. }
  184. static void
  185. execvp__deinit (void)
  186. {
  187. g_ptr_array_foreach (execvp__args__captured, (GFunc) g_free, NULL);
  188. g_ptr_array_free (execvp__args__captured, TRUE);
  189. execvp__args__captured = NULL;
  190. MC_PTR_FREE (execvp__file__captured);
  191. }
  192. /* --------------------------------------------------------------------------------------------- */
  193. #define VERIFY_SIGACTION__ACT_IGNORED(_pntr) { \
  194. struct sigaction *_act = (struct sigaction *) _pntr; \
  195. mctest_assert_ptr_eq (_act->sa_handler, SIG_IGN); \
  196. mctest_assert_int_eq (_act->sa_flags, 0); \
  197. }
  198. #define VERIFY_SIGACTION__IS_RESTORED(oldact_idx, act_idx) { \
  199. struct sigaction *_oldact = (struct sigaction *) g_ptr_array_index(sigaction_oldact__captured, oldact_idx); \
  200. struct sigaction *_act = (struct sigaction *) g_ptr_array_index(sigaction_act__captured, act_idx); \
  201. fail_unless (memcmp(_oldact, _act, sizeof(struct sigaction)) == 0, \
  202. "sigaction(): oldact[%d] should be equals to act[%d]", oldact_idx, act_idx); \
  203. }
  204. /* @Verify */
  205. #define VERIFY_SIGACTION_CALLS() { \
  206. mctest_assert_int_eq (sigaction_signum__captured->len, 6); \
  207. \
  208. mctest_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 0)), SIGINT); \
  209. mctest_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 1)), SIGQUIT); \
  210. mctest_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 2)), SIGTSTP); \
  211. mctest_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 3)), SIGINT); \
  212. mctest_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 4)), SIGQUIT); \
  213. mctest_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 5)), SIGTSTP); \
  214. \
  215. VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 0)); \
  216. VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 1)); \
  217. { \
  218. struct sigaction *_act = g_ptr_array_index(sigaction_act__captured, 2); \
  219. fail_unless (memcmp (_act, &startup_handler, sizeof(struct sigaction)) == 0, \
  220. "The 'act' in third call to sigaction() should be equals to startup_handler"); \
  221. } \
  222. \
  223. VERIFY_SIGACTION__IS_RESTORED (0, 3); \
  224. VERIFY_SIGACTION__IS_RESTORED (1, 4); \
  225. VERIFY_SIGACTION__IS_RESTORED (2, 5); \
  226. \
  227. fail_unless (g_ptr_array_index(sigaction_oldact__captured, 3) == NULL, \
  228. "oldact in fourth call to sigaction() should be NULL"); \
  229. fail_unless (g_ptr_array_index(sigaction_oldact__captured, 4) == NULL, \
  230. "oldact in fifth call to sigaction() should be NULL"); \
  231. fail_unless (g_ptr_array_index(sigaction_oldact__captured, 5) == NULL, \
  232. "oldact in sixth call to sigaction() should be NULL"); \
  233. }
  234. /* --------------------------------------------------------------------------------------------- */
  235. #define VERIFY_SIGNAL_HANDLER_IS_SIG_DFL(_idx) { \
  236. sighandler_t *tmp_handler = (sighandler_t *) g_ptr_array_index(signal_handler__captured, _idx);\
  237. mctest_assert_ptr_eq (tmp_handler, (sighandler_t *) SIG_DFL); \
  238. }
  239. /* @Verify */
  240. #define VERIFY_SIGNAL_CALLS() { \
  241. mctest_assert_int_eq (signal_signum__captured->len, 4); \
  242. mctest_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 0)), SIGINT); \
  243. mctest_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 1)), SIGQUIT); \
  244. mctest_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 2)), SIGTSTP); \
  245. mctest_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 3)), SIGCHLD); \
  246. \
  247. VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (0); \
  248. VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (1); \
  249. VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (2); \
  250. VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (3); \
  251. }
  252. /* --------------------------------------------------------------------------------------------- */
  253. /* @Before */
  254. static void
  255. setup (void)
  256. {
  257. signal__return_value = NULL;
  258. sigaction__init ();
  259. signal__init ();
  260. execvp__init ();
  261. }
  262. /* --------------------------------------------------------------------------------------------- */
  263. /* @After */
  264. static void
  265. teardown (void)
  266. {
  267. execvp__deinit ();
  268. signal__deinit ();
  269. sigaction__deinit ();
  270. }
  271. /* --------------------------------------------------------------------------------------------- */