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