tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / transport / test_transport_api_manipulation_recv_tcp.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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 /**
21  * @file transport/test_transport_api_manipulation_recv_tcp.c
22  * @brief base test case for transport traffic manipulation implementation
23  *
24  * This test case will setup 2 peers and connect them, the first message
25  * will be sent without manipulation, then a receive delay of 1 second will
26  * be configured and 2 more message will be sent. Time will be measured
27  *
28  * In addition the distance on receiver side will be manipulated to be 10
29  */
30 #include "platform.h"
31 #include "gnunet_transport_service.h"
32 #include "transport-testing.h"
33
34 /**
35  * How long until we give up on transmitting the message?
36  */
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
38
39
40 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
41
42 static int messages_recv;
43
44 static struct GNUNET_TIME_Absolute start_normal;
45
46 static struct GNUNET_TIME_Relative dur_normal;
47
48 static struct GNUNET_TIME_Absolute start_delayed;
49
50 static struct GNUNET_TIME_Relative dur_delayed;
51
52
53 static void
54 do_free (void *cls)
55 {
56   struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
57   
58   GNUNET_free (sc);
59 }
60
61
62 static void
63 delayed_transmit (void *cls)
64 {
65   struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
66   
67   start_delayed = GNUNET_TIME_absolute_get ();
68   GNUNET_TRANSPORT_TESTING_large_send (sc);
69 }
70
71
72 static void
73 sendtask (void *cls)
74 {
75   struct GNUNET_TRANSPORT_TESTING_SendClosure *sc;
76   struct GNUNET_ATS_Properties prop;
77   struct GNUNET_TIME_Relative delay;
78
79   sc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_SendClosure);
80   sc->num_messages = 1;
81   sc->ccc = ccc;
82   sc->cont = &do_free;
83   sc->cont_cls = sc;
84   if (0 == messages_recv)
85   {
86     start_normal = GNUNET_TIME_absolute_get();
87   }
88   if (0 < messages_recv)
89   {
90     memset (&prop,
91             0,
92             sizeof (prop));
93     delay = GNUNET_TIME_UNIT_SECONDS;
94     GNUNET_TRANSPORT_manipulation_set (ccc->p[1]->tmh,
95                                        &ccc->p[0]->id,
96                                        &prop,
97                                        delay,
98                                        GNUNET_TIME_UNIT_ZERO);
99     /* wait 1s to allow manipulation to go into effect */
100     if (1 == messages_recv)
101     {
102       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
103                                     &delayed_transmit,
104                                     sc);
105       return;
106     }
107   }
108   GNUNET_TRANSPORT_TESTING_large_send (sc);
109 }
110
111
112 static void
113 notify_receive (void *cls,
114                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
115                 const struct GNUNET_PeerIdentity *sender,
116                 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
117 {
118   {
119     char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
120
121     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
122                 "Peer %u (`%s') received message of type %d and size %u size from peer %s)!\n",
123                 receiver->no,
124                 ps,
125                 ntohs (message->header.type),
126                 ntohs (message->header.size),
127                 GNUNET_i2s (sender));
128     GNUNET_free (ps);
129   }
130   if ( (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (message->header.type)) ||
131        (GNUNET_TRANSPORT_TESTING_LARGE_MESSAGE_SIZE != ntohs (message->header.size)) )
132   {
133     GNUNET_break (0);
134     ccc->global_ret = GNUNET_SYSERR;
135     GNUNET_SCHEDULER_shutdown ();
136     return;
137   }
138
139   if (messages_recv <= 2)
140   {
141     /* Received non-delayed message */
142     dur_normal = GNUNET_TIME_absolute_get_duration (start_normal);
143     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
144                 "Received non-delayed message %u after %s\n",
145                 messages_recv,
146                 GNUNET_STRINGS_relative_time_to_string (dur_normal,
147                                                         GNUNET_YES));
148     GNUNET_SCHEDULER_add_now (&sendtask,
149                               NULL);
150     messages_recv++;
151     return;
152   }
153   /* Received manipulated message */
154   dur_delayed = GNUNET_TIME_absolute_get_duration(start_delayed);
155   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
156               "Received delayed message %u after %s\n",
157               messages_recv,
158               GNUNET_STRINGS_relative_time_to_string (dur_delayed,
159                                                       GNUNET_YES));
160   if (dur_delayed.rel_value_us < GNUNET_TIME_UNIT_SECONDS.rel_value_us)
161   {
162     GNUNET_break (0);
163     ccc->global_ret = GNUNET_SYSERR;
164     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
165                 "Delayed message was not delayed correctly: took only %s\n",
166                 GNUNET_STRINGS_relative_time_to_string (dur_delayed,
167                                                         GNUNET_YES));
168   }
169   else
170   { 
171     ccc->global_ret = GNUNET_OK;
172   }
173   /* shutdown */
174   GNUNET_SCHEDULER_shutdown ();
175 }
176
177
178 int
179 main (int argc,
180       char *argv[])
181 {
182   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
183     .connect_continuation = &sendtask,
184     .config_file = "test_transport_api_data.conf",
185     .rec = &notify_receive,
186     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
187     .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
188     .timeout = TIMEOUT,
189     .global_ret = GNUNET_NO
190   };
191
192   ccc = &my_ccc;
193   if (GNUNET_OK !=
194       GNUNET_TRANSPORT_TESTING_main (2,
195                                      &GNUNET_TRANSPORT_TESTING_connect_check,
196                                      ccc))
197     return 1;
198   return 0;
199 }
200
201 /* end of test_transport_api_manipulation_recv_tcp.c */