fix div by zero
[oweals/gnunet.git] / src / transport / test_transport_api_manipulation_cfg.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_cfg.c
20  * @brief base test case for transport traffic manipulation implementation
21  * based on cfg
22  *
23  * Peer 1 has inbound and outbound delay of 100ms
24  * Peer 2 has no inbound and outbound delay
25  *
26  * We send a request from P1 to P2 and expect delay of >= TEST_DELAY us
27  * Then we send response from P2 to P1 and expect delay of >= TEST_DELAY us
28  */
29 #include "platform.h"
30 #include "gnunet_transport_service.h"
31 #include "transport-testing.h"
32
33 /**
34  * How long until we give up on transmitting the message?
35  */
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
37
38
39 #define TEST_MESSAGE_SIZE 2600
40
41 #define TEST_RESPONSE_MESSAGE_TYPE 
42
43 /**
44  * Test delay, in microseconds.
45  */
46 #define TEST_DELAY 100 * 1000LL
47
48
49 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
50
51 static struct GNUNET_TIME_Absolute start_request;
52
53 static struct GNUNET_TIME_Absolute start_response;
54
55
56 static void
57 sendtask_response_task (void *cls)
58 {
59   int ret;
60   
61   start_response = GNUNET_TIME_absolute_get();
62   ret = GNUNET_TRANSPORT_TESTING_send (ccc->p[1],
63                                        ccc->p[0],
64                                        GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE2,
65                                        TEST_MESSAGE_SIZE,
66                                        1,
67                                        NULL,
68                                        NULL);
69   if (GNUNET_NO == ret)
70   {
71     GNUNET_break (0);
72     GNUNET_SCHEDULER_shutdown ();
73     return;
74   }
75   GNUNET_assert (GNUNET_SYSERR != ret);
76 }
77
78
79 static void
80 notify_receive (void *cls,
81                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
82                 const struct GNUNET_PeerIdentity *sender,
83                 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
84 {
85   struct GNUNET_TIME_Relative duration;
86
87   {
88     char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
89
90     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91                 "Peer %u (`%s') received message of type %d and size %u size from peer %s)!\n",
92                 receiver->no,
93                 ps,
94                 ntohs (message->header.type),
95                 ntohs (message->header.size),
96                 GNUNET_i2s (sender));
97     GNUNET_free (ps);
98   }
99
100   switch (ntohs (message->header.type)) {
101   case GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE:
102     duration = GNUNET_TIME_absolute_get_difference (start_request,
103                                                     GNUNET_TIME_absolute_get());
104     if (duration.rel_value_us >= TEST_DELAY)
105     {
106       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
107                   "Request message was delayed for %s\n",
108                   GNUNET_STRINGS_relative_time_to_string (duration,
109                                                           GNUNET_YES));
110     }
111     else
112     {
113       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
114                   "Request message was delayed for unexpected duration %s\n",
115                   GNUNET_STRINGS_relative_time_to_string (duration,
116                                                           GNUNET_YES));
117       ccc->global_ret = GNUNET_SYSERR;
118       GNUNET_SCHEDULER_shutdown ();
119     }
120     /* Send response */
121     GNUNET_SCHEDULER_add_now (&sendtask_response_task,
122                               NULL);
123     return;
124   case GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE2:
125     duration = GNUNET_TIME_absolute_get_difference(start_response,
126                                                    GNUNET_TIME_absolute_get());
127     if (duration.rel_value_us >= TEST_DELAY)
128     {
129       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
130                   "Response message was delayed for %s\n",
131                   GNUNET_STRINGS_relative_time_to_string (duration,
132                                                           GNUNET_YES));
133       ccc->global_ret = GNUNET_OK;
134     }
135     else
136     {
137       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
138                   "Response message was delayed for unexpected duration %s\n",
139                   GNUNET_STRINGS_relative_time_to_string (duration,
140                                                           GNUNET_YES));
141       ccc->global_ret = GNUNET_SYSERR;
142     }
143     GNUNET_SCHEDULER_shutdown ();
144     break;
145   default:
146     GNUNET_break (0);
147     break;
148   }
149 }
150
151
152 int
153 main (int argc,
154       char *argv[])
155 {
156   struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
157     .num_messages = 1
158   };
159   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
160     .connect_continuation = &GNUNET_TRANSPORT_TESTING_large_send,
161     .connect_continuation_cls = &sc,
162     .config_file = "test_transport_api_data.conf",
163     .rec = &notify_receive,
164     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
165     .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
166     .timeout = TIMEOUT
167   };
168
169   ccc = &my_ccc;
170   sc.ccc = ccc;
171   start_request = GNUNET_TIME_absolute_get ();
172   if (GNUNET_OK !=
173       GNUNET_TRANSPORT_TESTING_main (2,
174                                      &GNUNET_TRANSPORT_TESTING_connect_check,
175                                      ccc))
176     return 1;
177   return 0;
178 }
179
180
181 /* end of test_transport_api_manipulation_cfg.c */