utilunix__my_system-fork_child.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. lib - tests 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. #define TEST_SUITE_NAME "/lib/utilunix"
  20. #include "tests/mctest.h"
  21. #include "lib/util.h"
  22. #include "utilunix__my_system-common.c"
  23. /* --------------------------------------------------------------------------------------------- */
  24. /* *INDENT-OFF* */
  25. START_TEST (fork_child)
  26. /* *INDENT-ON* */
  27. {
  28. int actual_value;
  29. /* given */
  30. fork__return_value = 0;
  31. /* when */
  32. actual_value = my_system (0, "/bin/some-command", "some parameter");
  33. /* then */
  34. ck_assert_int_eq (actual_value, 0);
  35. VERIFY_SIGACTION_CALLS ();
  36. VERIFY_SIGNAL_CALLS ();
  37. mctest_assert_str_eq (execvp__file__captured, "/bin/some-command");
  38. ck_assert_int_eq (execvp__args__captured->len, 2);
  39. mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 0), "/bin/some-command");
  40. mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 1), "some parameter");
  41. /* All exec* calls is mocked, so call to _exit() function with 127 status code it's a normal
  42. * situation */
  43. ck_assert_int_eq (my_exit__status__captured, 127);
  44. }
  45. /* *INDENT-OFF* */
  46. END_TEST
  47. /* *INDENT-ON* */
  48. /* --------------------------------------------------------------------------------------------- */
  49. /* *INDENT-OFF* */
  50. START_TEST (fork_child_tokens)
  51. /* *INDENT-ON* */
  52. {
  53. int actual_value;
  54. /* given */
  55. fork__return_value = 0;
  56. /* when */
  57. actual_value = my_system (0, "vi +", "foo.txt");
  58. /* then */
  59. ck_assert_int_eq (actual_value, 0);
  60. VERIFY_SIGACTION_CALLS ();
  61. VERIFY_SIGNAL_CALLS ();
  62. mctest_assert_str_eq (execvp__file__captured, "vi");
  63. ck_assert_int_eq (execvp__args__captured->len, 3);
  64. mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 0), "vi");
  65. mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 1), "+");
  66. mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 2), "foo.txt");
  67. /* All exec* calls is mocked, so call to _exit() function with 127 status code it's a normal
  68. * situation */
  69. ck_assert_int_eq (my_exit__status__captured, 127);
  70. }
  71. /* *INDENT-OFF* */
  72. END_TEST
  73. /* *INDENT-ON* */
  74. /* --------------------------------------------------------------------------------------------- */
  75. /* *INDENT-OFF* */
  76. START_TEST (fork_child_tokens2)
  77. /* *INDENT-ON* */
  78. {
  79. int actual_value;
  80. /* given */
  81. fork__return_value = 0;
  82. /* when */
  83. actual_value = my_system (0, "qwe -a 'aa bb' -b -c cc -d \"dd ee\" -f ff\\ gg", "foo.txt");
  84. /* then */
  85. ck_assert_int_eq (actual_value, 0);
  86. VERIFY_SIGACTION_CALLS ();
  87. VERIFY_SIGNAL_CALLS ();
  88. mctest_assert_str_eq (execvp__file__captured, "qwe");
  89. ck_assert_int_eq (execvp__args__captured->len, 11);
  90. mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 0), "qwe");
  91. mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 1), "-a");
  92. mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 2), "'aa bb'");
  93. mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 3), "-b");
  94. mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 4), "-c");
  95. mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 5), "cc");
  96. mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 6), "-d");
  97. mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 7), "\"dd ee\"");
  98. mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 8), "-f");
  99. mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 9), "ff\\ gg");
  100. mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 10), "foo.txt");
  101. /* All exec* calls is mocked, so call to _exit() function with 127 status code it's a normal
  102. * situation */
  103. ck_assert_int_eq (my_exit__status__captured, 127);
  104. }
  105. /* *INDENT-OFF* */
  106. END_TEST
  107. /* *INDENT-ON* */
  108. /* --------------------------------------------------------------------------------------------- */
  109. int
  110. main (void)
  111. {
  112. TCase *tc_core;
  113. tc_core = tcase_create ("Core");
  114. tcase_add_checked_fixture (tc_core, setup, teardown);
  115. /* Add new tests here: *************** */
  116. tcase_add_test (tc_core, fork_child);
  117. tcase_add_test (tc_core, fork_child_tokens);
  118. tcase_add_test (tc_core, fork_child_tokens2);
  119. /* *********************************** */
  120. return mctest_run_all (tc_core);
  121. }
  122. /* --------------------------------------------------------------------------------------------- */