786522cddb9e6d1f2e491c676d96665b5eb0a6b7
[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    * HELLO message for this peer
323    */
324   struct GNUNET_HELLO_Message *hello;
325
326   /**
327    * Handle to a pipe for reading the hostkey.
328    */
329   struct GNUNET_DISK_PipeHandle *pipe_stdout;
330
331   /**
332    * Currently, a single char * pointing to a service
333    * that has been churned off.
334    *
335    * FIXME: make this a linked list of services that have been churned off!!!
336    */
337   char *churned_services;
338
339   /**
340    * ID of the current task.
341    */
342   GNUNET_SCHEDULER_TaskIdentifier task;
343
344   /**
345    * Identity of this peer (once started).
346    */
347   struct GNUNET_PeerIdentity id;
348
349   /**
350    * Flag to indicate that we've already been asked
351    * to terminate (but could not because some action
352    * was still pending).
353    */
354   int dead;
355
356   /**
357    * GNUNET_YES if the hostkey has been created
358    * for this peer, GNUNET_NO otherwise.
359    */
360   int have_hostkey;
361
362   /**
363    * In which phase are we during the start of
364    * this process?
365    */
366   enum GNUNET_TESTING_StartPhase phase;
367
368   /**
369    * Current position in 'hostkeybuf' (for reading from gnunet-peerinfo)
370    */
371   unsigned int hostkeybufpos;
372
373   /**
374    * Set to GNUNET_YES once the peer is up.
375    */
376   int running;
377
378   /**
379    * Used to tell shutdown not to remove configuration for the peer
380    * (if it's going to be restarted later)
381    */
382   int churn;
383
384   /**
385    * Output from gnunet-peerinfo is read into this buffer.
386    */
387   char hostkeybuf[105];
388
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 /**
448  * Starts a GNUnet daemon.  GNUnet must be installed on the target
449  * system and available in the PATH.  The machine must furthermore be
450  * reachable via "ssh" (unless the hostname is "NULL") without the
451  * need to enter a password.
452  *
453  * @param cfg configuration to use
454  * @param timeout how long to wait starting up peers
455  * @param pretend GNUNET_YES to set up files but not start peer GNUNET_NO
456  *                to really start the peer (default)
457  * @param hostname name of the machine where to run GNUnet
458  *        (use NULL for localhost).
459  * @param ssh_username ssh username to use when connecting to hostname
460  * @param sshport port to pass to ssh process when connecting to hostname
461  * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
462  * @param hostkey_callback function to call once the hostkey has been
463  *        generated for this peer, but it hasn't yet been started
464  *        (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
465  * @param hostkey_cls closure for hostkey callback
466  * @param cb function to call once peer is up, or failed to start
467  * @param cb_cls closure for cb
468  * @return handle to the daemon (actual start will be completed asynchronously)
469  */
470 struct GNUNET_TESTING_Daemon *
471 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
472                              struct GNUNET_TIME_Relative timeout, int pretend,
473                              const char *hostname, const char *ssh_username,
474                              uint16_t sshport, const char *hostkey,
475                              GNUNET_TESTING_NotifyHostkeyCreated
476                              hostkey_callback, void *hostkey_cls,
477                              GNUNET_TESTING_NotifyDaemonRunning cb,
478                              void *cb_cls);
479
480 /**
481  * Continues GNUnet daemon startup when user wanted to be notified
482  * once a hostkey was generated (for creating friends files, blacklists,
483  * etc.).
484  *
485  * @param daemon the daemon to finish starting
486  */
487 void
488 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon);
489
490
491 /**
492  * Check whether the given daemon is running.
493  *
494  * @param daemon the daemon to check
495  *
496  * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
497  *         daemon is down, GNUNET_SYSERR on error.
498  */
499 int
500 GNUNET_TESTING_daemon_running (struct GNUNET_TESTING_Daemon *daemon);
501
502
503 /**
504  * Restart (stop and start) a GNUnet daemon.
505  *
506  * @param d the daemon that should be restarted
507  * @param cb function called once the daemon is (re)started
508  * @param cb_cls closure for cb
509  */
510 void
511 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
512                                GNUNET_TESTING_NotifyDaemonRunning cb,
513                                void *cb_cls);
514
515
516 /**
517  * Start a peer that has previously been stopped using the daemon_stop
518  * call (and files weren't deleted and the allow restart flag)
519  *
520  * @param daemon the daemon to start (has been previously stopped)
521  * @param timeout how long to wait for restart
522  * @param cb the callback for notification when the peer is running
523  * @param cb_cls closure for the callback
524  */
525 void
526 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 /**
533  * Starts a GNUnet daemon's service.
534  *
535  * @param d the daemon for which the service should be started
536  * @param service the name of the service to start
537  * @param timeout how long to wait for process for startup
538  * @param cb function called once gnunet-arm returns
539  * @param cb_cls closure for cb
540  */
541 void
542 GNUNET_TESTING_daemon_start_service (struct GNUNET_TESTING_Daemon *d,
543                                      char *service,
544                                      struct GNUNET_TIME_Relative timeout,
545                                      GNUNET_TESTING_NotifyDaemonRunning cb,
546                                      void *cb_cls);
547
548
549 /**
550  * Starts a GNUnet daemon's service which has been previously turned off.
551  *
552  * @param d the daemon for which the service should be started
553  * @param service the name of the service to start
554  * @param timeout how long to wait for process for startup
555  * @param cb function called once gnunet-arm returns
556  * @param cb_cls closure for cb
557  */
558 void
559 GNUNET_TESTING_daemon_start_stopped_service (struct GNUNET_TESTING_Daemon *d,
560                                              char *service,
561                                              struct GNUNET_TIME_Relative
562                                              timeout,
563                                              GNUNET_TESTING_NotifyDaemonRunning
564                                              cb, void *cb_cls);
565
566
567 /**
568  * Get a certain testing daemon handle.
569  *
570  * @param pg handle to the set of running peers
571  * @param position the number of the peer to return
572  */
573 struct GNUNET_TESTING_Daemon *
574 GNUNET_TESTING_daemon_get (struct GNUNET_TESTING_PeerGroup *pg,
575                            unsigned int position);
576
577
578 /**
579  * Get a daemon by peer identity, so callers can
580  * retrieve the daemon without knowing it's offset.
581  *
582  * @param pg the peer group to retrieve the daemon from
583  * @param peer_id the peer identity of the daemon to retrieve
584  *
585  * @return the daemon on success, or NULL if no such peer identity is found
586  */
587 struct GNUNET_TESTING_Daemon *
588 GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg,
589                                  struct GNUNET_PeerIdentity *peer_id);
590
591
592 /**
593  * Stops a GNUnet daemon.
594  *
595  * @param d the daemon that should be stopped
596  * @param timeout how long to wait for process for shutdown to complete
597  * @param cb function called once the daemon was stopped
598  * @param cb_cls closure for cb
599  * @param delete_files GNUNET_YES to remove files, GNUNET_NO
600  *        to leave them (i.e. for restarting at a later time,
601  *        or logfile inspection once finished)
602  * @param allow_restart GNUNET_YES to restart peer later (using this API)
603  *        GNUNET_NO to kill off and clean up for good
604  */
605 void
606 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
607                             struct GNUNET_TIME_Relative timeout,
608                             GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
609                             int delete_files, int allow_restart);
610
611
612 /**
613  * Changes the configuration of a GNUnet daemon.
614  *
615  * @param d the daemon that should be modified
616  * @param cfg the new configuration for the daemon
617  * @param cb function called once the configuration was changed
618  * @param cb_cls closure for cb
619  */
620 void
621 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
622                                    struct GNUNET_CONFIGURATION_Handle *cfg,
623                                    GNUNET_TESTING_NotifyCompletion cb,
624                                    void *cb_cls);
625
626
627 /**
628  * Stops a single service of a GNUnet daemon.  Used like daemon_stop,
629  * only doesn't stop the entire peer in any case.  If the service
630  * is not currently running, this call is likely to fail after
631  * timeout!
632  *
633  * @param d the daemon that should be stopped
634  * @param service the name of the service to stop
635  * @param timeout how long to wait for process for shutdown to complete
636  * @param cb function called once the service was stopped
637  * @param cb_cls closure for cb
638  */
639 void
640 GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d,
641                                     char *service,
642                                     struct GNUNET_TIME_Relative timeout,
643                                     GNUNET_TESTING_NotifyCompletion cb,
644                                     void *cb_cls);
645
646
647 /**
648  * Read a testing hosts file based on a configuration.
649  * Returns a DLL of hosts (caller must free!) on success
650  * or NULL on failure.
651  *
652  * @param cfg a configuration with a testing section
653  *
654  * @return DLL of hosts on success, NULL on failure
655  */
656 struct GNUNET_TESTING_Host *
657 GNUNET_TESTING_hosts_load (const struct GNUNET_CONFIGURATION_Handle *cfg);
658
659
660 /**
661  * Start count gnunet instances with the same set of transports and
662  * applications.  The port numbers (any option called "PORT") will be
663  * adjusted to ensure that no two peers running on the same system
664  * have the same port(s) in their respective configurations.
665  *
666  * @param cfg configuration template to use
667  * @param total number of daemons to start
668  * @param max_concurrent_connections for testing, how many peers can
669 *                                   we connect to simultaneously
670  * @param max_concurrent_ssh when starting with ssh, how many ssh
671  *        connections will we allow at once (based on remote hosts allowed!)
672  * @param timeout total time allowed for peers to start
673  * @param hostkey_callback function to call on each peers hostkey generation
674  *        if NULL, peers will be started by this call, if non-null,
675  *        GNUNET_TESTING_daemons_continue_startup must be called after
676  *        successful hostkey generation
677  * @param hostkey_cls closure for hostkey callback
678  * @param cb function to call on each daemon that was started
679  * @param cb_cls closure for cb
680  * @param connect_callback function to call each time two hosts are connected
681  * @param connect_callback_cls closure for connect_callback
682  * @param hostnames linked list of host structs to use to start peers on
683  *                  (NULL to run on localhost only)
684  *
685  * @return NULL on error, otherwise handle to control peer group
686  */
687 struct GNUNET_TESTING_PeerGroup *
688 GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
689                               unsigned int total,
690                               unsigned int max_concurrent_connections,
691                               unsigned int max_concurrent_ssh,
692                               struct GNUNET_TIME_Relative timeout,
693                               GNUNET_TESTING_NotifyHostkeyCreated
694                               hostkey_callback, void *hostkey_cls,
695                               GNUNET_TESTING_NotifyDaemonRunning cb,
696                               void *cb_cls,
697                               GNUNET_TESTING_NotifyConnection connect_callback,
698                               void *connect_callback_cls,
699                               const struct GNUNET_TESTING_Host *hostnames);
700
701
702 /**
703  * Function which continues a peer group starting up
704  * after successfully generating hostkeys for each peer.
705  *
706  * @param pg the peer group to continue starting
707  */
708 void
709 GNUNET_TESTING_daemons_continue_startup (struct GNUNET_TESTING_PeerGroup *pg);
710
711
712 /**
713  * Establish a connection between two GNUnet daemons.
714  *
715  * @param d1 handle for the first daemon
716  * @param d2 handle for the second daemon
717  * @param timeout how long is the connection attempt
718  *        allowed to take?
719  * @param max_connect_attempts how many times should we try to reconnect
720  *        (within timeout)
721  * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
722  *                   the HELLO has already been exchanged
723  * @param cb function to call at the end
724  * @param cb_cls closure for cb
725  */
726 struct GNUNET_TESTING_ConnectContext *
727 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
728                                 struct GNUNET_TESTING_Daemon *d2,
729                                 struct GNUNET_TIME_Relative timeout,
730                                 unsigned int max_connect_attempts,
731                                 int send_hello,
732                                 GNUNET_TESTING_NotifyConnection cb,
733                                 void *cb_cls);
734
735
736 /**
737  * Cancel an attempt to connect two daemons.
738  *
739  * @param cc connect context
740  */
741 void
742 GNUNET_TESTING_daemons_connect_cancel (struct GNUNET_TESTING_ConnectContext
743                                        *cc);
744
745
746 /**
747  * Restart all peers in the given group.
748  *
749  * @param pg the handle to the peer group
750  * @param callback function to call on completion (or failure)
751  * @param callback_cls closure for the callback function
752  */
753 void
754 GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
755                                 GNUNET_TESTING_NotifyCompletion callback,
756                                 void *callback_cls);
757
758
759 /**
760  * Shutdown all peers started in the given group.
761  *
762  * @param pg handle to the peer group
763  * @param timeout how long to wait for shutdown
764  * @param cb callback to notify upon success or failure
765  * @param cb_cls closure for cb
766  */
767 void
768 GNUNET_TESTING_daemons_stop (struct GNUNET_TESTING_PeerGroup *pg,
769                              struct GNUNET_TIME_Relative timeout,
770                              GNUNET_TESTING_NotifyCompletion cb, void *cb_cls);
771
772
773 /**
774  * Count the number of running peers.
775  *
776  * @param pg handle for the peer group
777  *
778  * @return the number of currently running peers in the peer group
779  */
780 unsigned int
781 GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg);
782
783
784 /**
785  * Simulate churn by stopping some peers (and possibly
786  * re-starting others if churn is called multiple times).  This
787  * function can only be used to create leave-join churn (peers "never"
788  * leave for good).  First "voff" random peers that are currently
789  * online will be taken offline; then "von" random peers that are then
790  * offline will be put back online.  No notifications will be
791  * generated for any of these operations except for the callback upon
792  * completion.  Note that the implementation is at liberty to keep
793  * the ARM service itself (but none of the other services or daemons)
794  * running even though the "peer" is being varied offline.
795  *
796  * @param pg handle for the peer group
797  * @param service the service to churn on/off, NULL for all
798  * @param voff number of peers that should go offline
799  * @param von number of peers that should come back online;
800  *            must be zero on first call (since "testbed_start"
801  *            always starts all of the peers)
802  * @param timeout how long to wait for operations to finish before
803  *        giving up
804  * @param cb function to call at the end
805  * @param cb_cls closure for cb
806  */
807 void
808 GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
809                               char *service, unsigned int voff,
810                               unsigned int von,
811                               struct GNUNET_TIME_Relative timeout,
812                               GNUNET_TESTING_NotifyCompletion cb, void *cb_cls);
813
814
815 /**
816  * Start a given service for each of the peers in the peer group.
817  *
818  * @param pg handle for the peer group
819  * @param service the service to start
820  * @param timeout how long to wait for operations to finish before
821  *        giving up
822  * @param cb function to call once finished
823  * @param cb_cls closure for cb
824  *
825  */
826 void
827 GNUNET_TESTING_daemons_start_service (struct GNUNET_TESTING_PeerGroup *pg,
828                                       char *service,
829                                       struct GNUNET_TIME_Relative timeout,
830                                       GNUNET_TESTING_NotifyCompletion cb,
831                                       void *cb_cls);
832
833
834 /**
835  * Callback function to process statistic values.
836  *
837  * @param cls closure
838  * @param peer the peer the statistics belong to
839  * @param subsystem name of subsystem that created the statistic
840  * @param name the name of the datum
841  * @param value the current value
842  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
843  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
844  */
845 typedef int (*GNUNET_TESTING_STATISTICS_Iterator) (void *cls,
846                                                    const struct
847                                                    GNUNET_PeerIdentity * peer,
848                                                    const char *subsystem,
849                                                    const char *name,
850                                                    uint64_t value,
851                                                    int is_persistent);
852
853
854 /**
855  * Iterate over all (running) peers in the peer group, retrieve
856  * all statistics from each.
857  *
858  * @param pg the peergroup to iterate statistics of
859  * @param cont continuation to call once call is completed(?)
860  * @param proc processing function for each statistic retrieved
861  * @param cls closure to pass to proc
862  */
863 void
864 GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
865                                GNUNET_STATISTICS_Callback cont,
866                                GNUNET_TESTING_STATISTICS_Iterator proc,
867                                void *cls);
868
869
870 /**
871  * Topologies supported for testbeds.
872  */
873 enum GNUNET_TESTING_Topology
874 {
875   /**
876    * A clique (everyone connected to everyone else).
877    */
878   GNUNET_TESTING_TOPOLOGY_CLIQUE,
879
880   /**
881    * Small-world network (2d torus plus random links).
882    */
883   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD,
884
885   /**
886    * Small-world network (ring plus random links).
887    */
888   GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING,
889
890   /**
891    * Ring topology.
892    */
893   GNUNET_TESTING_TOPOLOGY_RING,
894
895   /**
896    * 2-d torus.
897    */
898   GNUNET_TESTING_TOPOLOGY_2D_TORUS,
899
900   /**
901    * Random graph.
902    */
903   GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI,
904
905   /**
906    * Certain percentage of peers are unable to communicate directly
907    * replicating NAT conditions
908    */
909   GNUNET_TESTING_TOPOLOGY_INTERNAT,
910
911   /**
912    * Scale free topology.
913    */
914   GNUNET_TESTING_TOPOLOGY_SCALE_FREE,
915
916   /**
917    * Straight line topology.
918    */
919   GNUNET_TESTING_TOPOLOGY_LINE,
920
921   /**
922    * All peers are disconnected.
923    */
924   GNUNET_TESTING_TOPOLOGY_NONE,
925
926   /**
927    * Read a topology from a given file.
928    */
929   GNUNET_TESTING_TOPOLOGY_FROM_FILE
930 };
931
932 /**
933  * Options for connecting a topology.
934  */
935 enum GNUNET_TESTING_TopologyOption
936 {
937   /**
938    * Try to connect all peers specified in the topology.
939    */
940   GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
941
942   /**
943    * Choose a random subset of connections to create.
944    */
945   GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM,
946
947   /**
948    * Create at least X connections for each peer.
949    */
950   GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM,
951
952   /**
953    * Using a depth first search, create one connection
954    * per peer.  If any are missed (graph disconnected)
955    * start over at those peers until all have at least one
956    * connection.
957    */
958   GNUNET_TESTING_TOPOLOGY_OPTION_DFS,
959
960   /**
961    * Find the N closest peers to each allowed peer in the
962    * topology and make sure a connection to those peers
963    * exists in the connect topology.
964    */
965   GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST,
966
967   /**
968    * No options specified.
969    */
970   GNUNET_TESTING_TOPOLOGY_OPTION_NONE
971 };
972
973
974 /**
975  * Get a topology from a string input.
976  *
977  * @param topology where to write the retrieved topology
978  * @param topology_string The string to attempt to
979  *        get a configuration value from
980  * @return GNUNET_YES if topology string matched a
981  *         known topology, GNUNET_NO if not
982  */
983 int
984 GNUNET_TESTING_topology_get (enum GNUNET_TESTING_Topology *topology,
985                              const char *topology_string);
986
987
988 /**
989  * Get connect topology option from string input.
990  *
991  * @param topology_option where to write the retrieved topology
992  * @param topology_string The string to attempt to
993  *        get a configuration value from
994  * @return GNUNET_YES if topology string matched a
995  *         known topology, GNUNET_NO if not
996  */
997 int
998 GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption
999                                     *topology_option,
1000                                     const char *topology_string);
1001
1002
1003 /**
1004  * Takes a peer group and creates a topology based on the
1005  * one specified.  Creates a topology means generates friend
1006  * files for the peers so they can only connect to those allowed
1007  * by the topology.  This will only have an effect once peers
1008  * are started if the FRIENDS_ONLY option is set in the base
1009  * config.
1010  *
1011  * Also takes an optional restrict topology which
1012  * disallows direct connections UNLESS they are specified in
1013  * the restricted topology.
1014  *
1015  * A simple example; if the topology option is set to LINE
1016  * peers can ONLY connect in a LINE.  However, if the topology
1017  * option is set to 2D-torus and the restrict option is set to
1018  * line with restrict_transports equal to "tcp udp", then peers
1019  * may connect in a 2D-torus, but will be restricted to tcp and
1020  * udp connections only in a LINE.  Generally it only makes
1021  * sense to do this if restrict_topology is a subset of topology.
1022  *
1023  * For testing peer discovery, etc. it is generally better to
1024  * leave restrict_topology as GNUNET_TESTING_TOPOLOGY_NONE and
1025  * then use the connect_topology function to restrict the initial
1026  * connection set.
1027  *
1028  * @param pg the peer group struct representing the running peers
1029  * @param topology which topology to connect the peers in
1030  * @param restrict_topology allow only direct connections in this topology,
1031  *        based on those listed in restrict_transports, set to
1032  *        GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
1033  * @param restrict_transports space delimited list of transports to blacklist
1034  *                            to create restricted topology, NULL for none
1035  *
1036  * @return the maximum number of connections were all allowed peers
1037  *         connected to each other
1038  */
1039 unsigned int
1040 GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
1041                                 enum GNUNET_TESTING_Topology topology,
1042                                 enum GNUNET_TESTING_Topology restrict_topology,
1043                                 const char *restrict_transports);
1044
1045
1046 /**
1047  * Iterate over all (running) peers in the peer group, retrieve
1048  * all connections that each currently has.
1049  *
1050  * @param pg the peer group we are concerned with
1051  * @param cb callback for topology information
1052  * @param cls closure for callback
1053  */
1054 void
1055 GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg,
1056                              GNUNET_TESTING_NotifyTopology cb, void *cls);
1057
1058
1059 /**
1060  * Stop the connection process temporarily.
1061  *
1062  * @param pg the peer group to stop connecting
1063  */
1064 void
1065 GNUNET_TESTING_stop_connections (struct GNUNET_TESTING_PeerGroup *pg);
1066
1067
1068 /**
1069  * Resume the connection process.
1070  *
1071  * @param pg the peer group to resume connecting
1072  */
1073 void
1074 GNUNET_TESTING_resume_connections (struct GNUNET_TESTING_PeerGroup *pg);
1075
1076
1077 /**
1078  * There are many ways to connect peers that are supported by this function.
1079  * To connect peers in the same topology that was created via the
1080  * GNUNET_TESTING_create_topology, the topology variable must be set to
1081  * GNUNET_TESTING_TOPOLOGY_NONE.  If the topology variable is specified,
1082  * a new instance of that topology will be generated and attempted to be
1083  * connected.  This could result in some connections being impossible,
1084  * because some topologies are non-deterministic.
1085  *
1086  * @param pg the peer group struct representing the running peers
1087  * @param topology which topology to connect the peers in
1088  * @param options options for connecting the topology
1089  * @param option_modifier modifier for options that take a parameter
1090  * @param connect_timeout how long to wait before giving up on connecting
1091  *                        two peers
1092  * @param connect_attempts how many times to attempt to connect two peers
1093  *                         over the connect_timeout duration
1094  * @param notify_callback notification to be called once all connections completed
1095  * @param notify_cls closure for notification callback
1096  *
1097  * @return the number of connections that will be attempted, GNUNET_SYSERR on error
1098  */
1099 int
1100 GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
1101                                  enum GNUNET_TESTING_Topology topology,
1102                                  enum GNUNET_TESTING_TopologyOption options,
1103                                  double option_modifier,
1104                                  struct GNUNET_TIME_Relative connect_timeout,
1105                                  unsigned int connect_attempts,
1106                                  GNUNET_TESTING_NotifyCompletion
1107                                  notify_callback, void *notify_cls);
1108
1109
1110 /**
1111  * Start or stop an individual peer from the given group.
1112  *
1113  * @param pg handle to the peer group
1114  * @param offset which peer to start or stop
1115  * @param desired_status GNUNET_YES to have it running, GNUNET_NO to stop it
1116  * @param timeout how long to wait for shutdown
1117  * @param cb function to call at the end
1118  * @param cb_cls closure for cb
1119  */
1120 void
1121 GNUNET_TESTING_daemons_vary (struct GNUNET_TESTING_PeerGroup *pg,
1122                              unsigned int offset, int desired_status,
1123                              struct GNUNET_TIME_Relative timeout,
1124                              GNUNET_TESTING_NotifyCompletion cb, void *cb_cls);
1125
1126
1127 /**
1128  * Start a peer group with a given number of peers.  Notify
1129  * on completion of peer startup and connection based on given
1130  * topological constraints.  Optionally notify on each
1131  * established connection.
1132  *
1133  * @param cfg configuration template to use
1134  * @param total number of daemons to start
1135  * @param timeout total time allowed for peers to start
1136  * @param connect_cb function to call each time two daemons are connected
1137  * @param peergroup_cb function to call once all peers are up and connected
1138  * @param peergroup_cls closure for peergroup callbacks
1139  * @param hostnames linked list of host structs to use to start peers on
1140  *                  (NULL to run on localhost only)
1141  *
1142  * @return NULL on error, otherwise handle to control peer group
1143  */
1144 struct GNUNET_TESTING_PeerGroup *
1145 GNUNET_TESTING_peergroup_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1146                                 unsigned int total,
1147                                 struct GNUNET_TIME_Relative timeout,
1148                                 GNUNET_TESTING_NotifyConnection connect_cb,
1149                                 GNUNET_TESTING_NotifyCompletion peergroup_cb,
1150                                 void *peergroup_cls,
1151                                 const struct GNUNET_TESTING_Host *hostnames);
1152
1153
1154 /**
1155  * Print current topology to a graphviz readable file.
1156  *
1157  * @param pg a currently running peergroup to print to file
1158  * @param output_filename the file to write the topology to
1159  * @param notify_cb callback to call upon completion or failure
1160  * @param notify_cb_cls closure for notify_cb
1161  *
1162  */
1163 void
1164 GNUNET_TESTING_peergroup_topology_to_file (struct GNUNET_TESTING_PeerGroup *pg,
1165                                            const char *output_filename,
1166                                            GNUNET_TESTING_NotifyCompletion
1167                                            notify_cb, void *notify_cb_cls);
1168
1169
1170 #if 0                           /* keep Emacsens' auto-indent happy */
1171 {
1172 #endif
1173 #ifdef __cplusplus
1174 }
1175 #endif
1176
1177 #endif