2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file transport/transport_api_blacklisting.c
23 * @brief test for the blacklisting API
25 * stage 1: connect peers and stop
26 * stage 2: blacklist whole peer and connect
27 * stage 3: blacklist tcp and try connect
29 * @author Matthias Wachs
33 #include "gnunet_transport_service.h"
34 #include "transport-testing.h"
36 struct PeerContext *p1;
38 struct PeerContext *p2;
40 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
42 struct GNUNET_TRANSPORT_TESTING_handle *tth;
45 * How long until we give up on transmitting the message?
47 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
49 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
56 static GNUNET_SCHEDULER_TaskIdentifier die_task;
58 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
60 static GNUNET_SCHEDULER_TaskIdentifier stage_task;
63 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
65 #define OKPP do { ok++; } while (0)
69 run_stage (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
72 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
74 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
76 if (die_task != GNUNET_SCHEDULER_NO_TASK)
78 GNUNET_SCHEDULER_cancel (die_task);
79 die_task = GNUNET_SCHEDULER_NO_TASK;
82 if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
84 GNUNET_SCHEDULER_cancel (timeout_task);
85 timeout_task = GNUNET_SCHEDULER_NO_TASK;
88 if (stage_task != GNUNET_SCHEDULER_NO_TASK)
90 GNUNET_SCHEDULER_cancel (stage_task);
91 stage_task = GNUNET_SCHEDULER_NO_TASK;
96 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
102 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
107 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
113 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 die_task = GNUNET_SCHEDULER_NO_TASK;
117 if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
119 GNUNET_SCHEDULER_cancel (timeout_task);
120 timeout_task = GNUNET_SCHEDULER_NO_TASK;
123 if (stage_task != GNUNET_SCHEDULER_NO_TASK)
125 GNUNET_SCHEDULER_cancel (stage_task);
126 stage_task = GNUNET_SCHEDULER_NO_TASK;
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
133 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
138 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
140 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
146 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
149 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
152 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
157 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers connected, but they were blacklisted\n");
162 stage_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
168 connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers not connected, next stage\n");
171 timeout_task = GNUNET_SCHEDULER_NO_TASK;
172 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
178 start_cb (struct PeerContext *p, void *cls)
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
183 GNUNET_i2s (&p->id));
188 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
191 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
192 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
193 GNUNET_free (sender_c);
195 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
201 run_stage (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
203 stage_task = GNUNET_SCHEDULER_NO_TASK;
204 if (GNUNET_SCHEDULER_NO_TASK != die_task)
205 GNUNET_SCHEDULER_cancel (die_task);
206 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running stage %u\n", stage);
210 /* Try to connect peers successfully */
212 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer1.conf", 1,
213 NULL, NULL, NULL, &start_cb, NULL);
215 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer2.conf", 2,
216 NULL, NULL, NULL, &start_cb, NULL);
223 /* Try to connect peers successfully with 2nd peer fully blacklisted*/
226 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
231 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
235 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer1.conf", 1,
236 NULL, NULL, NULL, &start_cb, NULL);
238 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_blp_peer2.conf", 2,
239 NULL, NULL, NULL, &start_cb, NULL);
241 timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT, &connect_timeout, NULL);
247 /* Try to connect peers successfully with 1st peer blacklisted on tcp */
250 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
255 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
259 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_blp_peer1.conf", 1,
260 NULL, NULL, NULL, &start_cb, NULL);
262 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer2.conf", 2,
263 NULL, NULL, NULL, &start_cb, NULL);
265 timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT, &connect_timeout, NULL);
269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Done!\n", stage);
273 GNUNET_SCHEDULER_add_now (&end, NULL);
277 run (void *cls, char *const *args, const char *cfgfile,
278 const struct GNUNET_CONFIGURATION_Handle *cfg)
280 connected = GNUNET_NO;
282 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
289 static char *const argv[] = { "test-transport-api-blacklisting",
291 "test_transport_api_data.conf",
294 static struct GNUNET_GETOPT_CommandLineOption options[] = {
295 GNUNET_GETOPT_OPTION_END
299 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-transport-api-blacklisting",
300 "nohelp", options, &run, &ok);
306 main (int argc, char *argv[])
310 GNUNET_log_setup ("test-transport-api-blacklisting",
314 tth = GNUNET_TRANSPORT_TESTING_init ();
318 GNUNET_TRANSPORT_TESTING_done (tth);
323 /* end of transport_api_blacklisting.c */