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