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)
79 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
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"));
140 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
142 if (send_task != NULL)
144 GNUNET_SCHEDULER_cancel (send_task);
148 if (shutdown_task != NULL)
150 GNUNET_SCHEDULER_cancel (shutdown_task);
151 shutdown_task = NULL;
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
157 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
162 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
165 if (blacklist_p1 != NULL)
166 GNUNET_TRANSPORT_blacklist_cancel (blacklist_p1);
168 if (blacklist_p2 != NULL)
169 GNUNET_TRANSPORT_blacklist_cancel (blacklist_p2);
172 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
174 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
181 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
182 const struct GNUNET_MessageHeader *message)
184 struct PeerContext *p = cls;
185 struct PeerContext *t = NULL;
187 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
189 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
191 GNUNET_assert (t != NULL);
193 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
197 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
198 GNUNET_i2s (&t->id));
201 if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
202 (TEST_MESSAGE_SIZE == ntohs (message->size)))
205 shutdown_task = GNUNET_SCHEDULER_add_now(&end, NULL);
211 shutdown_task = GNUNET_SCHEDULER_add_now(&end, NULL);
217 notify_ready (void *cls, size_t size, void *buf)
219 struct PeerContext *p = cls;
220 struct GNUNET_MessageHeader *hdr;
226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
227 "Timeout occurred while waiting for transmit_ready\n");
228 if (NULL != die_task)
230 GNUNET_SCHEDULER_cancel (die_task);
233 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
238 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
240 hdr->size = htons (TEST_MESSAGE_SIZE);
241 hdr->type = htons (TEST_MESSAGE_TYPE);
244 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
247 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
253 GNUNET_i2s (&p->id));
257 return TEST_MESSAGE_SIZE;
262 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
266 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
268 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
271 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
272 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
273 GNUNET_free (receiver_s);
275 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id,
277 TIMEOUT_TRANSMIT, ¬ify_ready,
283 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
288 struct PeerContext *p = cls;
289 struct PeerContext *t = NULL;
291 connected = GNUNET_YES;
292 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
294 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
296 GNUNET_assert (t != NULL);
299 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
313 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
315 struct PeerContext *p = cls;
316 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
325 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
331 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
334 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
336 connected = GNUNET_YES;
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
338 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
343 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
348 blacklist_cb (void *cls,
350 GNUNET_PeerIdentity * pid)
352 struct PeerContext * p = cls;
353 int res = GNUNET_SYSERR;
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "Peer %u : Blacklist request for peer `%s'\n",
362 blacklist_request_p1 = GNUNET_YES;
367 blacklist_request_p2 = GNUNET_YES;
371 if (((blacklist_request_p2 == GNUNET_YES) && (blacklist_request_p1 == GNUNET_YES)) && (shutdown_task == NULL))
373 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &end, NULL);
381 start_cb (struct PeerContext *p, void *cls)
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
388 GNUNET_i2s (&p->id));
393 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
397 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
398 GNUNET_free (sender_c);
400 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
407 run (void *cls, char *const *args, const char *cfgfile,
408 const struct GNUNET_CONFIGURATION_Handle *cfg)
410 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
411 connected = GNUNET_NO;
412 blacklist_request_p1 = GNUNET_NO;
413 blacklist_request_p2 = GNUNET_NO;
415 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer1.conf", 1,
416 ¬ify_receive, ¬ify_connect,
417 ¬ify_disconnect, &start_cb,
420 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer2.conf", 2,
421 ¬ify_receive, ¬ify_connect,
422 ¬ify_disconnect, &start_cb,
425 blacklist_p1 = GNUNET_TRANSPORT_blacklist (p1->cfg,
429 blacklist_p2 = GNUNET_TRANSPORT_blacklist (p2->cfg,
433 GNUNET_assert (blacklist_p1 != NULL);
434 GNUNET_assert (blacklist_p2 != NULL);
441 static char *const argv[] = { "test-transport-api-blacklisting",
443 "test_transport_api_data.conf",
446 static struct GNUNET_GETOPT_CommandLineOption options[] = {
447 GNUNET_GETOPT_OPTION_END
451 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-transport-api-blacklisting",
452 "nohelp", options, &run, &ok);
458 main (int argc, char *argv[])
462 GNUNET_log_setup ("test-transport-api-blacklisting",
466 tth = GNUNET_TRANSPORT_TESTING_init ();
470 GNUNET_TRANSPORT_TESTING_done (tth);
475 /* end of transport_api_blacklisting.c */