2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file transport/transport_api_blacklisting.c
23 * @brief test for the blacklisting API
24 * @author Matthias Wachs
28 #include "gnunet_transport_service.h"
29 #include "transport-testing.h"
31 static struct PeerContext *p1;
33 static struct PeerContext *p2;
35 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
37 static struct GNUNET_TRANSPORT_TransmitHandle *th;
39 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
42 * How long until we give up on transmitting the message?
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
47 * How long until we give up on transmitting the message?
49 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
51 #define TEST_MESSAGE_SIZE 2600
53 #define TEST_MESSAGE_TYPE 12345
58 static int blacklist_request_p1;
59 static int blacklist_request_p2;
61 static struct GNUNET_TRANSPORT_Blacklist * blacklist_p1;
63 static struct GNUNET_TRANSPORT_Blacklist * blacklist_p2;
65 static struct GNUNET_SCHEDULER_Task * die_task;
67 static struct GNUNET_SCHEDULER_Task * send_task;
69 static struct GNUNET_SCHEDULER_Task * shutdown_task;
72 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
74 #define OKPP do { ok++; } while (0)
81 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
83 if (send_task != NULL)
84 GNUNET_SCHEDULER_cancel (send_task);
88 GNUNET_SCHEDULER_cancel (die_task);
94 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
99 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
102 if (blacklist_p1 != NULL)
104 GNUNET_TRANSPORT_blacklist_cancel (blacklist_p1);
108 if (blacklist_p2 != NULL)
110 GNUNET_TRANSPORT_blacklist_cancel (blacklist_p2);
116 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
121 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
125 if ((blacklist_request_p1 == GNUNET_YES) &&
126 (blacklist_request_p2 == GNUNET_YES) &&
127 (connected == GNUNET_NO))
129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Peers were not connected, success\n"));
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Peers were not connected, fail\n"));
141 end_badly (void *cls)
143 if (send_task != NULL)
145 GNUNET_SCHEDULER_cancel (send_task);
149 if (shutdown_task != NULL)
151 GNUNET_SCHEDULER_cancel (shutdown_task);
152 shutdown_task = NULL;
157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
158 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
163 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
166 if (blacklist_p1 != NULL)
167 GNUNET_TRANSPORT_blacklist_cancel (blacklist_p1);
169 if (blacklist_p2 != NULL)
170 GNUNET_TRANSPORT_blacklist_cancel (blacklist_p2);
173 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
175 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
182 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
183 const struct GNUNET_MessageHeader *message)
185 struct PeerContext *p = cls;
186 struct PeerContext *t = NULL;
188 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
190 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
192 GNUNET_assert (t != NULL);
194 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
198 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
199 GNUNET_i2s (&t->id));
202 if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
203 (TEST_MESSAGE_SIZE == ntohs (message->size)))
206 shutdown_task = GNUNET_SCHEDULER_add_now(&end, NULL);
212 shutdown_task = GNUNET_SCHEDULER_add_now(&end, NULL);
218 notify_ready (void *cls, size_t size, void *buf)
220 struct PeerContext *p = cls;
221 struct GNUNET_MessageHeader *hdr;
227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
228 "Timeout occurred while waiting for transmit_ready\n");
229 if (NULL != die_task)
231 GNUNET_SCHEDULER_cancel (die_task);
234 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
239 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
241 hdr->size = htons (TEST_MESSAGE_SIZE);
242 hdr->type = htons (TEST_MESSAGE_TYPE);
245 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
248 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
254 GNUNET_i2s (&p->id));
258 return TEST_MESSAGE_SIZE;
266 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
269 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
270 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
271 GNUNET_free (receiver_s);
273 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id,
275 TIMEOUT_TRANSMIT, ¬ify_ready,
281 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
286 struct PeerContext *p = cls;
287 struct PeerContext *t = NULL;
289 connected = GNUNET_YES;
290 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
292 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
294 GNUNET_assert (t != NULL);
297 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
311 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
313 struct PeerContext *p = cls;
314 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
323 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
329 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
332 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
334 connected = GNUNET_YES;
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
336 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
341 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
346 blacklist_cb (void *cls,
348 GNUNET_PeerIdentity * pid)
350 struct PeerContext * p = cls;
351 int res = GNUNET_SYSERR;
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 "Peer %u : Blacklist request for peer `%s'\n",
360 blacklist_request_p1 = GNUNET_YES;
365 blacklist_request_p2 = GNUNET_YES;
369 if (((blacklist_request_p2 == GNUNET_YES) && (blacklist_request_p1 == GNUNET_YES)) && (shutdown_task == NULL))
371 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &end, NULL);
379 start_cb (struct PeerContext *p, void *cls)
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
386 GNUNET_i2s (&p->id));
391 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
395 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
396 GNUNET_free (sender_c);
398 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
405 run (void *cls, char *const *args, const char *cfgfile,
406 const struct GNUNET_CONFIGURATION_Handle *cfg)
408 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
409 connected = GNUNET_NO;
410 blacklist_request_p1 = GNUNET_NO;
411 blacklist_request_p2 = GNUNET_NO;
413 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer1.conf", 1,
414 ¬ify_receive, ¬ify_connect,
415 ¬ify_disconnect, &start_cb,
418 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer2.conf", 2,
419 ¬ify_receive, ¬ify_connect,
420 ¬ify_disconnect, &start_cb,
423 blacklist_p1 = GNUNET_TRANSPORT_blacklist (p1->cfg,
427 blacklist_p2 = GNUNET_TRANSPORT_blacklist (p2->cfg,
431 GNUNET_assert (blacklist_p1 != NULL);
432 GNUNET_assert (blacklist_p2 != NULL);
439 static char *const argv[] = { "test-transport-api-blacklisting",
441 "test_transport_api_data.conf",
444 static struct GNUNET_GETOPT_CommandLineOption options[] = {
445 GNUNET_GETOPT_OPTION_END
449 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-transport-api-blacklisting",
450 "nohelp", options, &run, &ok);
456 main (int argc, char *argv[])
460 GNUNET_log_setup ("test-transport-api-blacklisting",
464 tth = GNUNET_TRANSPORT_TESTING_init ();
468 GNUNET_TRANSPORT_TESTING_done (tth);
473 /* end of transport_api_blacklisting.c */