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