fix div by zero
[oweals/gnunet.git] / src / transport / test_transport_api_timeout.c
1 /*
2      This file is part of GNUnet.x
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_timeout.c
20  * @brief test case for transport plugin implementations complying timeout
21  * settings
22  *
23  *
24  * This test case serves ensures that no peer disconnect events occurs
25  * while plugins are idle
26  */
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 WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
36
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
38
39 #define MTYPE 12345
40
41 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
42
43 static struct GNUNET_TIME_Relative time_running;
44
45 static struct GNUNET_SCHEDULER_Task *timer_task;
46
47 static int shutdown_flag;
48
49 static unsigned int disconnects;
50
51
52 static void
53 custom_shutdown (void *cls)
54 {
55   if (NULL != timer_task)
56   {
57     GNUNET_SCHEDULER_cancel (timer_task);
58     timer_task = NULL;
59   }
60   if (0 == disconnects)
61   {
62     ccc->global_ret = GNUNET_OK;
63   }
64   else
65   {
66     ccc->global_ret =- GNUNET_SYSERR;
67     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
68                 "Fail! Had %u disconnects while waiting %s\n",
69                 disconnects,
70                 GNUNET_STRINGS_relative_time_to_string (WAIT,
71                                                         GNUNET_YES));
72   }
73 }
74
75
76 static void
77 notify_receive (void *cls,
78                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
79                 const struct GNUNET_PeerIdentity *sender,
80                 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
81 {
82   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
83               "Received message of type %d from peer %s!\n",
84               ntohs (message->header.type),
85               GNUNET_i2s (sender));
86 }
87
88
89 static void
90 notify_disconnect (void *cls,
91                    struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
92                    const struct GNUNET_PeerIdentity *other)
93 {
94   GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
95                                            me,
96                                            other);
97   if (shutdown_flag != GNUNET_YES)
98   {
99     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
100                 "FAIL! Peer `%s' disconnected during waiting period!\n",
101                 GNUNET_i2s (other));
102     disconnects++;
103   }
104 }
105
106
107 static void
108 timer (void *cls)
109 {
110   static unsigned int percentage;
111
112   timer_task = NULL;
113   percentage += 10;
114   time_running = GNUNET_TIME_relative_add (time_running,
115                                            GNUNET_TIME_relative_divide (WAIT,
116                                                                         10));
117
118   if (time_running.rel_value_us ==
119       GNUNET_TIME_relative_max (time_running, WAIT).rel_value_us)
120   {
121     FPRINTF (stderr, "%s",  "100%%\n");
122     shutdown_flag = GNUNET_YES;
123     GNUNET_SCHEDULER_shutdown ();
124   }
125   else
126   {
127     FPRINTF (stderr,
128              "%u%%..",
129              percentage);
130     timer_task =
131         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide (WAIT, 10),
132                                       &timer,
133                                       NULL);
134   }
135 }
136
137
138 int
139 main (int argc,
140       char *argv[])
141 {
142   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
143     .connect_continuation = &timer,
144     .config_file = "test_transport_api_data.conf",
145     .rec = &notify_receive,
146     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
147     .nd = &notify_disconnect,
148     .shutdown_task = &custom_shutdown,
149     .timeout = TIMEOUT
150   };
151
152   ccc = &my_ccc;
153   if (GNUNET_OK !=
154       GNUNET_TRANSPORT_TESTING_main (2,
155                                      &GNUNET_TRANSPORT_TESTING_connect_check,
156                                      ccc))
157     return 1;
158   return 0;
159 }
160
161 /* end of test_transport_api_timeout.c*/