test_util_process.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* Copyright (c) 2010-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define UTIL_PROCESS_PRIVATE
  4. #include "orconfig.h"
  5. #include "or.h"
  6. #include "test.h"
  7. #include "util_process.h"
  8. #include "log_test_helpers.h"
  9. #ifndef _WIN32
  10. #define NS_MODULE util_process
  11. static void
  12. temp_callback(int r, void *s)
  13. {
  14. (void)r;
  15. (void)s;
  16. }
  17. static void
  18. test_util_process_set_waitpid_callback(void *ignored)
  19. {
  20. (void)ignored;
  21. waitpid_callback_t *res1 = NULL, *res2 = NULL;
  22. setup_full_capture_of_logs(LOG_WARN);
  23. pid_t pid = (pid_t)42;
  24. res1 = set_waitpid_callback(pid, temp_callback, NULL);
  25. tt_assert(res1);
  26. res2 = set_waitpid_callback(pid, temp_callback, NULL);
  27. tt_assert(res2);
  28. expect_single_log_msg(
  29. "Replaced a waitpid monitor on pid 42. That should be "
  30. "impossible.\n");
  31. done:
  32. teardown_capture_of_logs();
  33. clear_waitpid_callback(res1);
  34. clear_waitpid_callback(res2);
  35. }
  36. static void
  37. test_util_process_clear_waitpid_callback(void *ignored)
  38. {
  39. (void)ignored;
  40. waitpid_callback_t *res;
  41. setup_capture_of_logs(LOG_WARN);
  42. pid_t pid = (pid_t)43;
  43. clear_waitpid_callback(NULL);
  44. res = set_waitpid_callback(pid, temp_callback, NULL);
  45. clear_waitpid_callback(res);
  46. expect_no_log_entry();
  47. #if 0
  48. /* No. This is use-after-free. We don't _do_ that. XXXX */
  49. clear_waitpid_callback(res);
  50. expect_log_msg("Couldn't remove waitpid monitor for pid 43.\n");
  51. #endif
  52. done:
  53. teardown_capture_of_logs();
  54. }
  55. #endif /* _WIN32 */
  56. #ifndef _WIN32
  57. #define TEST(name) { #name, test_util_process_##name, 0, NULL, NULL }
  58. #else
  59. #define TEST(name) { #name, NULL, TT_SKIP, NULL, NULL }
  60. #endif
  61. struct testcase_t util_process_tests[] = {
  62. TEST(set_waitpid_callback),
  63. TEST(clear_waitpid_callback),
  64. END_OF_TESTCASES
  65. };