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