GNUnet  0.18.1
Testing library

Library for writing testcases for GNUnet. More...

Collaboration diagram for Testing library:

Data Structures

struct  GNUNET_TESTING_SharedService
 Specification of a service that is to be shared among peers. More...
 

Macros

#define GNUNET_TESTING_HOSTKEYFILESIZE
 Size of each hostkey in the hostkey file (in BYTES). More...
 
#define GNUNET_TESTING_PREFIX   "GNUNET_TESTING_PREFIX"
 The environmental variable, if set, that dictates where testing should place generated peer configurations. More...
 

Typedefs

typedef void(* GNUNET_TESTING_PeerStopCallback) (void *cls, struct GNUNET_TESTING_Peer *peer, int success)
 Callback to inform whether the peer is running or stopped. More...
 
typedef void(* GNUNET_TESTING_TestMain) (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_TESTING_Peer *peer)
 Signature of the 'main' function for a (single-peer) testcase that is run using GNUNET_TESTING_peer_run(). More...
 

Functions

struct GNUNET_TESTING_SystemGNUNET_TESTING_system_create (const char *testdir, const char *trusted_ip, const char *hostname, const struct GNUNET_TESTING_SharedService *shared_services)
 Create a system handle. More...
 
struct GNUNET_TESTING_SystemGNUNET_TESTING_system_create_with_portrange (const char *testdir, const char *trusted_ip, const char *hostname, const struct GNUNET_TESTING_SharedService *shared_services, uint16_t lowport, uint16_t highport)
 Create a system handle. More...
 
void GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system, int remove_paths)
 Free system resources. More...
 
struct GNUNET_CRYPTO_EddsaPrivateKeyGNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system, uint32_t key_number, struct GNUNET_PeerIdentity *id)
 Testing includes a number of pre-created hostkeys for faster peer startup. More...
 
uint16_t GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system)
 Reserve a port for a peer. More...
 
void GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system, uint16_t port)
 Release reservation of a TCP or UDP port for a peer (used during GNUNET_TESTING_peer_destroy). More...
 
int GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system, struct GNUNET_CONFIGURATION_Handle *cfg)
 Create a new configuration using the given configuration as a template; ports and paths will be modified to select available ports on the local system. More...
 
struct GNUNET_TESTING_PeerGNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system, struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t key_number, struct GNUNET_PeerIdentity *id, char **emsg)
 Configure a GNUnet peer. More...
 
void GNUNET_TESTING_peer_get_identity (struct GNUNET_TESTING_Peer *peer, struct GNUNET_PeerIdentity *id)
 Obtain the peer identity from a peer handle. More...
 
int GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer)
 Start the peer. More...
 
int GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer)
 Stop the peer. More...
 
void GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer)
 Destroy the peer. More...
 
int GNUNET_TESTING_peer_kill (struct GNUNET_TESTING_Peer *peer)
 Sends SIGTERM to the peer's main process. More...
 
int GNUNET_TESTING_peer_wait (struct GNUNET_TESTING_Peer *peer)
 Waits for a peer to terminate. More...
 
int GNUNET_TESTING_peer_stop_async (struct GNUNET_TESTING_Peer *peer, GNUNET_TESTING_PeerStopCallback cb, void *cb_cls)
 Stop a peer asynchronously using ARM API. More...
 
void GNUNET_TESTING_peer_stop_async_cancel (struct GNUNET_TESTING_Peer *peer)
 Cancel a previous asynchronous peer stop request. More...
 
int GNUNET_TESTING_peer_run (const char *testdir, const char *cfgfilename, GNUNET_TESTING_TestMain tm, void *tm_cls)
 Start a single peer and run a test using the testing library. More...
 
int GNUNET_TESTING_service_run (const char *testdir, const char *service_name, const char *cfgfilename, GNUNET_TESTING_TestMain tm, void *tm_cls)
 Start a single service (no ARM, except of course if the given service name is 'arm') and run a test using the testing library. More...
 
char * GNUNET_TESTING_get_testname_from_underscore (const char *argv0)
 Sometimes we use the binary name to determine which specific test to run. More...
 

Detailed Description

Library for writing testcases for GNUnet.

It can start/stop one or more peers on a system; testing is responsible for managing private keys, ports and paths; it is a low-level library that does not support higher-level functions such as P2P connection, topology management or distributed testbed maintenance (those are provided by the Testbed service)

See also
Documentation

Macro Definition Documentation

◆ GNUNET_TESTING_HOSTKEYFILESIZE

#define GNUNET_TESTING_HOSTKEYFILESIZE
Value:
sizeof(struct \
GNUNET_CRYPTO_EddsaPrivateKey)

Size of each hostkey in the hostkey file (in BYTES).

Definition at line 63 of file gnunet_testing_lib.h.

◆ GNUNET_TESTING_PREFIX

#define GNUNET_TESTING_PREFIX   "GNUNET_TESTING_PREFIX"

The environmental variable, if set, that dictates where testing should place generated peer configurations.

Definition at line 70 of file gnunet_testing_lib.h.

Typedef Documentation

◆ GNUNET_TESTING_PeerStopCallback

typedef void(* GNUNET_TESTING_PeerStopCallback) (void *cls, struct GNUNET_TESTING_Peer *peer, int success)

Callback to inform whether the peer is running or stopped.

Parameters
clsthe closure given to GNUNET_TESTING_peer_stop_async()
peerthe respective peer whose status is being reported
successGNUNET_YES if the peer is stopped; GNUNET_SYSERR upon any error

Definition at line 350 of file gnunet_testing_lib.h.

◆ GNUNET_TESTING_TestMain

typedef void(* GNUNET_TESTING_TestMain) (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_TESTING_Peer *peer)

Signature of the 'main' function for a (single-peer) testcase that is run using GNUNET_TESTING_peer_run().

Parameters
clsclosure
cfgconfiguration of the peer that was started
peeridentity of the peer that was created

Definition at line 394 of file gnunet_testing_lib.h.

Function Documentation

◆ GNUNET_TESTING_system_create()

struct GNUNET_TESTING_System* GNUNET_TESTING_system_create ( const char *  testdir,
const char *  trusted_ip,
const char *  hostname,
const struct GNUNET_TESTING_SharedService shared_services 
)

Create a system handle.

There must only be one system handle per operating system. Uses a default range for allowed ports. Ports are still tested for availability.

Parameters
testdironly the directory name without any path. This is used for all service homes; the directory will be created in a temporary location depending on the underlying OS. This variable will be overridden with the value of the environmental variable GNUNET_TESTING_PREFIX, if it exists.
trusted_ipthe ip address which will be set as TRUSTED HOST in all service configurations generated to allow control connections from this ip. This can either be a single ip address or a network address in CIDR notation.
hostnamethe hostname of the system we are using for testing; NULL for localhost
shared_servicesNULL terminated array describing services that are to be shared among peers
Returns
handle to this system, NULL on error

Definition at line 458 of file testing.c.

