2 This file is part of GNUnet.
3 (C) 2009, 2010 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.
21 * @file transport/test_transport_api_monitoring_validation.c
22 * @brief base test case for transport validation monitorung
24 * This test case connects two peers and monitors validation monitoring
28 #include "gnunet_transport_service.h"
29 #include "transport-testing.h"
32 * How long until we give up on transmitting the message?
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
37 * How long until we give up on transmitting the message?
39 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
41 #define TEST_MESSAGE_SIZE 2600
43 #define TEST_MESSAGE_TYPE 12345
45 static char *test_source;
47 static char *test_plugin;
49 static char *test_name;
55 static int s_connected;
59 static GNUNET_SCHEDULER_TaskIdentifier die_task;
61 static GNUNET_SCHEDULER_TaskIdentifier send_task;
63 static struct PeerContext *p1;
65 static struct PeerContext *p2;
67 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
69 static struct GNUNET_TRANSPORT_TransmitHandle *th;
71 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
73 static char *cfg_file_p1;
75 static char *cfg_file_p2;
77 static struct GNUNET_TRANSPORT_ValidationMonitoringContext *vmc_p1;
79 static struct GNUNET_TRANSPORT_ValidationMonitoringContext *vmc_p2;
81 static int p1_c = GNUNET_NO;
83 static int p2_c = GNUNET_NO;
85 static int p1_c_notify = GNUNET_NO;
87 static int p2_c_notify = GNUNET_NO;
92 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping peers\n");
95 if (send_task != GNUNET_SCHEDULER_NO_TASK)
96 GNUNET_SCHEDULER_cancel (send_task);
98 if (die_task != GNUNET_SCHEDULER_NO_TASK)
99 GNUNET_SCHEDULER_cancel (die_task);
102 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
105 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
107 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
112 GNUNET_TRANSPORT_monitor_validation_entries_cancel (vmc_p1);
117 GNUNET_TRANSPORT_monitor_validation_entries_cancel (vmc_p2);
125 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
127 die_task = GNUNET_SCHEDULER_NO_TASK;
129 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Fail! Stopping peers\n");
132 if (send_task != GNUNET_SCHEDULER_NO_TASK)
133 GNUNET_SCHEDULER_cancel (send_task);
137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
138 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
143 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
147 if (s_started == GNUNET_NO)
148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
152 if (s_connected == GNUNET_NO)
153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
155 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
157 if (s_sending == GNUNET_NO)
158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
166 GNUNET_TRANSPORT_monitor_validation_entries_cancel (vmc_p1);
171 GNUNET_TRANSPORT_monitor_validation_entries_cancel (vmc_p2);
176 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
180 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
189 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
190 const struct GNUNET_MessageHeader *message)
192 struct PeerContext *p = cls;
193 struct PeerContext *t = NULL;
195 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
197 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
199 GNUNET_assert (t != NULL);
201 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
203 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
204 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
205 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
206 GNUNET_i2s (&t->id));
211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212 "Peer 1 did not receive validation callbacks for peer 2\n");
216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
217 "Peer 2 did not receive validation callbacks for peer 1\n");
220 if ((0 >= p1_c) || (0 >= p2_c))
222 if (GNUNET_SCHEDULER_NO_TASK != die_task)
223 GNUNET_SCHEDULER_cancel (die_task);
224 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
232 notify_ready (void *cls, size_t size, void *buf)
234 struct PeerContext *p = cls;
235 struct GNUNET_MessageHeader *hdr;
241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
242 "Timeout occurred while waiting for transmit_ready\n");
243 if (GNUNET_SCHEDULER_NO_TASK != die_task)
244 GNUNET_SCHEDULER_cancel (die_task);
245 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
250 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
253 memset (buf, '\0', TEST_MESSAGE_SIZE);
255 hdr->size = htons (TEST_MESSAGE_SIZE);
256 hdr->type = htons (TEST_MESSAGE_TYPE);
259 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
260 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
261 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
262 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
263 GNUNET_i2s (&p->id));
266 return TEST_MESSAGE_SIZE;
271 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
273 send_task = GNUNET_SCHEDULER_NO_TASK;
275 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
277 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
279 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
280 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
281 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
282 GNUNET_free (receiver_s);
283 s_sending = GNUNET_YES;
284 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE, 0,
285 TIMEOUT_TRANSMIT, ¬ify_ready,
291 if ((GNUNET_YES == p1_c) && (GNUNET_YES == p2_c) && p1_c_notify && p2_c_notify)
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Both peers state to be connected\n");
301 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
306 struct PeerContext *p = cls;
307 struct PeerContext *t = NULL;
309 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
311 p1_c_notify = GNUNET_YES;
314 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
316 p2_c_notify = GNUNET_YES;
319 GNUNET_assert (t != NULL);
321 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
323 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
324 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
325 t->no, GNUNET_i2s (peer));
326 if (p1_c_notify && p2_c_notify)
327 GNUNET_SCHEDULER_add_now(&done, NULL);
333 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
335 struct PeerContext *p = cls;
336 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
338 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
339 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
345 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
351 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
354 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
356 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
357 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
360 s_connected = GNUNET_YES;
361 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
366 start_cb (struct PeerContext *p, void *cls)
372 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer %u (`%s') started\n", p->no,
373 GNUNET_i2s (&p->id));
378 s_started = GNUNET_YES;
379 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
381 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
382 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
383 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
384 GNUNET_free (sender_c);
386 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
392 monitor1_cb (void *cls,
393 const struct GNUNET_PeerIdentity *peer,
394 const struct GNUNET_HELLO_Address *address,
395 struct GNUNET_TIME_Absolute last_validation,
396 struct GNUNET_TIME_Absolute valid_until,
397 struct GNUNET_TIME_Absolute next_validation,
398 enum GNUNET_TRANSPORT_ValidationState state)
400 if ((NULL == peer) || (NULL == p1))
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Monitor 1: %s %s %s\n",
404 GNUNET_i2s (peer), GNUNET_TRANSPORT_vs2s(state), GNUNET_STRINGS_absolute_time_to_string(valid_until));
405 if (0 == memcmp (peer, &p2->id, sizeof (p2->id)))
411 monitor2_cb (void *cls,
412 const struct GNUNET_PeerIdentity *peer,
413 const struct GNUNET_HELLO_Address *address,
414 struct GNUNET_TIME_Absolute last_validation,
415 struct GNUNET_TIME_Absolute valid_until,
416 struct GNUNET_TIME_Absolute next_validation,
417 enum GNUNET_TRANSPORT_ValidationState state)
419 if ((NULL == peer) || (NULL == p2))
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Monitor 2: %s %s %s\n",
423 GNUNET_i2s (peer), GNUNET_TRANSPORT_vs2s(state), GNUNET_STRINGS_absolute_time_to_string(valid_until));
424 if (0 == memcmp (peer, &p1->id, sizeof (p1->id)))
431 run (void *cls, char *const *args, const char *cfgfile,
432 const struct GNUNET_CONFIGURATION_Handle *cfg)
434 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
436 s_started = GNUNET_NO;
437 s_connected = GNUNET_NO;
438 s_sending = GNUNET_NO;
440 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
441 ¬ify_receive, ¬ify_connect,
442 ¬ify_disconnect, &start_cb,
444 vmc_p1 = GNUNET_TRANSPORT_monitor_validation_entries (p1->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor1_cb, NULL);
446 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
447 ¬ify_receive, ¬ify_connect,
448 ¬ify_disconnect, &start_cb,
450 vmc_p2 = GNUNET_TRANSPORT_monitor_validation_entries (p2->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor2_cb, NULL);
452 if ((p1 == NULL) || (p2 == NULL))
454 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
455 if (die_task != GNUNET_SCHEDULER_NO_TASK)
456 GNUNET_SCHEDULER_cancel (die_task);
457 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
466 static char *const argv[] = { "test-transport-api",
468 "test_transport_api_data.conf",
471 static struct GNUNET_GETOPT_CommandLineOption options[] = {
472 GNUNET_GETOPT_OPTION_END
475 send_task = GNUNET_SCHEDULER_NO_TASK;
478 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
479 "nohelp", options, &run, &ok);
485 main (int argc, char *argv[])
491 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
492 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
493 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
496 GNUNET_log_setup (test_name,
499 tth = GNUNET_TRANSPORT_TESTING_init ();
501 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
502 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
506 GNUNET_free (cfg_file_p1);
507 GNUNET_free (cfg_file_p2);
509 GNUNET_free (test_source);
510 GNUNET_free (test_plugin);
511 GNUNET_free (test_name);
513 GNUNET_TRANSPORT_TESTING_done (tth);
521 /* end of test_transport_api_monitoring_validation.c */