2 This file is part of GNUnet
3 (C) 2008, 2009, 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testing/testing_new.c
23 * @brief convenience API for writing testcases for GNUnet
24 * Many testcases need to start and stop a peer/service
25 * and this library is supposed to make that easier
26 * for TESTCASES. Normal programs should always
27 * use functions from gnunet_{util,arm}_lib.h. This API is
28 * ONLY for writing testcases (or internal use of the testbed).
29 * @author Christian Grothoff
33 #include "gnunet_disk_lib.h"
34 #include "gnunet_network_lib.h"
35 #include "gnunet_testing_lib-new.h"
37 #define LOG(kind,...) \
38 GNUNET_log_from (kind, "gnunettestingnew", __VA_ARGS__)
41 * AI_NUMERICSERV not defined in windows. A hack to keep on going.
43 #if !defined (AI_NUMERICSERV)
44 #define AI_NUMERICSERV 0
48 * Size of a hostkey when written to a file
50 #ifndef HOSTKEYFILESIZE
51 #define HOSTKEYFILESIZE 914
55 * Handle for a system on which GNUnet peers are executed;
56 * a system is used for reserving unique paths and ports.
58 struct GNUNET_TESTING_System
61 * Prefix (i.e. "/tmp/gnunet-testing/") we prepend to each
66 * The hostname of the controller
71 * Bitmap where each TCP port that has already been reserved for
72 * some GNUnet peer is recorded. Note that we additionally need to
73 * test if a port is already in use by non-GNUnet components before
74 * assigning it to a peer/service. If we detect that a port is
75 * already in use, we also mark it in this bitmap. So all the bits
76 * that are zero merely indicate ports that MIGHT be available for
79 uint32_t reserved_tcp_ports[65536 / 32];
82 * Bitmap where each UDP port that has already been reserved for
83 * some GNUnet peer is recorded. Note that we additionally need to
84 * test if a port is already in use by non-GNUnet components before
85 * assigning it to a peer/service. If we detect that a port is
86 * already in use, we also mark it in this bitmap. So all the bits
87 * that are zero merely indicate ports that MIGHT be available for
90 uint32_t reserved_udp_ports[65536 / 32];
93 * Counter we use to make service home paths unique on this system;
94 * the full path consists of the tmppath and this number. Each
95 * UNIXPATH for a peer is also modified to include the respective
96 * path counter to ensure uniqueness. This field is incremented
97 * by one for each configured peer. Even if peers are destroyed,
98 * we never re-use path counters.
100 uint32_t path_counter;
103 * Last used port number
110 * Handle for a GNUnet peer controlled by testing.
112 struct GNUNET_TESTING_Peer
116 * Path to the configuration file for this peer.
121 * Binary to be executed during 'GNUNET_TESTING_peer_start'.
122 * Typically 'gnunet-service-arm' (but can be set to a
123 * specific service by 'GNUNET_TESTING_service_run' if
129 * Handle to the running binary of the service, NULL if the
130 * peer/service is currently not running.
132 struct GNUNET_OS_Process *main_process;
138 * Lowest port used for GNUnet testing. Should be high enough to not
139 * conflict with other applications running on the hosts but be low
140 * enough to not conflict with client-ports (typically starting around
143 #define LOW_PORT 12000
147 * Highest port used for GNUnet testing. Should be low enough to not
148 * conflict with the port range for "local" ports (client apps; see
149 * /proc/sys/net/ipv4/ip_local_port_range on Linux for example).
151 #define HIGH_PORT 56000
155 * Create a system handle. There must only be one system
156 * handle per operating system.
158 * @param tmppath prefix path to use for all service homes
159 * @param controller hostname of the controlling host,
160 * service configurations are modified to allow
161 * control connections from this host; can be NULL
162 * @return handle to this system, NULL on error
164 struct GNUNET_TESTING_System *
165 GNUNET_TESTING_system_create (const char *tmppath,
166 const char *controller)
168 struct GNUNET_TESTING_System *system;
172 system = GNUNET_malloc (sizeof (struct GNUNET_TESTING_System));
173 system->tmppath = GNUNET_strdup (tmppath);
174 if (NULL != controller)
175 system->controller = GNUNET_strdup (controller);
181 * Free system resources.
183 * @param system system to be freed
184 * @param remove_paths should the 'tmppath' and all subdirectories
185 * be removed (clean up on shutdown)?
188 GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
191 GNUNET_assert (NULL != system);
192 if (GNUNET_YES == remove_paths)
193 GNUNET_DISK_directory_remove (system->tmppath);
194 GNUNET_free (system->tmppath);
195 GNUNET_free_non_null (system->controller);
196 GNUNET_free (system);
201 * Reserve a TCP or UDP port for a peer.
203 * @param system system to use for reservation tracking
204 * @param is_tcp GNUNET_YES for TCP ports, GNUNET_NO for UDP
205 * @return 0 if no free port was available
208 GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system,
211 struct GNUNET_NETWORK_Handle *socket;
212 struct addrinfo hint;
213 struct addrinfo *ret;
214 uint32_t *port_buckets;
222 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
223 hint.ai_socktype = (GNUNET_YES == is_tcp)? SOCK_STREAM : SOCK_DGRAM;
224 hint.ai_protocol = 0;
227 hint.ai_canonname = NULL;
229 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
230 port_buckets = (GNUNET_YES == is_tcp) ?
231 system->reserved_tcp_ports : system->reserved_udp_ports;
232 for (index = (LOW_PORT / 32) + 1; index < (HIGH_PORT / 32); index++)
234 xor_image = (UINT32_MAX ^ port_buckets[index]);
235 if (0 == xor_image) /* Ports in the bucket are full */
240 if (0 == ((xor_image >> pos) & 1U))
245 open_port = (index * 32) + pos;
246 GNUNET_asprintf (&open_port_str, "%u", open_port);
248 GNUNET_assert (0 == getaddrinfo (NULL, open_port_str, &hint, &ret));
249 GNUNET_free (open_port_str);
250 socket = GNUNET_NETWORK_socket_create (ret->ai_family,
251 (GNUNET_YES == is_tcp) ?
252 SOCK_STREAM : SOCK_DGRAM,
254 GNUNET_assert (NULL != socket);
255 bind_status = GNUNET_NETWORK_socket_bind (socket,
259 GNUNET_NETWORK_socket_close (socket);
261 port_buckets[index] |= (1U << pos); /* Set the port bit */
262 if (GNUNET_OK == bind_status)
272 * Release reservation of a TCP or UDP port for a peer
273 * (used during GNUNET_TESTING_peer_destroy).
275 * @param system system to use for reservation tracking
276 * @param is_tcp GNUNET_YES for TCP ports, GNUNET_NO for UDP
277 * @param port reserved port to release
280 GNUNET_TESTING_release_port (struct GNUNET_TESTING_System *system,
284 uint32_t *port_buckets;
288 GNUNET_assert (NULL != system);
289 port_buckets = (GNUNET_YES == is_tcp) ?
290 system->reserved_tcp_ports : system->reserved_udp_ports;
293 LOG (GNUNET_ERROR_TYPE_DEBUG, "Releasing port %u\n", port);
294 if (0 == (port_buckets[bucket] & (1U << pos)))
296 GNUNET_break(0); /* Port was not reserved by us using reserve_port() */
299 port_buckets[bucket] &= ~(1U << pos);
304 * Reserve a SERVICEHOME path for a peer.
306 * @param system system to use for reservation tracking
307 * @return NULL on error, otherwise fresh unique path to use
308 * as the servicehome for the peer; must be freed by the caller
312 reserve_path (struct GNUNET_TESTING_System *system)
316 GNUNET_asprintf (&reserved_path,
317 "%s/%u", system->tmppath, system->path_counter++);
318 return reserved_path;
323 * Testing includes a number of pre-created hostkeys for
324 * faster peer startup. This function can be used to
325 * access the n-th key of those pre-created hostkeys; note
326 * that these keys are ONLY useful for testing and not
327 * secure as the private keys are part of the public
328 * GNUnet source code.
330 * This is primarily a helper function used internally
331 * by 'GNUNET_TESTING_peer_configure'.
333 * @param key_number desired pre-created hostkey to obtain
334 * @param filename where to store the hostkey (file will
335 * be created, or overwritten if it already exists)
336 * @param id set to the peer's identity (hash of the public
337 * key; if NULL, GNUNET_SYSERR is returned immediately
338 * @return GNUNET_SYSERR on error (not enough keys)
341 GNUNET_TESTING_hostkey_get (uint32_t key_number,
342 const char *filename,
343 struct GNUNET_PeerIdentity *id)
345 struct GNUNET_DISK_FileHandle *fd;
346 struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
347 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
350 uint32_t total_hostkeys;
353 return GNUNET_SYSERR;
354 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
356 LOG (GNUNET_ERROR_TYPE_ERROR,
357 "Hostkeys file not found: %s\n", filename);
358 return GNUNET_SYSERR;
360 /* Check hostkey file size, read entire thing into memory */
361 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
362 GNUNET_DISK_PERM_NONE);
365 LOG (GNUNET_ERROR_TYPE_ERROR,
366 "Could not open hostkeys file: %s\n", filename);
367 return GNUNET_SYSERR;
370 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
374 GNUNET_DISK_file_close (fd);
375 return GNUNET_SYSERR; /* File is empty */
377 if (0 != (fs % HOSTKEYFILESIZE))
379 GNUNET_DISK_file_close (fd);
380 LOG (GNUNET_ERROR_TYPE_ERROR,
381 "Incorrect hostkey file format: %s\n", filename);
382 return GNUNET_SYSERR;
384 total_hostkeys = fs / HOSTKEYFILESIZE;
385 if (key_number >= total_hostkeys)
387 GNUNET_DISK_file_close (fd);
388 LOG (GNUNET_ERROR_TYPE_ERROR,
389 "Key number %u doesn't exist\n", key_number);
390 return GNUNET_SYSERR;
392 file_data = GNUNET_malloc_large (fs);
393 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, file_data, fs));
394 GNUNET_DISK_file_close (fd);
395 private_key = GNUNET_CRYPTO_rsa_decode_key (file_data +
396 (key_number * HOSTKEYFILESIZE),
398 if (NULL == private_key)
400 LOG (GNUNET_ERROR_TYPE_ERROR,
401 "Error while decoding key %u from %s\n", key_number, filename);
402 GNUNET_free (file_data);
403 return GNUNET_SYSERR;
405 GNUNET_CRYPTO_rsa_key_get_public (private_key, &public_key);
406 GNUNET_CRYPTO_hash (&public_key,
407 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
409 GNUNET_free (file_data);
415 * Create a new configuration using the given configuration
416 * as a template; ports and paths will be modified to select
417 * available ports on the local system. If we run
418 * out of "*port" numbers, return SYSERR.
420 * This is primarily a helper function used internally
421 * by 'GNUNET_TESTING_peer_configure'.
423 * @param system system to use to coordinate resource usage
424 * @param cfg template configuration to update
425 * @return GNUNET_OK on success, GNUNET_SYSERR on error
428 GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
429 struct GNUNET_CONFIGURATION_Handle *cfg)
432 return GNUNET_SYSERR;
437 * Configure a GNUnet peer. GNUnet must be installed on the local
438 * system and available in the PATH.
440 * @param system system to use to coordinate resource usage
441 * @param cfg configuration to use; will be UPDATED (to reflect needed
442 * changes in port numbers and paths)
443 * @param key_number number of the hostkey to use for the peer
444 * @param id identifier for the daemon, will be set, can be NULL
445 * @param emsg set to error message (set to NULL on success), can be NULL
446 * @return handle to the peer, NULL on error
448 struct GNUNET_TESTING_Peer *
449 GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
450 struct GNUNET_CONFIGURATION_Handle *cfg,
452 struct GNUNET_PeerIdentity *id,
463 * @param peer peer to start
464 * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer already running)
467 GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer)
470 return GNUNET_SYSERR;
477 * @param peer peer to stop
478 * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer not running)
481 GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer)
484 return GNUNET_SYSERR;
489 * Destroy the peer. Releases resources locked during peer configuration.
490 * If the peer is still running, it will be stopped AND a warning will be
491 * printed (users of the API should stop the peer explicitly first).
493 * @param peer peer to destroy
496 GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer)
504 * Start a single peer and run a test using the testing library.
505 * Starts a peer using the given configuration and then invokes the
506 * given callback. This function ALSO initializes the scheduler loop
507 * and should thus be called directly from "main". The testcase
508 * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
510 * @param tmppath path for storing temporary data for the test
511 * @param cfgfilename name of the configuration file to use;
512 * use NULL to only run with defaults
513 * @param tm main function of the testcase
514 * @param tm_cls closure for 'tm'
515 * @return 0 on success, 1 on error
518 GNUNET_TESTING_peer_run (const char *tmppath,
519 const char *cfgfilename,
520 GNUNET_TESTING_TestMain tm,
523 return GNUNET_TESTING_service_run (tmppath, "arm",
524 cfgfilename, tm, tm_cls);
530 * Start a single service (no ARM, except of course if the given
531 * service name is 'arm') and run a test using the testing library.
532 * Starts a service using the given configuration and then invokes the
533 * given callback. This function ALSO initializes the scheduler loop
534 * and should thus be called directly from "main". The testcase
535 * should self-terminate by invoking 'GNUNET_SCHEDULER_shutdown'.
537 * This function is useful if the testcase is for a single service
538 * and if that service doesn't itself depend on other services.
540 * @param tmppath path for storing temporary data for the test
541 * @param service_name name of the service to run
542 * @param cfgfilename name of the configuration file to use;
543 * use NULL to only run with defaults
544 * @param tm main function of the testcase
545 * @param tm_cls closure for 'tm'
546 * @return 0 on success, 1 on error
549 GNUNET_TESTING_service_run (const char *tmppath,
550 const char *service_name,
551 const char *cfgfilename,
552 GNUNET_TESTING_TestMain tm,
561 /* end of testing_new.c */