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