diff options
Diffstat (limited to 'src/lib/testing/testing_api_cmd_exec.c')
-rw-r--r-- | src/lib/testing/testing_api_cmd_exec.c | 254 |
1 files changed, 254 insertions, 0 deletions
diff --git a/src/lib/testing/testing_api_cmd_exec.c b/src/lib/testing/testing_api_cmd_exec.c new file mode 100644 index 000000000..3a931e220 --- /dev/null +++ b/src/lib/testing/testing_api_cmd_exec.c | |||
@@ -0,0 +1,254 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2023 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (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 GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file testing_api_cmd_exec.c | ||
23 | * @brief cmd to block the interpreter loop until all peers started. | ||
24 | * @author t3sserakt | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_testing_lib.h" | ||
29 | |||
30 | #define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__) | ||
31 | |||
32 | struct BashScriptState | ||
33 | { | ||
34 | /** | ||
35 | * Context for our asynchronous completion. | ||
36 | */ | ||
37 | struct GNUNET_TESTING_AsyncContext ac; | ||
38 | |||
39 | /** | ||
40 | * Callback handed over to the command, which should | ||
41 | * be called upon death or completion of the script. | ||
42 | */ | ||
43 | GNUNET_ChildCompletedCallback cb; | ||
44 | |||
45 | /** | ||
46 | * Wait for death of @e start_proc. | ||
47 | */ | ||
48 | struct GNUNET_ChildWaitHandle *cwh; | ||
49 | |||
50 | /** | ||
51 | * The process id of the script. | ||
52 | */ | ||
53 | struct GNUNET_OS_Process *start_proc; | ||
54 | |||
55 | /** | ||
56 | * NULL-terminated array of command-line arguments. | ||
57 | */ | ||
58 | char **args; | ||
59 | |||
60 | /** | ||
61 | * | ||
62 | */ | ||
63 | enum GNUNET_OS_ProcessStatusType expected_type; | ||
64 | |||
65 | /** | ||
66 | * | ||
67 | */ | ||
68 | unsigned long int expected_exit_code; | ||
69 | |||
70 | }; | ||
71 | |||
72 | /** | ||
73 | * The cleanup function of this cmd frees resources the cmd allocated. | ||
74 | * | ||
75 | */ | ||
76 | static void | ||
77 | exec_bash_script_cleanup (void *cls) | ||
78 | { | ||
79 | struct BashScriptState *bss = cls; | ||
80 | |||
81 | if (NULL != bss->cwh) | ||
82 | { | ||
83 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
84 | "Cancel child\n"); | ||
85 | GNUNET_wait_child_cancel (bss->cwh); | ||
86 | bss->cwh = NULL; | ||
87 | } | ||
88 | if (NULL != bss->start_proc) | ||
89 | { | ||
90 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
91 | "Kill process\n"); | ||
92 | GNUNET_assert (0 == | ||
93 | GNUNET_OS_process_kill (bss->start_proc, | ||
94 | SIGKILL)); | ||
95 | GNUNET_assert (GNUNET_OK == | ||
96 | GNUNET_OS_process_wait (bss->start_proc)); | ||
97 | GNUNET_OS_process_destroy (bss->start_proc); | ||
98 | bss->start_proc = NULL; | ||
99 | } | ||
100 | for (unsigned int i = 0; NULL != bss->args[i]; i++) | ||
101 | GNUNET_free (bss->args[i]); | ||
102 | GNUNET_free (bss->args); | ||
103 | GNUNET_free (bss); | ||
104 | } | ||
105 | |||
106 | |||
107 | /** | ||
108 | * Callback which will be called if the setup script finished. | ||
109 | * | ||
110 | */ | ||
111 | static void | ||
112 | child_completed_callback (void *cls, | ||
113 | enum GNUNET_OS_ProcessStatusType type, | ||
114 | long unsigned int exit_code) | ||
115 | { | ||
116 | struct BashScriptState *bss = cls; | ||
117 | |||
118 | bss->cwh = NULL; | ||
119 | GNUNET_OS_process_destroy (bss->start_proc); | ||
120 | bss->start_proc = NULL; | ||
121 | if ( (bss->expected_type != type) || | ||
122 | (bss->expected_exit_code != exit_code) ) | ||
123 | { | ||
124 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
125 | "Child failed with error %lu (wanted %lu) %d/%d!\n", | ||
126 | exit_code, | ||
127 | bss->expected_exit_code, | ||
128 | type, | ||
129 | bss->expected_type); | ||
130 | GNUNET_TESTING_async_fail (&bss->ac); | ||
131 | return; | ||
132 | } | ||
133 | GNUNET_TESTING_async_finish (&bss->ac); | ||
134 | } | ||
135 | |||
136 | |||
137 | /** | ||
138 | * Run method of the command created by the interpreter to wait for another | ||
139 | * command to finish. | ||
140 | * | ||
141 | */ | ||
142 | static void | ||
143 | exec_bash_script_run (void *cls, | ||
144 | struct GNUNET_TESTING_Interpreter *is) | ||
145 | { | ||
146 | struct BashScriptState *bss = cls; | ||
147 | |||
148 | GNUNET_assert (NULL == bss->cwh); | ||
149 | bss->start_proc | ||
150 | = GNUNET_OS_start_process_vap ( | ||
151 | GNUNET_OS_INHERIT_STD_ERR, | ||
152 | NULL, | ||
153 | NULL, | ||
154 | NULL, | ||
155 | bss->args[0], | ||
156 | bss->args); | ||
157 | bss->cwh = GNUNET_wait_child (bss->start_proc, | ||
158 | &child_completed_callback, | ||
159 | bss); | ||
160 | GNUNET_break (NULL != bss->cwh); | ||
161 | } | ||
162 | |||
163 | |||
164 | /** | ||
165 | * This function prepares an array with traits. | ||
166 | */ | ||
167 | static enum GNUNET_GenericReturnValue | ||
168 | traits (void *cls, | ||
169 | const void **ret, | ||
170 | const char *trait, | ||
171 | unsigned int index) | ||
172 | { | ||
173 | struct BashScriptState *bss = cls; | ||
174 | struct GNUNET_TESTING_Trait traits[] = { | ||
175 | GNUNET_TESTING_make_trait_process (&bss->start_proc), | ||
176 | GNUNET_TESTING_trait_end () | ||
177 | }; | ||
178 | |||
179 | return GNUNET_TESTING_get_trait (traits, | ||
180 | ret, | ||
181 | trait, | ||
182 | index); | ||
183 | } | ||
184 | |||
185 | |||
186 | const struct GNUNET_TESTING_Command | ||
187 | GNUNET_TESTING_cmd_exec ( | ||
188 | const char *label, | ||
189 | enum GNUNET_OS_ProcessStatusType expected_type, | ||
190 | unsigned long int expected_exit_code, | ||
191 | char *const script_argv[]) | ||
192 | { | ||
193 | struct BashScriptState *bss; | ||
194 | unsigned int cnt; | ||
195 | |||
196 | cnt = 0; | ||
197 | while (NULL != script_argv[cnt]) | ||
198 | cnt++; | ||
199 | bss = GNUNET_new (struct BashScriptState); | ||
200 | bss->args = GNUNET_new_array (cnt + 1, | ||
201 | char *); | ||
202 | for (unsigned int i = 0; i<cnt; i++) | ||
203 | bss->args[i] = GNUNET_strdup (script_argv[i]); | ||
204 | bss->expected_type = expected_type; | ||
205 | bss->expected_exit_code = expected_exit_code; | ||
206 | return GNUNET_TESTING_command_new_ac ( | ||
207 | bss, | ||
208 | label, | ||
209 | &exec_bash_script_run, | ||
210 | &exec_bash_script_cleanup, | ||
211 | &traits, | ||
212 | &bss->ac); | ||
213 | } | ||
214 | |||
215 | |||
216 | const struct GNUNET_TESTING_Command | ||
217 | GNUNET_TESTING_cmd_exec_va ( | ||
218 | const char *label, | ||
219 | enum GNUNET_OS_ProcessStatusType expected_type, | ||
220 | unsigned long int expected_exit_code, | ||
221 | ...) | ||
222 | { | ||
223 | struct BashScriptState *bss; | ||
224 | va_list ap; | ||
225 | const char *arg; | ||
226 | unsigned int cnt; | ||
227 | |||
228 | bss = GNUNET_new (struct BashScriptState); | ||
229 | va_start (ap, | ||
230 | expected_exit_code); | ||
231 | cnt = 1; | ||
232 | while (NULL != (arg = va_arg (ap, | ||
233 | const char *))) | ||
234 | cnt++; | ||
235 | va_end (ap); | ||
236 | bss->args = GNUNET_new_array (cnt, | ||
237 | char *); | ||
238 | cnt = 0; | ||
239 | va_start (ap, | ||
240 | expected_exit_code); | ||
241 | while (NULL != (arg = va_arg (ap, | ||
242 | const char *))) | ||
243 | bss->args[cnt++] = GNUNET_strdup (arg); | ||
244 | va_end (ap); | ||
245 | bss->expected_type = expected_type; | ||
246 | bss->expected_exit_code = expected_exit_code; | ||
247 | return GNUNET_TESTING_command_new_ac ( | ||
248 | bss, | ||
249 | label, | ||
250 | &exec_bash_script_run, | ||
251 | &exec_bash_script_cleanup, | ||
252 | &traits, | ||
253 | &bss->ac); | ||
254 | } | ||