test_util_process.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Copyright (c) 2010-2016, 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. int previous_log = setup_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_log_msg("Replaced a waitpid monitor on pid 42. That should be "
  29. "impossible.\n");
  30. done:
  31. teardown_capture_of_logs(previous_log);
  32. clear_waitpid_callback(res1);
  33. clear_waitpid_callback(res2);
  34. }
  35. static void
  36. test_util_process_clear_waitpid_callback(void *ignored)
  37. {
  38. (void)ignored;
  39. waitpid_callback_t *res;
  40. int previous_log = setup_capture_of_logs(LOG_WARN);
  41. pid_t pid = (pid_t)43;
  42. clear_waitpid_callback(NULL);
  43. res = set_waitpid_callback(pid, temp_callback, NULL);
  44. clear_waitpid_callback(res);
  45. expect_no_log_entry();
  46. #if 0
  47. /* No. This is use-after-free. We don't _do_ that. XXXX */
  48. clear_waitpid_callback(res);
  49. expect_log_msg("Couldn't remove waitpid monitor for pid 43.\n");
  50. #endif
  51. done:
  52. teardown_capture_of_logs(previous_log);
  53. }
  54. #endif /* _WIN32 */
  55. #ifndef _WIN32
  56. #define TEST(name) { #name, test_util_process_##name, 0, NULL, NULL }
  57. #else
  58. #define TEST(name) { #name, NULL, TT_SKIP, NULL, NULL }
  59. #endif
  60. struct testcase_t util_process_tests[] = {
  61. TEST(set_waitpid_callback),
  62. TEST(clear_waitpid_callback),
  63. END_OF_TESTCASES
  64. };