complete state reset functionality
[oweals/gnunet.git] / src / transport / test_transport_api_disconnect.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 transport/test_transport_api_disconnect.c
22  * @brief base test case for transport implementations
23  *
24  * This test case tests disconnect notifications in peer shutdown
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_scheduler_lib.h"
33 #include "gnunet_transport_service.h"
34 #include "transport.h"
35 #include "transport-testing.h"
36
37 #define VERBOSE GNUNET_EXTRA_LOGGING
38 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
39
40 #define START_ARM GNUNET_YES
41
42 /**
43  * How long until we give up on transmitting the message?
44  */
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
46
47 /**
48  * How long until we give up on transmitting the message?
49  */
50 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
51
52 #define MTYPE 12345
53
54 static char *test_source;
55
56 static char *test_plugin;
57
58 static char *test_name;
59
60 static int ok;
61
62 static GNUNET_SCHEDULER_TaskIdentifier die_task;
63
64 static GNUNET_SCHEDULER_TaskIdentifier send_task;
65
66 struct PeerContext *p1;
67
68 struct PeerContext *p2;
69
70 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
71
72 struct GNUNET_TRANSPORT_TransmitHandle *th;
73
74 struct GNUNET_TRANSPORT_TESTING_handle * tth;
75
76 int shutdown_;
77
78 char *cfg_file_p1;
79
80 char *cfg_file_p2;
81
82 #if VERBOSE
83 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
84 #else
85 #define OKPP do { ok++; } while (0)
86 #endif
87
88
89 static void
90 end ()
91 {
92   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
93
94   if (send_task != GNUNET_SCHEDULER_NO_TASK)
95   {
96     GNUNET_SCHEDULER_cancel (send_task);
97     send_task = GNUNET_SCHEDULER_NO_TASK;
98   }
99
100   if (die_task != GNUNET_SCHEDULER_NO_TASK)
101   {
102     GNUNET_SCHEDULER_cancel (die_task);
103     die_task = GNUNET_SCHEDULER_NO_TASK;
104   }
105
106   if (th != NULL)
107     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
108   th = NULL;
109
110   if (p1 != NULL)
111     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
112   p1 = NULL;
113
114   if (p2 != NULL)
115     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
116   p2 = NULL;
117
118   /* success */
119   ok = 0;
120 }
121
122 static void
123 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
124 {
125   die_task = GNUNET_SCHEDULER_NO_TASK;
126
127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
128
129   if (send_task != GNUNET_SCHEDULER_NO_TASK)
130   {
131     GNUNET_SCHEDULER_cancel (send_task);
132     send_task = GNUNET_SCHEDULER_NO_TASK;
133   }
134
135   if (th != NULL)
136     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
137   th = NULL;
138
139   if (p1 != NULL)
140     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
141   if (p2 != NULL)
142     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
143
144   ok = GNUNET_SYSERR;
145 }
146
147 static void
148 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
149 {
150   struct PeerContext *p = cls;
151   char * ps = strdup (GNUNET_i2s(&p->id));
152   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s'): peer (`%s') disconnected from me!\n",
153               p->no, ps, GNUNET_i2s (peer));
154
155   if (th != NULL)
156     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
157   th = NULL;
158
159   if (shutdown_ == GNUNET_YES)
160   {
161   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Complete, shutting down...\n");
162   GNUNET_SCHEDULER_add_now (&end, NULL);
163   }
164 }
165
166
167 static void
168 stop_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
169 {
170   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
171     return;
172
173   struct PeerContext * p = cls;
174   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down peer %u (`%s')\n",
175       p->no,
176       GNUNET_i2s (&p->id));
177   shutdown_ = GNUNET_YES;
178   GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
179   p2 = NULL;
180   GNUNET_assert (p2 == NULL);
181 }
182
183 static void
184 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
185                 const struct GNUNET_MessageHeader *message,
186                 const struct GNUNET_ATS_Information *ats,
187                 uint32_t ats_count)
188 {
189   struct PeerContext * p = cls;
190   struct PeerContext *t = NULL;
191
192   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
193     t = p1;
194   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
195     t = p2;
196   GNUNET_assert (t != NULL);
197
198   char * ps = strdup (GNUNET_i2s(&p->id));
199
200   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
201               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
202               p->no, ps, ntohs (message->type), ntohs (message->size), t->no, GNUNET_i2s (&t->id));
203
204   if ((MTYPE == ntohs (message->type)) &&
205       (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
206   {
207     ok = 1;
208     GNUNET_SCHEDULER_add_now(stop_peer, p2);
209     return;
210   }
211 }
212
213
214 static size_t
215 notify_ready (void *cls, size_t size, void *buf)
216 {
217   struct PeerContext *p = cls;
218   struct GNUNET_MessageHeader *hdr;
219
220   th = NULL;
221
222   if (buf == NULL)
223   {
224     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
225                 "Timeout occurred while waiting for transmit_ready\n");
226     if (GNUNET_SCHEDULER_NO_TASK != die_task)
227       GNUNET_SCHEDULER_cancel (die_task);
228     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
229     ok = 42;
230     return 0;
231   }
232
233   GNUNET_assert (size >= 256);
234
235   if (buf != NULL)
236   {
237     hdr = buf;
238     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
239     hdr->type = htons (MTYPE);
240   }
241   char * ps = strdup (GNUNET_i2s(&p2->id));
242   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243               "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
244               p2->no,
245               ps,
246               ntohs (hdr->type),
247               ntohs (hdr->size), p->no, GNUNET_i2s (&p->id));
248   GNUNET_free (ps);
249   return sizeof (struct GNUNET_MessageHeader);
250 }
251
252
253 static void
254 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
255 {
256   send_task = GNUNET_SCHEDULER_NO_TASK;
257
258   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
259     return;
260   char * receiver_s = strdup(GNUNET_i2s (&p1->id));
261   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
263               p2->no,
264               GNUNET_i2s (&p2->id), p1->no, receiver_s);
265   GNUNET_free (receiver_s);
266
267   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256, 0,
268                                                TIMEOUT_TRANSMIT, &notify_ready,
269                                                p1);
270 }
271
272
273 static void
274 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
275                 const struct GNUNET_ATS_Information *ats,
276                 uint32_t ats_count)
277 {
278   static int c;
279   c++;
280   struct PeerContext *p = cls;
281   struct PeerContext *t = NULL;
282
283   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
284     t = p1;
285   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
286     t = p2;
287   GNUNET_assert (t!=NULL);
288
289   char * ps = strdup (GNUNET_i2s(&p->id));
290   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
291               p->no, ps,
292               t->no, GNUNET_i2s (peer));
293   GNUNET_free (ps);
294 }
295
296
297 static void
298 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
299 {
300   cc = NULL;
301   char *p1_c = strdup (GNUNET_i2s (&p1->id));
302
303   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
304               p1->no, p1_c,
305               p2->no, GNUNET_i2s (&p2->id));
306   GNUNET_free (p1_c);
307
308   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
309 }
310
311
312
313 void start_cb (struct PeerContext * p,
314                void *cls)
315 {
316   static int started;
317   started++;
318
319   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n",
320        p->no,
321        GNUNET_i2s (&p->id));
322
323   if (started != 2)
324     return;
325
326   char *sender_c = strdup (GNUNET_i2s (&p1->id));
327   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
328               p1->no, sender_c,
329               p2->no, GNUNET_i2s (&p2->id));
330
331   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
332                                                NULL);
333
334 }
335
336 static void
337 run (void *cls, char *const *args, const char *cfgfile,
338      const struct GNUNET_CONFIGURATION_Handle *cfg)
339 {
340   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
341
342   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
343       &notify_receive,
344       &notify_connect,
345       &notify_disconnect,
346       &start_cb,
347       NULL);
348
349   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
350                                             &notify_receive,
351                                             &notify_connect,
352                                             &notify_disconnect,
353                                             &start_cb,
354                                             NULL);
355
356   if ((p1 == NULL) || (p2 == NULL))
357   {
358     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
359     if (die_task != GNUNET_SCHEDULER_NO_TASK)
360       GNUNET_SCHEDULER_cancel (die_task);
361     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
362     return;
363   }
364 }
365
366
367 static int
368 check ()
369 {
370   static char *const argv[] = { "test-transport-api",
371     "-c",
372     "test_transport_api_data.conf",
373 #if VERBOSE
374     "-L", "DEBUG",
375 #endif
376     NULL
377   };
378   static struct GNUNET_GETOPT_CommandLineOption options[] = {
379     GNUNET_GETOPT_OPTION_END
380   };
381
382 #if WRITECONFIG
383   setTransportOptions ("test_transport_api_data.conf");
384 #endif
385   send_task = GNUNET_SCHEDULER_NO_TASK;
386
387   ok = 1;
388   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
389                       "nohelp", options, &run, &ok);
390
391   return ok;
392 }
393
394 int
395 main (int argc, char *argv[])
396 {
397   int ret;
398   int nat_res;
399
400   tth = GNUNET_TRANSPORT_TESTING_init ();
401
402   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
403   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
404                                                  &test_plugin);
405   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
406
407   GNUNET_log_setup (test_name,
408 #if VERBOSE
409                     "DEBUG",
410 #else
411                     "WARNING",
412 #endif
413                     NULL);
414
415   if ((strcmp (test_plugin, "tcp_nat") == 0) ||
416       (strcmp (test_plugin, "udp_nat") == 0))
417   {
418     nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
419     if (GNUNET_NO == nat_res)
420     {
421       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
422                   "gnunet-nat-server", "SUID not set");
423       return 0;
424     }
425     if (GNUNET_SYSERR == nat_res)
426     {
427       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
428                   "gnunet-nat-server", "file not found");
429       return 0;
430     }
431   }
432
433   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
434   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
435
436   ret = check ();
437
438   GNUNET_free (cfg_file_p1);
439   GNUNET_free (cfg_file_p2);
440
441   GNUNET_free (test_source);
442   GNUNET_free (test_plugin);
443   GNUNET_free (test_name);
444
445   GNUNET_TRANSPORT_TESTING_done (tth);
446
447   return ret;
448 }
449
450 /* end of test_transport_api.c */