aboutsummaryrefslogtreecommitdiff
path: root/src/lib/util/test_child_management.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/test_child_management.c')
-rw-r--r--src/lib/util/test_child_management.c178
1 files changed, 178 insertions, 0 deletions
diff --git a/src/lib/util/test_child_management.c b/src/lib/util/test_child_management.c
new file mode 100644
index 000000000..90cc74c72
--- /dev/null
+++ b/src/lib/util/test_child_management.c
@@ -0,0 +1,178 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2014-2021 GNUnet e.V.
4
5 GNUNET is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 3, or
8 (at your option) any later version.
9
10 GNUNET is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with GNUNET; see the file COPYING. If not, see
17 <http://www.gnu.org/licenses/>
18*/
19
20/**
21 * @file lib/test_child_management.c
22 * @brief testcase to test the child management
23 * @author Christian Grothoff
24 * @author Dominik Meister
25 */
26
27#include "platform.h"
28#include "gnunet_util_lib.h"
29
30
31static struct GNUNET_ChildWaitHandle *cwh;
32
33static int global_ret;
34
35static struct GNUNET_OS_Process *pid;
36
37
38static void
39child_completed_callback (void *cls,
40 enum GNUNET_OS_ProcessStatusType type,
41 long unsigned int exit_code)
42{
43 cwh = NULL;
44 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
45 "Process extided with code: %lu \n",
46 exit_code);
47 FILE *file;
48 char code[9];
49
50 file = fopen ("child_management_test.txt", "r");
51 if (NULL == file)
52 {
53 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
54 "could not find file: child_management_test.txt in %s:%u\n",
55 __FILE__,
56 __LINE__);
57 global_ret = 1;
58 return;
59 }
60 if (0 == fscanf (file,
61 "%8s",
62 code))
63 {
64 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
65 "could not read file: child_management_test.txt in %s:%u\n",
66 __FILE__,
67 __LINE__);
68 global_ret = 1;
69 return;
70 }
71
72 if (0 != strcmp ("12345678", code))
73 {
74 global_ret = 1;
75 return;
76 }
77 GNUNET_OS_process_destroy (pid);
78 pid = NULL;
79 GNUNET_break (0 == unlink ("child_management_test.txt"));
80 GNUNET_SCHEDULER_shutdown ();
81 global_ret = 0;
82}
83
84
85static void
86do_shutdown (void *cls)
87{
88 if (NULL != cwh)
89 {
90 GNUNET_wait_child_cancel (cwh);
91 cwh = NULL;
92 }
93 if (NULL != pid)
94 {
95 GNUNET_assert (0 ==
96 GNUNET_OS_process_kill (pid,
97 SIGKILL));
98 GNUNET_assert (GNUNET_OK ==
99 GNUNET_OS_process_wait (pid));
100 GNUNET_OS_process_destroy (pid);
101 pid = NULL;
102 }
103}
104
105
106static void
107test_child_management (void *cls)
108{
109 const char *command = "./child_management_test.sh";
110 struct GNUNET_DISK_PipeHandle *p;
111 struct GNUNET_DISK_FileHandle *out;
112
113 (void) cls;
114 p = GNUNET_DISK_pipe (GNUNET_DISK_PF_NONE);
115 if (NULL == p)
116 {
117 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
118 "pipe");
119 global_ret = 2;
120 return;
121 }
122 pid = GNUNET_OS_start_process (0,
123 p,
124 NULL,
125 NULL,
126 command,
127 command,
128 "1234",
129 "5678",
130 NULL);
131 if (NULL == pid)
132 {
133 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
134 "fork");
135 GNUNET_break (GNUNET_OK ==
136 GNUNET_DISK_pipe_close (p));
137 global_ret = 1;
138 return;
139 }
140 GNUNET_break (GNUNET_OK ==
141 GNUNET_DISK_pipe_close_end (p,
142 GNUNET_DISK_PIPE_END_READ));
143 out = GNUNET_DISK_pipe_detach_end (p,
144 GNUNET_DISK_PIPE_END_WRITE);
145 GNUNET_assert (NULL != out);
146 GNUNET_break (GNUNET_OK ==
147 GNUNET_DISK_pipe_close (p));
148
149 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
150 NULL);
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Alright");
152 cwh = GNUNET_wait_child (pid,
153 &child_completed_callback,
154 cls);
155 GNUNET_break (NULL != cwh);
156 GNUNET_assert (5 ==
157 GNUNET_DISK_file_write (out,
158 "Hello",
159 5));
160 GNUNET_break (GNUNET_OK ==
161 GNUNET_DISK_file_close (out));
162}
163
164
165int
166main (int argc,
167 const char *const argv[])
168{
169 GNUNET_log_setup (argv[0],
170 "DEBUG",
171 NULL);
172 GNUNET_SCHEDULER_run (&test_child_management,
173 NULL);
174 return global_ret;
175}
176
177
178/* end of test_anastasis_child_management.c */