GNUnet 0.22.2
gnunet_testing_lib.h
Go to the documentation of this file.
1/*
2 This file is part of GNUnet
3 Copyright (C) 2021, 2023, 2024 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
27#ifndef GNUNET_TESTING_LIB_H
28#define GNUNET_TESTING_LIB_H
29
30#include "gnunet_util_lib.h"
31
35#define GNUNET_TESTING_CMD_MAX_LABEL_LENGTH 127
36
37/* ********************* Helper functions ********************* */
38
44#define GNUNET_TESTING_FAIL(is) \
45 do { \
46 GNUNET_break (0); \
47 GNUNET_TESTING_interpreter_fail (is); \
48 return; \
49 } while (0)
50
51
58#define GNUNET_TESTING_command_incomplete(is,label) \
59 do { \
60 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, \
61 "Command %s (%s:%u) did not complete (at %s)\n", \
62 label, \
63 __FILE__, \
64 __LINE__, \
65 GNUNET_TESTING_interpreter_current_cmd_get_label (is)); \
66 } while (0)
67
68
69/* ******************* Generic interpreter logic ************ */
70
76
81{
82
88
93
98
105
111};
112
113
119void
121 struct GNUNET_TESTING_AsyncContext *ac);
122
123
129void
131 struct GNUNET_TESTING_AsyncContext *ac);
132
133
145typedef void
147 void *cls,
149
150
157typedef void
159
160
171typedef enum GNUNET_GenericReturnValue
173 const void **ret,
174 const char *trait,
175 unsigned int index);
176
191 void *cls,
192 const char *label,
196 struct GNUNET_TESTING_AsyncContext *ac);
197
198
209#define GNUNET_TESTING_command_new(cls,label,run,cleanup,traits) \
210 GNUNET_TESTING_command_new_ac (cls,label,run,cleanup,traits,NULL)
211
212
217{
219};
220
221
229void
231 struct GNUNET_TESTING_CommandLabel *label,
232 const char *value);
233
234
239{
240
245
249 void *cls;
250
254 const char *name;
255
272
282
290
303
308
313
324
330 unsigned int num_tries;
331
339
340};
341
342
350const struct GNUNET_TESTING_Command *
353 const char *label);
354
355
363const struct GNUNET_TESTING_Command *
366 const char *name);
367
368
376void
379
380
387void
390
391
398const char *
401
402
408void
411
412
419void
422
423
430typedef void
432 void *cls,
433 const struct GNUNET_TESTING_Command *cmd);
434
435
445void
448 bool asc,
450 void *cb_cls);
451
452
453/* ************** Fundamental interpreter commands ************ */
454
455
463
464
477 const char *label,
478 struct GNUNET_TESTING_Command *batch);
479
480
485{
489 const char *prefix;
490
495
501
505 unsigned int num_commands;
506
510 unsigned int num_retries;
511};
512
522 const char *label,
523 struct GNUNET_TESTING_Timer *timers);
524
525
536 const char *name,
537 struct GNUNET_TESTING_Command cmd);
538
539
550 const char *label,
551 unsigned int number_to_be_reached);
552
553
564 const char *label,
565 const char *barrier_label);
566
567
568#define GNUNET_TESTING_NETJAIL_START_SCRIPT "netjail_start_new.sh"
569
570#define GNUNET_TESTING_NETJAIL_STOP_SCRIPT "netjail_stop.sh"
571
582 const char *label,
583 const char *topology_cmd_label,
585
586
598 const char *label,
599 const char *script,
600 const char *topology_cmd_label);
601
602
605 const char *label,
606 const char *filename);
607
608
611 const char *label,
612 const char *topology_data);
613
614
629 struct GNUNET_TESTING_Command cmd);
630
631
643const struct GNUNET_TESTING_Command
645 const char *finish_label,
646 const char *cmd_ref,
648
649
660 const char *label,
661 const char *process_label,
662 int signal);
663
664
674 const char *label,
676
677
683const struct GNUNET_TESTING_Command
685 const char *label,
686 enum GNUNET_OS_ProcessStatusType expected_type,
687 unsigned long int expected_exit_code,
688 char *const script_argv[]);
689
690
696const struct GNUNET_TESTING_Command
698 const char *label,
699 enum GNUNET_OS_ProcessStatusType expected_type,
700 unsigned long int expected_exit_code,
701 ...);
702
703
719 const char *label,
720 const char *target_label,
721 unsigned int counter);
722
723
724/* ***************** main loop logic ************* */
725
732typedef void
734 void *cls,
736
737
751 const struct GNUNET_TESTING_Command *commands,
754 void *rc_cls);
755
756
765int
767 const struct GNUNET_TESTING_Command *commands,
769
770
771/* ***************** plugin logic ************* */
772
773
778
779
782 const struct GNUNET_TESTING_Command *commands);
783
784#define GNUNET_TESTING_MAKE_PLUGIN(prefix,name,...) \
785 void * \
786 prefix ## _plugin_ ## name ## _init (void *cls) { \
787 const char *my_node_id = cls; (void) my_node_id; \
788 struct GNUNET_TESTING_Command commands[] = { \
789 __VA_ARGS__, \
790 GNUNET_TESTING_cmd_end () \
791 }; \
792 return GNUNET_TESTING_make_plugin (commands); \
793 } \
794 void * \
795 prefix ## _plugin_ ## name ## _done (void *cls) { \
796 struct GNUNET_TESTING_PluginFunctions *api = cls; \
797 GNUNET_free (api); \
798 return NULL; \
799 }
800
801
802/* *** Generic trait logic for implementing traits ********* */
803
814{
820 unsigned int index;
821
825 const char *trait_name;
826
830 const void *ptr;
831};
832
833
841
842
854 const struct GNUNET_TESTING_Trait *traits,
855 const void **ret,
856 const char *trait,
857 unsigned int index);
858
859
868#define GNUNET_TESTING_MAKE_DECL_SIMPLE_TRAIT(prefix,name,type) \
869 enum GNUNET_GenericReturnValue \
870 prefix ## _get_trait_ ## name ( \
871 const struct GNUNET_TESTING_Command *cmd, \
872 type * *ret); \
873 struct GNUNET_TESTING_Trait \
874 prefix ## _make_trait_ ## name ( \
875 type * value);
876
877
886#define GNUNET_TESTING_MAKE_IMPL_SIMPLE_TRAIT(prefix,name,type) \
887 enum GNUNET_GenericReturnValue \
888 prefix ## _get_trait_ ## name ( \
889 const struct GNUNET_TESTING_Command *cmd, \
890 type * *ret) \
891 { \
892 if (NULL == cmd->traits) return GNUNET_SYSERR; \
893 return cmd->traits (cmd->cls, \
894 (const void **) ret, \
895 GNUNET_S (name), \
896 0); \
897 } \
898 struct GNUNET_TESTING_Trait \
899 prefix ## _make_trait_ ## name ( \
900 type * value) \
901 { \
902 struct GNUNET_TESTING_Trait ret = { \
903 .trait_name = GNUNET_S (name), \
904 .ptr = (const void *) value \
905 }; \
906 return ret; \
907 }
908
909
918#define GNUNET_TESTING_MAKE_DECL_INDEXED_TRAIT(prefix,name,type) \
919 enum GNUNET_GenericReturnValue \
920 prefix ## _get_trait_ ## name ( \
921 const struct GNUNET_TESTING_Command *cmd, \
922 unsigned int index, \
923 type * *ret); \
924 struct GNUNET_TESTING_Trait \
925 prefix ## _make_trait_ ## name ( \
926 unsigned int index, \
927 type * value);
928
929
934#define GNUNET_TESTING_MAKE_IMPL_INDEXED_TRAIT(prefix,name,type) \
935 enum GNUNET_GenericReturnValue \
936 prefix ## _get_trait_ ## name ( \
937 const struct GNUNET_TESTING_Command *cmd, \
938 unsigned int index, \
939 type * *ret) \
940 { \
941 if (NULL == cmd->traits) return GNUNET_SYSERR; \
942 return cmd->traits (cmd->cls, \
943 (const void **) ret, \
944 GNUNET_S (name), \
945 index); \
946 } \
947 struct GNUNET_TESTING_Trait \
948 prefix ## _make_trait_ ## name ( \
949 unsigned int index, \
950 type * value) \
951 { \
952 struct GNUNET_TESTING_Trait ret = { \
953 .index = index, \
954 .trait_name = GNUNET_S (name), \
955 .ptr = (const void *) value \
956 }; \
957 return ret; \
958 }
959
960
967#define GNUNET_TESTING_SIMPLE_TRAITS(op,prefix) \
968 op (prefix, process, struct GNUNET_OS_Process *) \
969 op (prefix, cmd, const struct GNUNET_TESTING_Command) \
970 op (prefix, batch_cmds, struct GNUNET_TESTING_Command *)
971
972
974 GNUNET_TESTING)
975
976
977#endif
static int ret
Final status code.
Definition: gnunet-arm.c:93
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Main function that will be run by the scheduler.
Definition: gnunet-arm.c:918
static struct GNUNET_TIME_Relative timeout
User defined timestamp for completing operations.
Definition: gnunet-arm.c:118
static struct GNUNET_TESTING_Interpreter * is
static struct VoipCommand commands[]
List of supported commands.
static void cleanup(void *cls)
Disconnect and shutdown.
Definition: gnunet-did.c:130
static char * filename
static char * name
Name (label) of the records to list.
static char * value
Value of the record to add/remove.
static struct GNUNET_TIME_Relative duration
Option '-d': duration of the mapping.
Definition: gnunet-vpn.c:90
#define GNUNET_TESTING_MAKE_DECL_SIMPLE_TRAIT(prefix, name, type)
Create headers for a trait with name name for statically allocated data of type type.
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.
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_Command GNUNET_TESTING_cmd_sleep(const char *label, struct GNUNET_TIME_Relative duration)
Sleep for duration.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_netjail_start_helpers(const char *label, const char *topology_cmd_label, struct GNUNET_TIME_Relative timeout)
Create command.
void(* GNUNET_TESTING_CommandRunRoutine)(void *cls, struct GNUNET_TESTING_Interpreter *is)
Signature of a function used to start executing a command of a test.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_barrier_create(const char *label, unsigned int number_to_be_reached)
Command to create a barrier.
void GNUNET_TESTING_async_finish(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has finished.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_netjail_setup(const char *label, const char *script, const char *topology_cmd_label)
This command executes a shell script to setup the netjail environment.
void GNUNET_TESTING_interpreter_current_cmd_touch(struct GNUNET_TESTING_Interpreter *is)
Update the last request time of the current command to the current time.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_batch(const char *label, struct GNUNET_TESTING_Command *batch)
Create a "batch" command.
void GNUNET_TESTING_async_fail(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has failed.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_rewind_ip(const char *label, const char *target_label, unsigned int counter)
Make the instruction pointer point to target_label only if counter is greater than zero.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_load_topology_from_string(const char *label, const char *topology_data)
void GNUNET_TESTING_interpreter_commands_iterate(struct GNUNET_TESTING_Interpreter *is, bool asc, GNUNET_TESTING_CommandIterator cb, void *cb_cls)
Iterates over all of the top-level commands of an interpreter.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_signal(const char *label, const char *process_label, int signal)
Create a "signal" CMD.
void(* GNUNET_TESTING_CommandCleanupRoutine)(void *cls)
Signature of a function used to clean up resources allocated by a command.
void(* GNUNET_TESTING_CommandIterator)(void *cls, const struct GNUNET_TESTING_Command *cmd)
Callback over commands of an interpreter.
const char * GNUNET_TESTING_interpreter_current_cmd_get_label(struct GNUNET_TESTING_Interpreter *is)
Obtain label of the command being now run.
struct GNUNET_TESTING_Interpreter * GNUNET_TESTING_run(const struct GNUNET_TESTING_Command *commands, struct GNUNET_TIME_Relative timeout, GNUNET_TESTING_ResultCallback rc, void *rc_cls)
Run the testsuite.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_load_topology_from_file(const char *label, const char *filename)
void(* GNUNET_TESTING_ResultCallback)(void *cls, enum GNUNET_GenericReturnValue rv)
Function called with the final result of the test.
const struct GNUNET_TESTING_Command * GNUNET_TESTING_interpreter_lookup_command(struct GNUNET_TESTING_Interpreter *is, const char *label)
Lookup command by label.
void GNUNET_TESTING_interpreter_skip(struct GNUNET_TESTING_Interpreter *is)
Skips the current test, the environment is not prepared correctly.
const struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_finish(const char *finish_label, const char *cmd_ref, struct GNUNET_TIME_Relative timeout)
Create (synchronous) command that waits for another command to finish.
enum GNUNET_GenericReturnValue(* GNUNET_TESTING_CommandGetTraits)(void *cls, const void **ret, const char *trait, unsigned int index)
Signature of a function used to extract traits exposed by a command.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_make_unblocking(struct GNUNET_TESTING_Command cmd)
Turn asynchronous command into non-blocking command by setting asynchronous_finish to true.
void GNUNET_TESTING_interpreter_current_cmd_inc_tries(struct GNUNET_TESTING_Interpreter *is)
Increment the 'num_tries' counter for the current command.
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.
struct GNUNET_TESTING_Trait GNUNET_TESTING_trait_end(void)
"end" of traits array.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_stat(const char *label, struct GNUNET_TESTING_Timer *timers)
Obtain performance data from the interpreter.
int GNUNET_TESTING_main(const struct GNUNET_TESTING_Command *commands, struct GNUNET_TIME_Relative timeout)
Start a GNUnet scheduler event loop and run the testsuite.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_end(void)
Create command array terminator.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_set_var(const char *name, struct GNUNET_TESTING_Command cmd)
Set variable to command as side-effect of running a command.
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.
struct GNUNET_TESTING_PluginFunctions * GNUNET_TESTING_make_plugin(const struct GNUNET_TESTING_Command *commands)
void GNUNET_TESTING_set_label(struct GNUNET_TESTING_CommandLabel *label, const char *value)
Set label to value.
void GNUNET_TESTING_interpreter_fail(struct GNUNET_TESTING_Interpreter *is)
Current command failed, clean up and fail the test case.
#define GNUNET_TESTING_CMD_MAX_LABEL_LENGTH
Central interpreter and command loop for writing an interpreter to test asynchronous systems.
#define GNUNET_TESTING_SIMPLE_TRAITS(op, prefix)
Call op on all simple traits needed by testing core logic.
const struct GNUNET_TESTING_Command * GNUNET_TESTING_interpreter_get_command(struct GNUNET_TESTING_Interpreter *is, const char *name)
Get command from hash map by variable name.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_barrier_reached(const char *label, const char *barrier_label)
If this command is executed the the process is signaling the master process that it reached a barrier...
GNUNET_GenericReturnValue
Named constants for return values.
GNUNET_OS_ProcessStatusType
Process status types.
void(* GNUNET_SCHEDULER_TaskCallback)(void *cls)
Signature of the main function of a task.
State each asynchronous command must have in its closure.
GNUNET_SCHEDULER_TaskCallback notify_finished
Function to call when async operation is done.
bool next_called
Set to true if interpreter_next() has already been called for this command.
struct GNUNET_TESTING_Interpreter * is
Interpreter we are part of.
enum GNUNET_GenericReturnValue finished
Indication if the command finished (GNUNET_OK).
void * notify_finished_cls
Closure for notify_finished.
Structure with storage space for a label.
A command to be run by the interpreter.
GNUNET_TESTING_CommandCleanupRoutine cleanup
Clean up after the command.
struct GNUNET_TIME_Absolute finish_time
When did the execution of this command finish?
const char * name
Variable name for the command, NULL for none.
struct GNUNET_TESTING_CommandLabel label
Label for the command.
struct GNUNET_TIME_Absolute last_req_time
When did we start the last run of this command? Delta to finish_time gives the latency for the last s...
struct GNUNET_TESTING_AsyncContext * ac
Pointer to the asynchronous context in the command's closure.
bool asynchronous_finish
If "true", the interpreter should not immediately run the next command, even if this command did not ...
GNUNET_TESTING_CommandGetTraits traits
Extract information from a command that is useful for other commands.
GNUNET_TESTING_CommandRunRoutine run
Runs the command.
unsigned int num_tries
How often did we try to execute this command? (In case it is a request that is repated....
struct GNUNET_TIME_Absolute start_time
When did the execution of this command start?
void * cls
Closure for all commands with command-specific context information.
Global state of the interpreter, used by a command to access information about other commands.
GNUNET_TESTING_ResultCallback rc
Function to call with the test result.
void * rc_cls
Closure for rc.
The plugin API every test case plugin has to implement.
Performance counter.
unsigned int num_commands
Number of commands summed up.
const char * prefix
For which type of commands.
struct GNUNET_TIME_Relative total_duration
Total time spend in all commands of this type.
unsigned int num_retries
Number of retries summed up.
struct GNUNET_TIME_Relative success_latency
Total time spend waiting for the successful execution in all commands of this type.
A struct GNUNET_TESTING_Trait can be used to exchange data between cmds.
const void * ptr
Pointer to the piece of data to offer.
unsigned int index
Index number associated with the trait.
const char * trait_name
Trait type, for example "reserve-pub" or "coin-priv".
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.
static enum GNUNET_GenericReturnValue traits(void *cls, const void **ret, const char *trait, unsigned int index)
This function prepares an array with traits.