GNUnet 0.21.2
testing_api_cmd_exec.c
Go to the documentation of this file.
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
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
33{
38
44
49
54
58 char **args;
59
64
68 unsigned long int expected_exit_code;
69
70};
71
76static void
78{
79 struct BashScriptState *bss = cls;
80
81 if (NULL != bss->cwh)
82 {
84 "Cancel child\n");
86 bss->cwh = NULL;
87 }
88 if (NULL != bss->start_proc)
89 {
91 "Kill process\n");
92 GNUNET_assert (0 ==
94 SIGKILL));
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
111static void
114 long unsigned int exit_code)
115{
116 struct BashScriptState *bss = cls;
117
118 bss->cwh = NULL;
120 bss->start_proc = NULL;
121 if ( (bss->expected_type != type) ||
122 (bss->expected_exit_code != exit_code) )
123 {
125 "Child failed with error %lu (wanted %lu) %d/%d!\n",
126 exit_code,
128 type,
129 bss->expected_type);
131 return;
132 }
134}
135
136
142static void
145{
146 struct BashScriptState *bss = cls;
147
148 GNUNET_assert (NULL == bss->cwh);
149 bss->start_proc
152 NULL,
153 NULL,
154 NULL,
155 bss->args[0],
156 bss->args);
157 bss->cwh = GNUNET_wait_child (bss->start_proc,
159 bss);
160 GNUNET_break (NULL != bss->cwh);
161}
162
163
168traits (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[] = {
177 };
178
180 ret,
181 trait,
182 index);
183}
184
185
186const struct GNUNET_TESTING_Command
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]);
207 bss,
208 label,
211 &traits,
212 &bss->ac);
213}
214
215
216const struct GNUNET_TESTING_Command
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,
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,
241 while (NULL != (arg = va_arg (ap,
242 const char *)))
243 bss->args[cnt++] = GNUNET_strdup (arg);
244 va_end (ap);
248 bss,
249 label,
252 &traits,
253 &bss->ac);
254}
static int ret
Final status code.
Definition: gnunet-arm.c:94
static struct GNUNET_TESTING_Interpreter * is
static uint32_t type
Type string converted to DNS type value.
static int exit_code
Global exit code.
Definition: gnunet-qr.c:43
struct GNUNET_TESTING_Command GNUNET_TESTING_command_new_ac(void *cls, const char *label, GNUNET_TESTING_CommandRunRoutine run, GNUNET_TESTING_CommandCleanupRoutine cleanup, GNUNET_TESTING_CommandGetTraits traits, struct GNUNET_TESTING_AsyncContext *ac)
Create a new command that may be asynchronous.
struct GNUNET_TESTING_Trait GNUNET_TESTING_make_trait_process(struct GNUNET_OS_Process **value)
void GNUNET_TESTING_async_finish(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has finished.
void GNUNET_TESTING_async_fail(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has failed.
struct GNUNET_TESTING_Trait GNUNET_TESTING_trait_end(void)
"end" of traits array.
enum GNUNET_GenericReturnValue GNUNET_TESTING_get_trait(const struct GNUNET_TESTING_Trait *traits, const void **ret, const char *trait, unsigned int index)
Obtain value of a trait from a command.
#define GNUNET_log(kind,...)
void GNUNET_wait_child_cancel(struct GNUNET_ChildWaitHandle *cwh)
Stop waiting on this child.
struct GNUNET_ChildWaitHandle * GNUNET_wait_child(struct GNUNET_OS_Process *proc, GNUNET_ChildCompletedCallback cb, void *cb_cls)
Starts the handling of the child processes.
void(* GNUNET_ChildCompletedCallback)(void *cls, enum GNUNET_OS_ProcessStatusType type, long unsigned int exit_code)
Defines a GNUNET_ChildCompletedCallback which is sent back upon death or completion of a child proces...
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_OS_ProcessStatusType
Process status types.
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:260
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
Definition: os_priority.c:210
enum GNUNET_GenericReturnValue GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
Wait for a process to terminate.
Definition: os_priority.c:877
struct GNUNET_OS_Process * GNUNET_OS_start_process_vap(enum GNUNET_OS_InheritStdioFlags std_inheritance, struct GNUNET_DISK_PipeHandle *pipe_stdin, struct GNUNET_DISK_PipeHandle *pipe_stdout, struct GNUNET_DISK_PipeHandle *pipe_stderr, const char *filename, char *const argv[])
Start a process.
Definition: os_priority.c:573
@ GNUNET_OS_INHERIT_STD_ERR
When this flag is set, the child process will inherit stderr of the parent.
Definition: gnunet_os_lib.h:95
char ** args
NULL-terminated array of command-line arguments.
struct GNUNET_TESTING_AsyncContext ac
Context for our asynchronous completion.
struct GNUNET_ChildWaitHandle * cwh
Wait for death of start_proc.
enum GNUNET_OS_ProcessStatusType expected_type
GNUNET_ChildCompletedCallback cb
Callback handed over to the command, which should be called upon death or completion of the script.
unsigned long int expected_exit_code
struct GNUNET_OS_Process * start_proc
The process id of the script.
Struct which defines a Child Wait handle.
State each asynchronous command must have in its closure.
A command to be run by the interpreter.
struct GNUNET_TESTING_CommandLabel label
Label for the command.
Global state of the interpreter, used by a command to access information about other commands.
A struct GNUNET_TESTING_Trait can be used to exchange data between cmds.
unsigned int index
Index number associated with the trait.
const struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_exec(const char *label, enum GNUNET_OS_ProcessStatusType expected_type, unsigned long int expected_exit_code, char *const script_argv[])
Command to execute a command.
static void child_completed_callback(void *cls, enum GNUNET_OS_ProcessStatusType type, long unsigned int exit_code)
Callback which will be called if the setup script finished.
static enum GNUNET_GenericReturnValue traits(void *cls, const void **ret, const char *trait, unsigned int index)
This function prepares an array with traits.
const struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_exec_va(const char *label, enum GNUNET_OS_ProcessStatusType expected_type, unsigned long int expected_exit_code,...)
Command to execute a command.
static void exec_bash_script_cleanup(void *cls)
The cleanup function of this cmd frees resources the cmd allocated.
static void exec_bash_script_run(void *cls, struct GNUNET_TESTING_Interpreter *is)
Run method of the command created by the interpreter to wait for another command to finish.