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_transport_service.h"
29 #include "transport-testing.h"
31 struct PeerContext *p1;
33 struct PeerContext *p2;
35 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
37 struct GNUNET_TRANSPORT_TransmitHandle *th;
39 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 struct GNUNET_TRANSPORT_Blacklist * blacklist_p1;
63 struct GNUNET_TRANSPORT_Blacklist * blacklist_p2;
65 static GNUNET_SCHEDULER_TaskIdentifier die_task;
67 static GNUNET_SCHEDULER_TaskIdentifier send_task;
69 static GNUNET_SCHEDULER_TaskIdentifier 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 != GNUNET_SCHEDULER_NO_TASK)
84 GNUNET_SCHEDULER_cancel (send_task);
86 if (die_task != GNUNET_SCHEDULER_NO_TASK)
88 GNUNET_SCHEDULER_cancel (die_task);
89 die_task = GNUNET_SCHEDULER_NO_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 != GNUNET_SCHEDULER_NO_TASK)
144 GNUNET_SCHEDULER_cancel (send_task);
145 send_task = GNUNET_SCHEDULER_NO_TASK;
148 if (shutdown_task != GNUNET_SCHEDULER_NO_TASK)
150 GNUNET_SCHEDULER_cancel (shutdown_task);
151 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
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,
183 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
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 (GNUNET_SCHEDULER_NO_TASK != die_task)
231 GNUNET_SCHEDULER_cancel (die_task);
232 die_task = GNUNET_SCHEDULER_NO_TASK;
234 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
239 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
244 hdr->size = htons (TEST_MESSAGE_SIZE);
245 hdr->type = htons (TEST_MESSAGE_TYPE);
248 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
251 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
252 GNUNET_i2s (&p->id));
255 return TEST_MESSAGE_SIZE;
259 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
261 send_task = GNUNET_SCHEDULER_NO_TASK;
263 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
265 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
269 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
270 GNUNET_free (receiver_s);
272 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE, 0,
273 TIMEOUT_TRANSMIT, ¬ify_ready,
278 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
279 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
284 struct PeerContext *p = cls;
285 struct PeerContext *t = NULL;
287 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
289 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
291 GNUNET_assert (t != NULL);
293 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
297 t->no, GNUNET_i2s (peer));
303 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
305 struct PeerContext *p = cls;
306 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
309 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
315 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
320 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
323 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
326 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
331 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
335 int blacklist_cb (void *cls,
337 GNUNET_PeerIdentity * pid)
339 struct PeerContext * p = cls;
340 int res = GNUNET_SYSERR;
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u : Blacklist request for peer `%s'\n", p->no, GNUNET_i2s (pid));
345 blacklist_request_p1 = GNUNET_YES;
350 blacklist_request_p2 = GNUNET_YES;
354 if (((blacklist_request_p2 == GNUNET_YES) && (blacklist_request_p1 == GNUNET_YES)) && (shutdown_task == GNUNET_SCHEDULER_NO_TASK))
356 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &end, NULL);
363 start_cb (struct PeerContext *p, void *cls)
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
370 GNUNET_i2s (&p->id));
375 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
379 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
380 GNUNET_free (sender_c);
382 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
388 run (void *cls, char *const *args, const char *cfgfile,
389 const struct GNUNET_CONFIGURATION_Handle *cfg)
391 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
392 connected = GNUNET_NO;
393 blacklist_request_p1 = GNUNET_NO;
394 blacklist_request_p2 = GNUNET_NO;
396 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer1.conf", 1,
397 ¬ify_receive, ¬ify_connect,
398 ¬ify_disconnect, &start_cb,
401 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer2.conf", 2,
402 ¬ify_receive, ¬ify_connect,
403 ¬ify_disconnect, &start_cb,
406 blacklist_p1 = GNUNET_TRANSPORT_blacklist (p1->cfg,
410 blacklist_p2 = GNUNET_TRANSPORT_blacklist (p2->cfg,
414 GNUNET_assert (blacklist_p1 != NULL);
415 GNUNET_assert (blacklist_p2 != NULL);
422 static char *const argv[] = { "test-transport-api-blacklisting",
424 "test_transport_api_data.conf",
427 static struct GNUNET_GETOPT_CommandLineOption options[] = {
428 GNUNET_GETOPT_OPTION_END
432 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-transport-api-blacklisting",
433 "nohelp", options, &run, &ok);
439 main (int argc, char *argv[])
443 GNUNET_log_setup ("test-transport-api-blacklisting",
447 tth = GNUNET_TRANSPORT_TESTING_init ();
451 GNUNET_TRANSPORT_TESTING_done (tth);
456 /* end of transport_api_blacklisting.c */