Merge branch 'maint-0.4.8'
[tor.git] / src / test / test_util_process.c
blob28d5737cad21260272a4c2ebb33b0424af7664b0
1 /* Copyright (c) 2010-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "orconfig.h"
5 #include "core/or/or.h"
7 #include "test/test.h"
9 #include "lib/process/waitpid.h"
11 #include "test/log_test_helpers.h"
13 #ifndef _WIN32
15 static void
16 temp_callback(int r, void *s)
18 (void)r;
19 (void)s;
22 static void
23 test_util_process_set_waitpid_callback(void *ignored)
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;
30 res1 = set_waitpid_callback(pid, temp_callback, NULL);
31 tt_assert(res1);
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");
39 done:
40 teardown_capture_of_logs();
41 clear_waitpid_callback(res1);
42 clear_waitpid_callback(res2);
45 static void
46 test_util_process_clear_waitpid_callback(void *ignored)
48 (void)ignored;
49 waitpid_callback_t *res;
50 setup_capture_of_logs(LOG_WARN);
51 pid_t pid = (pid_t)43;
53 clear_waitpid_callback(NULL);
55 res = set_waitpid_callback(pid, temp_callback, NULL);
56 clear_waitpid_callback(res);
57 expect_no_log_entry();
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
65 done:
66 teardown_capture_of_logs();
68 #endif /* !defined(_WIN32) */
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) */
78 struct testcase_t util_process_tests[] = {
79 TEST(set_waitpid_callback),
80 TEST(clear_waitpid_callback),
81 END_OF_TESTCASES