463 {
465  trusted_ip,
466  hostname,
467  shared_services,
468  LOW_PORT,
469  HIGH_PORT);
470 }
static char * hostname
Our hostname; we give this to all the peers we start.
struct GNUNET_TESTING_System * GNUNET_TESTING_system_create_with_portrange(const char *testdir, const char *trusted_ip, const char *hostname, const struct GNUNET_TESTING_SharedService *shared_services, uint16_t lowport, uint16_t highport)
Create a system handle.
Definition: testing.c:373
#define HIGH_PORT
Highest port used for GNUnet testing.
Definition: testing.c:64
#define LOW_PORT
Lowest port used for GNUnet testing.
Definition: testing.c:57

References GNUNET_TESTING_system_create_with_portrange(), HIGH_PORT, hostname, LOW_PORT, GNUNET_TESTING_System::shared_services, and GNUNET_TESTING_System::trusted_ip.

Referenced by create_hostkeys(), create_unique_cfgs(), GNUNET_TESTING_service_run(), GNUNET_TRANSPORT_TESTING_init(), handle_init(), system_create_run(), and tokenizer_cb().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_system_create_with_portrange()

struct GNUNET_TESTING_System* GNUNET_TESTING_system_create_with_portrange ( const char *  testdir,
const char *  trusted_ip,
const char *  hostname,
const struct GNUNET_TESTING_SharedService shared_services,
uint16_t  lowport,
uint16_t  highport 
)

Create a system handle.

There must only be one system handle per operating system. Use this function directly if multiple system objects are created for the same host (only really useful when testing — or to make the port range configurable).

Parameters
testdironly the directory name without any path. This is used for all service homes; the directory will be created in a temporary location depending on the underlying OS. This variable will be overridden with the value of the environmental variable GNUNET_TESTING_PREFIX, if it exists.
trusted_ipthe ip address which will be set as TRUSTED HOST in all service configurations generated to allow control connections from this ip. This can either be a single ip address or a network address in CIDR notation.
hostnamethe hostname of the system we are using for testing; NULL for localhost
shared_servicesNULL terminated array describing services that are to be shared among peers
lowportlowest port number this system is allowed to allocate (inclusive)
highporthighest port number this system is allowed to allocate (exclusive)
Returns
handle to this system, NULL on error

There must only be one system handle per operating system.

Parameters
testdironly the directory name without any path. This is used for all service homes; the directory will be created in a temporary location depending on the underlying OS. This variable will be overridden with the value of the environmental variable GNUNET_TESTING_PREFIX, if it exists.
trusted_ipthe ip address which will be set as TRUSTED HOST in all service configurations generated to allow control connections from this ip. This can either be a single ip address or a network address in CIDR notation.
hostnamethe hostname of the system we are using for testing; NULL for localhost
shared_servicesNULL terminated array describing services that are to be shared among peers
lowportlowest port number this system is allowed to allocate (inclusive)
highporthighest port number this system is allowed to allocate (exclusive)
Returns
handle to this system, NULL on error

Definition at line 373 of file testing.c.

