utilunix__my_system-fork_child.c 5.2 KB

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