GNUnet  0.11.x
Data Structures | Macros | Typedefs | Functions
Testing library

Library for writing testcases for GNUnet. More...

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 59 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 66 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 346 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 390 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 449 of file testing.c.

454 {
456  trusted_ip,
457  hostname,
458  shared_services,
459  LOW_PORT,
460  HIGH_PORT);
461 }
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:364
#define HIGH_PORT
Highest port used for GNUnet testing.
Definition: testing.c:55
#define LOW_PORT
Lowest port used for GNUnet testing.
Definition: testing.c:48

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 364 of file testing.c.

371 {
372  struct GNUNET_TESTING_System *system;
373  struct GNUNET_TESTING_SharedService tss;
374  struct SharedService *ss;
375  unsigned int cnt;
376 
377  GNUNET_assert (NULL != testdir);
378  system = GNUNET_new (struct GNUNET_TESTING_System);
379  if (NULL == (system->tmppath = getenv (GNUNET_TESTING_PREFIX)))
380  system->tmppath = GNUNET_DISK_mkdtemp (testdir);
381  else
382  system->tmppath = GNUNET_strdup (system->tmppath);
383  system->lowport = lowport;
384  system->highport = highport;
385  if (NULL == system->tmppath)
386  {
387  GNUNET_free (system);
388  return NULL;
389  }
390  if (NULL != trusted_ip)
391  system->trusted_ip = GNUNET_strdup (trusted_ip);
392  if (NULL != hostname)
393  system->hostname = GNUNET_strdup (hostname);
394  if (GNUNET_OK != hostkeys_load (system))
395  {
397  return NULL;
398  }
399  if (NULL == shared_services)
400  return system;
401  for (cnt = 0; NULL != shared_services[cnt].service; cnt++)
402  {
403  tss = shared_services[cnt];
404  ss = GNUNET_new (struct SharedService);
405  ss->sname = GNUNET_strdup (tss.service);
408  ss->sname,
410  ss->cfg);
412  "TESTING",
414  ss->cfg);
416  "PATHS",
418  ss->cfg);
419  ss->share = tss.share;
421  system->n_shared_services,
422  ss);
423  }
424  return system;
425 }
char * getenv()
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_YES
Definition: gnunet_common.h:97
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:340
#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:529
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:94
uint16_t highport
Highest port we are allowed to use.
Definition: testing.c:159
char * trusted_ip
The trusted ip.
Definition: testing.c:105
char * tmppath
Prefix (e.g.
Definition: testing.c:99
unsigned int n_shared_services
Definition: testing.c:124
char * hostname
our hostname
Definition: testing.c:110
struct SharedService ** shared_services
Definition: testing.c:122
uint16_t lowport
Lowest port we are allowed to use.
Definition: testing.c:154
The structure for identifying a shared service.
unsigned int share
Definition: testing.c:83
struct GNUNET_CONFIGURATION_Handle * cfg
Definition: testing.c:79
char * sname
Definition: testing.c:75
static void cfg_copy_iterator(void *cls, const char *section, const char *option, const char *value)
Function to iterate over options.
Definition: testing.c:331
static int hostkeys_load(struct GNUNET_TESTING_System *system)
Testing includes a number of pre-created hostkeys for faster peer startup.
Definition: testing.c:248

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 529 of file testing.c.

531 {
532  struct SharedService *ss;
533  struct SharedServiceInstance *i;
534  unsigned int ss_cnt;
535  unsigned int i_cnt;
536 
537  if (NULL != system->hostkeys_data)
538  hostkeys_unload (system);
539  for (ss_cnt = 0; ss_cnt < system->n_shared_services; ss_cnt++)
540  {
541  ss = system->shared_services[ss_cnt];
542  for (i_cnt = 0; i_cnt < ss->n_instances; i_cnt++)
543  {
544  i = ss->instances[i_cnt];
545  if (NULL != i->proc)
548  }
551  GNUNET_free (ss->sname);
552  GNUNET_free (ss);
553  }
554  GNUNET_free (system->shared_services);
555  if (GNUNET_YES == remove_paths)
557  GNUNET_free (system->tmppath);
558  GNUNET_free (system->trusted_ip);
559  GNUNET_free (system->hostname);
560  GNUNET_free (system);
561 }
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:1085
char * hostkeys_data
Hostkeys data, contains "GNUNET_TESTING_HOSTKEYFILESIZE * total_hostkeys" bytes.
Definition: testing.c:115
struct SharedService * ss
Definition: testing.c:60
struct GNUNET_OS_Process * proc
Definition: testing.c:64
unsigned int n_instances
Definition: testing.c:85
struct SharedServiceInstance ** instances
Definition: testing.c:77
static void hostkeys_unload(struct GNUNET_TESTING_System *system)
Function to remove the loaded hostkeys.
Definition: testing.c:311
static void cleanup_shared_service_instance(struct SharedServiceInstance *i)
Definition: testing.c:465
static void stop_shared_service_instance(struct SharedServiceInstance *i)
Definition: testing.c:508

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 705 of file testing.c.

708 {
709  struct GNUNET_CRYPTO_EddsaPrivateKey *private_key;
710 
711  if ((NULL == id) || (NULL == system->hostkeys_data))
712  return NULL;
713  if (key_number >= system->total_hostkeys)
714  {
716  _ ("Key number %u does not exist\n"),
717  key_number);
718  return NULL;
719  }
720  private_key = GNUNET_new (struct GNUNET_CRYPTO_EddsaPrivateKey);
721  GNUNET_memcpy (private_key,
722  system->hostkeys_data
723  + (key_number * GNUNET_TESTING_HOSTKEYFILESIZE),
725  GNUNET_CRYPTO_eddsa_key_get_public (private_key, &id->public_key);
726  return private_key;
727 }
static struct GNUNET_IDENTITY_Handle * id
Handle to identity service.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
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
@ 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:149
#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(), 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 571 of file testing.c.

572 {
573  struct GNUNET_NETWORK_Handle *socket;
574  struct addrinfo hint;
575  struct addrinfo *ret;
576  struct addrinfo *ai;
577  uint32_t *port_buckets;
578  char *open_port_str;
579  int bind_status;
580  uint32_t xor_image;
581  uint16_t index;
582  uint16_t open_port;
583  uint16_t pos;
584 
585  /*
586  FIXME: Instead of using getaddrinfo we should try to determine the port
587  status by the following heurestics.
588 
589  On systems which support both IPv4 and IPv6, only ports open on both
590  address families are considered open.
591  On system with either IPv4 or IPv6. A port is considered open if it's
592  open in the respective address family
593  */hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
594  hint.ai_socktype = 0;
595  hint.ai_protocol = 0;
596  hint.ai_addrlen = 0;
597  hint.ai_addr = NULL;
598  hint.ai_canonname = NULL;
599  hint.ai_next = NULL;
600  hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
601  port_buckets = system->reserved_ports;
602  for (index = (system->lowport / 32) + 1; index < (system->highport / 32);
603  index++)
604  {
605  xor_image = (UINT32_MAX ^ port_buckets[index]);
606  if (0 == xor_image) /* Ports in the bucket are full */
607  continue;
608  pos = system->lowport % 32;
609  while (pos < 32)
610  {
611  if (0 == ((xor_image >> pos) & 1U))
612  {
613  pos++;
614  continue;
615  }
616  open_port = (index * 32) + pos;
617  if (open_port >= system->highport)
618  return 0;
619  GNUNET_asprintf (&open_port_str, "%u", (unsigned int) open_port);
620  ret = NULL;
621  GNUNET_assert (0 == getaddrinfo (NULL, open_port_str, &hint, &ret));
622  GNUNET_free (open_port_str);
623  bind_status = GNUNET_NO;
624  for (ai = ret; NULL != ai; ai = ai->ai_next)
625  {
626  socket = GNUNET_NETWORK_socket_create (ai->ai_family, SOCK_STREAM, 0);
627  if (NULL == socket)
628  continue;
629  bind_status =
630  GNUNET_NETWORK_socket_bind (socket, ai->ai_addr, ai->ai_addrlen);
632  if (GNUNET_OK != bind_status)
633  break;
634  socket = GNUNET_NETWORK_socket_create (ai->ai_family, SOCK_DGRAM, 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  }
643  port_buckets[index] |= (1U << pos); /* Set the port bit */
644  freeaddrinfo (ret);
645  if (GNUNET_OK == bind_status)
646  {
648  "Found a free port %u\n",
649  (unsigned int) open_port);
650  return open_port;
651  }
652  pos++;
653  }
654  }
655  return 0;
656 }
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
Definition: gnunet_common.h:94
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:907
int 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:485
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:560
#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:134

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 667 of file testing.c.

669 {
670  uint32_t *port_buckets;
671  uint16_t bucket;
672  uint16_t pos;
673 
674  port_buckets = system->reserved_ports;
675  bucket = port / 32;
676  pos = port % 32;
677  LOG (GNUNET_ERROR_TYPE_DEBUG, "Releasing port %u\n", port);
678  if (0 == (port_buckets[bucket] & (1U << pos)))
679  {
680  GNUNET_break (0); /* Port was not reserved by us using reserve_port() */
681  return;
682  }
683  port_buckets[bucket] &= ~(1U << pos);
684 }
static uint16_t port
Port number.
Definition: gnunet-bcd.c:83
#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 1140 of file testing.c.

1142 {
1143  return GNUNET_TESTING_configuration_create_ (system, cfg, NULL, NULL);
1144 }
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:1077

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 1161 of file testing.c.

1166 {
1167  struct GNUNET_TESTING_Peer *peer;
1168  struct GNUNET_DISK_FileHandle *fd;
1169  char *hostkey_filename;
1170  char *config_filename;
1171  char *libexec_binary;
1172  char *emsg_;
1174  uint16_t *ports;
1175  struct SharedService *ss;
1176  struct SharedServiceInstance **ss_instances;
1177  unsigned int cnt;
1178  unsigned int nports;
1179 
1180  ports = NULL;
1181  nports = 0;
1182  ss_instances = NULL;
1183  if (NULL != emsg)
1184  *emsg = NULL;
1185  if (key_number >= system->total_hostkeys)
1186  {
1187  GNUNET_asprintf (
1188  &emsg_,
1189  _ (
1190  "You attempted to create a testbed with more than %u hosts. Please precompute more hostkeys first.\n"),
1191  (unsigned int) system->total_hostkeys);
1192  goto err_ret;
1193  }
1194  pk = NULL;
1195  if ((NULL != id) &&
1196  (NULL == (pk = GNUNET_TESTING_hostkey_get (system, key_number, id))))
1197  {
1198  GNUNET_asprintf (&emsg_,
1199  _ ("Failed to initialize hostkey for peer %u\n"),
1200  (unsigned int) key_number);
1201  goto err_ret;
1202  }
1203  if (NULL != pk)
1204  GNUNET_free (pk);
1205  if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (cfg, "PEER", "PRIVATE_KEY"))
1206  {
1207  GNUNET_asprintf (
1208  &emsg_,
1209  _ ("PRIVATE_KEY option in PEER section missing in configuration\n"));
1210  goto err_ret;
1211  }
1212  /* Remove sections for shared services */
1213  for (cnt = 0; cnt < system->n_shared_services; cnt++)
1214  {
1215  ss = system->shared_services[cnt];
1217  }
1218  if (GNUNET_OK !=
1219  GNUNET_TESTING_configuration_create_ (system, cfg, &ports, &nports))
1220  {
1221  GNUNET_asprintf (&emsg_,
1222  _ ("Failed to create configuration for peer "
1223  "(not enough free ports?)\n"));
1224  goto err_ret;
1225  }
1228  "PEER",
1229  "PRIVATE_KEY",
1230  &hostkey_filename));
1231  fd = GNUNET_DISK_file_open (hostkey_filename,
1235  if (NULL == fd)
1236  {
1237  GNUNET_asprintf (&emsg_,
1238  _ ("Cannot open hostkey file `%s': %s\n"),
1239  hostkey_filename,
1240  strerror (errno));
1241  GNUNET_free (hostkey_filename);
1242  goto err_ret;
1243  }
1244  GNUNET_free (hostkey_filename);
1247  system->hostkeys_data
1248  + (key_number * GNUNET_TESTING_HOSTKEYFILESIZE),
1250  {
1251  GNUNET_asprintf (&emsg_,
1252  _ ("Failed to write hostkey file for peer %u: %s\n"),
1253  (unsigned int) key_number,
1254  strerror (errno));
1256  goto err_ret;
1257  }
1259  ss_instances = GNUNET_malloc (sizeof(struct SharedServiceInstance *)
1260  * system->n_shared_services);
1261  for (cnt = 0; cnt < system->n_shared_services; cnt++)
1262  {
1263  ss = system->shared_services[cnt];
1264  ss_instances[cnt] = associate_shared_service (system, ss, cfg);
1265  if (NULL == ss_instances[cnt])
1266  {
1267  emsg_ = GNUNET_strdup ("FIXME");
1268  goto err_ret;
1269  }
1270  }
1273  "PATHS",
1274  "DEFAULTCONFIG",
1275  &config_filename));
1276  if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, config_filename))
1277  {
1278  GNUNET_asprintf (&emsg_,
1279  _ (
1280  "Failed to write configuration file `%s' for peer %u: %s\n"),
1281  config_filename,
1282  (unsigned int) key_number,
1283  strerror (errno));
1284  GNUNET_free (config_filename);
1285  goto err_ret;
1286  }
1287  peer = GNUNET_new (struct GNUNET_TESTING_Peer);
1288  peer->ss_instances = ss_instances;
1289  peer->cfgfile = config_filename; /* Free in peer_destroy */
1291  libexec_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
1292  if (GNUNET_SYSERR ==
1294  "arm",
1295  "PREFIX",
1296  &peer->main_binary))
1297  {
1298  /* No prefix */
1299  GNUNET_asprintf (&peer->main_binary, "%s", libexec_binary);
1300  peer->args = GNUNET_strdup ("");
1301  }
1302  else
1303  {
1304  peer->args = GNUNET_strdup (libexec_binary);
1305  }
1306  peer->system = system;
1307  peer->key_number = key_number;
1308  GNUNET_free (libexec_binary);
1309  peer->ports = ports; /* Free in peer_destroy */
1310  peer->nports = nports;
1311  return peer;
1312 
1313  err_ret:
1314  GNUNET_free (ss_instances);
1315  GNUNET_free (ports);
1316  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_);
1317  if (NULL != emsg)
1318  *emsg = emsg_;
1319  else
1320  GNUNET_free (emsg_);
1321  return NULL;
1322 }
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
#define GNUNET_log(kind,...)
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
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:1235
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:686
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1306
@ 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_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:705
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:167
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:984

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 1332 of file testing.c.

