codesonar fixes
[oweals/gnunet.git] / src / testing / test_testing_topology_churn.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_topology_churn.c
22  * @brief base testcase for testing simple churn functionality
23  */
24 #include "platform.h"
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_core_service.h"
27
28 #define VERBOSE GNUNET_YES
29
30 /**
31  * How long until we fail the whole testcase?
32  */
33 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
34
35 /**
36  * How long until we give up on starting the peers? (Must be longer than the connect timeout!)
37  */
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
39
40 #define DEFAULT_NUM_PEERS 4
41
42 static int ok;
43
44 static unsigned long long num_peers;
45
46 static unsigned int expected_connections;
47
48 static unsigned int expected_failed_connections;
49
50 static unsigned long long peers_left;
51
52 static struct GNUNET_TESTING_PeerGroup *pg;
53
54 const struct GNUNET_CONFIGURATION_Handle *main_cfg;
55
56 GNUNET_SCHEDULER_TaskIdentifier die_task;
57
58 static char *test_directory;
59
60 #define MTYPE 12345
61
62 struct GNUNET_TestMessage
63 {
64   /**
65    * Header of the message
66    */
67   struct GNUNET_MessageHeader header;
68
69   /**
70    * Unique identifier for this message.
71    */
72   uint32_t uid;
73 };
74
75 /**
76  * Check whether peers successfully shut down.
77  */
78 void shutdown_callback (void *cls,
79                         const char *emsg)
80 {
81   if (emsg != NULL)
82     {
83 #if VERBOSE
84       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85                   "Shutdown of peers failed!\n");
86 #endif
87       if (ok == 0)
88         ok = 666;
89     }
90   else
91     {
92 #if VERBOSE
93       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94                   "All peers successfully shut down!\n");
95 #endif
96     }
97 }
98
99 static void
100 finish_testing ()
101 {
102   GNUNET_assert (pg != NULL);
103
104   if (die_task != GNUNET_SCHEDULER_NO_TASK)
105     GNUNET_SCHEDULER_cancel(die_task);
106
107 #if VERBOSE
108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109               "Called finish testing, stopping daemons.\n");
110 #endif
111
112 #if VERBOSE
113           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
114                       "Calling daemons_stop\n");
115 #endif
116   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
117 #if VERBOSE
118           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
119                       "daemons_stop finished\n");
120 #endif
121
122   ok = 0;
123 }
124
125 static void
126 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
127 {
128   char *msg = cls;
129   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
130               "End badly was called (%s)... stopping daemons.\n", msg);
131
132   if (pg != NULL)
133     {
134       GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
135       ok = 7331;                /* Opposite of leet */
136     }
137   else
138     ok = 401;                   /* Never got peers started */
139
140 }
141
142 struct ChurnTestContext
143 {
144   GNUNET_SCHEDULER_Task next_task;
145
146 };
147
148 static struct ChurnTestContext churn_ctx;
149
150 /**
151  * Churn callback, report on success or failure of churn operation.
152  *
153  * @param cls closure
154  * @param emsg NULL on success
155  */
156 void churn_callback(void *cls,
157                     const char *emsg)
158 {
159   if (emsg == NULL)
160     {
161       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Successfully churned peers!\n", emsg);
162       GNUNET_SCHEDULER_add_now(churn_ctx.next_task, NULL);
163     }
164   else
165     {
166       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to churn peers with error `%s'\n", emsg);
167       GNUNET_SCHEDULER_cancel(die_task);
168       die_task = GNUNET_SCHEDULER_add_now(&end_badly, NULL);
169     }
170 }
171
172
173 static void
174 churn_peers_both()
175 {
176   churn_ctx.next_task = &finish_testing;
177   GNUNET_TESTING_daemons_churn(pg, 1, 1, TIMEOUT, &churn_callback, NULL);
178 }
179
180 static void
181 churn_peers_off_again()
182 {
183   churn_ctx.next_task = &churn_peers_both;
184   GNUNET_TESTING_daemons_churn(pg, 2, 0, TIMEOUT, &churn_callback, NULL);
185 }
186
187 static void
188 churn_peers_on()
189 {
190   churn_ctx.next_task = &churn_peers_off_again;
191   GNUNET_TESTING_daemons_churn(pg, 0, 2, TIMEOUT, &churn_callback, NULL);
192 }
193
194 static void
195 churn_peers_off()
196 {
197   churn_ctx.next_task = &churn_peers_on;
198   GNUNET_TESTING_daemons_churn(pg, 2, 0, TIMEOUT, &churn_callback, NULL);
199 }
200
201 static void
202 peers_started_callback (void *cls,
203        const struct GNUNET_PeerIdentity *id,
204        const struct GNUNET_CONFIGURATION_Handle *cfg,
205        struct GNUNET_TESTING_Daemon *d, const char *emsg)
206 {
207   if (emsg != NULL)
208     {
209       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to start daemon with error: `%s'\n",
210                   emsg);
211       return;
212     }
213   GNUNET_assert (id != NULL);
214 #if VERBOSE
215   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
216               (num_peers - peers_left) + 1, num_peers);
217 #endif
218   peers_left--;
219   if (peers_left == 0)
220     {
221 #if VERBOSE
222       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223                   "All %d daemons started, now testing churn!\n",
224                   num_peers);
225 #endif
226       GNUNET_SCHEDULER_cancel (die_task);
227       /* Set up task in case topology creation doesn't finish
228        * within a reasonable amount of time */
229       die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
230                                                (GNUNET_TIME_UNIT_MINUTES, 5),
231                                                &end_badly, "from peers_started_callback");
232       churn_peers_off ();
233       ok = 0;
234     }
235 }
236
237
238 static void
239 run (void *cls,
240      char *const *args,
241      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
242 {
243   ok = 1;
244
245 #if VERBOSE
246   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247               "Starting daemons based on config file %s\n", cfgfile);
248 #endif
249
250   if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory))
251     {
252       ok = 404;
253       return;
254     }
255
256   if (GNUNET_SYSERR ==
257       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
258                                              &num_peers))
259     num_peers = DEFAULT_NUM_PEERS;
260
261   main_cfg = cfg;
262
263   peers_left = num_peers;
264   GNUNET_assert(num_peers > 0 && num_peers < (unsigned int)-1);
265
266   /* For this specific test we only really want a CLIQUE topology as the
267    * overlay allowed topology, and a RING topology as the underlying connection
268    * allowed topology.  So we will expect only num_peers * 2 connections to
269    * work, and (num_peers * (num_peers - 1)) - (num_peers * 2) to fail.
270    */
271   expected_connections = num_peers * (num_peers - 1);
272   expected_failed_connections = expected_connections - (num_peers * 2);
273
274
275   /* Set up a task to end testing if peer start fails */
276   die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
277                                            (GNUNET_TIME_UNIT_MINUTES, 5),
278                                            &end_badly, "didn't start all daemons in reasonable amount of time!!!");
279
280   pg = GNUNET_TESTING_daemons_start (cfg,
281                                      peers_left, TIMEOUT, NULL, NULL, &peers_started_callback, NULL,
282                                      NULL, NULL, NULL);
283
284 }
285
286 static int
287 check ()
288 {
289   int ret;
290   char *const argv[] = {"test-testing-topology-churn",
291     "-c",
292     "test_testing_data_topology_churn.conf",
293 #if VERBOSE
294     "-L", "DEBUG",
295 #endif
296     NULL
297   };
298   struct GNUNET_GETOPT_CommandLineOption options[] = {
299     GNUNET_GETOPT_OPTION_END
300   };
301   ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
302                       argv, "test-testing-topology-churn", "nohelp",
303                       options, &run, &ok);
304   if (ret != GNUNET_OK)
305     {
306       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`test-testing-topology-churn': Failed with error code %d\n", ret);
307     }
308
309   return ok;
310 }
311
312 int
313 main (int argc, char *argv[])
314 {
315   int ret;
316
317   GNUNET_log_setup ("test_testing_topology_churn",
318 #if VERBOSE
319                     "DEBUG",
320 #else
321                     "WARNING",
322 #endif
323                     NULL);
324   ret = check ();
325
326   /**
327    * Need to remove base directory, subdirectories taken care
328    * of by the testing framework.
329    */
330   if (test_directory != NULL)
331     {
332       if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
333         {
334           GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to remove testing directory %s\n", test_directory);
335         }
336     }
337
338   return ret;
339 }
340
341 /* end of test_testing_topology_churn.c */