fix div by zero
[oweals/gnunet.git] / src / transport / test_transport_api_blacklisting.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2011, 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 /**
20  * @file transport/test_transport_api_blacklisting.c
21  * @brief test for the blacklisting API
22  * @author Matthias Wachs
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_transport_service.h"
27 #include "transport-testing.h"
28
29 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
30
31 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
32
33 static int connected;
34
35 static int blacklist_request_p1;
36
37 static int blacklist_request_p2;
38
39 static struct GNUNET_TRANSPORT_Blacklist *blacklist_p1;
40
41 static struct GNUNET_TRANSPORT_Blacklist *blacklist_p2;
42
43 static struct GNUNET_SCHEDULER_Task *shutdown_task;
44
45
46 static void
47 end (void *cls)
48 {
49   shutdown_task = NULL;
50   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
51               "Stopping\n");
52   if ((GNUNET_YES == blacklist_request_p1) &&
53       (GNUNET_YES == blacklist_request_p2) &&
54       (GNUNET_NO == connected) )
55   {
56     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
57                 "Peers were never connected, success\n");
58     ccc->global_ret = GNUNET_OK;
59   }
60   else
61   {
62     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
63                 "Peers were not connected, fail\n");
64     ccc->global_ret = GNUNET_SYSERR;
65   }
66   GNUNET_SCHEDULER_shutdown ();
67 }
68
69
70 static void
71 custom_shutdown (void *cls)
72 {
73   if (NULL != shutdown_task)
74   {
75     GNUNET_SCHEDULER_cancel (shutdown_task);
76     shutdown_task = NULL;
77   }
78   if (NULL != blacklist_p1)
79   {
80     GNUNET_TRANSPORT_blacklist_cancel (blacklist_p1);
81     blacklist_p1 = NULL;
82   }
83   if (NULL != blacklist_p2)
84   {
85     GNUNET_TRANSPORT_blacklist_cancel (blacklist_p2);
86     blacklist_p2 = NULL;
87   }
88 }
89
90
91 static void
92 notify_receive (void *cls,
93                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
94                 const struct GNUNET_PeerIdentity *sender,
95                 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
96 {
97   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
98               "Unexpectedly even received the message despite blacklist\n");
99   connected = GNUNET_YES;
100   GNUNET_SCHEDULER_cancel (shutdown_task);
101   end (NULL);
102 }
103
104
105 static void
106 notify_connect (void *cls,
107                 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
108                 const struct GNUNET_PeerIdentity *other)
109 {
110   GNUNET_TRANSPORT_TESTING_log_connect (cls,
111                                         me,
112                                         other);
113   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
114               "Peers connected despite blacklist!\n");
115   connected = GNUNET_YES; /* this test now failed */
116   GNUNET_SCHEDULER_cancel (shutdown_task);
117   end (NULL);
118 }
119
120
121 static int
122 blacklist_cb (void *cls,
123               const struct GNUNET_PeerIdentity *pid)
124 {
125   struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
126   int res = GNUNET_SYSERR;
127
128   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129               "Peer %u: Blacklist request for peer `%s'\n",
130               p->no,
131               GNUNET_i2s (pid));
132
133   if (p == ccc->p[0])
134   {
135     blacklist_request_p1 = GNUNET_YES;
136     res = GNUNET_OK;
137   }
138   if (p == ccc->p[1])
139   {
140     blacklist_request_p2 = GNUNET_YES;
141     res = GNUNET_SYSERR;
142   }
143
144   if ( (GNUNET_YES == blacklist_request_p2) &&
145        (GNUNET_YES == blacklist_request_p1) &&
146        (NULL == shutdown_task) )
147   {
148     shutdown_task
149       = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3),
150                                       &end,
151                                       NULL);
152   }
153   return res;
154 }
155
156
157 static void
158 start_blacklist (void *cls)
159 {
160   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161               "Starting blacklists\n");
162   blacklist_p1 = GNUNET_TRANSPORT_blacklist (ccc->p[0]->cfg,
163                                              &blacklist_cb,
164                                              ccc->p[0]);
165   GNUNET_assert (NULL != blacklist_p1);
166   blacklist_p2 = GNUNET_TRANSPORT_blacklist (ccc->p[1]->cfg,
167                                              &blacklist_cb,
168                                              ccc->p[1]);
169   GNUNET_assert (NULL != blacklist_p2);
170 }
171
172
173 int
174 main (int argc,
175       char *argv[])
176 {
177   struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
178     .num_messages = 1
179   };
180   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
181     .pre_connect_task = &start_blacklist,
182     .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
183     .connect_continuation_cls = &sc,
184     .config_file = "test_transport_api_data.conf",
185     .rec = &notify_receive,
186     .nc = &notify_connect,
187     .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
188     .shutdown_task = &custom_shutdown,
189     .timeout = TIMEOUT,
190     .bi_directional = GNUNET_YES
191   };
192
193   ccc = &my_ccc;
194   if (GNUNET_OK !=
195       GNUNET_TRANSPORT_TESTING_main (2,
196                                      &GNUNET_TRANSPORT_TESTING_connect_check,
197                                      ccc))
198     return 1;
199   return 0;
200 }
201
202
203 /* end of transport_api_blacklisting.c */