1334 {
1335  if (NULL != peer->id)
1336  {
1337  GNUNET_memcpy (id, peer->id, sizeof(struct GNUNET_PeerIdentity));
1338  return;
1339  }
1340  peer->id = GNUNET_new (struct GNUNET_PeerIdentity);
1341  GNUNET_free_nz (
1342  GNUNET_TESTING_hostkey_get (peer->system, peer->key_number, peer->id));
1343  GNUNET_memcpy (id, peer->id, sizeof(struct GNUNET_PeerIdentity));
1344 }
#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 1354 of file testing.c.

1355 {
1356  struct SharedServiceInstance *i;
1357  unsigned int cnt;
1358 
1359  if (NULL != peer->main_process)
1360  {
1361  GNUNET_break (0);
1362  return GNUNET_SYSERR;
1363  }
1364  GNUNET_assert (NULL != peer->cfgfile);
1365  for (cnt = 0; cnt < peer->system->n_shared_services; cnt++)
1366  {
1367  i = peer->ss_instances[cnt];
1368  if ((0 == i->n_refs) &&
1370  return GNUNET_SYSERR;
1371  i->n_refs++;
1372  }
1373  peer->main_binary =
1374  GNUNET_CONFIGURATION_expand_dollar (peer->cfg, peer->main_binary);
1375  peer->main_process =
1377  NULL,
1378  peer->main_binary,
1379  peer->args,
1380  "-c",
1381  peer->cfgfile,
1382  NULL);
1383  if (NULL == peer->main_process)
1384  {
1386  _ ("Failed to start `%s': %s\n"),
1387  peer->main_binary,
1388  strerror (errno));
1389  return GNUNET_SYSERR;
1390  }
1391  return GNUNET_OK;
1392 }
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:747
@ GNUNET_OS_INHERIT_STD_OUT_AND_ERR
When these flags are set, the child process will inherit stdout and stderr of the parent.
Definition: gnunet_os_lib.h:97
unsigned int n_refs
Definition: testing.c:70
static int start_shared_service_instance(struct SharedServiceInstance *i)
Definition: testing.c:481

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 1458 of file testing.c.

