GNUnet 0.26.2-98-gb402d9955
 
Loading...
Searching...
No Matches
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
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");
94 SIGKILL));
97 true,
98 NULL,
99 NULL));
101 bss->start_proc = NULL;
102 }
103 for (unsigned int i = 0; NULL != bss->args[i]; i++)
104 GNUNET_free (bss->args[i]);
105 GNUNET_free (bss->args);
106 GNUNET_free (bss);
107}
108
109
114static void
117 long unsigned int exit_code)
118{
119 struct BashScriptState *bss = cls;
120
121 bss->cwh = NULL;
123 bss->start_proc = NULL;
124 if ( (bss->expected_type != type) ||
125 (bss->expected_exit_code != exit_code) )
126 {
128 "Child failed with error %lu (wanted %lu) %d/%d!\n",
129 exit_code,
131 type,
132 bss->expected_type);
134 return;
135 }
137}
138
139
145static void
148{
149 struct BashScriptState *bss = cls;
150
151 GNUNET_assert (NULL == bss->cwh);
153 if (GNUNET_OK !=
155 bss->args[0],
156 (const char **) bss->args))
157 {
158 GNUNET_break (0);
160 return;
161 }
162 bss->cwh = GNUNET_wait_child (bss->start_proc,
164 bss);
165 GNUNET_break (NULL != bss->cwh);
166}
167
168
173traits (void *cls,
174 const void **ret,
175 const char *trait,
176 unsigned int index)
177{
178 struct BashScriptState *bss = cls;
179 struct GNUNET_TESTING_Trait traits[] = {
182 };
183
185 ret,
186 trait,
187 index);
188}
189
190
193 const char *label,
194 enum GNUNET_OS_ProcessStatusType expected_type,
195 unsigned long int expected_exit_code,
196 char *const script_argv[])
197{
198 struct BashScriptState *bss;
199 unsigned int cnt;
200
201 cnt = 0;
202 while (NULL != script_argv[cnt])
203 cnt++;
204 bss = GNUNET_new (struct BashScriptState);
205 bss->args = GNUNET_new_array (cnt + 1,
206 char *);
207 for (unsigned int i = 0; i<cnt; i++)
208 bss->args[i] = GNUNET_strdup (script_argv[i]);
212 bss,
213 label,
216 &traits,
217 &bss->ac);
218}
219
220
223 const char *label,
224 enum GNUNET_OS_ProcessStatusType expected_type,
225 unsigned long int expected_exit_code,
226 ...)
227{
228 struct BashScriptState *bss;
229 va_list ap;
230 const char *arg;
231 unsigned int cnt;
232
233 bss = GNUNET_new (struct BashScriptState);
234 va_start (ap,
236 cnt = 1;
237 while (NULL != (arg = va_arg (ap,
238 const char *)))
239 cnt++;
240 va_end (ap);
241 bss->args = GNUNET_new_array (cnt,
242 char *);
243 cnt = 0;
244 va_start (ap,
246 while (NULL != (arg = va_arg (ap,
247 const char *)))
248 bss->args[cnt++] = GNUNET_strdup (arg);
249 va_end (ap);
253 bss,
254 label,
257 &traits,
258 &bss->ac);
259}
static int ret
Final status code.
Definition gnunet-arm.c:93
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.
void GNUNET_TESTING_async_finish(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has finished.
#define GNUNET_TESTING_FAIL(is)
Print failing line number and trigger shutdown.
struct GNUNET_TESTING_Trait GNUNET_TESTING_make_trait_process(struct GNUNET_Process **value)
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_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.
enum GNUNET_GenericReturnValue GNUNET_process_run_command_argv(struct GNUNET_Process *p, const char *filename, const char **argv)
Set the command and start a process.
Definition os_process.c:840
enum GNUNET_GenericReturnValue GNUNET_process_wait(struct GNUNET_Process *proc, bool blocking, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
Wait for a process to terminate.
void GNUNET_process_destroy(struct GNUNET_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition os_process.c:363
GNUNET_OS_ProcessStatusType
Process status types.
enum GNUNET_GenericReturnValue GNUNET_process_kill(struct GNUNET_Process *proc, int sig)
Sends a signal to the process.
Definition os_process.c:307
struct GNUNET_Process * GNUNET_process_create(enum GNUNET_OS_InheritStdioFlags std_inheritance)
Create a process handle.
Definition os_process.c:462
@ GNUNET_OS_INHERIT_STD_ERR
When this flag is set, the child process will inherit stderr of the parent.
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_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.
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.
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.
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.