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