1459 {
1461  return GNUNET_SYSERR;
1463  return GNUNET_SYSERR;
1464  return GNUNET_OK;
1465 }
int GNUNET_TESTING_peer_kill(struct GNUNET_TESTING_Peer *peer)
Sends SIGTERM to the peer's main process.
Definition: testing.c:1403
int GNUNET_TESTING_peer_wait(struct GNUNET_TESTING_Peer *peer)
Waits for a peer to terminate.
Definition: testing.c:1435

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 1550 of file testing.c.

1551 {
1552  unsigned int cnt;
1553 
1554  if (NULL != peer->main_process)
1556  if (NULL != peer->ah)
1558  GNUNET_free (peer->cfgfile);
1559  if (NULL != peer->cfg)
1561  GNUNET_free (peer->main_binary);
1562  GNUNET_free (peer->args);
1563  GNUNET_free (peer->id);
1564  GNUNET_free (peer->ss_instances);
1565  if (NULL != peer->ports)
1566  {
1567  for (cnt = 0; cnt < peer->nports; cnt++)
1568  GNUNET_TESTING_release_port (peer->system, peer->ports[cnt]);
1569  GNUNET_free (peer->ports);
1570  }
1571  GNUNET_free (peer);
1572 }
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:1458
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:667

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 1403 of file testing.c.

