utilunix__my_system-common.c 9.9 KB

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