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