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