summaryrefslogtreecommitdiff
path: root/src/test/test_util_process.c
blob: 04a21b86f79952207b1d9f1cbd3c84cb59d9ea75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* Copyright (c) 2010-2015, The Tor Project, Inc. */
/* See LICENSE for licensing information */

#define UTIL_PROCESS_PRIVATE
#include "orconfig.h"
#include "or.h"

#include "test.h"

#include "util_process.h"

#include "log_test_helpers.h"

#define NS_MODULE util_process

static void
temp_callback(int r, void *s)
{
  (void)r;
  (void)s;
}

static void
test_util_process_set_waitpid_callback(void *ignored)
{
  (void)ignored;
  waitpid_callback_t *res1 = NULL, *res2 = NULL;
  int previous_log = setup_capture_of_logs(LOG_WARN);
  pid_t pid = (pid_t)42;

  res1 = set_waitpid_callback(pid, temp_callback, NULL);
  tt_assert(res1);

  res2 = set_waitpid_callback(pid, temp_callback, NULL);
  tt_assert(res2);
  tt_str_op(mock_saved_log_at(0), OP_EQ,
            "Replaced a waitpid monitor on pid 42. That should be "
            "impossible.\n");

 done:
  teardown_capture_of_logs(previous_log);
  clear_waitpid_callback(res1);
  clear_waitpid_callback(res2);
}

static void
test_util_process_clear_waitpid_callback(void *ignored)
{
  (void)ignored;
  waitpid_callback_t *res;
  int previous_log = setup_capture_of_logs(LOG_WARN);
  pid_t pid = (pid_t)43;

  clear_waitpid_callback(NULL);

  res = set_waitpid_callback(pid, temp_callback, NULL);
  clear_waitpid_callback(res);
  tt_int_op(mock_saved_log_number(), OP_EQ, 0);

#if 0
  /* No.  This is use-after-free.  We don't _do_ that. XXXX */
  clear_waitpid_callback(res);
  tt_str_op(mock_saved_log_at(0), OP_EQ,
            "Couldn't remove waitpid monitor for pid 43.\n");
#endif

 done:
  teardown_capture_of_logs(previous_log);
}

struct testcase_t util_process_tests[] = {
  { "set_waitpid_callback", test_util_process_set_waitpid_callback, 0,
    NULL, NULL },
  { "clear_waitpid_callback", test_util_process_clear_waitpid_callback, 0,
    NULL, NULL },
  END_OF_TESTCASES
};