380 {
381  struct GNUNET_TESTING_System *system;
382  struct GNUNET_TESTING_SharedService tss;
383  struct SharedService ss;
384  unsigned int cnt;
385 
386  GNUNET_assert (NULL != testdir);
387  system = GNUNET_new (struct GNUNET_TESTING_System);
388  if (NULL == (system->tmppath = getenv (GNUNET_TESTING_PREFIX)))
389  system->tmppath = GNUNET_DISK_mkdtemp (testdir);
390  else
391  system->tmppath = GNUNET_strdup (system->tmppath);
392  system->lowport = lowport;
393  system->highport = highport;
394  if (NULL == system->tmppath)
395  {
396  GNUNET_free (system);
397  return NULL;
398  }
399  if (NULL != trusted_ip)
400  system->trusted_ip = GNUNET_strdup (trusted_ip);
401  if (NULL != hostname)
402  system->hostname = GNUNET_strdup (hostname);
403  if (GNUNET_OK != hostkeys_load (system))
404  {
406  return NULL;
407  }
408  if (NULL == shared_services)
409  return system;
410  for (cnt = 0; NULL != shared_services[cnt].service; cnt++)
411  {
412  tss = shared_services[cnt];
413  memset (&ss, 0, sizeof (ss));
414  ss.sname = GNUNET_strdup (tss.service);
415  ss.cfg = GNUNET_CONFIGURATION_create ();
417  ss.sname,
419  ss.cfg);
421  "TESTING",
423  ss.cfg);
425  "PATHS",
427  ss.cfg);
428  ss.share = tss.share;
430  system->n_shared_services,
431  ss);
432  }
433  return system;
434 }
char * getenv()
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
void GNUNET_CONFIGURATION_iterate_section_values(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, GNUNET_CONFIGURATION_Iterator iter, void *iter_cls)
Iterate over values of a section in the configuration.
char * GNUNET_DISK_mkdtemp(const char *t)
Create an (empty) temporary directory on disk.
Definition: disk.c:339
@ GNUNET_OK
@ GNUNET_YES
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_array_append(arr, len, element)
Append an element to an array (growing the array by one).
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_TESTING_PREFIX
The environmental variable, if set, that dictates where testing should place generated peer configura...
void GNUNET_TESTING_system_destroy(struct GNUNET_TESTING_System *system, int remove_paths)
Free system resources.
Definition: testing.c:538
Specification of a service that is to be shared among peers.
const char * service
The name of the service.
Handle for a system on which GNUnet peers are executed; a system is used for reserving unique paths a...
Definition: testing.c:103
uint16_t highport
Highest port we are allowed to use.
Definition: testing.c:168
char * trusted_ip
The trusted ip.
Definition: testing.c:114
char * tmppath
Prefix (e.g.
Definition: testing.c:108
unsigned int n_shared_services
Definition: testing.c:133
char * hostname
our hostname
Definition: testing.c:119
struct SharedService * shared_services
Definition: testing.c:131
uint16_t lowport
Lowest port we are allowed to use.
Definition: testing.c:163
The structure for identifying a shared service.
static void cfg_copy_iterator(void *cls, const char *section, const char *option, const char *value)
Function to iterate over options.
Definition: testing.c:340
static int hostkeys_load(struct GNUNET_TESTING_System *system)
Testing includes a number of pre-created hostkeys for faster peer startup.
Definition: testing.c:257

References GNUNET_TESTING_SharedService::cfg, SharedService::cfg, cfg_copy_iterator(), getenv(), GNUNET_array_append, GNUNET_assert, GNUNET_CONFIGURATION_create(), GNUNET_CONFIGURATION_iterate_section_values(), GNUNET_DISK_mkdtemp(), GNUNET_free, GNUNET_new, GNUNET_OK, GNUNET_strdup, GNUNET_TESTING_PREFIX, GNUNET_TESTING_system_destroy(), GNUNET_YES, GNUNET_TESTING_System::highport, hostkeys_load(), hostname, GNUNET_TESTING_System::hostname, GNUNET_TESTING_System::lowport, GNUNET_TESTING_System::n_shared_services, GNUNET_TESTING_SharedService::service, GNUNET_TESTING_SharedService::share, SharedService::share, GNUNET_TESTING_System::shared_services, SharedService::sname, GNUNET_TESTING_System::tmppath, and GNUNET_TESTING_System::trusted_ip.

Referenced by GNUNET_TESTING_system_create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_system_destroy()

void GNUNET_TESTING_system_destroy ( struct GNUNET_TESTING_System system,
int  remove_paths 
)

Free system resources.

Parameters
systemsystem to be freed
remove_pathsshould the 'testdir' and all subdirectories be removed (clean up on shutdown)?

Definition at line 538 of file testing.c.

540 {
541  struct SharedService *ss;
542  struct SharedServiceInstance *i;
543  unsigned int ss_cnt;
544  unsigned int i_cnt;
545 
546  if (NULL != system->hostkeys_data)
547  hostkeys_unload (system);
548  for (ss_cnt = 0; ss_cnt < system->n_shared_services; ss_cnt++)
549  {
550  ss = &system->shared_services[ss_cnt];
551  for (i_cnt = 0; i_cnt < ss->n_instances; i_cnt++)
552  {
553  i = ss->instances[i_cnt];
554  if (NULL != i->proc)
557  }
560  GNUNET_free (ss->sname);
561  }
562  GNUNET_free (system->shared_services);
563  if (GNUNET_YES == remove_paths)
565  GNUNET_free (system->tmppath);
566  GNUNET_free (system->trusted_ip);
567  GNUNET_free (system->hostname);
568  GNUNET_free (system);
569 }
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_remove(const char *filename)
Remove all files in a directory (rm -rf).
Definition: disk.c:1084
char * hostkeys_data
Hostkeys data, contains "GNUNET_TESTING_HOSTKEYFILESIZE * total_hostkeys" bytes.
Definition: testing.c:124
struct SharedService * ss
Definition: testing.c:69
struct GNUNET_OS_Process * proc
Definition: testing.c:73
struct GNUNET_CONFIGURATION_Handle * cfg
Definition: testing.c:88
unsigned int n_instances
Definition: testing.c:94
struct SharedServiceInstance ** instances
Definition: testing.c:86
char * sname
Definition: testing.c:84
static void hostkeys_unload(struct GNUNET_TESTING_System *system)
Function to remove the loaded hostkeys.
Definition: testing.c:320
static void cleanup_shared_service_instance(struct SharedServiceInstance *i)
Definition: testing.c:474
static void stop_shared_service_instance(struct SharedServiceInstance *i)
Definition: testing.c:517

References SharedService::cfg, cleanup_shared_service_instance(), GNUNET_CONFIGURATION_destroy(), GNUNET_DISK_directory_remove(), GNUNET_free, GNUNET_YES, GNUNET_TESTING_System::hostkeys_data, hostkeys_unload(), GNUNET_TESTING_System::hostname, SharedService::instances, SharedService::n_instances, GNUNET_TESTING_System::n_shared_services, SharedServiceInstance::proc, GNUNET_TESTING_System::shared_services, SharedService::sname, SharedServiceInstance::ss, stop_shared_service_instance(), GNUNET_TESTING_System::tmppath, and GNUNET_TESTING_System::trusted_ip.

Referenced by create_hostkeys(), create_unique_cfgs(), GNUNET_TESTING_service_run(), GNUNET_TESTING_system_create_with_portrange(), GNUNET_TRANSPORT_TESTING_done(), shutdown_task(), and system_destroy_run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_hostkey_get()

struct GNUNET_CRYPTO_EddsaPrivateKey* GNUNET_TESTING_hostkey_get ( const struct GNUNET_TESTING_System system,
uint32_t  key_number,
struct GNUNET_PeerIdentity id 
)

Testing includes a number of pre-created hostkeys for faster peer startup.

This function can be used to access the n-th key of those pre-created hostkeys; note that these keys are ONLY useful for testing and not secure as the private keys are part of the public GNUnet source code.

This is primarily a helper function used internally by GNUNET_TESTING_peer_configure().

Parameters
systemthe testing system handle
key_numberdesired pre-created hostkey to obtain
idset to the peer's identity (hash of the public key; if NULL, GNUNET_SYSERR is returned immediately
Returns
NULL on error (not enough keys)

This function can be used to access the n-th key of those pre-created hostkeys; note that these keys are ONLY useful for testing and not secure as the private keys are part of the public GNUnet source code.

This is primarily a helper function used internally by GNUNET_TESTING_peer_configure.

Parameters
systemthe testing system handle
key_numberdesired pre-created hostkey to obtain
idset to the peer's identity (hash of the public key; if NULL, NULL is returned immediately
Returns
NULL on error (not enough keys)

Definition at line 713 of file testing.c.

716 {
717  struct GNUNET_CRYPTO_EddsaPrivateKey *private_key;
718 
719  if ((NULL == id) || (NULL == system->hostkeys_data))
720  return NULL;
721  if (key_number >= system->total_hostkeys)
722  {
724  _ ("Key number %u does not exist\n"),
725  key_number);
726  return NULL;
727  }
728  private_key = GNUNET_new (struct GNUNET_CRYPTO_EddsaPrivateKey);
729  GNUNET_memcpy (private_key,
730  system->hostkeys_data
731  + (key_number * GNUNET_TESTING_HOSTKEYFILESIZE),
733  GNUNET_CRYPTO_eddsa_key_get_public (private_key, &id->public_key);
734  return private_key;
735 }
static struct GNUNET_IDENTITY_Handle * id
Handle to identity service.
void GNUNET_CRYPTO_eddsa_key_get_public(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:197
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_TESTING_HOSTKEYFILESIZE
Size of each hostkey in the hostkey file (in BYTES).
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
Private ECC key encoded for transmission.
uint32_t total_hostkeys
The number of hostkeys.
Definition: testing.c:158
#define LOG(kind,...)
Definition: testing.c:39

References _, GNUNET_CRYPTO_eddsa_key_get_public(), GNUNET_ERROR_TYPE_ERROR, GNUNET_memcpy, GNUNET_new, GNUNET_TESTING_HOSTKEYFILESIZE, GNUNET_TESTING_System::hostkeys_data, id, LOG, and GNUNET_TESTING_System::total_hostkeys.

Referenced by create_hostkeys(), GNUNET_TESTING_get_pub_key(), GNUNET_TESTING_peer_configure(), and GNUNET_TESTING_peer_get_identity().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_reserve_port()

uint16_t GNUNET_TESTING_reserve_port ( struct GNUNET_TESTING_System system)

Reserve a port for a peer.

Parameters
systemsystem to use for reservation tracking
Returns
0 if no free port was available

Reserve a port for a peer.

Parameters
systemsystem to use for reservation tracking
Returns
0 if no free port was available

Definition at line 579 of file testing.c.

580 {
581  struct GNUNET_NETWORK_Handle *socket;
582  struct addrinfo hint;
583  struct addrinfo *ret;
584  struct addrinfo *ai;
585  uint32_t *port_buckets;
586  char *open_port_str;
587  int bind_status;
588  uint32_t xor_image;
589  uint16_t index;
590  uint16_t open_port;
591  uint16_t pos;
592 
593  /*
594  FIXME: Instead of using getaddrinfo we should try to determine the port
595  status by the following heurestics.
596 
597  On systems which support both IPv4 and IPv6, only ports open on both
598  address families are considered open.
599  On system with either IPv4 or IPv6. A port is considered open if it's
600  open in the respective address family
601  */hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
602  hint.ai_socktype = 0;
603  hint.ai_protocol = 0;
604  hint.ai_addrlen = 0;
605  hint.ai_addr = NULL;
606  hint.ai_canonname = NULL;
607  hint.ai_next = NULL;
608  hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
609  port_buckets = system->reserved_ports;
610  for (index = (system->lowport / 32) + 1; index < (system->highport / 32);
611  index++)
612  {
613  xor_image = (UINT32_MAX ^ port_buckets[index]);
614  if (0 == xor_image) /* Ports in the bucket are full */
615  continue;
616  pos = system->lowport % 32;
617  while (pos < 32)
618  {
619  if (0 == ((xor_image >> pos) & 1U))
620  {
621  pos++;
622  continue;
623  }
624  open_port = (index * 32) + pos;
625  if (open_port >= system->highport)
626  return 0;
627  GNUNET_asprintf (&open_port_str, "%u", (unsigned int) open_port);
628  ret = NULL;
629  GNUNET_assert (0 == getaddrinfo (NULL, open_port_str, &hint, &ret));
630  GNUNET_free (open_port_str);
631  bind_status = GNUNET_NO;
632  for (ai = ret; NULL != ai; ai = ai->ai_next)
633  {
634  socket = GNUNET_NETWORK_socket_create (ai->ai_family, SOCK_STREAM, 0);
635  if (NULL == socket)
636  continue;
637  bind_status =
638  GNUNET_NETWORK_socket_bind (socket, ai->ai_addr, ai->ai_addrlen);
640  if (GNUNET_OK != bind_status)
641  break;
642  socket = GNUNET_NETWORK_socket_create (ai->ai_family, SOCK_DGRAM, 0);
643  if (NULL == socket)
644  continue;
645  bind_status =
646  GNUNET_NETWORK_socket_bind (socket, ai->ai_addr, ai->ai_addrlen);
648  if (GNUNET_OK != bind_status)
649  break;
650  }
651  port_buckets[index] |= (1U << pos); /* Set the port bit */
652  freeaddrinfo (ret);
653  if (GNUNET_OK == bind_status)
654  {
656  "Found a free port %u\n",
657  (unsigned int) open_port);
658  return open_port;
659  }
660  pos++;
661  }
662  }
663  return 0;
664 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static struct GNUNET_TRANSPORT_AddressIdentifier * ai
Handle to the operation that publishes our address.
@ GNUNET_NO
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:508
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:832
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Bind a socket to a particular address.
Definition: network.c:439
#define AI_NUMERICSERV
AI_NUMERICSERV not defined in windows.
Definition: platform.h:218
handle to a socket
Definition: network.c:53
uint32_t reserved_ports[65536/32]
Bitmap where each port that has already been reserved for some GNUnet peer is recorded.
Definition: testing.c:143

References ai, AI_NUMERICSERV, GNUNET_asprintf(), GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_NETWORK_socket_bind(), GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_create(), GNUNET_NO, GNUNET_OK, GNUNET_TESTING_System::highport, LOG, GNUNET_TESTING_System::lowport, GNUNET_TESTING_System::reserved_ports, and ret.

Referenced by associate_shared_service(), and update_config().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_release_port()

void GNUNET_TESTING_release_port ( struct GNUNET_TESTING_System system,
uint16_t  port 
)

Release reservation of a TCP or UDP port for a peer (used during GNUNET_TESTING_peer_destroy).

Parameters
systemsystem to use for reservation tracking
portreserved port to release

Release reservation of a TCP or UDP port for a peer (used during GNUNET_TESTING_peer_destroy).

Parameters
systemsystem to use for reservation tracking
portreserved port to release

Definition at line 675 of file testing.c.

677 {
678  uint32_t *port_buckets;
679  uint16_t bucket;
680  uint16_t pos;
681 
682  port_buckets = system->reserved_ports;
683  bucket = port / 32;
684  pos = port % 32;
685  LOG (GNUNET_ERROR_TYPE_DEBUG, "Releasing port %u\n", port);
686  if (0 == (port_buckets[bucket] & (1U << pos)))
687  {
688  GNUNET_break (0); /* Port was not reserved by us using reserve_port() */
689  return;
690  }
691  port_buckets[bucket] &= ~(1U << pos);
692 }
static uint16_t port
Port number.
Definition: gnunet-bcd.c:147
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.

References GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, LOG, port, and GNUNET_TESTING_System::reserved_ports.

Referenced by GNUNET_TESTING_peer_destroy().

Here is the caller graph for this function:

◆ GNUNET_TESTING_configuration_create()

int GNUNET_TESTING_configuration_create ( struct GNUNET_TESTING_System system,
struct GNUNET_CONFIGURATION_Handle cfg 
)

Create a new configuration using the given configuration as a template; ports and paths will be modified to select available ports on the local system.

The default configuration will be available in PATHS section under the option DEFAULTCONFIG after the call. SERVICE_HOME is also set in PATHS section to the temporary directory specific to this configuration. If we run out of "*port" numbers, return GNUNET_SYSERR.

This is primarily a helper function used internally by GNUNET_TESTING_peer_configure().

Parameters
systemsystem to use to coordinate resource usage
cfgtemplate configuration to update
Returns
GNUNET_OK on success, GNUNET_SYSERR on error - the configuration will be incomplete and should not be used there upon

The default configuration will be available in PATHS section under the option DEFAULTCONFIG after the call. GNUNET_HOME is also set in PATHS section to the temporary directory specific to this configuration. If we run out of "*port" numbers, return GNUNET_SYSERR.

This is primarily a helper function used internally by GNUNET_TESTING_peer_configure().

Parameters
systemsystem to use to coordinate resource usage
cfgtemplate configuration to update
Returns
GNUNET_OK on success, GNUNET_SYSERR on error - the configuration will be incomplete and should not be used there upon

Definition at line 1148 of file testing.c.

1150 {
1151  return GNUNET_TESTING_configuration_create_ (system, cfg, NULL, NULL);
1152 }
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static int GNUNET_TESTING_configuration_create_(struct GNUNET_TESTING_System *system, struct GNUNET_CONFIGURATION_Handle *cfg, uint16_t **ports, unsigned int *nports)
Create a new configuration using the given configuration as a template; ports and paths will be modif...
Definition: testing.c:1085

References cfg, GNUNET_TESTING_configuration_create_(), and UpdateContext::system.

Referenced by create_unique_cfgs(), GNUNET_TRANSPORT_TESTING_start_peer(), start_peer_run(), and tokenizer_cb().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_peer_configure()

struct GNUNET_TESTING_Peer* GNUNET_TESTING_peer_configure ( struct GNUNET_TESTING_System system,
struct GNUNET_CONFIGURATION_Handle cfg,
uint32_t  key_number,
struct GNUNET_PeerIdentity id,
char **  emsg 
)

Configure a GNUnet peer.

GNUnet must be installed on the local system and available in the PATH.

Parameters
systemsystem to use to coordinate resource usage
cfgconfiguration to use; will be UPDATED (to reflect needed changes in port numbers and paths)
key_numbernumber of the hostkey to use for the peer
ididentifier for the daemon, will be set, can be NULL
emsgset to freshly allocated error message (set to NULL on success), can be NULL
Returns
handle to the peer, NULL on error

Definition at line 1169 of file testing.c.

1174 {
1175  struct GNUNET_TESTING_Peer *peer;
1176  struct GNUNET_DISK_FileHandle *fd;
1177  char *hostkey_filename;
1178  char *config_filename;
1179  char *libexec_binary;
1180  char *emsg_;
1182  uint16_t *ports;
1183  struct SharedService *ss;
1184  struct SharedServiceInstance **ss_instances;
1185  unsigned int cnt;
1186  unsigned int nports;
1187 
1188  ports = NULL;
1189  nports = 0;
1190  ss_instances = NULL;
1191  if (NULL != emsg)
1192  *emsg = NULL;
1193  if (key_number >= system->total_hostkeys)
1194  {
1195  GNUNET_asprintf (
1196  &emsg_,
1197  _ (
1198  "You attempted to create a testbed with more than %u hosts. Please precompute more hostkeys first.\n"),
1199  (unsigned int) system->total_hostkeys);
1200  goto err_ret;
1201  }
1202  pk = NULL;
1203  if ((NULL != id) &&
1204  (NULL == (pk = GNUNET_TESTING_hostkey_get (system, key_number, id))))
1205  {
1206  GNUNET_asprintf (&emsg_,
1207  _ ("Failed to initialize hostkey for peer %u\n"),
1208  (unsigned int) key_number);
1209  goto err_ret;
1210  }
1211  if (NULL != pk)
1212  GNUNET_free (pk);
1213  if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (cfg, "PEER", "PRIVATE_KEY"))
1214  {
1215  GNUNET_asprintf (
1216  &emsg_,
1217  _ ("PRIVATE_KEY option in PEER section missing in configuration\n"));
1218  goto err_ret;
1219  }
1220  /* Remove sections for shared services */
1221  for (cnt = 0; cnt < system->n_shared_services; cnt++)
1222  {
1223  ss = &system->shared_services[cnt];
1225  }
1226  if (GNUNET_OK !=
1227  GNUNET_TESTING_configuration_create_ (system, cfg, &ports, &nports))
1228  {
1229  GNUNET_asprintf (&emsg_,
1230  _ ("Failed to create configuration for peer "
1231  "(not enough free ports?)\n"));
1232  goto err_ret;
1233  }
1236  "PEER",
1237  "PRIVATE_KEY",
1238  &hostkey_filename));
1239  fd = GNUNET_DISK_file_open (hostkey_filename,
1243  if (NULL == fd)
1244  {
1245  GNUNET_asprintf (&emsg_,
1246  _ ("Cannot open hostkey file `%s': %s\n"),
1247  hostkey_filename,
1248  strerror (errno));
1249  GNUNET_free (hostkey_filename);
1250  goto err_ret;
1251  }
1252  GNUNET_free (hostkey_filename);
1255  system->hostkeys_data
1256  + (key_number * GNUNET_TESTING_HOSTKEYFILESIZE),
1258  {
1259  GNUNET_asprintf (&emsg_,
1260  _ ("Failed to write hostkey file for peer %u: %s\n"),
1261  (unsigned int) key_number,
1262  strerror (errno));
1264  goto err_ret;
1265  }
1267  ss_instances = GNUNET_malloc (sizeof(struct SharedServiceInstance *)
1268  * system->n_shared_services);
1269  for (cnt = 0; cnt < system->n_shared_services; cnt++)
1270  {
1271  ss = &system->shared_services[cnt];
1272  ss_instances[cnt] = associate_shared_service (system, ss, cfg);
1273  if (NULL == ss_instances[cnt])
1274  {
1275  emsg_ = GNUNET_strdup ("FIXME");
1276  goto err_ret;
1277  }
1278  }
1281  "PATHS",
1282  "DEFAULTCONFIG",
1283  &config_filename));
1284  if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config_filename))
1285  {
1286  GNUNET_asprintf (&emsg_,
1287  _ (
1288  "Failed to write configuration file `%s' for peer %u: %s\n"),
1289  config_filename,
1290  (unsigned int) key_number,
1291  strerror (errno));
1292  GNUNET_free (config_filename);
1293  goto err_ret;
1294  }
1295  peer = GNUNET_new (struct GNUNET_TESTING_Peer);
1296  peer->ss_instances = ss_instances;
1297  peer->cfgfile = config_filename; /* Free in peer_destroy */
1299  libexec_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
1300  if (GNUNET_SYSERR ==
1302  "arm",
1303  "PREFIX",
1304  &peer->main_binary))
1305  {
1306  /* No prefix */
1307  GNUNET_asprintf (&peer->main_binary, "%s", libexec_binary);
1308  peer->args = GNUNET_strdup ("");
1309  }
1310  else
1311  {
1312  peer->args = GNUNET_strdup (libexec_binary);
1313  }
1314  peer->system = system;
1315  peer->key_number = key_number;
1316  GNUNET_free (libexec_binary);
1317  peer->ports = ports; /* Free in peer_destroy */
1318  peer->nports = nports;
1319  return peer;
1320 
1321  err_ret:
1322  GNUNET_free (ss_instances);
1323  GNUNET_free (ports);
1324  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_);
1325  if (NULL != emsg)
1326  *emsg = emsg_;
1327  else
1328  GNUNET_free (emsg_);
1329  return NULL;
1330 }
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
void GNUNET_CONFIGURATION_remove_section(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
Remove the given section and all options in it.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_have_value(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Test if we have a value for a particular option.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_write(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Write configuration file.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_dup(const struct GNUNET_CONFIGURATION_Handle *cfg)
Duplicate an existing configuration object.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1234
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:685
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1305
@ GNUNET_DISK_OPEN_WRITE
Open the file for writing.
@ GNUNET_DISK_OPEN_CREATE
Create file if it doesn't exist.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
#define GNUNET_log(kind,...)
@ GNUNET_SYSERR
#define GNUNET_malloc(size)
Wrapper around malloc.
char * GNUNET_OS_get_libexec_binary_path(const char *progname)
Given the name of a gnunet-helper, gnunet-service or gnunet-daemon binary, try to prefix it with the ...
struct GNUNET_CRYPTO_EddsaPrivateKey * GNUNET_TESTING_hostkey_get(const struct GNUNET_TESTING_System *system, uint32_t key_number, struct GNUNET_PeerIdentity *id)
Testing includes a number of pre-created hostkeys for faster peer startup.
Definition: testing.c:713
Handle used to access files (and pipes).
int fd
File handle on Unix-like systems.
Handle for a GNUnet peer controlled by testing.
Definition: testing.c:176
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
static struct SharedServiceInstance * associate_shared_service(struct GNUNET_TESTING_System *system, struct SharedService *ss, struct GNUNET_CONFIGURATION_Handle *cfg)
Definition: testing.c:992

References _, associate_shared_service(), cfg, GNUNET_DISK_FileHandle::fd, GNUNET_asprintf(), GNUNET_assert, GNUNET_CONFIGURATION_dup(), GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_have_value(), GNUNET_CONFIGURATION_remove_section(), GNUNET_CONFIGURATION_write(), GNUNET_DISK_file_close(), GNUNET_DISK_file_open(), GNUNET_DISK_file_write(), GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_OPEN_WRITE, GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_PERM_USER_WRITE, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_malloc, GNUNET_new, GNUNET_NO, GNUNET_OK, GNUNET_OS_get_libexec_binary_path(), GNUNET_strdup, GNUNET_SYSERR, GNUNET_TESTING_configuration_create_(), GNUNET_TESTING_hostkey_get(), GNUNET_TESTING_HOSTKEYFILESIZE, GNUNET_TESTING_System::hostkeys_data, GNUNET_TESTING_System::n_shared_services, peer, pk, GNUNET_TESTING_System::shared_services, SharedService::sname, SharedServiceInstance::ss, and GNUNET_TESTING_System::total_hostkeys.

Referenced by GNUNET_TESTING_service_run(), GNUNET_TRANSPORT_TESTING_start_peer(), handle_peer_create(), start_peer_run(), and update_peer_config().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_peer_get_identity()

void GNUNET_TESTING_peer_get_identity ( struct GNUNET_TESTING_Peer peer,
struct GNUNET_PeerIdentity id 
)

Obtain the peer identity from a peer handle.

Parameters
peerpeer handle for which we want the peer's identity
ididentifier for the daemon, will be set

Definition at line 1340 of file testing.c.

1342 {
1343  if (NULL != peer->id)
1344  {
1345  GNUNET_memcpy (id, peer->id, sizeof(struct GNUNET_PeerIdentity));
1346  return;
1347  }
1348  peer->id = GNUNET_new (struct GNUNET_PeerIdentity);
1349  GNUNET_free_nz (
1350  GNUNET_TESTING_hostkey_get (peer->system, peer->key_number, peer->id));
1351  GNUNET_memcpy (id, peer->id, sizeof(struct GNUNET_PeerIdentity));
1352 }
#define GNUNET_free_nz(ptr)
Wrapper around free.
The identity of the host (wraps the signing key of the peer).

References GNUNET_free_nz, GNUNET_memcpy, GNUNET_new, GNUNET_TESTING_hostkey_get(), and peer.

Referenced by GNUNET_TRANSPORT_TESTING_start_peer(), handle_overlay_connect(), handle_peer_get_config(), handle_remote_overlay_connect(), and start_peer_run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_peer_start()

int GNUNET_TESTING_peer_start ( struct GNUNET_TESTING_Peer peer)

Start the peer.

Parameters
peerpeer to start
Returns
GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer already running)

Definition at line 1362 of file testing.c.

1363 {
1364  struct SharedServiceInstance *i;
1365  unsigned int cnt;
1366 
1367  if (NULL != peer->main_process)
1368  {
1369  GNUNET_break (0);
1370  return GNUNET_SYSERR;
1371  }
1372  GNUNET_assert (NULL != peer->cfgfile);
1373  for (cnt = 0; cnt < peer->system->n_shared_services; cnt++)
1374  {
1375  i = peer->ss_instances[cnt];
1376  if ((0 == i->n_refs) &&
1378  return GNUNET_SYSERR;
1379  i->n_refs++;
1380  }
1381  peer->main_binary =
1382  GNUNET_CONFIGURATION_expand_dollar (peer->cfg, peer->main_binary);
1383  peer->main_process =
1385  NULL,
1386  peer->main_binary,
1387  peer->args,
1388  "-c",
1389  peer->cfgfile,
1390  NULL);
1391  if (NULL == peer->main_process)
1392  {
1394  _ ("Failed to start `%s': %s\n"),
1395  peer->main_binary,
1396  strerror (errno));
1397  return GNUNET_SYSERR;
1398  }
1399  return GNUNET_OK;
1400 }
char * GNUNET_CONFIGURATION_expand_dollar(const struct GNUNET_CONFIGURATION_Handle *cfg, char *orig)
Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR" where either in the "PATHS" section or...
struct GNUNET_OS_Process * GNUNET_OS_start_process_s(enum GNUNET_OS_InheritStdioFlags std_inheritance, const int *lsocks, const char *filename,...)
Start a process.
Definition: os_priority.c:702
@ GNUNET_OS_INHERIT_STD_OUT_AND_ERR
When these flags are set, the child process will inherit stdout and stderr of the parent.
unsigned int n_refs
Definition: testing.c:79
static int start_shared_service_instance(struct SharedServiceInstance *i)
Definition: testing.c:490

References _, GNUNET_assert, GNUNET_break, GNUNET_CONFIGURATION_expand_dollar(), GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_OK, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, GNUNET_OS_start_process_s(), GNUNET_SYSERR, SharedServiceInstance::n_refs, peer, and start_shared_service_instance().

Referenced by GNUNET_TESTING_service_run(), GNUNET_TRANSPORT_TESTING_restart_peer(), GNUNET_TRANSPORT_TESTING_start_peer(), start_peer(), start_peer_run(), and stdin_cb().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_peer_stop()

int GNUNET_TESTING_peer_stop ( struct GNUNET_TESTING_Peer peer)

Stop the peer.

This call is blocking as it kills the peer's main ARM process by sending a SIGTERM and waits on it. For asynchronous shutdown of peer, see GNUNET_TESTING_peer_stop_async().

Parameters
peerpeer to stop
Returns
GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer not running)
Parameters
peerpeer to stop
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 1466 of file testing.c.

