3b294684d0ab65afd439a51489600c930da660f0
[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 serves tests disconnect notification if peer shutsdown
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_YES
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_ERROR, "Stopping peers\n");
93
94   if (send_task != GNUNET_SCHEDULER_NO_TASK)
95     GNUNET_SCHEDULER_cancel (send_task);
96
97   if (die_task != GNUNET_SCHEDULER_NO_TASK)
98     GNUNET_SCHEDULER_cancel (die_task);
99
100   if (th != NULL)
101     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
102   th = NULL;
103
104   if (p1 != NULL)
105     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
106   if (p2 != NULL)
107     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
108
109
110 }
111
112 static void
113 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
114 {
115   die_task = GNUNET_SCHEDULER_NO_TASK;
116
117   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
118
119
120   if (send_task != GNUNET_SCHEDULER_NO_TASK)
121     GNUNET_SCHEDULER_cancel (send_task);
122
123   if (cc != NULL)
124   {
125     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
126     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
127     cc = NULL;
128   }
129
130   if (th != NULL)
131     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
132   th = NULL;
133
134   if (p1 != NULL)
135     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
136   if (p2 != NULL)
137     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
138
139   ok = GNUNET_SYSERR;
140 }
141
142
143 static void
144 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
145                 const struct GNUNET_MessageHeader *message,
146                 const struct GNUNET_ATS_Information *ats,
147                 uint32_t ats_count)
148 {
149   struct PeerContext * p = cls;
150   struct PeerContext *t = NULL;
151
152   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
153     t = p1;
154   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
155     t = p2;
156   GNUNET_assert (t != NULL);
157
158   char * ps = strdup (GNUNET_i2s(&p->id));
159
160   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
162               p->no, ps, ntohs (message->type), ntohs (message->size), t->no, GNUNET_i2s (&t->id));
163
164   if ((MTYPE == ntohs (message->type)) &&
165       (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
166   {
167     ok = 1;
168     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down peer\n");
169     shutdown_ = GNUNET_YES;
170     GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
171     return;
172   }
173 }
174
175
176 static size_t
177 notify_ready (void *cls, size_t size, void *buf)
178 {
179   struct PeerContext *p = cls;
180   struct GNUNET_MessageHeader *hdr;
181
182   th = NULL;
183
184   if (buf == NULL)
185   {
186     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
187                 "Timeout occurred while waiting for transmit_ready\n");
188     if (GNUNET_SCHEDULER_NO_TASK != die_task)
189       GNUNET_SCHEDULER_cancel (die_task);
190     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
191     ok = 42;
192     return 0;
193   }
194
195   GNUNET_assert (size >= 256);
196
197   if (buf != NULL)
198   {
199     hdr = buf;
200     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
201     hdr->type = htons (MTYPE);
202   }
203   char * ps = strdup (GNUNET_i2s(&p2->id));
204   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205               "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
206               p2->no,
207               ps,
208               ntohs (hdr->type),
209               ntohs (hdr->size), p->no, GNUNET_i2s (&p->id));
210   GNUNET_free (ps);
211   return sizeof (struct GNUNET_MessageHeader);
212 }
213
214
215 static void
216 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
217 {
218   send_task = GNUNET_SCHEDULER_NO_TASK;
219
220   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
221     return;
222   char * receiver_s = strdup(GNUNET_i2s (&p1->id));
223   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
225               p2->no,
226               GNUNET_i2s (&p2->id), p1->no, receiver_s);
227   GNUNET_free (receiver_s);
228
229   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256, 0,
230                                                TIMEOUT_TRANSMIT, &notify_ready,
231                                                p1);
232 }
233
234
235 static void
236 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
237                 const struct GNUNET_ATS_Information *ats,
238                 uint32_t ats_count)
239 {
240   static int c;
241   c++;
242   struct PeerContext *p = cls;
243   struct PeerContext *t = NULL;
244
245   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
246     t = p1;
247   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
248     t = p2;
249   GNUNET_assert (t!=NULL);
250
251   char * ps = strdup (GNUNET_i2s(&p->id));
252   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
253               p->no, ps,
254               t->no, GNUNET_i2s (peer));
255   GNUNET_free (ps);
256 }
257
258
259 static void
260 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
261 {
262   struct PeerContext *p = cls;
263   char * ps = strdup (GNUNET_i2s(&p->id));
264   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer %u (`%4s'): peer (`%s') disconnected from me!\n",
265               p->no, ps, GNUNET_i2s (peer));
266
267   if (th != NULL)
268     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
269   th = NULL;
270
271   if (shutdown_ == GNUNET_YES)
272   {
273   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Complete, shutting down...\n");
274   GNUNET_SCHEDULER_add_now (&end, NULL);
275   }
276 }
277
278 static void
279 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
280 {
281   cc = NULL;
282   char *p1_c = strdup (GNUNET_i2s (&p1->id));
283
284   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
285               p1->no, p1_c,
286               p2->no, GNUNET_i2s (&p2->id));
287   GNUNET_free (p1_c);
288
289   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
290 }
291
292
293
294 void start_cb (struct PeerContext * p,
295                void *cls)
296 {
297   static int started;
298   started++;
299
300   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n",
301        p->no,
302        GNUNET_i2s (&p->id));
303
304   if (started != 2)
305     return;
306
307   char *sender_c = strdup (GNUNET_i2s (&p1->id));
308   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
309               p1->no, sender_c,
310               p2->no, GNUNET_i2s (&p2->id));
311
312   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
313                                                NULL);
314
315 }
316
317 static void
318 run (void *cls, char *const *args, const char *cfgfile,
319      const struct GNUNET_CONFIGURATION_Handle *cfg)
320 {
321   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
322
323   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
324       &notify_receive,
325       &notify_connect,
326       &notify_disconnect,
327       &start_cb,
328       NULL);
329
330   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
331                                             &notify_receive,
332                                             &notify_connect,
333                                             &notify_disconnect,
334                                             &start_cb,
335                                             NULL);
336
337   if ((p1 == NULL) || (p2 == NULL))
338   {
339     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
340     if (die_task != GNUNET_SCHEDULER_NO_TASK)
341       GNUNET_SCHEDULER_cancel (die_task);
342     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
343     return;
344   }
345 }
346
347
348 static int
349 check ()
350 {
351   static char *const argv[] = { "test-transport-api",
352     "-c",
353     "test_transport_api_data.conf",
354 #if VERBOSE
355     "-L", "DEBUG",
356 #endif
357     NULL
358   };
359   static struct GNUNET_GETOPT_CommandLineOption options[] = {
360     GNUNET_GETOPT_OPTION_END
361   };
362
363 #if WRITECONFIG
364   setTransportOptions ("test_transport_api_data.conf");
365 #endif
366   send_task = GNUNET_SCHEDULER_NO_TASK;
367
368   ok = 1;
369   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
370                       "nohelp", options, &run, &ok);
371
372   return ok;
373 }
374
375 int
376 main (int argc, char *argv[])
377 {
378   int ret;
379   int nat_res;
380
381   tth = GNUNET_TRANSPORT_TESTING_init ();
382
383   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
384   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
385                                                  &test_plugin);
386   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
387
388   GNUNET_log_setup (test_name,
389 #if VERBOSE
390                     "DEBUG",
391 #else
392                     "WARNING",
393 #endif
394                     NULL);
395
396   if ((strcmp (test_plugin, "tcp_nat") == 0) ||
397       (strcmp (test_plugin, "udp_nat") == 0))
398   {
399     nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
400     if (GNUNET_NO == nat_res)
401     {
402       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
403                   "gnunet-nat-server", "SUID not set");
404       return 0;
405     }
406     if (GNUNET_SYSERR == nat_res)
407     {
408       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
409                   "gnunet-nat-server", "file not found");
410       return 0;
411     }
412   }
413
414   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
415   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
416
417   ret = check ();
418
419   GNUNET_free (cfg_file_p1);
420   GNUNET_free (cfg_file_p2);
421
422   GNUNET_free (test_source);
423   GNUNET_free (test_plugin);
424   GNUNET_free (test_name);
425
426   GNUNET_TRANSPORT_TESTING_done (tth);
427
428   return ret;
429 }
430
431 /* end of test_transport_api.c */