diff options
-rw-r--r-- | src/include/gnunet_child_management_lib.h | 2 | ||||
-rw-r--r-- | src/include/gnunet_util_lib.h | 1 | ||||
-rw-r--r-- | src/util/Makefile.am | 13 | ||||
-rwxr-xr-x | src/util/child_management_test.sh | 2 | ||||
-rw-r--r-- | src/util/test_child_management.c | 177 |
5 files changed, 193 insertions, 2 deletions
diff --git a/src/include/gnunet_child_management_lib.h b/src/include/gnunet_child_management_lib.h index 465f71f0e..a35b37865 100644 --- a/src/include/gnunet_child_management_lib.h +++ b/src/include/gnunet_child_management_lib.h | |||
@@ -19,7 +19,7 @@ | |||
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file include/anastasis_util_lib.h | 22 | * @file include/gnunet_child_management_lib.h |
23 | * @brief GNUnet child management api | 23 | * @brief GNUnet child management api |
24 | * @author Christian Grothoff | 24 | * @author Christian Grothoff |
25 | * @author Dominik Meister | 25 | * @author Dominik Meister |
diff --git a/src/include/gnunet_util_lib.h b/src/include/gnunet_util_lib.h index 5e790550f..3acd4cb9b 100644 --- a/src/include/gnunet_util_lib.h +++ b/src/include/gnunet_util_lib.h | |||
@@ -84,6 +84,7 @@ extern "C" | |||
84 | #include "gnunet_tun_lib.h" | 84 | #include "gnunet_tun_lib.h" |
85 | #include "gnunet_dnsstub_lib.h" | 85 | #include "gnunet_dnsstub_lib.h" |
86 | #include "gnunet_dnsparser_lib.h" | 86 | #include "gnunet_dnsparser_lib.h" |
87 | #include "gnunet_child_management_lib.h" | ||
87 | 88 | ||
88 | #if 0 /* keep Emacsens' auto-indent happy */ | 89 | #if 0 /* keep Emacsens' auto-indent happy */ |
89 | { | 90 | { |
diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 09620aee0..9c1fb01a4 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am | |||
@@ -43,6 +43,7 @@ libgnunetutil_la_SOURCES = \ | |||
43 | $(BENCHMARK) \ | 43 | $(BENCHMARK) \ |
44 | bio.c \ | 44 | bio.c \ |
45 | buffer.c \ | 45 | buffer.c \ |
46 | child_management.c \ | ||
46 | client.c \ | 47 | client.c \ |
47 | common_allocation.c \ | 48 | common_allocation.c \ |
48 | common_endian.c \ | 49 | common_endian.c \ |
@@ -271,6 +272,7 @@ check_SCRIPTS = \ | |||
271 | 272 | ||
272 | check_PROGRAMS = \ | 273 | check_PROGRAMS = \ |
273 | test_bio \ | 274 | test_bio \ |
275 | test_child_management \ | ||
274 | test_client.nc \ | 276 | test_client.nc \ |
275 | $(TEST_CLIENT_UNIX_NC) \ | 277 | $(TEST_CLIENT_UNIX_NC) \ |
276 | test_common_allocation \ | 278 | test_common_allocation \ |
@@ -323,6 +325,14 @@ check_PROGRAMS = \ | |||
323 | test_common_logging_runtime_loglevels | 325 | test_common_logging_runtime_loglevels |
324 | 326 | ||
325 | 327 | ||
328 | test_child_management_SOURCES = \ | ||
329 | test_child_management.c | ||
330 | test_child_management_LDADD = \ | ||
331 | libgnunetutil.la \ | ||
332 | $(XLIB) | ||
333 | |||
334 | |||
335 | |||
326 | # Declare .nc (NO-CONCURRENCY) as a test extension so that we can impart | 336 | # Declare .nc (NO-CONCURRENCY) as a test extension so that we can impart |
327 | # sequential execution order for them | 337 | # sequential execution order for them |
328 | TEST_EXTENSIONS = .nc | 338 | TEST_EXTENSIONS = .nc |
@@ -650,4 +660,5 @@ EXTRA_DIST = \ | |||
650 | test_program_data.conf \ | 660 | test_program_data.conf \ |
651 | test_resolver_api_data.conf \ | 661 | test_resolver_api_data.conf \ |
652 | test_service_data.conf \ | 662 | test_service_data.conf \ |
653 | test_speedup_data.conf | 663 | test_speedup_data.conf \ |
664 | child_management_test.sh | ||
diff --git a/src/util/child_management_test.sh b/src/util/child_management_test.sh new file mode 100755 index 000000000..a35b865f3 --- /dev/null +++ b/src/util/child_management_test.sh | |||
@@ -0,0 +1,2 @@ | |||
1 | #!/usr/bin/env bash | ||
2 | echo "$1$2" >> child_management_test.txt | ||
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 | |||
30 | static struct GNUNET_ChildWaitHandle *cwh; | ||
31 | |||
32 | static int global_ret; | ||
33 | |||
34 | static struct GNUNET_OS_Process *pid; | ||
35 | |||
36 | |||
37 | static void | ||
38 | child_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 | |||
84 | static void | ||
85 | do_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 | |||
105 | static void | ||
106 | test_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 | |||
164 | int | ||
165 | main (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 */ | ||