1467 {
1469  return GNUNET_SYSERR;
1471  return GNUNET_SYSERR;
1472  return GNUNET_OK;
1473 }
int GNUNET_TESTING_peer_kill(struct GNUNET_TESTING_Peer *peer)
Sends SIGTERM to the peer's main process.
Definition: testing.c:1411
int GNUNET_TESTING_peer_wait(struct GNUNET_TESTING_Peer *peer)
Waits for a peer to terminate.
Definition: testing.c:1443

References GNUNET_OK, GNUNET_SYSERR, GNUNET_TESTING_peer_kill(), GNUNET_TESTING_peer_wait(), and peer.

Referenced by GNUNET_TESTING_peer_destroy(), GNUNET_TESTING_service_run(), GNUNET_TRANSPORT_TESTING_restart_peer(), GNUNET_TRANSPORT_TESTING_stop_peer(), GST_destroy_peer(), stdin_cb(), and stop_peer_run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_peer_destroy()

void GNUNET_TESTING_peer_destroy ( struct GNUNET_TESTING_Peer peer)

Destroy the peer.

Releases resources locked during peer configuration. If the peer is still running, it will be stopped AND a warning will be printed (users of the API should stop the peer explicitly first).

Parameters
peerpeer to destroy

Definition at line 1547 of file testing.c.

