adding Ludo's gnunet-download-manager.scm back to SVN HEAD
[oweals/gnunet.git] / src / include / gnunet_testing_lib.h
1 /*
2       This file is part of GNUnet
3       (C) 2008, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
10       GNUnet is distributed in the hope that it will be useful, but
11       WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13       General Public License for more details.
14
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.
19  */
20
21 /**
22  * @file include/gnunet_testing_lib.h
23  * @brief convenience API for writing testcases for GNUnet
24  *        Many testcases need to start and stop gnunetd,
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!
29  * @author Christian Grothoff
30  */
31
32 #ifndef GNUNET_TESTING_LIB_H
33 #define GNUNET_TESTING_LIB_H
34
35 #include "gnunet_util_lib.h"
36 #include "gnunet_statistics_service.h"
37
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #if 0                           /* keep Emacsens' auto-indent happy */
42 }
43 #endif
44 #endif
45
46 #define HOSTKEYFILESIZE 914
47
48 /**
49  * Handle for a GNUnet daemon (technically a set of
50  * daemons; the handle is really for the master ARM
51  * daemon) started by the testing library.
52  */
53 struct GNUNET_TESTING_Daemon;
54
55 /**
56  * Linked list of hostnames and ports to use for starting daemons.
57  */
58 struct GNUNET_TESTING_Host
59 {
60   /**
61    * Pointer to next item in the list.
62    */
63   struct GNUNET_TESTING_Host *next;
64
65   /**
66    * Hostname to connect to.
67    */
68   char *hostname;
69
70   /**
71    * Username to use when connecting (may be null).
72    */
73   char *username;
74
75   /**
76    * Port to use for SSH connection (used for ssh
77    * connection forwarding, 0 to let ssh decide)
78    */
79   uint16_t port;
80 };
81
82 /**
83  * Prototype of a function that will be called whenever
84  * a daemon was started by the testing library.
85  *
86  * @param cls closure
87  * @param id identifier for the daemon, NULL on error
88  * @param d handle for the daemon
89  * @param emsg error message (NULL on success)
90  */
91 typedef void (*GNUNET_TESTING_NotifyHostkeyCreated) (void *cls,
92                                                      const struct
93                                                      GNUNET_PeerIdentity * id,
94                                                      struct
95                                                      GNUNET_TESTING_Daemon * d,
96                                                      const char *emsg);
97
98 /**
99  * Prototype of a function that will be called whenever
100  * a daemon was started by the testing library.
101  *
102  * @param cls closure
103  * @param id identifier for the daemon, NULL on error
104  * @param cfg configuration used by this daemon
105  * @param d handle for the daemon
106  * @param emsg error message (NULL on success)
107  */
108 typedef void (*GNUNET_TESTING_NotifyDaemonRunning) (void *cls,
109                                                     const struct
110                                                     GNUNET_PeerIdentity * id,
111                                                     const struct
112                                                     GNUNET_CONFIGURATION_Handle
113                                                     * cfg,
114                                                     struct GNUNET_TESTING_Daemon
115                                                     * d, const char *emsg);
116
117 /**
118  * Handle to an entire testbed of GNUnet peers.
119  */
120 struct GNUNET_TESTING_Testbed;
121
122 /**
123  * Phases of starting GNUnet on a system.
124  */
125 enum GNUNET_TESTING_StartPhase
126 {
127   /**
128    * Copy the configuration file to the target system.
129    */
130   SP_COPYING,
131
132   /**
133    * Configuration file has been copied, generate hostkey.
134    */
135   SP_COPIED,
136
137   /**
138    * Create the hostkey for the peer.
139    */
140   SP_HOSTKEY_CREATE,
141
142   /**
143    * Hostkey generated, wait for topology to be finished.
144    */
145   SP_HOSTKEY_CREATED,
146
147   /**
148    * Topology has been created, now start ARM.
149    */
150   SP_TOPOLOGY_SETUP,
151
152   /**
153    * ARM has been started, check that it has properly daemonized and
154    * then try to connect to the CORE service (which should be
155    * auto-started by ARM).
156    */
157   SP_START_ARMING,
158
159   /**
160    * We're waiting for CORE to start.
161    */
162   SP_START_CORE,
163
164   /**
165    * CORE is up, now make sure we get the HELLO for this peer.
166    */
167   SP_GET_HELLO,
168
169   /**
170    * Core has notified us that we've established a connection to the service.
171    * The main FSM halts here and waits to be moved to UPDATE or CLEANUP.
172    */
173   SP_START_DONE,
174
175   /**
176    * We've been asked to terminate the instance and are now waiting for
177    * the remote command to stop the gnunet-arm process and delete temporary
178    * files.
179    */
180   SP_SHUTDOWN_START,
181
182   /**
183    * We should shutdown a *single* service via gnunet-arm.  Call the dead_cb
184    * upon notification from gnunet-arm that the service has been stopped.
185    */
186   SP_SERVICE_SHUTDOWN_START,
187
188   /**
189    * We should start a *single* service via gnunet-arm.  Call the daemon cb
190    * upon notification from gnunet-arm that the service has been started.
191    */
192   SP_SERVICE_START,
193
194   /**
195    * We've received a configuration update and are currently waiting for
196    * the copy process for the update to complete.  Once it is, we will
197    * return to "SP_START_DONE" (and rely on ARM to restart all affected
198    * services).
199    */
200   SP_CONFIG_UPDATE
201 };
202
203 /**
204  * Prototype of a function that will be called when a
205  * particular operation was completed the testing library.
206  *
207  * @param cls closure
208  * @param emsg NULL on success
209  */
210 typedef void (*GNUNET_TESTING_NotifyCompletion) (void *cls, const char *emsg);
211
212 /**
213  * Prototype of a function that will be called with the
214  * number of connections created for a particular topology.
215  *
216  * @param cls closure
217  * @param num_connections the number of connections created
218  */
219 typedef void (*GNUNET_TESTING_NotifyConnections) (void *cls,
220                                                   unsigned int num_connections);
221
222 /**
223  * Handle for a GNUnet daemon (technically a set of
224  * daemons; the handle is really for the master ARM
225  * daemon) started by the testing library.
226  */
227 struct GNUNET_TESTING_Daemon
228 {
229   /**
230    * Our configuration.
231    */
232   struct GNUNET_CONFIGURATION_Handle *cfg;
233
234   /**
235    * At what time to give up starting the peer
236    */
237   struct GNUNET_TIME_Absolute max_timeout;
238
239   /**
240    * Host to run GNUnet on.
241    */
242   char *hostname;
243
244   /**
245    * Port to use for ssh, NULL to let system choose default.
246    */
247   char *ssh_port_str;
248
249   /**
250    * Result of GNUNET_i2s of this peer,
251    * for printing
252    */
253   char *shortname;
254
255   /**
256    * Username we are using.
257    */
258   char *username;
259
260   /**
261    * Name of the configuration file
262    */
263   char *cfgfile;
264
265   /**
266    * Callback to inform initiator that the peer's
267    * hostkey has been created.
268    */
269   GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback;
270
271   /**
272    * Closure for hostkey creation callback.
273    */
274   void *hostkey_cls;
275
276   /**
277    * Function to call when the peer is running.
278    */
279   GNUNET_TESTING_NotifyDaemonRunning cb;
280
281   /**
282    * Closure for cb.
283    */
284   void *cb_cls;
285
286   /**
287    * Arguments from "daemon_stop" call.
288    */
289   GNUNET_TESTING_NotifyCompletion dead_cb;
290
291   /**
292    * Closure for 'dead_cb'.
293    */
294   void *dead_cb_cls;
295
296   /**
297    * Arguments from "daemon_stop" call.
298    */
299   GNUNET_TESTING_NotifyCompletion update_cb;
300
301   /**
302    * Closure for 'update_cb'.
303    */
304   void *update_cb_cls;
305
306   /**
307    * PID of the process that we started last.
308    */
309   struct GNUNET_OS_Process *proc;
310
311   /**
312    * Handle to the server.
313    */
314   struct GNUNET_CORE_Handle *server;
315
316   /**
317    * Handle to the transport service of this peer
318    */
319   struct GNUNET_TRANSPORT_Handle *th;
320
321   /**
322    * Handle for getting HELLOs from transport
323    */
324   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
325
326   /**
327    * HELLO message for this peer
328    */
329   struct GNUNET_HELLO_Message *hello;
330
331   /**
332    * Handle to a pipe for reading the hostkey.
333    */
334   struct GNUNET_DISK_PipeHandle *pipe_stdout;
335
336   /**
337    * Currently, a single char * pointing to a service
338    * that has been churned off.
339    *
340    * FIXME: make this a linked list of services that have been churned off!!!
341    */
342   char *churned_services;
343
344   /**
345    * ID of the current task.
346    */
347   GNUNET_SCHEDULER_TaskIdentifier task;
348
349   /**
350    * Identity of this peer (once started).
351    */
352   struct GNUNET_PeerIdentity id;
353
354   /**
355    * Flag to indicate that we've already been asked
356    * to terminate (but could not because some action
357    * was still pending).
358    */
359   int dead;
360
361   /**
362    * GNUNET_YES if the hostkey has been created
363    * for this peer, GNUNET_NO otherwise.
364    */
365   int have_hostkey;
366
367   /**
368    * In which phase are we during the start of
369    * this process?
370    */
371   enum GNUNET_TESTING_StartPhase phase;
372
373   /**
374    * Current position in 'hostkeybuf' (for reading from gnunet-peerinfo)
375    */
376   unsigned int hostkeybufpos;
377
378   /**
379    * Set to GNUNET_YES once the peer is up.
380    */
381   int running;
382
383   /**
384    * Used to tell shutdown not to remove configuration for the peer
385    * (if it's going to be restarted later)
386    */
387   int churn;
388
389   /**
390    * Output from gnunet-peerinfo is read into this buffer.
391    */
392   char hostkeybuf[105];
393
394 };
395
396
397 /**
398  * Handle to a group of GNUnet peers.
399  */
400 struct GNUNET_TESTING_PeerGroup;
401
402
403 /**
404  * Prototype of a function that will be called whenever
405  * two daemons are connected by the testing library.
406  *
407  * @param cls closure
408  * @param first peer id for first daemon
409  * @param second peer id for the second daemon
410  * @param distance distance between the connected peers
411  * @param first_cfg config for the first daemon
412  * @param second_cfg config for the second daemon
413  * @param first_daemon handle for the first daemon
414  * @param second_daemon handle for the second daemon
415  * @param emsg error message (NULL on success)
416  */
417 typedef void (*GNUNET_TESTING_NotifyConnection) (void *cls,
418                                                  const struct
419                                                  GNUNET_PeerIdentity * first,
420                                                  const struct
421                                                  GNUNET_PeerIdentity * second,
422                                                  uint32_t distance,
423                                                  const struct
424                                                  GNUNET_CONFIGURATION_Handle *
425                                                  first_cfg,
426                                                  const struct
427                                                  GNUNET_CONFIGURATION_Handle *
428                                                  second_cfg,
429                                                  struct GNUNET_TESTING_Daemon *
430                                                  first_daemon,
431                                                  struct GNUNET_TESTING_Daemon *
432                                                  second_daemon,
433                                                  const char *emsg);
434
435
436 /**
437  * Prototype of a callback function indicating that two peers
438  * are currently connected.
439  *
440  * @param cls closure
441  * @param first peer id for first daemon
442  * @param second peer id for the second daemon
443  * @param distance distance between the connected peers
444  * @param emsg error message (NULL on success)
445  */
446 typedef void (*GNUNET_TESTING_NotifyTopology) (void *cls,
447                                                const struct GNUNET_PeerIdentity
448                                                * first,
449                                                const struct GNUNET_PeerIdentity
450                                                * second, const char *emsg);
451
452
453 /**
454  * Starts a GNUnet daemon.  GNUnet must be installed on the target
455  * system and available in the PATH.  The machine must furthermore be
456  * reachable via "ssh" (unless the hostname is "NULL") without the
457  * need to enter a password.
458  *
459  * @param cfg configuration to use
460  * @param timeout how long to wait starting up peers
461  * @param pretend GNUNET_YES to set up files but not start peer GNUNET_NO
462  *                to really start the peer (default)
463  * @param hostname name of the machine where to run GNUnet
464  *        (use NULL for localhost).
465  * @param ssh_username ssh username to use when connecting to hostname
466  * @param sshport port to pass to ssh process when connecting to hostname
467  * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
468  * @param hostkey_callback function to call once the hostkey has been
469  *        generated for this peer, but it hasn't yet been started
470  *        (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
471  * @param hostkey_cls closure for hostkey callback
472  * @param cb function to call once peer is up, or failed to start
473  * @param cb_cls closure for cb
474  * @return handle to the daemon (actual start will be completed asynchronously)
475  */
476 struct GNUNET_TESTING_Daemon *
477 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
478                              struct GNUNET_TIME_Relative timeout, int pretend,
479                              const char *hostname, const char *ssh_username,
480                              uint16_t sshport, const char *hostkey,
481                              GNUNET_TESTING_NotifyHostkeyCreated
482                              hostkey_callback, void *hostkey_cls,
483                              GNUNET_TESTING_NotifyDaemonRunning cb,
484                              void *cb_cls);
485
486 /**
487  * Continues GNUnet daemon startup when user wanted to be notified
488  * once a hostkey was generated (for creating friends files, blacklists,
489  * etc.).
490  *
491  * @param daemon the daemon to finish starting
492  */
493 void
494 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon);
495
496
497 /**
498  * Check whether the given daemon is running.
499  *
500  * @param daemon the daemon to check
501  * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
502  *         daemon is down, GNUNET_SYSERR on error.
503  */
504 int
505 GNUNET_TESTING_test_daemon_running (struct GNUNET_TESTING_Daemon *daemon);
506
507
508 /**
509  * Restart (stop and start) a GNUnet daemon.
510  *
511  * @param d the daemon that should be restarted
512  * @param cb function called once the daemon is (re)started
513  * @param cb_cls closure for cb
514  */
515 void
516 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
517                                GNUNET_TESTING_NotifyDaemonRunning cb,
518                                void *cb_cls);
519
520
521 /**
522  * Start a peer that has previously been stopped using the daemon_stop
523  * call (and files weren't deleted and the allow restart flag)
524  *
525  * @param daemon the daemon to start (has been previously stopped)
526  * @param timeout how long to wait for restart
527  * @param cb the callback for notification when the peer is running
528  * @param cb_cls closure for the callback
529  */
530 void
531 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
532                                      struct GNUNET_TIME_Relative timeout,
533                                      GNUNET_TESTING_NotifyDaemonRunning cb,
534                                      void *cb_cls);
535
536
537 /**
538  * Starts a GNUnet daemon's service.
539  *
540  * @param d the daemon for which the service should be started
541  * @param service the name of the service to start
542  * @param timeout how long to wait for process for startup
543  * @param cb function called once gnunet-arm returns
544  * @param cb_cls closure for cb
545  */
546 void
547 GNUNET_TESTING_daemon_start_service (struct GNUNET_TESTING_Daemon *d,
548                                      const char *service,
549                                      struct GNUNET_TIME_Relative timeout,
550                                      GNUNET_TESTING_NotifyDaemonRunning cb,
551                                      void *cb_cls);
552
553
554 /**
555  * Starts a GNUnet daemon's service which has been previously turned off.
556  *
557  * @param d the daemon for which the service should be started
558  * @param service the name of the service to start
559  * @param timeout how long to wait for process for startup
560  * @param cb function called once gnunet-arm returns
561  * @param cb_cls closure for cb
562  */
563 void
564 GNUNET_TESTING_daemon_start_stopped_service (struct GNUNET_TESTING_Daemon *d,
565                                              char *service,
566                                              struct GNUNET_TIME_Relative
567                                              timeout,
568                                              GNUNET_TESTING_NotifyDaemonRunning
569                                              cb, void *cb_cls);
570
571
572 /**
573  * Get a certain testing daemon handle.
574  *
575  * @param pg handle to the set of running peers
576  * @param position the number of the peer to return
577  */
578 struct GNUNET_TESTING_Daemon *
579 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
580                            unsigned int position);
581
582
583 /**
584  * Get a daemon by peer identity, so callers can
585  * retrieve the daemon without knowing it's offset.
586  *
587  * @param pg the peer group to retrieve the daemon from
588  * @param peer_id the peer identity of the daemon to retrieve
589  *
590  * @return the daemon on success, or NULL if no such peer identity is found
591  */
592 struct GNUNET_TESTING_Daemon *
593 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
594                                  const struct GNUNET_PeerIdentity *peer_id);
595
596
597 /**
598  * Stops a GNUnet daemon.
599  *
600  * @param d the daemon that should be stopped
601  * @param timeout how long to wait for process for shutdown to complete
602  * @param cb function called once the daemon was stopped
603  * @param cb_cls closure for cb
604  * @param delete_files GNUNET_YES to remove files, GNUNET_NO
605  *        to leave them (i.e. for restarting at a later time,
606  *        or logfile inspection once finished)
607  * @param allow_restart GNUNET_YES to restart peer later (using this API)
608  *        GNUNET_NO to kill off and clean up for good
609  */
610 void
611 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
612                             struct GNUNET_TIME_Relative timeout,
613                             GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
614                             int delete_files, int allow_restart);
615
616
617
618 /**
619  * Create a new configuration using the given configuration
620  * as a template; however, each PORT in the existing cfg
621  * must be renumbered by incrementing "*port".  If we run
622  * out of "*port" numbers, return NULL.
623  *
624  * @param cfg template configuration
625  * @param off the current peer offset
626  * @param port port numbers to use, update to reflect
627  *             port numbers that were used
628  * @param upnum number to make unix domain socket names unique
629  * @param hostname hostname of the controlling host, to allow control connections from
630  * @param fdnum number used to offset the unix domain socket for grouped processes
631  *              (such as statistics or peerinfo, which can be shared among others)
632  *
633  * @return new configuration, NULL on error
634  */
635 struct GNUNET_CONFIGURATION_Handle *
636 GNUNET_TESTING_create_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t off,
637              uint16_t * port, uint32_t * upnum, const char *hostname,
638              uint32_t * fdnum);
639
640 /**
641  * Changes the configuration of a GNUnet daemon.
642  *
643  * @param d the daemon that should be modified
644  * @param cfg the new configuration for the daemon
645  * @param cb function called once the configuration was changed
646  * @param cb_cls closure for cb
647  */
648 void
649 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
650                                    struct GNUNET_CONFIGURATION_Handle *cfg,
651                                    GNUNET_TESTING_NotifyCompletion cb,
652                                    void *cb_cls);
653
654
655 /**
656  * Stops a single service of a GNUnet daemon.  Used like daemon_stop,
657  * only doesn't stop the entire peer in any case.  If the service
658  * is not currently running, this call is likely to fail after
659  * timeout!
660  *
661  * @param d the daemon that should be stopped
662  * @param service the name of the service to stop
663  * @param timeout how long to wait for process for shutdown to complete
664  * @param cb function called once the service was stopped
665  * @param cb_cls closure for cb
666  */
667 void
668 GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d,
669                                     const char *service,
670                                     struct GNUNET_TIME_Relative timeout,
671                                     GNUNET_TESTING_NotifyCompletion cb,
672                                     void *cb_cls);
673
674
675 /**
676  * Read a testing hosts file based on a configuration.
677  * Returns a DLL of hosts (caller must free!) on success
678  * or NULL on failure.
679  *
680  * @param cfg a configuration with a testing section
681  *
682  * @return DLL of hosts on success, NULL on failure
683  */
684 struct GNUNET_TESTING_Host *
685 GNUNET_TESTING_hosts_load (const struct GNUNET_CONFIGURATION_Handle *cfg);
686
687
688 /**
689  * Start count gnunet instances with the same set of transports and
690  * applications.  The port numbers (any option called "PORT") will be
691  * adjusted to ensure that no two peers running on the same system
692  * have the same port(s) in their respective configurations.
693  *
694  * @param cfg configuration template to use
695  * @param total number of daemons to start
696  * @param max_concurrent_connections for testing, how many peers can
697 *                                   we connect to simultaneously
698  * @param max_concurrent_ssh when starting with ssh, how many ssh
699  *        connections will we allow at once (based on remote hosts allowed!)
700  * @param timeout total time allowed for peers to start
701  * @param hostkey_callback function to call on each peers hostkey generation
702  *        if NULL, peers will be started by this call, if non-null,
703  *        GNUNET_TESTING_daemons_continue_startup must be called after
704  *        successful hostkey generation
705  * @param hostkey_cls closure for hostkey callback
706  * @param cb function to call on each daemon that was started
707  * @param cb_cls closure for cb
708  * @param connect_callback function to call each time two hosts are connected
709  * @param connect_callback_cls closure for connect_callback
710  * @param hostnames linked list of host structs to use to start peers on
711  *                  (NULL to run on localhost only)
712  *
713  * @return NULL on error, otherwise handle to control peer group
714  */
715 struct GNUNET_TESTING_PeerGroup *
716 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
717                               unsigned int total,
718                               unsigned int max_concurrent_connections,
719                               unsigned int max_concurrent_ssh,
720                               struct GNUNET_TIME_Relative timeout,
721                               GNUNET_TESTING_NotifyHostkeyCreated
722                               hostkey_callback, void *hostkey_cls,
723                               GNUNET_TESTING_NotifyDaemonRunning cb,
724                               void *cb_cls,
725                               GNUNET_TESTING_NotifyConnection connect_callback,
726                               void *connect_callback_cls,
727                               const struct GNUNET_TESTING_Host *hostnames);
728
729
730 /**
731  * Function which continues a peer group starting up
732  * after successfully generating hostkeys for each peer.
733  *
734  * @param pg the peer group to continue starting
735  */
736 void
737 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg);
738
739
740 /**
741  * Handle for an active request to connect two peers.
742  */
743 struct GNUNET_TESTING_ConnectContext;
744
745
746 /**
747  * Establish a connection between two GNUnet daemons.  The daemons
748  * must both be running and not be stopped until either the
749  * 'cb' callback is called OR the connection request has been
750  * explicitly cancelled.
751  *
752  * @param d1 handle for the first daemon
753  * @param d2 handle for the second daemon
754  * @param timeout how long is the connection attempt
755  *        allowed to take?
756  * @param max_connect_attempts how many times should we try to reconnect
757  *        (within timeout)
758  * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
759  *                   the HELLO has already been exchanged
760  * @param cb function to call at the end
761  * @param cb_cls closure for cb
762  * @return handle to cancel the request, NULL on error
763  */
764 struct GNUNET_TESTING_ConnectContext *
765 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
766                                 struct GNUNET_TESTING_Daemon *d2,
767                                 struct GNUNET_TIME_Relative timeout,
768                                 unsigned int max_connect_attempts,
769                                 int send_hello,
770                                 GNUNET_TESTING_NotifyConnection cb,
771                                 void *cb_cls);
772
773
774
775 /**
776  * Cancel an attempt to connect two daemons.
777  *
778  * @param cc connect context
779  */
780 void
781 GNUNET_TESTING_daemons_connect_cancel (struct GNUNET_TESTING_ConnectContext
782                                        *cc);
783
784
785
786 /**
787  * Restart all peers in the given group.
788  *
789  * @param pg the handle to the peer group
790  * @param callback function to call on completion (or failure)
791  * @param callback_cls closure for the callback function
792  */
793 void
794 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
795                                 GNUNET_TESTING_NotifyCompletion callback,
796                                 void *callback_cls);
797
798
799 /**
800  * Shutdown all peers started in the given group.
801  *
802  * @param pg handle to the peer group
803  * @param timeout how long to wait for shutdown
804  * @param cb callback to notify upon success or failure
805  * @param cb_cls closure for cb
806  */
807 void
808 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
809                              struct GNUNET_TIME_Relative timeout,
810                              GNUNET_TESTING_NotifyCompletion cb, void *cb_cls);
811
812
813 /**
814  * Count the number of running peers.
815  *
816  * @param pg handle for the peer group
817  *
818  * @return the number of currently running peers in the peer group
819  */
820 unsigned int
821 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg);
822
823
824 /**
825  * Simulate churn by stopping some peers (and possibly
826  * re-starting others if churn is called multiple times).  This
827  * function can only be used to create leave-join churn (peers "never"
828  * leave for good).  First "voff" random peers that are currently
829  * online will be taken offline; then "von" random peers that are then
830  * offline will be put back online.  No notifications will be
831  * generated for any of these operations except for the callback upon
832  * completion.  Note that the implementation is at liberty to keep
833  * the ARM service itself (but none of the other services or daemons)
834  * running even though the "peer" is being varied offline.
835  *
836  * @param pg handle for the peer group
837  * @param service the service to churn on/off, NULL for all
838  * @param voff number of peers that should go offline
839  * @param von number of peers that should come back online;
840  *            must be zero on first call (since "testbed_start"
841  *            always starts all of the peers)
842  * @param timeout how long to wait for operations to finish before
843  *        giving up
844  * @param cb function to call at the end
845  * @param cb_cls closure for cb
846  */
847 void
848 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
849                               char *service, unsigned int voff,
850                               unsigned int von,
851                               struct GNUNET_TIME_Relative timeout,
852                               GNUNET_TESTING_NotifyCompletion cb, void *cb_cls);
853
854
855 /**
856  * Start a given service for each of the peers in the peer group.
857  *
858  * @param pg handle for the peer group
859  * @param service the service to start
860  * @param timeout how long to wait for operations to finish before
861  *        giving up
862  * @param cb function to call once finished
863  * @param cb_cls closure for cb
864  *
865  */
866 void
867 GNUNET_TESTING_daemons_start_service (struct GNUNET_TESTING_PeerGroup *pg,
868                                       char *service,
869                                       struct GNUNET_TIME_Relative timeout,
870                                       GNUNET_TESTING_NotifyCompletion cb,
871                                       void *cb_cls);
872
873
874 /**
875  * Callback function to process statistic values.
876  *
877  * @param cls closure
878  * @param peer the peer the statistics belong to
879  * @param subsystem name of subsystem that created the statistic
880  * @param name the name of the datum
881  * @param value the current value
882  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
883  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
884  */
885 typedef int (*GNUNET_TESTING_STATISTICS_Iterator) (void *cls,
886                                                    const struct
887                                                    GNUNET_PeerIdentity * peer,
888                                                    const char *subsystem,
889                                                    const char *name,
890                                                    uint64_t value,
891                                                    int is_persistent);
892
893
894 /**
895  * Iterate over all (running) peers in the peer group, retrieve
896  * all statistics from each.
897  *
898  * @param pg the peergroup to iterate statistics of
899  * @param cont continuation to call once call is completed(?)
900  * @param proc processing function for each statistic retrieved
901  * @param cls closure to pass to proc
902  */
903 void
904 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
905                                GNUNET_STATISTICS_Callback cont,
906                                GNUNET_TESTING_STATISTICS_Iterator proc,
907                                void *cls);
908
909
910 /**
911  * Topologies supported for testbeds.
912  */
913 enum GNUNET_TESTING_Topology
914 {
915   /**
916    * A clique (everyone connected to everyone else).
917    */
918   GNUNET_TESTING_TOPOLOGY_CLIQUE,
919
920   /**
921    * Small-world network (2d torus plus random links).
922    */
923   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
924
925   /**
926    * Small-world network (ring plus random links).
927    */
928   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
929
930   /**
931    * Ring topology.
932    */
933   GNUNET_TESTING_TOPOLOGY_RING,
934
935   /**
936    * 2-d torus.
937    */
938   GNUNET_TESTING_TOPOLOGY_2D_TORUS,
939
940   /**
941    * Random graph.
942    */
943   GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
944
945   /**
946    * Certain percentage of peers are unable to communicate directly
947    * replicating NAT conditions
948    */
949   GNUNET_TESTING_TOPOLOGY_INTERNAT,
950
951   /**
952    * Scale free topology.
953    */
954   GNUNET_TESTING_TOPOLOGY_SCALE_FREE,
955
956   /**
957    * Straight line topology.
958    */
959   GNUNET_TESTING_TOPOLOGY_LINE,
960
961   /**
962    * All peers are disconnected.
963    */
964   GNUNET_TESTING_TOPOLOGY_NONE,
965
966   /**
967    * Read a topology from a given file.
968    */
969   GNUNET_TESTING_TOPOLOGY_FROM_FILE
970 };
971
972 /**
973  * Options for connecting a topology.
974  */
975 enum GNUNET_TESTING_TopologyOption
976 {
977   /**
978    * Try to connect all peers specified in the topology.
979    */
980   GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
981
982   /**
983    * Choose a random subset of connections to create.
984    */
985   GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM,
986
987   /**
988    * Create at least X connections for each peer.
989    */
990   GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM,
991
992   /**
993    * Using a depth first search, create one connection
994    * per peer.  If any are missed (graph disconnected)
995    * start over at those peers until all have at least one
996    * connection.
997    */
998   GNUNET_TESTING_TOPOLOGY_OPTION_DFS,
999
1000   /**
1001    * Find the N closest peers to each allowed peer in the
1002    * topology and make sure a connection to those peers
1003    * exists in the connect topology.
1004    */
1005   GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST,
1006
1007   /**
1008    * No options specified.
1009    */
1010   GNUNET_TESTING_TOPOLOGY_OPTION_NONE
1011 };
1012
1013
1014 /**
1015  * Get a topology from a string input.
1016  *
1017  * @param topology where to write the retrieved topology
1018  * @param topology_string The string to attempt to
1019  *        get a configuration value from
1020  * @return GNUNET_YES if topology string matched a
1021  *         known topology, GNUNET_NO if not
1022  */
1023 int
1024 GNUNET_TESTING_topology_get (enum GNUNET_TESTING_Topology *topology,
1025                              const char *topology_string);
1026
1027
1028 /**
1029  * Get connect topology option from string input.
1030  *
1031  * @param topology_option where to write the retrieved topology
1032  * @param topology_string The string to attempt to
1033  *        get a configuration value from
1034  * @return GNUNET_YES if topology string matched a
1035  *         known topology, GNUNET_NO if not
1036  */
1037 int
1038 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption
1039                                     *topology_option,
1040                                     const char *topology_string);
1041
1042
1043 /**
1044  * Takes a peer group and creates a topology based on the
1045  * one specified.  Creates a topology means generates friend
1046  * files for the peers so they can only connect to those allowed
1047  * by the topology.  This will only have an effect once peers
1048  * are started if the FRIENDS_ONLY option is set in the base
1049  * config.
1050  *
1051  * Also takes an optional restrict topology which
1052  * disallows direct connections UNLESS they are specified in
1053  * the restricted topology.
1054  *
1055  * A simple example; if the topology option is set to LINE
1056  * peers can ONLY connect in a LINE.  However, if the topology
1057  * option is set to 2D-torus and the restrict option is set to
1058  * line with restrict_transports equal to "tcp udp", then peers
1059  * may connect in a 2D-torus, but will be restricted to tcp and
1060  * udp connections only in a LINE.  Generally it only makes
1061  * sense to do this if restrict_topology is a subset of topology.
1062  *
1063  * For testing peer discovery, etc. it is generally better to
1064  * leave restrict_topology as GNUNET_TESTING_TOPOLOGY_NONE and
1065  * then use the connect_topology function to restrict the initial
1066  * connection set.
1067  *
1068  * @param pg the peer group struct representing the running peers
1069  * @param topology which topology to connect the peers in
1070  * @param restrict_topology allow only direct connections in this topology,
1071  *        based on those listed in restrict_transports, set to
1072  *        GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
1073  * @param restrict_transports space delimited list of transports to blacklist
1074  *                            to create restricted topology, NULL for none
1075  *
1076  * @return the maximum number of connections were all allowed peers
1077  *         connected to each other
1078  */
1079 unsigned int
1080 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
1081                                 enum GNUNET_TESTING_Topology topology,
1082                                 enum GNUNET_TESTING_Topology restrict_topology,
1083                                 const char *restrict_transports);
1084
1085
1086 /**
1087  * Iterate over all (running) peers in the peer group, retrieve
1088  * all connections that each currently has.
1089  *
1090  * @param pg the peer group we are concerned with
1091  * @param cb callback for topology information
1092  * @param cls closure for callback
1093  */
1094 void
1095 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
1096                              GNUNET_TESTING_NotifyTopology cb, void *cls);
1097
1098
1099 /**
1100  * Stop the connection process temporarily.
1101  *
1102  * @param pg the peer group to stop connecting
1103  */
1104 void
1105 GNUNET_TESTING_stop_connections (struct GNUNET_TESTING_PeerGroup *pg);
1106
1107
1108 /**
1109  * Resume the connection process.
1110  *
1111  * @param pg the peer group to resume connecting
1112  */
1113 void
1114 GNUNET_TESTING_resume_connections (struct GNUNET_TESTING_PeerGroup *pg);
1115
1116
1117 /**
1118  * There are many ways to connect peers that are supported by this function.
1119  * To connect peers in the same topology that was created via the
1120  * GNUNET_TESTING_create_topology, the topology variable must be set to
1121  * GNUNET_TESTING_TOPOLOGY_NONE.  If the topology variable is specified,
1122  * a new instance of that topology will be generated and attempted to be
1123  * connected.  This could result in some connections being impossible,
1124  * because some topologies are non-deterministic.
1125  *
1126  * @param pg the peer group struct representing the running peers
1127  * @param topology which topology to connect the peers in
1128  * @param options options for connecting the topology
1129  * @param option_modifier modifier for options that take a parameter
1130  * @param connect_timeout how long to wait before giving up on connecting
1131  *                        two peers
1132  * @param connect_attempts how many times to attempt to connect two peers
1133  *                         over the connect_timeout duration
1134  * @param notify_callback notification to be called once all connections completed
1135  * @param notify_cls closure for notification callback
1136  *
1137  * @return the number of connections that will be attempted, GNUNET_SYSERR on error
1138  */
1139 int
1140 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
1141                                  enum GNUNET_TESTING_Topology topology,
1142                                  enum GNUNET_TESTING_TopologyOption options,
1143                                  double option_modifier,
1144                                  struct GNUNET_TIME_Relative connect_timeout,
1145                                  unsigned int connect_attempts,
1146                                  GNUNET_TESTING_NotifyCompletion
1147                                  notify_callback, void *notify_cls);
1148
1149
1150 /**
1151  * Start or stop an individual peer from the given group.
1152  *
1153  * @param pg handle to the peer group
1154  * @param offset which peer to start or stop
1155  * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
1156  * @param timeout how long to wait for shutdown
1157  * @param cb function to call at the end
1158  * @param cb_cls closure for cb
1159  */
1160 void
1161 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
1162                              unsigned int offset, int desired_status,
1163                              struct GNUNET_TIME_Relative timeout,
1164                              GNUNET_TESTING_NotifyCompletion cb, void *cb_cls);
1165
1166
1167 /**
1168  * Start a peer group with a given number of peers.  Notify
1169  * on completion of peer startup and connection based on given
1170  * topological constraints.  Optionally notify on each
1171  * established connection.
1172  *
1173  * @param cfg configuration template to use
1174  * @param total number of daemons to start
1175  * @param timeout total time allowed for peers to start
1176  * @param connect_cb function to call each time two daemons are connected
1177  * @param peergroup_cb function to call once all peers are up and connected
1178  * @param peergroup_cls closure for peergroup callbacks
1179  * @param hostnames linked list of host structs to use to start peers on
1180  *                  (NULL to run on localhost only)
1181  *
1182  * @return NULL on error, otherwise handle to control peer group
1183  */
1184 struct GNUNET_TESTING_PeerGroup *
1185 GNUNET_TESTING_peergroup_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1186                                 unsigned int total,
1187                                 struct GNUNET_TIME_Relative timeout,
1188                                 GNUNET_TESTING_NotifyConnection connect_cb,
1189                                 GNUNET_TESTING_NotifyCompletion peergroup_cb,
1190                                 void *peergroup_cls,
1191                                 const struct GNUNET_TESTING_Host *hostnames);
1192
1193
1194 /**
1195  * Print current topology to a graphviz readable file.
1196  *
1197  * @param pg a currently running peergroup to print to file
1198  * @param output_filename the file to write the topology to
1199  * @param notify_cb callback to call upon completion or failure
1200  * @param notify_cb_cls closure for notify_cb
1201  *
1202  */
1203 void
1204 GNUNET_TESTING_peergroup_topology_to_file (struct GNUNET_TESTING_PeerGroup *pg,
1205                                            const char *output_filename,
1206                                            GNUNET_TESTING_NotifyCompletion
1207                                            notify_cb, void *notify_cb_cls);
1208
1209
1210 #if 0                           /* keep Emacsens' auto-indent happy */
1211 {
1212 #endif
1213 #ifdef __cplusplus
1214 }
1215 #endif
1216
1217 #endif