change to configs, use readable names for topologies and connect options instead...
[oweals/gnunet.git] / src / testing / test_testing_group.c
1 /*
2      This file is part of GNUnet.
3      (C) 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 2, 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  * @file testing/test_testing_group.c
22  * @brief testcase for functions to connect peers in testing.c
23  */
24 #include "platform.h"
25 #include "gnunet_testing_lib.h"
26
27 #define VERBOSE GNUNET_YES
28
29 #define NUM_PEERS 4
30
31 /**
32  * How long until we give up on connecting the peers?
33  */
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
35
36 static int ok;
37
38 static int peers_left;
39
40 static int failed_peers;
41
42 static struct GNUNET_TESTING_PeerGroup *pg;
43
44 static struct GNUNET_SCHEDULER_Handle *sched;
45
46
47 static void
48 my_cb (void *cls,
49        const struct GNUNET_PeerIdentity *id,
50        const struct GNUNET_CONFIGURATION_Handle *cfg,
51        struct GNUNET_TESTING_Daemon *d, const char *emsg)
52 {
53   if (id == NULL)
54     {
55       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start callback called with error (too long starting peers), aborting test!\n");
56       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
57       failed_peers++;
58       if (failed_peers == peers_left)
59         {
60           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Too many peers failed, ending test!\n");
61           ok = 1;
62           GNUNET_TESTING_daemons_stop (pg, TIMEOUT);
63         }
64       return;
65     }
66
67   peers_left--;
68   if (peers_left == 0)
69     {
70       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers started successfully, ending test!\n");
71       GNUNET_TESTING_daemons_stop (pg, TIMEOUT);
72       ok = 0;
73     }
74   else if (failed_peers == peers_left)
75     {
76       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Too many peers failed, ending test!\n");
77       ok = 1;
78       GNUNET_TESTING_daemons_stop (pg, TIMEOUT);
79     }
80 }
81
82
83 static void
84 run (void *cls,
85      struct GNUNET_SCHEDULER_Handle *s,
86      char *const *args,
87      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
88 {
89   sched = s;
90   ok = 1;
91 #if VERBOSE
92   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
93 #endif
94   peers_left = NUM_PEERS;
95   pg = GNUNET_TESTING_daemons_start (sched, cfg,
96                                      peers_left,
97                                      TIMEOUT,
98                                      NULL, NULL,
99                                      &my_cb, NULL, NULL, NULL, NULL);
100   GNUNET_assert (pg != NULL);
101 }
102
103 static int
104 check ()
105 {
106   char *const argv[] = { "test-testing",
107     "-c",
108     "test_testing_data.conf",
109 #if VERBOSE
110     "-L", "DEBUG",
111 #endif
112     NULL
113   };
114   struct GNUNET_GETOPT_CommandLineOption options[] = {
115     GNUNET_GETOPT_OPTION_END
116   };
117   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
118                       argv, "test-testing-group", "nohelp",
119                       options, &run, &ok);
120   return ok;
121 }
122
123 int
124 main (int argc, char *argv[])
125 {
126   int ret;
127
128   GNUNET_log_setup ("test-testing-group",
129 #if VERBOSE
130                     "DEBUG",
131 #else
132                     "WARNING",
133 #endif
134                     NULL);
135   ret = check ();
136   /**
137    * Still need to remove the base testing directory here,
138    * because group starts will create subdirectories under this
139    * main dir.  However, we no longer need to sleep, as the
140    * shutdown sequence won't return until everything is cleaned
141    * up.
142    */
143   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
144   return ret;
145 }
146
147 /* end of test_testing_group.c */