glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / transport / test_transport_api_manipulation_cfg.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2016 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15 /**
16  * @file transport/test_transport_api_manipulation_cfg.c
17  * @brief base test case for transport traffic manipulation implementation
18  * based on cfg
19  *
20  * Peer 1 has inbound and outbound delay of 100ms
21  * Peer 2 has no inbound and outbound delay
22  *
23  * We send a request from P1 to P2 and expect delay of >= TEST_DELAY us
24  * Then we send response from P2 to P1 and expect delay of >= TEST_DELAY us
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 #define TEST_MESSAGE_SIZE 2600
37
38 #define TEST_RESPONSE_MESSAGE_TYPE 
39
40 /**
41  * Test delay, in microseconds.
42  */
43 #define TEST_DELAY 100 * 1000LL
44
45
46 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
47
48 static struct GNUNET_TIME_Absolute start_request;
49
50 static struct GNUNET_TIME_Absolute start_response;
51
52
53 static void
54 sendtask_response_task (void *cls)
55 {
56   int ret;
57   
58   start_response = GNUNET_TIME_absolute_get();
59   ret = GNUNET_TRANSPORT_TESTING_send (ccc->p[1],
60                                        ccc->p[0],
61                                        GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE2,
62                                        TEST_MESSAGE_SIZE,
63                                        1,
64                                        NULL,
65                                        NULL);
66   if (GNUNET_NO == ret)
67   {
68     GNUNET_break (0);
69     GNUNET_SCHEDULER_shutdown ();
70     return;
71   }
72   GNUNET_assert (GNUNET_SYSERR != ret);
73 }
74
75
76 static void
77 notify_receive (void *cls,
78                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
79                 const struct GNUNET_PeerIdentity *sender,
80                 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
81 {
82   struct GNUNET_TIME_Relative duration;
83
84   {
85     char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
86
87     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
88                 "Peer %u (`%s') received message of type %d and size %u size from peer %s)!\n",
89                 receiver->no,
90                 ps,
91                 ntohs (message->header.type),
92                 ntohs (message->header.size),
93                 GNUNET_i2s (sender));
94     GNUNET_free (ps);
95   }
96
97   switch (ntohs (message->header.type)) {
98   case GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE:
99     duration = GNUNET_TIME_absolute_get_difference (start_request,
100                                                     GNUNET_TIME_absolute_get());
101     if (duration.rel_value_us >= TEST_DELAY)
102     {
103       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
104                   "Request message was delayed for %s\n",
105                   GNUNET_STRINGS_relative_time_to_string (duration,
106                                                           GNUNET_YES));
107     }
108     else
109     {
110       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
111                   "Request message was delayed for unexpected duration %s\n",
112                   GNUNET_STRINGS_relative_time_to_string (duration,
113                                                           GNUNET_YES));
114       ccc->global_ret = GNUNET_SYSERR;
115       GNUNET_SCHEDULER_shutdown ();
116     }
117     /* Send response */
118     GNUNET_SCHEDULER_add_now (&sendtask_response_task,
119                               NULL);
120     return;
121   case GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE2:
122     duration = GNUNET_TIME_absolute_get_difference(start_response,
123                                                    GNUNET_TIME_absolute_get());
124     if (duration.rel_value_us >= TEST_DELAY)
125     {
126       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
127                   "Response message was delayed for %s\n",
128                   GNUNET_STRINGS_relative_time_to_string (duration,
129                                                           GNUNET_YES));
130       ccc->global_ret = GNUNET_OK;
131     }
132     else
133     {
134       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
135                   "Response message was delayed for unexpected duration %s\n",
136                   GNUNET_STRINGS_relative_time_to_string (duration,
137                                                           GNUNET_YES));
138       ccc->global_ret = GNUNET_SYSERR;
139     }
140     GNUNET_SCHEDULER_shutdown ();
141     break;
142   default:
143     GNUNET_break (0);
144     break;
145   }
146 }
147
148
149 int
150 main (int argc,
151       char *argv[])
152 {
153   struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
154     .num_messages = 1
155   };
156   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
157     .connect_continuation = &GNUNET_TRANSPORT_TESTING_large_send,
158     .connect_continuation_cls = &sc,
159     .config_file = "test_transport_api_data.conf",
160     .rec = &notify_receive,
161     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
162     .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
163     .timeout = TIMEOUT
164   };
165
166   ccc = &my_ccc;
167   sc.ccc = ccc;
168   if (GNUNET_OK !=
169       GNUNET_TRANSPORT_TESTING_main (2,
170                                      &GNUNET_TRANSPORT_TESTING_connect_check,
171                                      ccc))
172     return 1;
173   return 0;
174 }
175
176
177 /* end of test_transport_api_manipulation_cfg.c */