1548 {
1549  unsigned int cnt;
1550 
1551  if (NULL != peer->main_process)
1553  if (NULL != peer->ah)
1555  GNUNET_free (peer->cfgfile);
1556  if (NULL != peer->cfg)
1558  GNUNET_free (peer->main_binary);
1559  GNUNET_free (peer->args);
1560  GNUNET_free (peer->id);
1561  GNUNET_free (peer->ss_instances);
1562  if (NULL != peer->ports)
1563  {
1564  for (cnt = 0; cnt < peer->nports; cnt++)
1565  GNUNET_TESTING_release_port (peer->system, peer->ports[cnt]);
1566  GNUNET_free (peer->ports);
1567  }
1568  GNUNET_free (peer);
1569 }
void GNUNET_ARM_disconnect(struct GNUNET_ARM_Handle *h)
Disconnect from the ARM service (if connected) and destroy the context.
Definition: arm_api.c:602
int GNUNET_TESTING_peer_stop(struct GNUNET_TESTING_Peer *peer)
Stop the peer.
Definition: testing.c:1466
void GNUNET_TESTING_release_port(struct GNUNET_TESTING_System *system, uint16_t port)
Release reservation of a TCP or UDP port for a peer (used during GNUNET_TESTING_peer_destroy()).
Definition: testing.c:675

