-minor cleanups in testing
[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 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  * @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 unsigned long long num_peers;
48
49
50 /**
51  * Check whether peers successfully shut down.
52  */
53 void
54 shutdown_callback (void *cls, const char *emsg)
55 {
56   if (emsg != NULL)
57   {
58 #if VERBOSE
59     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
60                 "Shutdown of peers failed (error %s)!\n", emsg);
61 #endif
62     if (ok == 0)
63       ok = 666;
64   }
65   else
66   {
67 #if VERBOSE
68     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
69 #endif
70   }
71 }
72
73
74 static void
75 my_cb (void *cls, const struct GNUNET_PeerIdentity *id,
76        const struct GNUNET_CONFIGURATION_Handle *cfg,
77        struct GNUNET_TESTING_Daemon *d, const char *emsg)
78 {
79   if (emsg != NULL)
80   {
81     peers_failed++;
82   }
83
84   peers_left--;
85   if (peers_left == 0)
86   {
87     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
88     ok = 0;
89   }
90   else if (peers_failed == peers_left)
91   {
92     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
93                 "Too many peers failed, ending test!\n");
94     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
95   }
96 }
97
98
99 static void
100 run (void *cls, char *const *args, const char *cfgfile,
101      const struct GNUNET_CONFIGURATION_Handle *cfg)
102 {
103   struct GNUNET_TESTING_Host *hosts;
104   struct GNUNET_TESTING_Host *hostpos;
105   struct GNUNET_TESTING_Host *temphost;
106   char *hostfile;
107   struct stat frstat;
108   char *buf;
109   char *data;
110   int count;
111   int ret;
112
113   ok = 1;
114 #if VERBOSE
115   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
116 #endif
117
118   if (GNUNET_SYSERR ==
119       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
120                                              &num_peers))
121     num_peers = DEFAULT_NUM_PEERS;
122
123   GNUNET_assert (num_peers > 0 && num_peers < (unsigned long long) -1);
124   if (GNUNET_OK !=
125       GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "hostfile",
126                                              &hostfile))
127     hostfile = NULL;
128
129   hosts = NULL;
130   data = NULL;
131   if (hostfile != NULL)
132   {
133     if (GNUNET_OK != GNUNET_DISK_file_test (hostfile))
134       GNUNET_DISK_fn_write (hostfile, NULL, 0,
135                             GNUNET_DISK_PERM_USER_READ |
136                             GNUNET_DISK_PERM_USER_WRITE);
137     if ((0 != STAT (hostfile, &frstat)) || (frstat.st_size == 0))
138     {
139       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
140                   "Could not open file specified for host list, ending test!");
141       ok = 1119;
142       GNUNET_free (hostfile);
143       return;
144     }
145
146     data = GNUNET_malloc_large (frstat.st_size);
147     GNUNET_assert (data != NULL);
148     if (frstat.st_size != GNUNET_DISK_fn_read (hostfile, data, frstat.st_size))
149     {
150       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
151                   "Could not read file %s specified for host list, ending test!",
152                   hostfile);
153       GNUNET_free (hostfile);
154       GNUNET_free (data);
155       return;
156     }
157
158     GNUNET_free_non_null (hostfile);
159
160     buf = data;
161     count = 0;
162     while (count < frstat.st_size)
163     {
164       count++;
165       if (count >= frstat.st_size)
166         break;
167
168       /* if (((data[count] == '\n') || (data[count] == '\0')) && (buf != &data[count])) */
169       if (((data[count] == '\n')) && (buf != &data[count]))
170       {
171         data[count] = '\0';
172         temphost = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Host));
173         ret =
174             SSCANF (buf, "%a[a-zA-Z0-9]@%a[a-zA-Z0-9.]:%hd",
175                     &temphost->username, &temphost->hostname, &temphost->port);
176         if (3 == ret)
177         {
178           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
179                       "Successfully read host %s, port %d and user %s from file\n",
180                       temphost->hostname, temphost->port, temphost->username);
181         }
182         else
183         {
184           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
185                       "Error reading line `%s' in hostfile\n", buf);
186           GNUNET_free (temphost);
187           buf = &data[count + 1];
188           continue;
189         }
190         /* temphost->hostname = buf; */
191         temphost->next = hosts;
192         hosts = temphost;
193         buf = &data[count + 1];
194       }
195       else if ((data[count] == '\n') || (data[count] == '\0'))
196         buf = &data[count + 1];
197     }
198   }
199
200   peers_left = num_peers;
201   pg = GNUNET_TESTING_daemons_start (cfg, peers_left,   /* Total number of peers */
202                                      peers_left,        /* Number of outstanding connections */
203                                      peers_left,        /* Number of parallel ssh connections, or peers being started at once */
204                                      TIMEOUT, NULL, NULL, &my_cb, NULL, NULL,
205                                      NULL, hosts);
206   hostpos = hosts;
207   while (hostpos != NULL)
208   {
209     temphost = hostpos->next;
210     GNUNET_free (hostpos->hostname);
211     GNUNET_free (hostpos->username);
212     GNUNET_free (hostpos);
213     hostpos = temphost;
214   }
215   GNUNET_free_non_null (data);
216   GNUNET_assert (pg != NULL);
217
218 }
219
220 static int
221 check ()
222 {
223   char *const argv[] = { "test-testing",
224     "-c",
225     "test_testing_data_remote.conf",
226 #if VERBOSE
227     "-L", "DEBUG",
228 #endif
229     NULL
230   };
231   struct GNUNET_GETOPT_CommandLineOption options[] = {
232     GNUNET_GETOPT_OPTION_END
233   };
234   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
235                       "test-testing-group", "nohelp", options, &run, &ok);
236   return ok;
237 }
238
239 int
240 main (int argc, char *argv[])
241 {
242   int ret;
243
244   GNUNET_log_setup ("test-testing-group",
245 #if VERBOSE
246                     "DEBUG",
247 #else
248                     "WARNING",
249 #endif
250                     NULL);
251   ret = check ();
252   /**
253    * Still need to remove the base testing directory here,
254    * because group starts will create subdirectories under this
255    * main dir.  However, we no longer need to sleep, as the
256    * shutdown sequence won't return until everything is cleaned
257    * up.
258    */
259   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
260   return ret;
261 }
262
263 /* end of test_testing_group.c */