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