1404 {
1405  struct SharedServiceInstance *i;
1406  unsigned int cnt;
1407 
1408  if (NULL == peer->main_process)
1409  {
1410  GNUNET_break (0);
1411  return GNUNET_SYSERR;
1412  }
1413  if (0 != GNUNET_OS_process_kill (peer->main_process, GNUNET_TERM_SIG))
1414  return GNUNET_SYSERR;
1415  for (cnt = 0; cnt < peer->system->n_shared_services; cnt++)
1416  {
1417  i = peer->ss_instances[cnt];
1418  GNUNET_assert (0 != i->n_refs);
1419  i->n_refs--;
1420  if (0 == i->n_refs)
1422  }
1423  return GNUNET_OK;
1424 }
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
Definition: os_priority.c:224
#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 1435 of file testing.c.

1436 {
1437  int ret;
1438 
1439  if (NULL == peer->main_process)
1440  {
1441  GNUNET_break (0);
1442  return GNUNET_SYSERR;
1443  }
1444  ret = GNUNET_OS_process_wait (peer->main_process);
1445  GNUNET_OS_process_destroy (peer->main_process);
1446  peer->main_process = NULL;
1447  return ret;
1448 }
int GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
Wait for a process to terminate.
Definition: os_priority.c:983
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:286

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.

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 above callback
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 1509 of file testing.c.

