LCOV - code coverage report
Current view: top level - test - test_util_process.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 27 31 87.1 %
Date: 2021-11-24 03:28:48 Functions: 2 3 66.7 %

          Line data    Source code
       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           0 : temp_callback(int r, void *s)
      17             : {
      18           0 :   (void)r;
      19           0 :   (void)s;
      20           0 : }
      21             : 
      22             : static void
      23           1 : test_util_process_set_waitpid_callback(void *ignored)
      24             : {
      25           1 :   (void)ignored;
      26           1 :   waitpid_callback_t *res1 = NULL, *res2 = NULL;
      27           1 :   setup_full_capture_of_logs(LOG_WARN);
      28           1 :   pid_t pid = (pid_t)42;
      29             : 
      30           1 :   res1 = set_waitpid_callback(pid, temp_callback, NULL);
      31           1 :   tt_assert(res1);
      32             : 
      33           1 :   res2 = set_waitpid_callback(pid, temp_callback, NULL);
      34           1 :   tt_assert(res2);
      35           1 :   expect_single_log_msg(
      36             :             "Replaced a waitpid monitor on pid 42. That should be "
      37             :             "impossible.\n");
      38             : 
      39           1 :  done:
      40           1 :   teardown_capture_of_logs();
      41           1 :   clear_waitpid_callback(res1);
      42           1 :   clear_waitpid_callback(res2);
      43           1 : }
      44             : 
      45             : static void
      46           1 : test_util_process_clear_waitpid_callback(void *ignored)
      47             : {
      48           1 :   (void)ignored;
      49           1 :   waitpid_callback_t *res;
      50           1 :   setup_capture_of_logs(LOG_WARN);
      51           1 :   pid_t pid = (pid_t)43;
      52             : 
      53           1 :   clear_waitpid_callback(NULL);
      54             : 
      55           1 :   res = set_waitpid_callback(pid, temp_callback, NULL);
      56           1 :   clear_waitpid_callback(res);
      57           1 :   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           1 :  done:
      66           1 :   teardown_capture_of_logs();
      67           1 : }
      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             : };

Generated by: LCOV version 1.14