References GNUNET_ARM_disconnect(), GNUNET_CONFIGURATION_destroy(), GNUNET_free, GNUNET_TESTING_peer_stop(), GNUNET_TESTING_release_port(), and peer.

Referenced by GNUNET_TESTING_service_run(), GNUNET_TRANSPORT_TESTING_stop_peer(), GST_destroy_peer(), GST_destroy_peers(), stop_peer_run(), and update_peer_config().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_peer_kill()

int GNUNET_TESTING_peer_kill ( struct GNUNET_TESTING_Peer peer)

Sends SIGTERM to the peer's main process.

Parameters
peerthe handle to the peer
Returns
GNUNET_OK if successful; GNUNET_SYSERR if the main process is NULL or upon any error while sending SIGTERM

Definition at line 1411 of file testing.c.

1412 {
1413  struct SharedServiceInstance *i;
1414  unsigned int cnt;
1415 
1416  if (NULL == peer->main_process)
1417  {
1418  GNUNET_break (0);
1419  return GNUNET_SYSERR;
1420  }
1421  if (0 != GNUNET_OS_process_kill (peer->main_process, GNUNET_TERM_SIG))
1422  return GNUNET_SYSERR;
1423  for (cnt = 0; cnt < peer->system->n_shared_services; cnt++)
1424  {
1425  i = peer->ss_instances[cnt];
1426  GNUNET_assert (0 != i->n_refs);
1427  i->n_refs--;
1428  if (0 == i->n_refs)
1430  }
1431  return GNUNET_OK;
1432 }
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
Definition: os_priority.c:210
#define GNUNET_TERM_SIG
The termination signal.
Definition: platform.h:233

