Merge remote-tracking branch 'origin/identity_abe' into identity_oidc
[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 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 (GNUNET_TIME_UNIT_SECONDS, 3),
152                                       &end,
153                                       NULL);
154   }
155   return res;
156 }
157
158
159 static void
160 start_blacklist (void *cls)
161 {
162   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163               "Starting blacklists\n");
164   blacklist_p1 = GNUNET_TRANSPORT_blacklist (ccc->p[0]->cfg,
165                                              &blacklist_cb,
166                                              ccc->p[0]);
167   GNUNET_assert (NULL != blacklist_p1);
168   blacklist_p2 = GNUNET_TRANSPORT_blacklist (ccc->p[1]->cfg,
169                                              &blacklist_cb,
170                                              ccc->p[1]);
171   GNUNET_assert (NULL != blacklist_p2);
172 }
173
174
175 int
176 main (int argc,
177       char *argv[])
178 {
179   struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
180     .num_messages = 1
181   };
182   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
183     .pre_connect_task = &start_blacklist,
184     .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
185     .connect_continuation_cls = &sc,
186     .config_file = "test_transport_api_data.conf",
187     .rec = &notify_receive,
188     .nc = &notify_connect,
189     .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
190     .shutdown_task = &custom_shutdown,
191     .timeout = TIMEOUT,
192     .bi_directional = GNUNET_YES
193   };
194
195   ccc = &my_ccc;
196   if (GNUNET_OK !=
197       GNUNET_TRANSPORT_TESTING_main (2,
198                                      &GNUNET_TRANSPORT_TESTING_connect_check,
199                                      ccc))
200     return 1;
201   return 0;
202 }
203
204
205 /* end of transport_api_blacklisting.c */