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