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