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