REST/NAMESTORE: rework API
[oweals/gnunet.git] / src / transport / test_transport_api_manipulation_send_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_send_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 send delay of 1 second will
26  * be configured and 1 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, 30)
38
39 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
40
41 static int messages_recv;
42
43 static struct GNUNET_TIME_Absolute start_normal;
44
45 static struct GNUNET_TIME_Relative dur_normal;
46
47 static struct GNUNET_TIME_Absolute start_delayed;
48
49 static struct GNUNET_TIME_Relative dur_delayed;
50
51
52 static void
53 do_free (void *cls)
54 {
55   struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
56   
57   GNUNET_free (sc);
58 }
59
60
61 static void
62 delayed_transmit (void *cls)
63 {
64   struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
65   
66   start_delayed = GNUNET_TIME_absolute_get ();
67   GNUNET_TRANSPORT_TESTING_large_send (sc);
68 }
69
70
71 static void
72 sendtask (void *cls)
73 {
74   struct GNUNET_TRANSPORT_TESTING_SendClosure *sc;
75   struct GNUNET_TIME_Relative delay;
76   struct GNUNET_ATS_Properties prop;
77
78   sc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_SendClosure);
79   sc->num_messages = 1;
80   sc->ccc = ccc;
81   sc->cont = &do_free;
82   sc->cont_cls = sc;
83   if (0 == messages_recv)
84   {
85     start_normal = GNUNET_TIME_absolute_get ();
86   }
87   if (1 == messages_recv)
88   {
89     memset (&prop,
90             0,
91             sizeof (prop));
92     delay = GNUNET_TIME_UNIT_SECONDS;
93     GNUNET_TRANSPORT_manipulation_set (ccc->p[0]->tmh,
94                                        &ccc->p[1]->id,
95                                        &prop,
96                                        GNUNET_TIME_UNIT_ZERO,
97                                        delay);
98     /* wait 1s to allow manipulation to go into effect */
99     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
100                                   &delayed_transmit,
101                                   sc);
102     return;
103   }
104   GNUNET_TRANSPORT_TESTING_large_send (sc);
105 }
106
107
108 static void
109 notify_receive (void *cls,
110                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
111                 const struct GNUNET_PeerIdentity *sender,
112                 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
113 {
114   {
115     char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
116
117     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
118                 "Peer %u (`%s') received message of type %d and size %u size from peer %s)!\n",
119                 receiver->no,
120                 ps,
121                 ntohs (message->header.type),
122                 ntohs (message->header.size),
123                 GNUNET_i2s (sender));
124     GNUNET_free (ps);
125   }
126
127   if ( (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (message->header.type)) ||
128        (GNUNET_TRANSPORT_TESTING_LARGE_MESSAGE_SIZE != ntohs (message->header.size)) )
129   {
130     GNUNET_break (0);
131     ccc->global_ret = GNUNET_SYSERR;
132     GNUNET_SCHEDULER_shutdown ();
133     return;
134   }
135
136   if (0 == messages_recv)
137   {
138     /* Received non-delayed message */
139     dur_normal = GNUNET_TIME_absolute_get_duration (start_normal);
140     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
141                 "Received non-delayed message %u after %s\n",
142                 messages_recv,
143                 GNUNET_STRINGS_relative_time_to_string (dur_normal,
144                                                         GNUNET_YES));
145     GNUNET_SCHEDULER_add_now (&sendtask,
146                               NULL);
147     messages_recv++;
148     return;
149   }
150   /* Received manipulated message */
151   dur_delayed = GNUNET_TIME_absolute_get_duration(start_delayed);
152   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153               "Received delayed message %u after %s\n",
154               messages_recv,
155               GNUNET_STRINGS_relative_time_to_string (dur_delayed,
156                                                       GNUNET_YES));
157   if (dur_delayed.rel_value_us < GNUNET_TIME_UNIT_SECONDS.rel_value_us)
158   {
159     GNUNET_break (0);
160     ccc->global_ret = GNUNET_SYSERR;
161     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
162                 "Delayed message was not delayed correctly: took only %s\n",
163                 GNUNET_STRINGS_relative_time_to_string (dur_delayed,
164                                                         GNUNET_YES));
165   }
166   else
167   {
168     ccc->global_ret = GNUNET_OK;
169   }
170   GNUNET_SCHEDULER_shutdown ();
171 }
172
173
174 int
175 main (int argc,
176       char *argv[])
177 {
178   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
179     .connect_continuation = &sendtask,
180     .config_file = "test_transport_api_data.conf",
181     .rec = &notify_receive,
182     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
183     .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
184     .timeout = TIMEOUT,
185     .global_ret = GNUNET_NO
186   };
187
188   ccc = &my_ccc;
189   if (GNUNET_OK !=
190       GNUNET_TRANSPORT_TESTING_main (2,
191                                      &GNUNET_TRANSPORT_TESTING_connect_check,
192                                      ccc))
193     return 1;
194   return 0;
195 }
196
197 /* end of test_transport_api_manipulation_send_tcp.c */