aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_child_management.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_child_management.c')
-rw-r--r--src/util/test_child_management.c177
1 files changed, 177 insertions, 0 deletions
diff --git a/src/util/test_child_management.c b/src/util/test_child_management.c
new file mode 100644
index 000000000..62c143420
--- /dev/null
+++ b/src/util/test_child_management.c
@@ -0,0 +1,177 @@
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#include "platform.h"
27#include "gnunet_util_lib.h"
28
29
30static struct GNUNET_ChildWaitHandle *cwh;
31
32static int global_ret;
33
34static struct GNUNET_OS_Process *pid;
35
36
37static void
38child_completed_callback (void *cls,
39 enum GNUNET_OS_ProcessStatusType type,
40 long unsigned int exit_code)
41{
42 cwh = NULL;
43 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
44 "Process extided with code: %lu \n",
45 exit_code);
46 FILE *file;
47 char code[9];
48
49 file = fopen ("child_management_test.txt", "r");
50 if (NULL == file)
51 {
52 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
53 "could not find file: child_management_test.txt in %s:%u\n",
54 __FILE__,
55 __LINE__);
56 global_ret = 1;
57 return;
58 }
59 if (0 == fscanf (file,
60 "%8s",
61 code))
62 {
63 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
64 "could not read file: child_management_test.txt in %s:%u\n",
65 __FILE__,
66 __LINE__);
67 global_ret = 1;
68 return;
69 }
70
71 if (0 != strcmp ("12345678", code))
72 {
73 global_ret = 1;
74 return;
75 }
76 GNUNET_OS_process_destroy (pid);
77 pid = NULL;
78 GNUNET_break (0 == unlink ("child_management_test.txt"));
79 GNUNET_SCHEDULER_shutdown ();
80 global_ret = 0;
81}
82
83
84static void
85do_shutdown (void *cls)
86{
87 if (NULL != cwh)
88 {
89 GNUNET_wait_child_cancel (cwh);
90 cwh = NULL;
91 }
92 if (NULL != pid)
93 {
94 GNUNET_assert (0 ==
95 GNUNET_OS_process_kill (pid,
96 SIGKILL));
97 GNUNET_assert (GNUNET_OK ==
98 GNUNET_OS_process_wait (pid));
99 GNUNET_OS_process_destroy (pid);
100 pid = NULL;
101 }
102}
103
104
105static void
106test_child_management (void *cls)
107{
108 const char *command = "./child_management_test.sh";
109 struct GNUNET_DISK_PipeHandle *p;
110 struct GNUNET_DISK_FileHandle *out;
111
112 (void) cls;
113 p = GNUNET_DISK_pipe (GNUNET_DISK_PF_NONE);
114 if (NULL == p)
115 {
116 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
117 "pipe");
118 global_ret = 2;
119 return;
120 }
121 pid = GNUNET_OS_start_process (0,
122 p,
123 NULL,
124 NULL,
125 command,
126 command,
127 "1234",
128 "5678",
129 NULL);
130 if (NULL == pid)
131 {
132 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
133 "fork");
134 GNUNET_break (GNUNET_OK ==
135 GNUNET_DISK_pipe_close (p));
136 global_ret = 1;
137 return;
138 }
139 GNUNET_break (GNUNET_OK ==
140 GNUNET_DISK_pipe_close_end (p,
141 GNUNET_DISK_PIPE_END_READ));
142 out = GNUNET_DISK_pipe_detach_end (p,
143 GNUNET_DISK_PIPE_END_WRITE);
144 GNUNET_assert (NULL != out);
145 GNUNET_break (GNUNET_OK ==
146 GNUNET_DISK_pipe_close (p));
147
148 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
149 NULL);
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Alright");
151 cwh = GNUNET_wait_child (pid,
152 &child_completed_callback,
153 cls);
154 GNUNET_break (NULL != cwh);
155 GNUNET_assert (5 ==
156 GNUNET_DISK_file_write (out,
157 "Hello",
158 5));
159 GNUNET_break (GNUNET_OK ==
160 GNUNET_DISK_file_close (out));
161}
162
163
164int
165main (int argc,
166 const char *const argv[])
167{
168 GNUNET_log_setup (argv[0],
169 "DEBUG",
170 NULL);
171 GNUNET_SCHEDULER_run (&test_child_management,
172 NULL);
173 return global_ret;
174}
175
176
177/* end of test_anastasis_child_management.c */