References GNUNET_assert, GNUNET_break, GNUNET_OK, GNUNET_OS_process_kill(), GNUNET_SYSERR, GNUNET_TERM_SIG, SharedServiceInstance::n_refs, peer, and stop_shared_service_instance().

Referenced by disconn_status(), GNUNET_TESTING_peer_stop(), GST_destroy_peers(), and stop_peer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_peer_wait()

int GNUNET_TESTING_peer_wait ( struct GNUNET_TESTING_Peer peer)

Waits for a peer to terminate.

The peer's main process will also be destroyed.

Parameters
peerthe handle to the peer
Returns
GNUNET_OK if successful; GNUNET_SYSERR if the main process is NULL or upon any error while waiting

Definition at line 1443 of file testing.c.

1444 {
1445  int ret;
1446 
1447  if (NULL == peer->main_process)
1448  {
1449  GNUNET_break (0);
1450  return GNUNET_SYSERR;
1451  }
1452  ret = GNUNET_OS_process_wait (peer->main_process);
1453  GNUNET_OS_process_destroy (peer->main_process);
1454  peer->main_process = NULL;
1455  return ret;
1456 }
int GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
Wait for a process to terminate.
Definition: os_priority.c:942
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:273

References GNUNET_break, GNUNET_OS_process_destroy(), GNUNET_OS_process_wait(), GNUNET_SYSERR, peer, and ret.

Referenced by disconn_status(), GNUNET_TESTING_peer_stop(), GST_destroy_peers(), and handle_peer_stop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_peer_stop_async()

int GNUNET_TESTING_peer_stop_async ( struct GNUNET_TESTING_Peer peer,
GNUNET_TESTING_PeerStopCallback  cb,
void *  cb_cls 
)

Stop a peer asynchronously using ARM API.

Peer's shutdown is signaled through the GNUNET_TESTING_PeerStopCallback().

Parameters
peerthe peer to stop
cbthe callback to signal peer shutdown
cb_clsclosure for the cb
Returns
GNUNET_OK upon successfully giving the request to the ARM API (this does not mean that the peer is successfully stopped); GNUNET_SYSERR upon any error.

Definition at line 1506 of file testing.c.

1509 {
1510  if (NULL == peer->main_process)
1511  return GNUNET_SYSERR;
1513  if (NULL == peer->ah)
1514  return GNUNET_SYSERR;
1515  peer->cb = cb;
1516  peer->cb_cls = cb_cls;
1517  return GNUNET_OK;
1518 }
struct GNUNET_ARM_Handle * GNUNET_ARM_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_ARM_ConnectionStatusCallback conn_status, void *conn_status_cls)
Set up a context for communicating with ARM, then start connecting to the ARM service using that cont...
Definition: arm_api.c:577
static void disconn_status(void *cls, int connected)
Function called whenever we connect to or disconnect from ARM.
Definition: testing.c:1484

References GNUNET_TESTING_Peer::cb, GNUNET_TESTING_Peer::cb_cls, disconn_status(), GNUNET_ARM_connect(), GNUNET_OK, GNUNET_SYSERR, and peer.

Referenced by handle_peer_reconfigure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_peer_stop_async_cancel()

void GNUNET_TESTING_peer_stop_async_cancel ( struct GNUNET_TESTING_Peer peer)

Cancel a previous asynchronous peer stop request.

GNUNET_TESTING_peer_stop_async() should have been called before on the given peer. It is an error to call this function if the peer stop callback was already called

Parameters
peerthe peer on which GNUNET_TESTING_peer_stop_async() was called before.

Definition at line 1531 of file testing.c.

1532 {
1533  GNUNET_assert (NULL != peer->ah);
1535  peer->ah = NULL;
1536 }

References GNUNET_ARM_disconnect(), GNUNET_assert, and peer.

Referenced by cleanup_prc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_peer_run()

int GNUNET_TESTING_peer_run ( const char *  testdir,
const char *  cfgfilename,
GNUNET_TESTING_TestMain  tm,
void *  tm_cls 
)

Start a single peer and run a test using the testing library.

Starts a peer using the given configuration and then invokes the given callback. This function ALSO initializes the scheduler loop and should thus be called directly from "main". The testcase should self-terminate by invoking GNUNET_SCHEDULER_shutdown().

