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