utilunix__my_system-fork_child.c 4.9 KB

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