-rps service: prevent division by zero
[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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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 sendtask (void *cls)
64 {
65   struct GNUNET_TRANSPORT_TESTING_SendClosure *sc;
66   struct GNUNET_ATS_Properties prop;
67   struct GNUNET_TIME_Relative delay;
68
69   sc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_SendClosure);
70   sc->num_messages = 1;
71   sc->ccc = ccc;
72   sc->cont = &do_free;
73   sc->cont_cls = sc;
74   if (0 == messages_recv)
75   {
76     start_normal = GNUNET_TIME_absolute_get();
77   }
78   if (0 < messages_recv)
79   {
80     memset (&prop, 0, sizeof (prop));
81     delay = GNUNET_TIME_UNIT_SECONDS;
82     GNUNET_TRANSPORT_manipulation_set (ccc->p[1]->tmh,
83                                        &ccc->p[0]->id,
84                                        &prop,
85                                        delay,
86                                        GNUNET_TIME_UNIT_ZERO);
87     start_delayed = GNUNET_TIME_absolute_get();
88   }
89   GNUNET_TRANSPORT_TESTING_large_send (sc);
90 }
91
92
93 static void
94 notify_receive (void *cls,
95                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
96                 const struct GNUNET_PeerIdentity *sender,
97                 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
98 {
99   {
100     char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
101
102     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
103                 "Peer %u (`%s') received message of type %d and size %u size from peer %s)!\n",
104                 receiver->no,
105                 ps,
106                 ntohs (message->header.type),
107                 ntohs (message->header.size),
108                 GNUNET_i2s (sender));
109     GNUNET_free (ps);
110   }
111   if ( (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (message->header.type)) ||
112        (GNUNET_TRANSPORT_TESTING_LARGE_MESSAGE_SIZE != ntohs (message->header.size)) )
113   {
114     GNUNET_break (0);
115     ccc->global_ret = GNUNET_SYSERR;
116     GNUNET_SCHEDULER_shutdown ();
117     return;
118   }
119
120   if (messages_recv <= 1)
121   {
122     /* Received non-delayed message */
123     dur_normal = GNUNET_TIME_absolute_get_duration (start_normal);
124     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125                 "Received non-delayed message %u after %s\n",
126                 messages_recv,
127                 GNUNET_STRINGS_relative_time_to_string (dur_normal,
128                                                         GNUNET_YES));
129     GNUNET_SCHEDULER_add_now (&sendtask,
130                               NULL);
131     messages_recv++;
132     return;
133   }
134   /* Received manipulated message */
135   dur_delayed = GNUNET_TIME_absolute_get_duration(start_delayed);
136   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137               "Received delayed message %u after %s\n",
138               messages_recv,
139               GNUNET_STRINGS_relative_time_to_string (dur_delayed,
140                                                       GNUNET_YES));
141   if (dur_delayed.rel_value_us < GNUNET_TIME_UNIT_SECONDS.rel_value_us)
142   {
143     GNUNET_break (0);
144     ccc->global_ret = GNUNET_SYSERR;
145     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
146                 "Delayed message was not delayed correctly: took only %s\n",
147                 GNUNET_STRINGS_relative_time_to_string (dur_delayed,
148                                                         GNUNET_YES));
149   }
150   else
151   { 
152     ccc->global_ret = GNUNET_OK;
153   }
154   /* shutdown */
155   GNUNET_SCHEDULER_shutdown ();
156 }
157
158
159 int
160 main (int argc,
161       char *argv[])
162 {
163   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
164     .connect_continuation = &sendtask,
165     .config_file = "test_transport_api_data.conf",
166     .rec = &notify_receive,
167     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
168     .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
169     .timeout = TIMEOUT,
170     .global_ret = GNUNET_NO
171   };
172
173   ccc = &my_ccc;
174   if (GNUNET_OK !=
175       GNUNET_TRANSPORT_TESTING_main (2,
176                                      &GNUNET_TRANSPORT_TESTING_connect_check,
177                                      ccc))
178     return 1;
179   return 0;
180 }
181
182 /* end of test_transport_api_manipulation_recv_tcp.c */