Parameters
testdironly the directory name without any path. This is used for all service homes; the directory will be created in a temporary location depending on the underlying OS
cfgfilenamename of the configuration file to use; use NULL to only run with defaults
tmmain function of the testcase
tm_clsclosure for tm
Returns
0 on success, 1 on error

Definition at line 1573 of file testing.c.

1577 {
1578  return GNUNET_TESTING_service_run (testdir, "arm", cfgfilename, tm, tm_cls);
1579 }
int GNUNET_TESTING_service_run(const char *testdir, const char *service_name, const char *cfgfilename, GNUNET_TESTING_TestMain tm, void *tm_cls)
Start a single service (no ARM, except of course if the given service name is 'arm') and run a test u...
Definition: testing.c:1624

References GNUNET_TESTING_service_run().

Referenced by pre_run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_service_run()

int GNUNET_TESTING_service_run ( const char *  testdir,
const char *  service_name,
const char *  cfgfilename,
GNUNET_TESTING_TestMain  tm,
void *  tm_cls 
)

Start a single service (no ARM, except of course if the given service name is 'arm') and run a test using the testing library.

Starts a service using the given configuration and then invokes the given callback. This function ALSO initializes the scheduler loop and should thus be called directly from "main". The testcase should self-terminate by invoking GNUNET_SCHEDULER_shutdown().

This function is useful if the testcase is for a single service and if that service doesn't itself depend on other services.

Parameters
testdironly the directory name without any path. This is used for all service homes; the directory will be created in a temporary location depending on the underlying OS
service_namename of the service to run
cfgfilenamename of the configuration file to use; use NULL to only run with defaults
tmmain function of the testcase
tm_clsclosure for tm
Returns
0 on success, 1 on error

Definition at line 1624 of file testing.c.

1629 {
1630  struct ServiceContext sc;
1631  struct GNUNET_TESTING_System *system;
1632  struct GNUNET_TESTING_Peer *peer;
1634  char *binary;
1635  char *libexec_binary;
1636 
1637  GNUNET_log_setup (testdir, "WARNING", NULL);
1638  system = GNUNET_TESTING_system_create (testdir, "127.0.0.1", NULL, NULL);
1639  if (NULL == system)
1640  return 1;
1642  if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfgfilename))
1643  {
1645  _ ("Failed to load configuration from %s\n"),
1646  cfgfilename);
1649  return 1;
1650  }
1651  peer = GNUNET_TESTING_peer_configure (system, cfg, 0, NULL, NULL);
1652  if (NULL == peer)
1653  {
1655  hostkeys_unload (system);
1657  return 1;
1658  }
1659  GNUNET_free (peer->main_binary);
1660  GNUNET_free (peer->args);
1661  GNUNET_asprintf (&binary, "gnunet-service-%s", service_name);
1662  libexec_binary = GNUNET_OS_get_libexec_binary_path (binary);
1663  if (GNUNET_SYSERR ==
1665  service_name,
1666  "PREFIX",
1667  &peer->main_binary))
1668  {
1669  /* No prefix */
1670  GNUNET_asprintf (&peer->main_binary, "%s", libexec_binary);
1671  peer->args = GNUNET_strdup ("");
1672  }
1673  else
1674  peer->args = GNUNET_strdup (libexec_binary);
1675 
1676  GNUNET_free (libexec_binary);
1677  GNUNET_free (binary);
1679  {
1683  return 1;
1684  }
1685  sc.cfg = cfg;
1686  sc.tm = tm;
1687  sc.tm_cls = tm_cls;
1688  sc.peer = peer;
1689  GNUNET_SCHEDULER_run (&service_run_main, &sc); /* Scheduler loop */
1690  if ((NULL != peer->main_process) &&
1692  {
1696  return 1;
1697  }
1701  return 0;
1702 }
static struct GNUNET_FS_SearchContext * sc
Definition: gnunet-search.c:87
static char * service_name
Option -s: service name (hash to get service descriptor)
Definition: gnunet-vpn.c:50
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_load(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Load configuration.
int GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
void GNUNET_SCHEDULER_run(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Initialize and run scheduler.
Definition: scheduler.c:692
struct GNUNET_TESTING_System * GNUNET_TESTING_system_create(const char *testdir, const char *trusted_ip, const char *hostname, const struct GNUNET_TESTING_SharedService *shared_services)
Create a system handle.
Definition: testing.c:458
struct GNUNET_TESTING_Peer * GNUNET_TESTING_peer_configure(struct GNUNET_TESTING_System *system, struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t key_number, struct GNUNET_PeerIdentity *id, char **emsg)
Configure a GNUnet peer.
Definition: testing.c:1169
int GNUNET_TESTING_peer_start(struct GNUNET_TESTING_Peer *peer)
Start the peer.
Definition: testing.c:1362
void GNUNET_TESTING_peer_destroy(struct GNUNET_TESTING_Peer *peer)
Destroy the peer.
Definition: testing.c:1547
Structure for holding service data.
Definition: testing.c:1586
static void service_run_main(void *cls)
Callback to be called when SCHEDULER has been started.
Definition: testing.c:1615

References _, cfg, GNUNET_asprintf(), GNUNET_CONFIGURATION_create(), GNUNET_CONFIGURATION_destroy(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_load(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log_setup(), GNUNET_OK, GNUNET_OS_get_libexec_binary_path(), GNUNET_SCHEDULER_run(), GNUNET_strdup, GNUNET_SYSERR, GNUNET_TESTING_peer_configure(), GNUNET_TESTING_peer_destroy(), GNUNET_TESTING_peer_start(), GNUNET_TESTING_peer_stop(), GNUNET_TESTING_system_create(), GNUNET_TESTING_system_destroy(), GNUNET_YES, hostkeys_unload(), LOG, peer, sc, service_name, and service_run_main().

Referenced by GNUNET_TESTING_peer_run(), and run_no_scheduler().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_TESTING_get_testname_from_underscore()

char* GNUNET_TESTING_get_testname_from_underscore ( const char *  argv0)

Sometimes we use the binary name to determine which specific test to run.

In those cases, the string after the last "_" in 'argv[0]' specifies a string that determines the configuration file or plugin to use.

This function returns the respective substring, taking care of issues such as binaries ending in '.exe' on W32.

Parameters
argv0the name of the binary
Returns
string between the last '_' and the '.exe' (or the end of the string), NULL if argv0 has no '_'

Definition at line 1719 of file testing.c.

1720 {
1721  size_t slen = strlen (argv0) + 1;
1722  char sbuf[slen];
1723  char *ret;
1724  char *dot;
1725 
1726  GNUNET_memcpy (sbuf, argv0, slen);
1727  ret = strrchr (sbuf, '_');
1728  if (NULL == ret)
1729  return NULL;
1730  ret++; /* skip underscore */
1731  dot = strchr (ret, '.');
1732  if (NULL != dot)
1733  *dot = '\0';
1734  return GNUNET_strdup (ret);
1735 }

References GNUNET_memcpy, GNUNET_strdup, and ret.