check_check_fork.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Check: a unit test framework for C
  3. * Copyright (C) 2001, 2002 Arien Malec
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the
  17. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  18. * MA 02110-1301, USA.
  19. */
  20. #include "../lib/libcompat.h"
  21. #include <sys/types.h>
  22. #include <stdlib.h>
  23. #include <check.h>
  24. #include "check_check.h"
  25. static int counter;
  26. static pid_t mypid;
  27. static void fork_sub_setup (void)
  28. {
  29. counter = 0;
  30. mypid = getpid();
  31. }
  32. START_TEST(test_inc)
  33. {
  34. counter++;
  35. }
  36. END_TEST
  37. START_TEST(test_nofork_sideeffects)
  38. {
  39. ck_assert_msg(counter == 1,
  40. "Side effects not seen across tests");
  41. }
  42. END_TEST
  43. START_TEST(test_nofork_pid)
  44. {
  45. ck_assert_msg(mypid == getpid(),
  46. "Unit test is in a different adresss space from setup code");
  47. }
  48. END_TEST
  49. static Suite *make_fork_sub_suite (void)
  50. {
  51. Suite *s;
  52. TCase *tc;
  53. s = suite_create("Fork Sub");
  54. tc = tcase_create("Core");
  55. suite_add_tcase (s, tc);
  56. tcase_add_unchecked_fixture(tc, fork_sub_setup,NULL);
  57. tcase_add_test(tc,test_inc);
  58. tcase_add_test(tc,test_nofork_sideeffects);
  59. tcase_add_test(tc,test_nofork_pid);
  60. return s;
  61. }
  62. static SRunner *fork_sr;
  63. static SRunner *fork_dummy_sr;
  64. void fork_setup (void)
  65. {
  66. fork_sr = srunner_create(make_fork_sub_suite());
  67. fork_dummy_sr = srunner_create (make_fork_sub_suite());
  68. srunner_set_fork_status(fork_sr,CK_NOFORK);
  69. srunner_run_all(fork_sr,CK_VERBOSE);
  70. }
  71. void fork_teardown (void)
  72. {
  73. srunner_free(fork_sr);
  74. }
  75. START_TEST(test_default_fork)
  76. {
  77. #if defined(HAVE_FORK) && HAVE_FORK == 1
  78. ck_assert_msg(srunner_fork_status(fork_dummy_sr) == CK_FORK,
  79. "Default fork status not set correctly");
  80. #else
  81. ck_assert_msg(srunner_fork_status(fork_dummy_sr) == CK_NOFORK,
  82. "Default fork status not set correctly");
  83. #endif /* HAVE_FORK */
  84. }
  85. END_TEST
  86. START_TEST(test_set_nofork)
  87. {
  88. srunner_set_fork_status(fork_dummy_sr, CK_NOFORK);
  89. ck_assert_msg(srunner_fork_status(fork_dummy_sr) == CK_NOFORK,
  90. "Fork status not changed correctly");
  91. }
  92. END_TEST
  93. /*
  94. * The following tests will fail if fork is unavailable, as
  95. * attempting to set the fork mode as anything but
  96. * CK_NOFORK is considered an error.
  97. */
  98. #if defined(HAVE_FORK) && HAVE_FORK==1
  99. START_TEST(test_set_fork)
  100. {
  101. srunner_set_fork_status(fork_dummy_sr, CK_FORK);
  102. ck_assert_msg(srunner_fork_status(fork_dummy_sr) == CK_FORK,
  103. "Fork status not changed correctly");
  104. }
  105. END_TEST
  106. START_TEST(test_env)
  107. {
  108. char envvar[] = "CK_FORK=no";
  109. putenv(envvar);
  110. ck_assert_msg(srunner_fork_status(fork_dummy_sr) == CK_NOFORK,
  111. "Fork status does not obey environment variable");
  112. }
  113. END_TEST
  114. START_TEST(test_env_and_set)
  115. {
  116. char envvar[] = "CK_FORK=no";
  117. putenv(envvar);
  118. srunner_set_fork_status(fork_dummy_sr, CK_FORK);
  119. ck_assert_msg(srunner_fork_status(fork_dummy_sr) == CK_FORK,
  120. "Explicit setting of fork status should override env");
  121. }
  122. END_TEST
  123. #endif /* HAVE_FORK */
  124. START_TEST(test_nofork)
  125. {
  126. ck_assert_msg(srunner_ntests_failed(fork_sr) == 0,
  127. "Errors on nofork test");
  128. }
  129. END_TEST
  130. Suite *make_fork_suite(void)
  131. {
  132. Suite *s;
  133. TCase *tc;
  134. s = suite_create("Fork");
  135. tc = tcase_create("Core");
  136. suite_add_tcase(s, tc);
  137. tcase_add_test(tc,test_default_fork);
  138. tcase_add_test(tc,test_set_nofork);
  139. #if defined(HAVE_FORK) && HAVE_FORK==1
  140. tcase_add_test(tc,test_set_fork);
  141. tcase_add_test(tc,test_env);
  142. tcase_add_test(tc,test_env_and_set);
  143. #endif /* HAVE_FORK */
  144. tcase_add_test(tc,test_nofork);
  145. return s;
  146. }