From 254aa70ac813c4f8197179362020f96500b5c765 Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Sat, 5 Jan 2013 13:50:29 +0300 Subject: [PATCH] Add tests for lib/utilunix.c:my_system() function Signed-off-by: Slava Zanko --- tests/lib/Makefile.am | 17 + tests/lib/utilunix__my_system-common.c | 380 +++++++++++++++++++++++ tests/lib/utilunix__my_system-fork_child.c | 96 ++++++ tests/lib/utilunix__my_system-fork_child_shell.c | 97 ++++++ tests/lib/utilunix__my_system-fork_fail.c | 91 ++++++ 5 files changed, 681 insertions(+) create mode 100644 tests/lib/utilunix__my_system-common.c create mode 100644 tests/lib/utilunix__my_system-fork_child.c create mode 100644 tests/lib/utilunix__my_system-fork_child_shell.c create mode 100644 tests/lib/utilunix__my_system-fork_fail.c diff --git a/tests/lib/Makefile.am b/tests/lib/Makefile.am index f5ed3a3d8..a2f38dbc8 100644 --- a/tests/lib/Makefile.am +++ b/tests/lib/Makefile.am @@ -2,13 +2,20 @@ SUBDIRS = . mcconfig search vfs widget AM_CPPFLAGS = $(GLIB_CFLAGS) -I$(top_srcdir) @CHECK_CFLAGS@ +AM_LDFLAGS = @TESTS_LDFLAGS@ + LIBS = @CHECK_LIBS@ $(top_builddir)/lib/libmc.la +EXTRA_DIST = utilunix__my_system-common.c + TESTS = \ library_independ \ mc_build_filename \ name_quote \ serialize \ + utilunix__my_system_fork_fail \ + utilunix__my_system_fork_child_shell \ + utilunix__my_system_fork_child \ x_basename check_PROGRAMS = $(TESTS) @@ -25,5 +32,15 @@ name_quote_SOURCES = \ serialize_SOURCES = \ serialize.c +utilunix__my_system_fork_fail_SOURCES = \ + utilunix__my_system-fork_fail.c + +utilunix__my_system_fork_child_shell_SOURCES = \ + utilunix__my_system-fork_child_shell.c + +utilunix__my_system_fork_child_SOURCES = \ + utilunix__my_system-fork_child.c + x_basename_SOURCES = \ x_basename.c + diff --git a/tests/lib/utilunix__my_system-common.c b/tests/lib/utilunix__my_system-common.c new file mode 100644 index 000000000..69354cef0 --- /dev/null +++ b/tests/lib/utilunix__my_system-common.c @@ -0,0 +1,380 @@ +/* + lib - common code for testing lib/utilinux:my_system() function + + Copyright (C) 2013 + The Free Software Foundation, Inc. + + Written by: + Slava Zanko , 2013 + + This file is part of the Midnight Commander. + + The Midnight Commander is free software: you can redistribute it + and/or modify it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + The Midnight Commander is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#include +#include + +#include "lib/global.h" + + +/* --------------------------------------------------------------------------------------------- */ + +/* @CapturedValue */ +static sigset_t *sigemptyset_set__captured; +/* @ThenReturnValue */ +static int sigemptyset__return_value = 0; + +/* @Mock */ +int +sigemptyset (sigset_t * set) +{ + sigemptyset_set__captured = set; + return sigemptyset__return_value; +} + +/* --------------------------------------------------------------------------------------------- */ + +/* @CapturedValue */ +static GPtrArray *sigaction_signum__captured = NULL; +/* @CapturedValue */ +static GPtrArray *sigaction_act__captured = NULL; +/* @CapturedValue */ +static GPtrArray *sigaction_oldact__captured = NULL; +/* @ThenReturnValue */ +static int sigaction__return_value = 0; + +/* @Mock */ +int +sigaction (int signum, const struct sigaction *act, struct sigaction *oldact) +{ + int *tmp_signum; + struct sigaction *tmp_act; + + /* store signum */ + tmp_signum = g_new (int, 1); + memcpy (tmp_signum, &signum, sizeof (int)); + if (sigaction_signum__captured != NULL) + g_ptr_array_add (sigaction_signum__captured, tmp_signum); + + /* store act */ + if (act != NULL) + { + tmp_act = g_new (struct sigaction, 1); + memcpy (tmp_act, act, sizeof (struct sigaction)); + } + else + tmp_act = NULL; + if (sigaction_act__captured != NULL) + g_ptr_array_add (sigaction_act__captured, tmp_act); + + /* store oldact */ + if (oldact != NULL) + { + tmp_act = g_new (struct sigaction, 1); + memcpy (tmp_act, oldact, sizeof (struct sigaction)); + } + else + tmp_act = NULL; + if (sigaction_oldact__captured != NULL) + g_ptr_array_add (sigaction_oldact__captured, tmp_act); + + return sigaction__return_value; +} + +static void +sigaction__init (void) +{ + sigaction_signum__captured = g_ptr_array_new (); + sigaction_act__captured = g_ptr_array_new (); + sigaction_oldact__captured = g_ptr_array_new (); +} + +static void +sigaction__deinit (void) +{ + g_ptr_array_foreach (sigaction_signum__captured, (GFunc) g_free, NULL); + g_ptr_array_free (sigaction_signum__captured, TRUE); + + g_ptr_array_foreach (sigaction_act__captured, (GFunc) g_free, NULL); + g_ptr_array_free (sigaction_act__captured, TRUE); + + g_ptr_array_foreach (sigaction_oldact__captured, (GFunc) g_free, NULL); + g_ptr_array_free (sigaction_oldact__captured, TRUE); +} + +/* --------------------------------------------------------------------------------------------- */ + +/* @CapturedValue */ +static GPtrArray *signal_signum__captured; +/* @CapturedValue */ +static GPtrArray *signal_handler__captured; +/* @ThenReturnValue */ +static sighandler_t signal__return_value = NULL; + +/* @Mock */ +sighandler_t +signal (int signum, sighandler_t handler) +{ + int *tmp_signum; + sighandler_t *tmp_handler; + + /* store signum */ + tmp_signum = g_new (int, 1); + memcpy (tmp_signum, &signum, sizeof (int)); + g_ptr_array_add (signal_signum__captured, tmp_signum); + + /* store handler */ + if (handler != SIG_DFL) + { + tmp_handler = g_new (sighandler_t, 1); + memcpy (tmp_handler, handler, sizeof (sighandler_t)); + } + else + tmp_handler = (void *) SIG_DFL; + g_ptr_array_add (signal_handler__captured, tmp_handler); + + return signal__return_value; +} + +static void +signal__init (void) +{ + signal_signum__captured = g_ptr_array_new (); + signal_handler__captured = g_ptr_array_new (); +} + +static void +signal__deinit (void) +{ + g_ptr_array_foreach (signal_signum__captured, (GFunc) g_free, NULL); + g_ptr_array_free (signal_signum__captured, TRUE); + + g_ptr_array_foreach (signal_handler__captured, (GFunc) g_free, NULL); + g_ptr_array_free (signal_handler__captured, TRUE); +} + +/* --------------------------------------------------------------------------------------------- */ + +/* @ThenReturnValue */ +static pid_t fork__return_value; + +/* @Mock */ +pid_t +fork (void) +{ + return fork__return_value; +} + +/* --------------------------------------------------------------------------------------------- */ +/* @CapturedValue */ +static int my_exit__status__captured; + +/* @Mock */ +void +my_exit (int status) +{ + my_exit__status__captured = status; +} + +/* --------------------------------------------------------------------------------------------- */ + +/* @CapturedValue */ +static char *execl__path__captured = NULL; +/* @CapturedValue */ +static char *execl__arg__captured = NULL; +/* @CapturedValue */ +static GPtrArray *execl__args__captured; +/* @ThenReturnValue */ +static int execl__return_value = 0; + +/* @Mock */ +int +execl (const char *path, const char *arg, ...) +{ + char *one_arg; + va_list vargs; + + execl__path__captured = g_strdup (path); + execl__arg__captured = g_strdup (arg); + + va_start (vargs, arg); + + while ((one_arg = va_arg (vargs, char *)) != NULL) + { + g_ptr_array_add (execl__args__captured, g_strdup (one_arg)); + } + va_end (vargs); + + return execl__return_value; +} + +static void +execl__init (void) +{ + execl__args__captured = g_ptr_array_new (); +} + +static void +execl__deinit (void) +{ + g_ptr_array_foreach (execl__args__captured, (GFunc) g_free, NULL); + g_ptr_array_free (execl__args__captured, TRUE); + g_free (execl__path__captured); + g_free (execl__arg__captured); +} + +/* --------------------------------------------------------------------------------------------- */ + +/* @CapturedValue */ +static char *execlp__file__captured = NULL; +/* @CapturedValue */ +static char *execlp__arg__captured = NULL; +/* @CapturedValue */ +static GPtrArray *execlp__args__captured; +/* @ThenReturnValue */ +static int execlp__return_value = 0; + +/* @Mock */ +int +execlp(const char *file, const char *arg, ...) +{ + char *one_arg; + va_list vargs; + + execlp__file__captured = g_strdup (file); + execlp__arg__captured = g_strdup (arg); + + va_start (vargs, arg); + + while ((one_arg = va_arg (vargs, char *)) != NULL) + { + g_ptr_array_add (execlp__args__captured, g_strdup (one_arg)); + } + va_end (vargs); + + return execlp__return_value; +} + +static void +execlp__init (void) +{ + execlp__args__captured = g_ptr_array_new (); +} + +static void +execlp__deinit (void) +{ + g_ptr_array_foreach (execlp__args__captured, (GFunc) g_free, NULL); + g_ptr_array_free (execlp__args__captured, TRUE); + + g_free (execlp__file__captured); + g_free (execlp__arg__captured); +} + +/* --------------------------------------------------------------------------------------------- */ + +#define VERIFY_SIGACTION__ACT_IGNORED(_pntr) { \ + struct sigaction *_act = (struct sigaction *) _pntr; \ + fail_unless (_act->sa_handler == SIG_IGN, " sa_handler should be equals to SIG_IGN"); \ + ck_assert_int_eq (_act->sa_flags, 0); \ +} + +#define VERIFY_SIGACTION__IS_RESTORED(oldact_idx, act_idx) { \ + struct sigaction *_oldact = (struct sigaction *) g_ptr_array_index(sigaction_oldact__captured, oldact_idx); \ + struct sigaction *_act = (struct sigaction *) g_ptr_array_index(sigaction_act__captured, act_idx); \ + fail_unless (memcmp(_oldact, _act, sizeof(struct sigaction)) == 0, \ + "sigaction(): oldact[%d] should be equals to act[%d]", oldact_idx, act_idx); \ +} + +/* @Verify */ +#define VERIFY_SIGACTION_CALLS() { \ + ck_assert_int_eq (sigaction_signum__captured->len, 6); \ +\ + ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 0)), SIGINT); \ + ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 1)), SIGQUIT); \ + ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 2)), SIGTSTP); \ + ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 3)), SIGINT); \ + ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 4)), SIGQUIT); \ + ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 5)), SIGTSTP); \ +\ + VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 0)); \ + VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 1)); \ + { \ + struct sigaction *_act = g_ptr_array_index(sigaction_act__captured, 2); \ + fail_unless (memcmp (_act, &startup_handler, sizeof(struct sigaction)) == 0, \ + "The 'act' in third call to sigaction() should be equals to startup_handler"); \ + } \ +\ + VERIFY_SIGACTION__IS_RESTORED (0, 3); \ + VERIFY_SIGACTION__IS_RESTORED (1, 4); \ + VERIFY_SIGACTION__IS_RESTORED (2, 5); \ +\ + fail_unless (g_ptr_array_index(sigaction_oldact__captured, 3) == NULL, \ + "oldact in fourth call to sigaction() should be NULL"); \ + fail_unless (g_ptr_array_index(sigaction_oldact__captured, 4) == NULL, \ + "oldact in fifth call to sigaction() should be NULL"); \ + fail_unless (g_ptr_array_index(sigaction_oldact__captured, 5) == NULL, \ + "oldact in sixth call to sigaction() should be NULL"); \ +} + +/* --------------------------------------------------------------------------------------------- */ + +#define VERIFY_SIGNAL_HANDLER_IS_SIG_DFL(_idx) { \ + sighandler_t *tmp_handler = (sighandler_t *) g_ptr_array_index(signal_handler__captured, _idx);\ + fail_unless (tmp_handler == (sighandler_t *) SIG_DFL, \ + "signal handler should be SIG_DFL"); \ +} + +/* @Verify */ +#define VERIFY_SIGNAL_CALLS() { \ + ck_assert_int_eq (signal_signum__captured->len, 4); \ + ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 0)), SIGINT); \ + ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 1)), SIGQUIT); \ + ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 2)), SIGTSTP); \ + ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 3)), SIGCHLD); \ + \ + VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (0); \ + VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (1); \ + VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (2); \ + VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (3); \ +} + +/* --------------------------------------------------------------------------------------------- */ + +/* @Before */ +static void +setup (void) +{ + signal__return_value = NULL; + + sigaction__init (); + signal__init (); + execl__init (); + execlp__init (); +} + +/* --------------------------------------------------------------------------------------------- */ + +/* @After */ +static void +teardown (void) +{ + execlp__deinit (); + execl__deinit (); + signal__deinit (); + sigaction__deinit (); +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/tests/lib/utilunix__my_system-fork_child.c b/tests/lib/utilunix__my_system-fork_child.c new file mode 100644 index 000000000..135d1233a --- /dev/null +++ b/tests/lib/utilunix__my_system-fork_child.c @@ -0,0 +1,96 @@ +/* + lib - tests lib/utilinux:my_system() function + + Copyright (C) 2013 + The Free Software Foundation, Inc. + + Written by: + Slava Zanko , 2013 + + This file is part of the Midnight Commander. + + The Midnight Commander is free software: you can redistribute it + and/or modify it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + The Midnight Commander is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#define TEST_SUITE_NAME "/lib/utilunix" + +#include +#include + +#include "lib/global.h" + +#include "lib/util.h" +#include "lib/utilunix.h" + +#include "utilunix__my_system-common.c" + +/* --------------------------------------------------------------------------------------------- */ + +/* *INDENT-OFF* */ +START_TEST (fork_child) +/* *INDENT-ON* */ +{ + int actual_value; + /* given */ + fork__return_value = 0; + + /* when */ + actual_value = my_system (0, "/bin/some-command", "some parameter"); + + /* then */ + ck_assert_int_eq (actual_value, 0); + + VERIFY_SIGACTION_CALLS (); + VERIFY_SIGNAL_CALLS (); + + g_assert_cmpstr (execlp__file__captured, ==, "/bin/some-command"); + g_assert_cmpstr (execlp__arg__captured, ==, "/bin/some-command"); + ck_assert_int_eq (execlp__args__captured->len, 1); + + g_assert_cmpstr (g_ptr_array_index(execlp__args__captured, 0), ==, "some parameter"); + + /* All exec* calls is mocked, so call to _exit() function with 127 status code it's a normal situation */ + ck_assert_int_eq (my_exit__status__captured, 127); +} +/* *INDENT-OFF* */ +END_TEST +/* *INDENT-ON* */ + +/* --------------------------------------------------------------------------------------------- */ + +int +main (void) +{ + int number_failed; + + Suite *s = suite_create (TEST_SUITE_NAME); + TCase *tc_core = tcase_create ("Core"); + SRunner *sr; + + tcase_add_checked_fixture (tc_core, setup, teardown); + + /* Add new tests here: *************** */ + tcase_add_test (tc_core, fork_child); + /* *********************************** */ + + suite_add_tcase (s, tc_core); + sr = srunner_create (s); + srunner_set_log (sr, "utilinux__my_system.log"); + srunner_run_all (sr, CK_NOFORK); + number_failed = srunner_ntests_failed (sr); + srunner_free (sr); + return (number_failed == 0) ? 0 : 1; +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/tests/lib/utilunix__my_system-fork_child_shell.c b/tests/lib/utilunix__my_system-fork_child_shell.c new file mode 100644 index 000000000..107833065 --- /dev/null +++ b/tests/lib/utilunix__my_system-fork_child_shell.c @@ -0,0 +1,97 @@ +/* + lib - tests lib/utilinux:my_system() function + + Copyright (C) 2013 + The Free Software Foundation, Inc. + + Written by: + Slava Zanko , 2013 + + This file is part of the Midnight Commander. + + The Midnight Commander is free software: you can redistribute it + and/or modify it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + The Midnight Commander is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#define TEST_SUITE_NAME "/lib/utilunix" + +#include +#include + +#include "lib/global.h" + +#include "lib/util.h" +#include "lib/utilunix.h" + +#include "utilunix__my_system-common.c" + +/* --------------------------------------------------------------------------------------------- */ + +/* *INDENT-OFF* */ +START_TEST (fork_child_as_shell) +/* *INDENT-ON* */ +{ + int actual_value; + /* given */ + fork__return_value = 0; + + /* when */ + actual_value = my_system (EXECUTE_AS_SHELL, "/bin/shell", "some command"); + + /* then */ + ck_assert_int_eq (actual_value, 0); + + VERIFY_SIGACTION_CALLS (); + VERIFY_SIGNAL_CALLS (); + + g_assert_cmpstr (execl__path__captured, ==, "/bin/shell"); + g_assert_cmpstr (execl__arg__captured, ==, "/bin/shell"); + ck_assert_int_eq (execl__args__captured->len, 2); + + g_assert_cmpstr (g_ptr_array_index(execl__args__captured, 0), ==, "-c"); + g_assert_cmpstr (g_ptr_array_index(execl__args__captured, 1), ==, "some command"); + + /* All exec* calls is mocked, so call to _exit() function with 127 status code it's a normal situation */ + ck_assert_int_eq (my_exit__status__captured, 127); +} +/* *INDENT-OFF* */ +END_TEST +/* *INDENT-ON* */ + +/* --------------------------------------------------------------------------------------------- */ + +int +main (void) +{ + int number_failed; + + Suite *s = suite_create (TEST_SUITE_NAME); + TCase *tc_core = tcase_create ("Core"); + SRunner *sr; + + tcase_add_checked_fixture (tc_core, setup, teardown); + + /* Add new tests here: *************** */ + tcase_add_test (tc_core, fork_child_as_shell); + /* *********************************** */ + + suite_add_tcase (s, tc_core); + sr = srunner_create (s); + srunner_set_log (sr, "utilinux__my_system.log"); + srunner_run_all (sr, CK_NOFORK); + number_failed = srunner_ntests_failed (sr); + srunner_free (sr); + return (number_failed == 0) ? 0 : 1; +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/tests/lib/utilunix__my_system-fork_fail.c b/tests/lib/utilunix__my_system-fork_fail.c new file mode 100644 index 000000000..319f3f1af --- /dev/null +++ b/tests/lib/utilunix__my_system-fork_fail.c @@ -0,0 +1,91 @@ +/* + lib - tests lib/utilinux:my_system() function + + Copyright (C) 2013 + The Free Software Foundation, Inc. + + Written by: + Slava Zanko , 2013 + + This file is part of the Midnight Commander. + + The Midnight Commander is free software: you can redistribute it + and/or modify it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + The Midnight Commander is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#define TEST_SUITE_NAME "/lib/utilunix" + +#include +#include +#include +#include + +#include "lib/global.h" + +#include "lib/util.h" +#include "lib/utilunix.h" + +#include "utilunix__my_system-common.c" + +/* --------------------------------------------------------------------------------------------- */ + +/* *INDENT-OFF* */ +START_TEST (fork_fail) +/* *INDENT-ON* */ +{ + int actual_value; + + /* given */ + fork__return_value = -1; + + /* when */ + actual_value = my_system (0, NULL, NULL); + + /* then */ + ck_assert_int_eq (actual_value, -1); + + VERIFY_SIGACTION_CALLS (); + + ck_assert_int_eq (signal_signum__captured->len, 0); +} +/* *INDENT-OFF* */ +END_TEST +/* *INDENT-ON* */ + +/* --------------------------------------------------------------------------------------------- */ + +int +main (void) +{ + int number_failed; + + Suite *s = suite_create (TEST_SUITE_NAME); + TCase *tc_core = tcase_create ("Core"); + SRunner *sr; + + tcase_add_checked_fixture (tc_core, setup, teardown); + + /* Add new tests here: *************** */ + tcase_add_test (tc_core, fork_fail); + /* *********************************** */ + + suite_add_tcase (s, tc_core); + sr = srunner_create (s); + srunner_set_log (sr, "utilinux__my_system.log"); + srunner_run_all (sr, CK_NOFORK); + number_failed = srunner_ntests_failed (sr); + srunner_free (sr); + return (number_failed == 0) ? 0 : 1; +} + +/* --------------------------------------------------------------------------------------------- */ -- 2.11.4.GIT