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