2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010 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.
21 * @file transport/test_transport_api_disconnect.c
22 * @brief base test case for transport implementations
24 * This test case tests disconnect notifications in peer shutdown
27 #include "gnunet_transport_service.h"
28 #include "transport-testing.h"
31 * How long until we give up on transmitting the message?
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
36 * How long until we give up on transmitting the message?
38 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
42 static char *test_source;
44 static char *test_plugin;
46 static char *test_name;
50 static struct GNUNET_SCHEDULER_Task * die_task;
52 static struct GNUNET_SCHEDULER_Task * send_task;
54 struct PeerContext *p1;
56 struct PeerContext *p2;
58 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
60 struct GNUNET_TRANSPORT_TransmitHandle *th;
62 struct GNUNET_TRANSPORT_TESTING_handle *tth;
71 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
73 #define OKPP do { ok++; } while (0)
80 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
82 if (send_task != NULL)
84 GNUNET_SCHEDULER_cancel (send_task);
90 GNUNET_SCHEDULER_cancel (die_task);
95 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
99 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
103 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
112 end_badly (void *cls)
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
120 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
124 if (send_task != NULL)
126 GNUNET_SCHEDULER_cancel (send_task);
131 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
135 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
137 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
143 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
145 struct PeerContext *p = cls;
146 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
154 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
157 if (shutdown_ == GNUNET_YES)
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Complete, shutting down...\n");
160 GNUNET_SCHEDULER_add_now (&end, NULL);
166 stop_peer (void *cls)
168 struct PeerContext *p = cls;
170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down peer %u (`%s')\n", p->no,
171 GNUNET_i2s (&p->id));
172 shutdown_ = GNUNET_YES;
173 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
175 GNUNET_assert (p2 == NULL);
180 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
181 const struct GNUNET_MessageHeader *message)
183 struct PeerContext *p = cls;
184 struct PeerContext *t = NULL;
186 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
188 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
190 GNUNET_assert (t != NULL);
192 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
196 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
197 GNUNET_i2s (&t->id));
200 if ((MTYPE == ntohs (message->type)) &&
201 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
204 GNUNET_SCHEDULER_add_now (stop_peer, p2);
211 notify_ready (void *cls, size_t size, void *buf)
213 struct PeerContext *p = cls;
214 struct GNUNET_MessageHeader *hdr;
220 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
221 "Timeout occurred while waiting for transmit_ready\n");
222 if (NULL != die_task)
223 GNUNET_SCHEDULER_cancel (die_task);
224 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
229 GNUNET_assert (size >= 256);
234 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
235 hdr->type = htons (MTYPE);
237 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
241 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
242 GNUNET_i2s (&p->id));
244 return sizeof (struct GNUNET_MessageHeader);
252 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
256 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
257 GNUNET_free (receiver_s);
259 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256,
260 TIMEOUT_TRANSMIT, ¬ify_ready,
266 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
271 struct PeerContext *p = cls;
272 struct PeerContext *t = NULL;
274 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
276 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
278 GNUNET_assert (t != NULL);
280 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
283 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
284 t->no, GNUNET_i2s (peer));
290 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
293 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
296 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
299 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
304 start_cb (struct PeerContext *p, void *cls)
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310 "Peer %u (`%s') started\n", p->no,
311 GNUNET_i2s (&p->id));
314 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
318 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
319 GNUNET_free (sender_c);
320 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
327 run (void *cls, char *const *args, const char *cfgfile,
328 const struct GNUNET_CONFIGURATION_Handle *cfg)
330 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
332 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
333 ¬ify_receive, ¬ify_connect,
334 ¬ify_disconnect, &start_cb,
337 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
338 ¬ify_receive, ¬ify_connect,
339 ¬ify_disconnect, &start_cb,
342 if ((p1 == NULL) || (p2 == NULL))
344 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
345 if (die_task != NULL)
346 GNUNET_SCHEDULER_cancel (die_task);
347 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
356 static char *const argv[] = { "test-transport-api",
358 "test_transport_api_data.conf",
361 static struct GNUNET_GETOPT_CommandLineOption options[] = {
362 GNUNET_GETOPT_OPTION_END
368 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
369 "nohelp", options, &run, &ok);
375 main (int argc, char *argv[])
379 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
381 GNUNET_log_setup (test_name,
385 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
386 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
389 tth = GNUNET_TRANSPORT_TESTING_init ();
391 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
392 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
396 GNUNET_free (cfg_file_p1);
397 GNUNET_free (cfg_file_p2);
399 GNUNET_free (test_source);
400 GNUNET_free (test_plugin);
401 GNUNET_free (test_name);
403 GNUNET_TRANSPORT_TESTING_done (tth);
408 /* end of test_transport_api.c */