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
24 * @author Matthias Wachs
28 #include "gnunet_common.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_program_lib.h"
33 #include "gnunet_scheduler_lib.h"
34 #include "gnunet_transport_service.h"
35 #include "transport.h"
36 #include "transport-testing.h"
38 #define VERBOSE GNUNET_NO
39 #define VERBOSE_ARM GNUNET_NO
41 #define START_ARM GNUNET_YES
44 struct PeerContext *p1;
46 struct PeerContext *p2;
48 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
50 struct GNUNET_TRANSPORT_TransmitHandle *th;
52 struct GNUNET_TRANSPORT_TESTING_handle *tth;
55 * How long until we give up on transmitting the message?
57 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
60 * How long until we give up on transmitting the message?
62 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
64 #define TEST_MESSAGE_SIZE 2600
66 #define TEST_MESSAGE_TYPE 12345
71 static int blacklist_request_p1;
72 static int blacklist_request_p2;
74 struct GNUNET_TRANSPORT_Blacklist * blacklist_p1;
76 struct GNUNET_TRANSPORT_Blacklist * blacklist_p2;
78 static GNUNET_SCHEDULER_TaskIdentifier die_task;
80 static GNUNET_SCHEDULER_TaskIdentifier send_task;
82 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
85 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
87 #define OKPP do { ok++; } while (0)
92 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
96 if (send_task != GNUNET_SCHEDULER_NO_TASK)
97 GNUNET_SCHEDULER_cancel (send_task);
99 if (die_task != GNUNET_SCHEDULER_NO_TASK)
101 GNUNET_SCHEDULER_cancel (die_task);
102 die_task = GNUNET_SCHEDULER_NO_TASK;
107 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
112 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
115 if (blacklist_p1 != NULL)
117 GNUNET_TRANSPORT_blacklist_cancel (blacklist_p1);
121 if (blacklist_p2 != NULL)
123 GNUNET_TRANSPORT_blacklist_cancel (blacklist_p2);
129 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
134 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
138 if ((blacklist_request_p1 == GNUNET_YES) &&
139 (blacklist_request_p2 == GNUNET_YES) &&
140 (connected == GNUNET_NO))
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Peers were not connected, success\n"));
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Peers were not connected, fail\n"));
153 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
155 if (send_task != GNUNET_SCHEDULER_NO_TASK)
157 GNUNET_SCHEDULER_cancel (send_task);
158 send_task = GNUNET_SCHEDULER_NO_TASK;
161 if (shutdown_task != GNUNET_SCHEDULER_NO_TASK)
163 GNUNET_SCHEDULER_cancel (shutdown_task);
164 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
170 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
175 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
178 if (blacklist_p1 != NULL)
179 GNUNET_TRANSPORT_blacklist_cancel (blacklist_p1);
181 if (blacklist_p2 != NULL)
182 GNUNET_TRANSPORT_blacklist_cancel (blacklist_p2);
185 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
187 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
194 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
195 const struct GNUNET_MessageHeader *message,
196 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
198 struct PeerContext *p = cls;
199 struct PeerContext *t = NULL;
201 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
203 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
205 GNUNET_assert (t != NULL);
207 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
211 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
212 GNUNET_i2s (&t->id));
215 if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
216 (TEST_MESSAGE_SIZE == ntohs (message->size)))
219 shutdown_task = GNUNET_SCHEDULER_add_now(&end, NULL);
225 shutdown_task = GNUNET_SCHEDULER_add_now(&end, NULL);
231 notify_ready (void *cls, size_t size, void *buf)
233 struct PeerContext *p = cls;
234 struct GNUNET_MessageHeader *hdr;
240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
241 "Timeout occurred while waiting for transmit_ready\n");
242 if (GNUNET_SCHEDULER_NO_TASK != die_task)
244 GNUNET_SCHEDULER_cancel (die_task);
245 die_task = GNUNET_SCHEDULER_NO_TASK;
247 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
252 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
257 hdr->size = htons (TEST_MESSAGE_SIZE);
258 hdr->type = htons (TEST_MESSAGE_TYPE);
261 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
263 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
264 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
265 GNUNET_i2s (&p->id));
268 return TEST_MESSAGE_SIZE;
272 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
274 send_task = GNUNET_SCHEDULER_NO_TASK;
276 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
278 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
282 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
283 GNUNET_free (receiver_s);
285 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE, 0,
286 TIMEOUT_TRANSMIT, ¬ify_ready,
291 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
292 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
297 struct PeerContext *p = cls;
298 struct PeerContext *t = NULL;
300 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
302 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
304 GNUNET_assert (t != NULL);
306 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
309 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
310 t->no, GNUNET_i2s (peer));
316 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
318 struct PeerContext *p = cls;
319 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
328 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
333 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
336 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
339 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
344 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
348 int blacklist_cb (void *cls,
350 GNUNET_PeerIdentity * pid)
352 struct PeerContext * p = cls;
353 int res = GNUNET_SYSERR;
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u : Blacklist request for peer `%s'\n", p->no, GNUNET_i2s (pid));
358 blacklist_request_p1 = GNUNET_YES;
363 blacklist_request_p2 = GNUNET_YES;
367 if (((blacklist_request_p2 == GNUNET_YES) && (blacklist_request_p1 == GNUNET_YES)) && (shutdown_task == GNUNET_SCHEDULER_NO_TASK))
369 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &end, NULL);
376 start_cb (struct PeerContext *p, void *cls)
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
383 GNUNET_i2s (&p->id));
388 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
392 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
393 GNUNET_free (sender_c);
395 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
401 run (void *cls, char *const *args, const char *cfgfile,
402 const struct GNUNET_CONFIGURATION_Handle *cfg)
404 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
405 connected = GNUNET_NO;
406 blacklist_request_p1 = GNUNET_NO;
407 blacklist_request_p2 = GNUNET_NO;
409 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer1.conf", 1,
410 ¬ify_receive, ¬ify_connect,
411 ¬ify_disconnect, &start_cb,
414 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer2.conf", 2,
415 ¬ify_receive, ¬ify_connect,
416 ¬ify_disconnect, &start_cb,
419 blacklist_p1 = GNUNET_TRANSPORT_blacklist (p1->cfg,
423 blacklist_p2 = GNUNET_TRANSPORT_blacklist (p2->cfg,
427 GNUNET_assert (blacklist_p1 != NULL);
428 GNUNET_assert (blacklist_p2 != NULL);
435 static char *const argv[] = { "test-transport-api-blacklisting",
437 "test_transport_api_data.conf",
443 static struct GNUNET_GETOPT_CommandLineOption options[] = {
444 GNUNET_GETOPT_OPTION_END
448 setTransportOptions ("test_transport_api_data.conf");
452 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-transport-api-blacklisting",
453 "nohelp", options, &run, &ok);
459 main (int argc, char *argv[])
463 GNUNET_log_setup ("test-transport-api-blacklisting",
471 tth = GNUNET_TRANSPORT_TESTING_init ();
475 GNUNET_TRANSPORT_TESTING_done (tth);
480 /* end of transport_api_blacklisting.c */