small API change: do no longer pass rarely needed GNUNET_SCHEDULER_TaskContext to...
[oweals/gnunet.git] / src / transport / test_transport_api_disconnect.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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_transport_service.h"
28 #include "transport-testing.h"
29
30 /**
31  * How long until we give up on transmitting the message?
32  */
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
34
35 /**
36  * How long until we give up on transmitting the message?
37  */
38 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
39
40 #define MTYPE 12345
41
42 static char *test_source;
43
44 static char *test_plugin;
45
46 static char *test_name;
47
48 static int ok;
49
50 static struct GNUNET_SCHEDULER_Task * die_task;
51
52 static struct GNUNET_SCHEDULER_Task * send_task;
53
54 struct PeerContext *p1;
55
56 struct PeerContext *p2;
57
58 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
59
60 struct GNUNET_TRANSPORT_TransmitHandle *th;
61
62 struct GNUNET_TRANSPORT_TESTING_handle *tth;
63
64 int shutdown_;
65
66 char *cfg_file_p1;
67
68 char *cfg_file_p2;
69
70 #if VERBOSE
71 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
72 #else
73 #define OKPP do { ok++; } while (0)
74 #endif
75
76
77 static void
78 end ()
79 {
80   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
81
82   if (send_task != NULL)
83   {
84     GNUNET_SCHEDULER_cancel (send_task);
85     send_task = NULL;
86   }
87
88   if (die_task != NULL)
89   {
90     GNUNET_SCHEDULER_cancel (die_task);
91     die_task = NULL;
92   }
93
94   if (th != NULL)
95     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
96   th = NULL;
97
98   if (p1 != NULL)
99     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
100   p1 = NULL;
101
102   if (p2 != NULL)
103     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
104   p2 = NULL;
105
106   /* success */
107   ok = 0;
108 }
109
110
111 static void
112 end_badly (void *cls)
113 {
114   die_task = NULL;
115
116   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
117
118   if (cc != NULL)
119   {
120     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
121     cc = NULL;
122   }
123
124   if (send_task != NULL)
125   {
126     GNUNET_SCHEDULER_cancel (send_task);
127     send_task = 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 static void
143 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
144 {
145   struct PeerContext *p = cls;
146   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
147
148   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
150               GNUNET_i2s (peer));
151   GNUNET_free (ps);
152
153   if (th != NULL)
154     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
155   th = NULL;
156
157   if (shutdown_ == GNUNET_YES)
158   {
159     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Complete, shutting down...\n");
160     GNUNET_SCHEDULER_add_now (&end, NULL);
161   }
162 }
163
164
165 static void
166 stop_peer (void *cls)
167 {
168   const struct GNUNET_SCHEDULER_TaskContext *tc;
169
170   tc = GNUNET_SCHEDULER_get_task_context ();
171   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
172     return;
173
174   struct PeerContext *p = cls;
175
176   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down peer %u (`%s')\n", p->no,
177               GNUNET_i2s (&p->id));
178   shutdown_ = GNUNET_YES;
179   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
180   p2 = NULL;
181   GNUNET_assert (p2 == NULL);
182 }
183
184
185 static void
186 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
187                 const struct GNUNET_MessageHeader *message)
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 = GNUNET_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,
203               GNUNET_i2s (&t->id));
204   GNUNET_free (ps);
205
206   if ((MTYPE == ntohs (message->type)) &&
207       (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
208   {
209     ok = 1;
210     GNUNET_SCHEDULER_add_now (stop_peer, p2);
211     return;
212   }
213 }
214
215
216 static size_t
217 notify_ready (void *cls, size_t size, void *buf)
218 {
219   struct PeerContext *p = cls;
220   struct GNUNET_MessageHeader *hdr;
221
222   th = NULL;
223
224   if (buf == NULL)
225   {
226     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
227                 "Timeout occurred while waiting for transmit_ready\n");
228     if (NULL != die_task)
229       GNUNET_SCHEDULER_cancel (die_task);
230     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
231     ok = 42;
232     return 0;
233   }
234
235   GNUNET_assert (size >= 256);
236
237   if (buf != NULL)
238   {
239     hdr = buf;
240     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
241     hdr->type = htons (MTYPE);
242   }
243   char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
244
245   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
246               "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
247               p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
248               GNUNET_i2s (&p->id));
249   GNUNET_free (ps);
250   return sizeof (struct GNUNET_MessageHeader);
251 }
252
253
254 static void
255 sendtask (void *cls)
256 {
257   const struct GNUNET_SCHEDULER_TaskContext *tc;
258
259   send_task = NULL;
260   tc = GNUNET_SCHEDULER_get_task_context ();
261   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
262     return;
263   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
264
265   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
267               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
268   GNUNET_free (receiver_s);
269
270   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256,
271                                                TIMEOUT_TRANSMIT, &notify_ready,
272                                                p1);
273 }
274
275
276 static void
277 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
278 {
279   static int c;
280
281   c++;
282   struct PeerContext *p = cls;
283   struct PeerContext *t = NULL;
284
285   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
286     t = p1;
287   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
288     t = p2;
289   GNUNET_assert (t != NULL);
290
291   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
292
293   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
295               t->no, GNUNET_i2s (peer));
296   GNUNET_free (ps);
297 }
298
299
300 static void
301 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
302 {
303   cc = NULL;
304   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
305
306   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
307               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
308   GNUNET_free (p1_c);
309
310   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
311 }
312
313
314 static void
315 start_cb (struct PeerContext *p, void *cls)
316 {
317   static int started;
318
319   started++;
320   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321               "Peer %u (`%s') started\n", p->no,
322               GNUNET_i2s (&p->id));
323   if (started != 2)
324     return;
325   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
326
327   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
328               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
329               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
330   GNUNET_free (sender_c);
331   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
332                                                NULL);
333
334 }
335
336
337 static void
338 run (void *cls, char *const *args, const char *cfgfile,
339      const struct GNUNET_CONFIGURATION_Handle *cfg)
340 {
341   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
342
343   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
344                                             &notify_receive, &notify_connect,
345                                             &notify_disconnect, &start_cb,
346                                             NULL);
347
348   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
349                                             &notify_receive, &notify_connect,
350                                             &notify_disconnect, &start_cb,
351                                             NULL);
352
353   if ((p1 == NULL) || (p2 == NULL))
354   {
355     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
356     if (die_task != NULL)
357       GNUNET_SCHEDULER_cancel (die_task);
358     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
359     return;
360   }
361 }
362
363
364 static int
365 check ()
366 {
367   static char *const argv[] = { "test-transport-api",
368     "-c",
369     "test_transport_api_data.conf",
370     NULL
371   };
372   static struct GNUNET_GETOPT_CommandLineOption options[] = {
373     GNUNET_GETOPT_OPTION_END
374   };
375
376   send_task = NULL;
377
378   ok = 1;
379   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
380                       "nohelp", options, &run, &ok);
381
382   return ok;
383 }
384
385 int
386 main (int argc, char *argv[])
387 {
388   int ret;
389
390   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
391
392   GNUNET_log_setup (test_name,
393                     "WARNING",
394                     NULL);
395
396   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
397   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
398                                                  &test_plugin);
399
400   tth = GNUNET_TRANSPORT_TESTING_init ();
401
402   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
403   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
404
405   ret = check ();
406
407   GNUNET_free (cfg_file_p1);
408   GNUNET_free (cfg_file_p2);
409
410   GNUNET_free (test_source);
411   GNUNET_free (test_plugin);
412   GNUNET_free (test_name);
413
414   GNUNET_TRANSPORT_TESTING_done (tth);
415
416   return ret;
417 }
418
419 /* end of test_transport_api.c */