utilunix__my_system-common.c 10 KB

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