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