1512 {
1513  if (NULL == peer->main_process)
1514  return GNUNET_SYSERR;
1516  if (NULL == peer->ah)
1517  return GNUNET_SYSERR;
1518  peer->cb = cb;
1519  peer->cb_cls = cb_cls;
1520  return GNUNET_OK;
1521 }
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:1476

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 1534 of file testing.c.

1535 {
1536  GNUNET_assert (NULL != peer->ah);
1538  peer->ah = NULL;
1539 }

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

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 1592 of file testing.c.

1596 {
1597  return GNUNET_TESTING_service_run (testdir, "arm", cfgfilename, tm, tm_cls);
1598 }
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:1664

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

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 1664 of file testing.c.

1669 {
1670  struct ServiceContext sc;
1671  struct GNUNET_TESTING_System *system;
1672  struct GNUNET_TESTING_Peer *peer;
1674  char *binary;
1675  char *libexec_binary;
1676 
1677  GNUNET_log_setup (testdir, "WARNING", NULL);
1678  system = GNUNET_TESTING_system_create (testdir, "127.0.0.1", NULL, NULL);
1679  if (NULL == system)
1680  return 1;
1682  if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfgfilename))
1683  {
1685  _ ("Failed to load configuration from %s\n"),
1686  cfgfilename);
1689  return 1;
1690  }
1691  peer = GNUNET_TESTING_peer_configure (system, cfg, 0, NULL, NULL);
1692  if (NULL == peer)
1693  {
1695  hostkeys_unload (system);
1697  return 1;
1698  }
1699  GNUNET_free (peer->main_binary);
1700  GNUNET_free (peer->args);
1701  GNUNET_asprintf (&binary, "gnunet-service-%s", service_name);
1702  libexec_binary = GNUNET_OS_get_libexec_binary_path (binary);
1703  if (GNUNET_SYSERR ==
1705  service_name,
1706  "PREFIX",
1707  &peer->main_binary))
1708  {
1709  /* No prefix */
1710  GNUNET_asprintf (&peer->main_binary, "%s", libexec_binary);
1711  peer->args = GNUNET_strdup ("");
1712  }
1713  else
1714  peer->args = GNUNET_strdup (libexec_binary);
1715 
1716  GNUNET_free (libexec_binary);
1717  GNUNET_free (binary);
1719  {
1723  return 1;
1724  }
1725  sc.cfg = cfg;
1726  sc.tm = tm;
1727  sc.tm_cls = tm_cls;
1728  sc.peer = peer;
1729  GNUNET_SCHEDULER_run (&service_run_main, &sc); /* Scheduler loop */
1730  if ((NULL != peer->main_process) &&
1732  {
1736  return 1;
1737  }
1741  return 0;
1742 }
static struct GNUNET_FS_SearchContext * sc
Definition: gnunet-search.c:37
static char * service_name
Option -s: service name (hash to get service descriptor)
Definition: gnunet-vpn.c:51
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:720
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:449
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:1161
int GNUNET_TESTING_peer_start(struct GNUNET_TESTING_Peer *peer)
Start the peer.
Definition: testing.c:1354
void GNUNET_TESTING_peer_destroy(struct GNUNET_TESTING_Peer *peer)
Destroy the peer.
Definition: testing.c:1550
Structure for holding service data.
Definition: testing.c:1605
static void service_run_main(void *cls)
Callback to be called when SCHEDULER has been started.
Definition: testing.c:1634

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 1759 of file testing.c.

1760 {
1761  size_t slen = strlen (argv0) + 1;
1762  char sbuf[slen];
1763  char *ret;
1764  char *dot;
1765 
1766  GNUNET_memcpy (sbuf, argv0, slen);
1767  ret = strrchr (sbuf, '_');
1768  if (NULL == ret)
1769  return NULL;
1770  ret++; /* skip underscore */
1771  dot = strchr (ret, '.');
1772  if (NULL != dot)
1773  *dot = '\0';
1774  return GNUNET_strdup (ret);
1775 }

References GNUNET_memcpy, GNUNET_strdup, and ret.