aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_blacklisting.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/test_transport_blacklisting.c')
-rw-r--r--src/transport/test_transport_blacklisting.c338
1 files changed, 338 insertions, 0 deletions
diff --git a/src/transport/test_transport_blacklisting.c b/src/transport/test_transport_blacklisting.c
new file mode 100644
index 000000000..8423c641e
--- /dev/null
+++ b/src/transport/test_transport_blacklisting.c
@@ -0,0 +1,338 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/**
22 * @file transport/transport_api_blacklisting.c
23 * @brief test for the blacklisting API
24 * stage 0: init
25 * stage 1: connect peers and stop
26 * stage 2: blacklist whole peer and connect
27 * stage 3: blacklist tcp and try connect
28 *
29 * @author Matthias Wachs
30 *
31 */
32#include "platform.h"
33#include "gnunet_transport_service.h"
34#include "transport-testing.h"
35
36char *test_name;
37
38struct PeerContext *p1;
39
40struct PeerContext *p2;
41
42static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
43
44struct GNUNET_TRANSPORT_TESTING_handle *tth;
45
46/**
47 * How long until we give up on transmitting the message?
48 */
49#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
50
51#define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
52
53
54static int stage;
55static int ok;
56static int connected;
57
58static GNUNET_SCHEDULER_TaskIdentifier die_task;
59
60static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
61
62static GNUNET_SCHEDULER_TaskIdentifier stage_task;
63
64#if VERBOSE
65#define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
66#else
67#define OKPP do { ok++; } while (0)
68#endif
69
70static void
71run_stage (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
72
73static void
74end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
75{
76 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
77
78 if (die_task != GNUNET_SCHEDULER_NO_TASK)
79 {
80 GNUNET_SCHEDULER_cancel (die_task);
81 die_task = GNUNET_SCHEDULER_NO_TASK;
82 }
83
84 if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
85 {
86 GNUNET_SCHEDULER_cancel (timeout_task);
87 timeout_task = GNUNET_SCHEDULER_NO_TASK;
88 }
89
90 if (stage_task != GNUNET_SCHEDULER_NO_TASK)
91 {
92 GNUNET_SCHEDULER_cancel (stage_task);
93 stage_task = GNUNET_SCHEDULER_NO_TASK;
94 }
95
96 if (cc != NULL)
97 {
98 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
99 cc = NULL;
100 }
101
102 if (p1 != NULL)
103 {
104 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
105 p1 = NULL;
106 }
107 if (p2 != NULL)
108 {
109 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
110 p2 = NULL;
111 }
112}
113
114static void
115end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116{
117 die_task = GNUNET_SCHEDULER_NO_TASK;
118
119 if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
120 {
121 GNUNET_SCHEDULER_cancel (timeout_task);
122 timeout_task = GNUNET_SCHEDULER_NO_TASK;
123 }
124
125 if (stage_task != GNUNET_SCHEDULER_NO_TASK)
126 {
127 GNUNET_SCHEDULER_cancel (stage_task);
128 stage_task = GNUNET_SCHEDULER_NO_TASK;
129 }
130
131
132 if (cc != NULL)
133 {
134 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
135 cc = NULL;
136 }
137 if (p1 != NULL)
138 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
139 if (p2 != NULL)
140 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
141
142 ok = GNUNET_SYSERR;
143}
144
145static void
146testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
147{
148 cc = NULL;
149 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
150
151 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
152 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
153 GNUNET_free (p1_c);
154 connected = GNUNET_YES;
155 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
156}
157
158static void
159connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160{
161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers not connected, next stage\n");
162 timeout_task = GNUNET_SCHEDULER_NO_TASK;
163 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
164}
165
166static int started;
167
168void
169start_cb (struct PeerContext *p, void *cls)
170{
171
172 started++;
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
174 GNUNET_i2s (&p->id));
175
176 if (started != 2)
177 return;
178
179 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
180
181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
182 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
183 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
184 GNUNET_free (sender_c);
185
186 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
187 NULL);
188
189}
190
191static void
192run_stage (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
193{
194 stage_task = GNUNET_SCHEDULER_NO_TASK;
195 if (GNUNET_SCHEDULER_NO_TASK != die_task)
196 GNUNET_SCHEDULER_cancel (die_task);
197 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running stage %u\n", stage);
199
200 if (0 == stage)
201 {
202 started = GNUNET_NO;
203 connected = GNUNET_NO;
204 if (0 == strcmp(test_name, "test_transport_blacklisting_no_bl"))
205 {
206 /* Try to connect peers successfully */
207 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer1.conf", 1,
208 NULL, NULL, NULL, &start_cb, NULL);
209
210 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer2.conf", 2,
211 NULL, NULL, NULL, &start_cb, NULL);
212 }
213 else if (0 == strcmp(test_name, "test_transport_blacklisting_outbound_bl_full"))
214 {
215 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_blp_peer1_full.conf", 1,
216 NULL, NULL, NULL, &start_cb, NULL);
217
218 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer2.conf", 2,
219 NULL, NULL, NULL, &start_cb, NULL);
220 }
221 else if (0 == strcmp(test_name, "test_transport_blacklisting_outbound_bl_plugin"))
222 {
223 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_blp_peer1_plugin.conf", 1,
224 NULL, NULL, NULL, &start_cb, NULL);
225
226 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer2.conf", 2,
227 NULL, NULL, NULL, &start_cb, NULL);
228 }
229 else if (0 == strcmp(test_name, "test_transport_blacklisting_inbound_bl_full"))
230 {
231 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer1.conf", 1,
232 NULL, NULL, NULL, &start_cb, NULL);
233
234 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_blp_peer2_full.conf", 2,
235 NULL, NULL, NULL, &start_cb, NULL);
236 }
237 else if (0 == strcmp(test_name, "test_transport_blacklisting_inbound_bl_plugin"))
238 {
239 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer1.conf", 1,
240 NULL, NULL, NULL, &start_cb, NULL);
241
242 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_blp_peer2_plugin.conf", 2,
243 NULL, NULL, NULL, &start_cb, NULL);
244 }
245
246 timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT, &connect_timeout, NULL);
247 stage ++;
248 return;
249 }
250
251
252 if (cc != NULL)
253 {
254 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
255 cc = NULL;
256 }
257
258 if (p1 != NULL)
259 {
260 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
261 p1 = NULL;
262 }
263 if (p2 != NULL)
264 {
265 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
266 p2 = NULL;
267 }
268
269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Done in stage %u: Peers %s and %s!\n", stage,
270 (GNUNET_NO == started) ? "NOT STARTED" : "STARTED",
271 (GNUNET_YES == connected) ? "CONNECTED" : "NOT CONNECTED");
272
273 if (0 == strcmp(test_name, "test_transport_blacklisting_no_bl"))
274 {
275 if ((GNUNET_NO != started) && (GNUNET_YES == connected))
276 ok = 0;
277 else
278 {
279 GNUNET_break (0);
280 ok = 1;
281 }
282 }
283 else
284 {
285 if ((GNUNET_NO != started) && (GNUNET_YES != connected))
286 ok = 0;
287 else
288 {
289 ok = 1;
290 }
291 }
292 GNUNET_SCHEDULER_add_now (&end, NULL);
293}
294
295static void
296run (void *cls, char *const *args, const char *cfgfile,
297 const struct GNUNET_CONFIGURATION_Handle *cfg)
298{
299 connected = GNUNET_NO;
300 stage = 0;
301 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running test `%s'!\n", test_name);
302 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
303}
304
305
306int
307main (int argc, char *argv0[])
308{
309 ok = 1;
310
311 GNUNET_TRANSPORT_TESTING_get_test_name (argv0[0], &test_name);
312
313 GNUNET_log_setup ("test-transport-api-blacklisting",
314 "WARNING",
315 NULL);
316
317 static char *const argv[] = { "date",
318 "-c",
319 "test_transport_api_data.conf",
320 NULL
321 };
322 static struct GNUNET_GETOPT_CommandLineOption options[] = {
323 GNUNET_GETOPT_OPTION_END
324 };
325
326 tth = GNUNET_TRANSPORT_TESTING_init ();
327
328 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
329 "test-transport-api-blacklisting",
330 "nohelp", options, &run, NULL);
331
332
333 GNUNET_TRANSPORT_TESTING_done (tth);
334
335 return ok;
336}
337
338/* end of transport_api_blacklisting.c */