fixing coverity 10465: Resource leak
[oweals/gnunet.git] / src / transport / test_transport_api_manipulation_cfg.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_manipulation_send_tcp.c
22  * @brief base test case for transport traffic manipulation implementation
23  * based onf cfg
24  *
25  * Peer 1 has inbound and outbound delay of 100ms
26  * Peer 2 has no inbound and outbound delay
27  *
28  * We send a request from P1 to P2 and expect delay of >= TEST_DELAY ms
29  * Then we send response from P2 to P1 and expect delay of >= TEST_DELAY ms
30  */
31 #include "platform.h"
32 #include "gnunet_transport_service.h"
33 #include "transport-testing.h"
34
35 /**
36  * How long until we give up on transmitting the message?
37  */
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
39
40 /**
41  * How long until we give up on transmitting the message?
42  */
43 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
44
45 #define TEST_MESSAGE_SIZE 2600
46
47 #define TEST_REQUEST_MESSAGE_TYPE 12345
48
49 #define TEST_RESPONSE_MESSAGE_TYPE 12346
50
51 #define TEST_DELAY 100
52
53 static char *test_source;
54
55 static char *test_plugin;
56
57 static char *test_name;
58
59 static int ok;
60
61 static int s_started;
62
63 static int s_connected;
64
65 static int s_sending;
66
67 static GNUNET_SCHEDULER_TaskIdentifier die_task;
68
69 static GNUNET_SCHEDULER_TaskIdentifier send_task;
70
71 static struct PeerContext *p1;
72
73 static struct PeerContext *p2;
74
75 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
76
77 static struct GNUNET_TRANSPORT_TransmitHandle *th;
78
79 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
80
81 static char *cfg_file_p1;
82
83 static char *cfg_file_p2;
84
85 static struct GNUNET_TIME_Absolute start_request;
86 static struct GNUNET_TIME_Absolute start_response;
87
88 static void
89 end ()
90 {
91   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
92
93   if (send_task != GNUNET_SCHEDULER_NO_TASK)
94     GNUNET_SCHEDULER_cancel (send_task);
95
96   if (die_task != GNUNET_SCHEDULER_NO_TASK)
97     GNUNET_SCHEDULER_cancel (die_task);
98
99   if (th != NULL)
100     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
101   th = NULL;
102
103   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
104   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
105 }
106
107 static void
108 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
109 {
110   die_task = GNUNET_SCHEDULER_NO_TASK;
111
112   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
113
114
115   if (send_task != GNUNET_SCHEDULER_NO_TASK)
116     GNUNET_SCHEDULER_cancel (send_task);
117
118   if (cc != NULL)
119   {
120     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
121     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
122     cc = NULL;
123   }
124
125   if (th != NULL)
126     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
127   else
128     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
129
130   if (s_started == GNUNET_NO)
131     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
132   else
133     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
134
135   if (s_connected == GNUNET_NO)
136     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
137   else
138     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
139
140   if (s_sending == GNUNET_NO)
141     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
142   else
143     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
144
145   th = NULL;
146
147   if (p1 != NULL)
148     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
149   else
150     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
151   if (p2 != NULL)
152     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
153   else
154     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
155
156   ok = GNUNET_SYSERR;
157 }
158
159
160 static size_t
161 notify_request_ready (void *cls, size_t size, void *buf)
162 {
163   struct PeerContext *p = cls;
164   struct GNUNET_MessageHeader *hdr;
165
166   th = NULL;
167
168   if (buf == NULL)
169   {
170     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
171                 "Timeout occurred while waiting for transmit_ready\n");
172     if (GNUNET_SCHEDULER_NO_TASK != die_task)
173       GNUNET_SCHEDULER_cancel (die_task);
174     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
175     ok = 42;
176     return 0;
177   }
178
179   GNUNET_assert (size >= TEST_MESSAGE_SIZE);
180   if (buf != NULL)
181   {
182     memset (buf, '\0', TEST_MESSAGE_SIZE);
183     hdr = buf;
184     hdr->size = htons (TEST_MESSAGE_SIZE);
185     hdr->type = htons (TEST_REQUEST_MESSAGE_TYPE);
186   }
187
188   char *ps = GNUNET_strdup (GNUNET_i2s (&p1->id));
189   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190               "Sending request message from peer %u (`%4s') with type %u and size %u bytes to peer %u (`%4s')\n",
191               p1->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
192               GNUNET_i2s (&p->id));
193   GNUNET_free (ps);
194   return TEST_MESSAGE_SIZE;
195 }
196
197
198 static void
199 sendtask_request_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
200 {
201   send_task = GNUNET_SCHEDULER_NO_TASK;
202
203   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
204     return;
205   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
206
207   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
209               p1->no, GNUNET_i2s (&p1->id), p2->no, receiver_s);
210   GNUNET_free (receiver_s);
211
212   s_sending = GNUNET_YES;
213         start_request = GNUNET_TIME_absolute_get();
214   th = GNUNET_TRANSPORT_notify_transmit_ready (p1->th, &p2->id, TEST_MESSAGE_SIZE, 0,
215                                                TIMEOUT_TRANSMIT, &notify_request_ready,
216                                                p2);
217 }
218
219
220 static size_t
221 notify_response_ready (void *cls, size_t size, void *buf)
222 {
223   struct PeerContext *p = cls;
224   struct GNUNET_MessageHeader *hdr;
225
226   th = NULL;
227
228   if (buf == NULL)
229   {
230     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
231                 "Timeout occurred while waiting for transmit_ready\n");
232     if (GNUNET_SCHEDULER_NO_TASK != die_task)
233       GNUNET_SCHEDULER_cancel (die_task);
234     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
235     ok = 42;
236     return 0;
237   }
238
239   GNUNET_assert (size >= TEST_MESSAGE_SIZE);
240   if (buf != NULL)
241   {
242     memset (buf, '\0', TEST_MESSAGE_SIZE);
243     hdr = buf;
244     hdr->size = htons (TEST_MESSAGE_SIZE);
245     hdr->type = htons (TEST_RESPONSE_MESSAGE_TYPE);
246   }
247
248   char *ps = GNUNET_strdup (GNUNET_i2s (&p1->id));
249   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250               "Sending response message from peer %u (`%4s') with type %u and size %u bytes to peer %u (`%4s')\n",
251               p1->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
252               GNUNET_i2s (&p->id));
253   GNUNET_free (ps);
254
255   return TEST_MESSAGE_SIZE;
256 }
257
258 static void
259 sendtask_response_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
260 {
261   send_task = GNUNET_SCHEDULER_NO_TASK;
262
263   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
264     return;
265   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
266
267   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
269               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
270   GNUNET_free (receiver_s);
271
272   s_sending = GNUNET_YES;
273         start_response = GNUNET_TIME_absolute_get();
274   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE, 0,
275                                                TIMEOUT_TRANSMIT, &notify_response_ready,
276                                                p1);
277 }
278
279
280
281 static void
282 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
283                 const struct GNUNET_MessageHeader *message)
284 {
285   struct PeerContext *p = cls;
286   struct PeerContext *t = NULL;
287   struct GNUNET_TIME_Relative duration;
288
289   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
290     t = p1;
291   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
292     t = p2;
293   GNUNET_assert (t != NULL);
294
295   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
296
297   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
299               p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
300               GNUNET_i2s (&t->id));
301   GNUNET_free (ps);
302
303   switch (ntohs (message->type)) {
304                 case TEST_REQUEST_MESSAGE_TYPE:
305                         duration = GNUNET_TIME_absolute_get_difference(start_request,
306                                         GNUNET_TIME_absolute_get());
307                         if (duration.rel_value >= TEST_DELAY)
308                                 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
309                               "Request message was delayed for %llu ms\n",
310                                                                 duration.rel_value);
311                         else
312                         {
313                                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
314                               "Request message was delayed for %llu ms, should be %u ms\n",
315                                                                 duration.rel_value, TEST_DELAY);
316                                 ok = 1;
317                         }
318
319                   /* Send response */
320                   send_task = GNUNET_SCHEDULER_add_now (&sendtask_response_task, NULL);
321                   return;
322                         break;
323                 case TEST_RESPONSE_MESSAGE_TYPE:
324                         duration = GNUNET_TIME_absolute_get_difference(start_response,
325                                         GNUNET_TIME_absolute_get());
326                         if (duration.rel_value >= TEST_DELAY)
327                                 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
328                               "Response message was delayed for %llu ms\n", duration);
329                         else
330                         {
331                                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
332                               "Response message was delayed for %llu ms, should be %u ms\n",
333                                                                 duration.rel_value, TEST_DELAY);
334                                 ok = 1;
335                         }
336                   /* Done */
337                         ok = 0;
338                   end();
339                         break;
340                 default:
341                         break;
342         }
343 }
344
345
346
347 static void
348 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
349 {
350   static int c;
351
352   c++;
353   struct PeerContext *p = cls;
354   struct PeerContext *t = NULL;
355
356   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
357     t = p1;
358   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
359     t = p2;
360   GNUNET_assert (t != NULL);
361
362   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
363
364   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
366               t->no, GNUNET_i2s (peer));
367   GNUNET_free (ps);
368 }
369
370
371 static void
372 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
373 {
374   struct PeerContext *p = cls;
375   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
376
377   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
379               GNUNET_i2s (peer));
380
381   GNUNET_free (ps);
382
383   if (th != NULL)
384     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
385   th = NULL;
386 }
387
388
389 static void
390 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
391 {
392   cc = NULL;
393   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
394
395   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
396               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
397   GNUNET_free (p1_c);
398
399   s_connected = GNUNET_YES;
400   send_task = GNUNET_SCHEDULER_add_now (&sendtask_request_task, NULL);
401 }
402
403
404 static void
405 start_cb (struct PeerContext *p, void *cls)
406 {
407   static int started;
408   started++;
409
410   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
411               GNUNET_i2s (&p->id));
412
413   if (started != 2)
414     return;
415   else
416     s_started = GNUNET_YES;
417   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
418
419   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
421               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
422   GNUNET_free (sender_c);
423
424   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
425                                                NULL);
426
427 }
428
429
430 static void
431 run (void *cls, char *const *args, const char *cfgfile,
432      const struct GNUNET_CONFIGURATION_Handle *cfg)
433 {
434   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
435
436   s_started = GNUNET_NO;
437   s_connected = GNUNET_NO;
438   s_sending = GNUNET_NO;
439
440   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
441                                             &notify_receive, &notify_connect,
442                                             &notify_disconnect, &start_cb,
443                                             NULL);
444
445   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
446                                             &notify_receive, &notify_connect,
447                                             &notify_disconnect, &start_cb,
448                                             NULL);
449
450   if ((p1 == NULL) || (p2 == NULL))
451   {
452     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
453     if (die_task != GNUNET_SCHEDULER_NO_TASK)
454       GNUNET_SCHEDULER_cancel (die_task);
455     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
456     return;
457   }
458 }
459
460
461 static int
462 check ()
463 {
464   static char *const argv[] = { "test-transport-api-manipulation",
465     "-c",
466     "test_transport_api_data.conf",
467     NULL
468   };
469   static struct GNUNET_GETOPT_CommandLineOption options[] = {
470     GNUNET_GETOPT_OPTION_END
471   };
472
473   send_task = GNUNET_SCHEDULER_NO_TASK;
474
475   ok = 1;
476   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
477                       "nohelp", options, &run, &ok);
478
479   return ok;
480 }
481
482 int
483 main (int argc, char *argv[])
484 {
485   int ret;
486
487   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
488   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
489   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
490                                                  &test_plugin);
491
492   GNUNET_log_setup (test_name,
493                     "WARNING",
494                     NULL);
495   tth = GNUNET_TRANSPORT_TESTING_init ();
496
497   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
498   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
499
500   ret = check ();
501
502   GNUNET_free (cfg_file_p1);
503   GNUNET_free (cfg_file_p2);
504
505   GNUNET_free (test_source);
506   GNUNET_free (test_plugin);
507   GNUNET_free (test_name);
508
509   GNUNET_TRANSPORT_TESTING_done (tth);
510
511   return ret;
512 }
513
514 /* end of test_transport_api.c */