72f8a71a377e37493a5d0605fa548ec33765a7a2
[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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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 #define TEST_MESSAGE_SIZE 2600
34
35 #define TEST_MESSAGE_TYPE 12345
36
37
38 static struct GNUNET_TRANSPORT_TransmitHandle *th;
39
40 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
41
42 static int connected;
43
44 static int blacklist_request_p1;
45
46 static int blacklist_request_p2;
47
48 static struct GNUNET_TRANSPORT_Blacklist *blacklist_p1;
49
50 static struct GNUNET_TRANSPORT_Blacklist *blacklist_p2;
51
52 static struct GNUNET_SCHEDULER_Task *send_task;
53
54 static struct GNUNET_SCHEDULER_Task *shutdown_task;
55
56
57 static void
58 end (void *cls)
59 {
60   shutdown_task = NULL;
61   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
62               "Stopping\n");
63   if ((GNUNET_YES == blacklist_request_p1) &&
64       (GNUNET_YES == blacklist_request_p2) &&
65       (GNUNET_NO == connected) )
66   {
67     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
68                 "Peers were never connected, success\n");
69     ccc->global_ret = GNUNET_OK;
70   }
71   else
72   {
73     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
74                 "Peers were not connected, fail\n");
75     ccc->global_ret = GNUNET_SYSERR;
76   }
77   GNUNET_SCHEDULER_shutdown ();
78 }
79
80
81 static void
82 custom_shutdown (void *cls)
83 {
84   if (NULL != send_task)
85   {
86     GNUNET_SCHEDULER_cancel (send_task);
87     send_task = NULL;
88   }
89   if (NULL != shutdown_task)
90   {
91     GNUNET_SCHEDULER_cancel (shutdown_task);
92     shutdown_task = NULL;
93   }
94   if (NULL != th)
95   {
96     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
97     th = NULL;
98   }
99   if (NULL != blacklist_p1)
100   {
101     GNUNET_TRANSPORT_blacklist_cancel (blacklist_p1);
102     blacklist_p1 = NULL;
103   }
104   if (NULL != blacklist_p2)
105   {
106     GNUNET_TRANSPORT_blacklist_cancel (blacklist_p2);
107     blacklist_p2 = NULL;
108   }
109 }
110
111
112 static void
113 notify_receive (void *cls,
114                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
115                 const struct GNUNET_PeerIdentity *sender,
116                 const struct GNUNET_MessageHeader *message)
117 {
118   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
119               "Unexpectedly even received the message despite blacklist\n");
120   GNUNET_SCHEDULER_shutdown ();
121 }
122
123
124 static size_t
125 notify_ready (void *cls,
126               size_t size,
127               void *buf)
128 {
129   struct GNUNET_MessageHeader *hdr;
130
131   th = NULL;
132   if (NULL == buf)
133   {
134     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
135                 "Timeout occurred while waiting for transmit_ready\n");
136     GNUNET_SCHEDULER_shutdown ();
137     return 0;
138   }
139   GNUNET_assert (size >= TEST_MESSAGE_SIZE);
140   hdr = buf;
141   hdr->size = htons (TEST_MESSAGE_SIZE);
142   hdr->type = htons (TEST_MESSAGE_TYPE);
143   return TEST_MESSAGE_SIZE;
144 }
145
146
147 static void
148 sendtask (void *cls)
149 {
150   th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[1]->th,
151                                                &ccc->p[0]->id,
152                                                TEST_MESSAGE_SIZE,
153                                                TIMEOUT,
154                                                &notify_ready,
155                                                ccc->p[0]);
156 }
157
158
159 static void
160 notify_connect (void *cls,
161                 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
162                 const struct GNUNET_PeerIdentity *other)
163 {
164   GNUNET_TRANSPORT_TESTING_log_connect (cls,
165                                         me,
166                                         other);
167   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
168               "Peers connected despite blacklist!\n");
169   connected = GNUNET_YES; /* this test now failed */
170   GNUNET_SCHEDULER_cancel (shutdown_task);
171   end (NULL);
172 }
173
174
175 static void
176 notify_disconnect (void *cls,
177                    struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
178                    const struct GNUNET_PeerIdentity *other)
179 {
180   GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
181                                            me,
182                                            other);
183   if (NULL != th)
184   {
185     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
186     th = NULL;
187   }
188 }
189
190
191 static int
192 blacklist_cb (void *cls,
193               const struct GNUNET_PeerIdentity *pid)
194 {
195   struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
196   int res = GNUNET_SYSERR;
197
198   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199               "Peer %u: Blacklist request for peer `%s'\n",
200               p->no,
201               GNUNET_i2s (pid));
202
203   if (p == ccc->p[0])
204   {
205     blacklist_request_p1 = GNUNET_YES;
206     res = GNUNET_OK;
207   }
208   if (p == ccc->p[1])
209   {
210     blacklist_request_p2 = GNUNET_YES;
211     res = GNUNET_SYSERR;
212   }
213
214   if ( (GNUNET_YES == blacklist_request_p2) &&
215        (GNUNET_YES == blacklist_request_p1) &&
216        (NULL == shutdown_task) )
217   {
218     shutdown_task
219       = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3),
220                                       &end,
221                                       NULL);
222   }
223   return res;
224 }
225
226
227 static void
228 start_blacklist (void *cls)
229 {
230   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
231               "Starting blacklists\n");
232   blacklist_p1 = GNUNET_TRANSPORT_blacklist (ccc->p[0]->cfg,
233                                              &blacklist_cb,
234                                              ccc->p[0]);
235   GNUNET_assert (NULL != blacklist_p1);
236   blacklist_p2 = GNUNET_TRANSPORT_blacklist (ccc->p[1]->cfg,
237                                              &blacklist_cb,
238                                              ccc->p[1]);
239   GNUNET_assert (NULL != blacklist_p2);
240 }
241
242
243 int
244 main (int argc,
245       char *argv[])
246 {
247   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
248     .pre_connect_task = &start_blacklist,
249     .connect_continuation = &sendtask,
250     .config_file = "test_transport_api_data.conf",
251     .rec = &notify_receive,
252     .nc = &notify_connect,
253     .nd = &notify_disconnect,
254     .shutdown_task = &custom_shutdown,
255     .timeout = TIMEOUT,
256     .bi_directional = GNUNET_YES
257   };
258
259   ccc = &my_ccc;
260   if (GNUNET_OK !=
261       GNUNET_TRANSPORT_TESTING_main (2,
262                                      &GNUNET_TRANSPORT_TESTING_connect_check,
263                                      ccc))
264     return 1;
265   return 0;
266 }
267
268
269 /* end of transport_api_blacklisting.c */