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_restart_1peer.c
22 * @brief base test case for transport implementations
24 * This test case starts 2 peers, connects and exchanges a message
25 * 1 peer is restarted and tested if peers reconnect
29 #include "gnunet_common.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_getopt_lib.h"
32 #include "gnunet_os_lib.h"
33 #include "gnunet_program_lib.h"
34 #include "gnunet_scheduler_lib.h"
35 #include "gnunet_transport_service.h"
36 #include "transport.h"
37 #include "transport-testing.h"
39 #define VERBOSE GNUNET_NO
40 #define VERBOSE_ARM GNUNET_NO
42 #define START_ARM GNUNET_YES
45 * How long until we give up on transmitting the message?
47 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
50 * How long until we give up on transmitting the message?
52 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
56 static char *test_name;
60 static GNUNET_SCHEDULER_TaskIdentifier die_task;
62 static GNUNET_SCHEDULER_TaskIdentifier send_task;
64 static GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
66 struct PeerContext *p1;
70 struct PeerContext *p2;
74 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
76 struct GNUNET_TRANSPORT_TransmitHandle *th;
78 struct GNUNET_TRANSPORT_TESTING_handle *tth;
87 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
89 #define OKPP do { ok++; } while (0)
96 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
98 if (send_task != GNUNET_SCHEDULER_NO_TASK)
99 GNUNET_SCHEDULER_cancel (send_task);
100 send_task = GNUNET_SCHEDULER_NO_TASK;
102 if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
103 GNUNET_SCHEDULER_cancel (reconnect_task);
104 reconnect_task = GNUNET_SCHEDULER_NO_TASK;
106 if (die_task != GNUNET_SCHEDULER_NO_TASK)
107 GNUNET_SCHEDULER_cancel (die_task);
108 die_task = GNUNET_SCHEDULER_NO_TASK;
111 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
114 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
115 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
119 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
121 die_task = GNUNET_SCHEDULER_NO_TASK;
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
125 if (restarted == GNUNET_YES)
126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was restarted\n");
128 if (restarted == GNUNET_NO)
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was NOT restarted\n");
131 if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
132 GNUNET_SCHEDULER_cancel (reconnect_task);
133 reconnect_task = GNUNET_SCHEDULER_NO_TASK;
135 if (send_task != GNUNET_SCHEDULER_NO_TASK)
136 GNUNET_SCHEDULER_cancel (send_task);
137 send_task = GNUNET_SCHEDULER_NO_TASK;
141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
142 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
147 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
151 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
153 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
159 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
161 struct PeerContext *p = cls;
163 reconnect_task = GNUNET_SCHEDULER_NO_TASK;
165 GNUNET_TRANSPORT_try_connect (p->th, &p2->id);
167 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect, p);
171 restart_cb (struct PeerContext *p, void *cls)
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 "Restarted peer %u (`%4s'), issuing reconnect\n", p->no,
176 GNUNET_i2s (&p->id));
178 reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, p);
182 restart (struct PeerContext *p, char *cfg_file)
184 GNUNET_assert (p != NULL);
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Restarting peer %u (`%4s')\n", p->no,
186 GNUNET_i2s (&p->id));
187 GNUNET_TRANSPORT_TESTING_restart_peer (tth, p, cfg_file, &restart_cb, p);
192 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
193 const struct GNUNET_MessageHeader *message,
194 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
196 struct PeerContext *p = cls;
197 struct PeerContext *t = NULL;
199 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
201 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
203 GNUNET_assert (t != NULL);
205 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
209 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
210 GNUNET_i2s (&t->id));
213 if ((MTYPE == ntohs (message->type)) &&
214 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
216 if (restarted == GNUNET_NO)
218 restarted = GNUNET_YES;
219 restart (p1, cfg_file_p1);
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225 "Restarted peers connected and message was sent, stopping test...\n");
234 if (die_task != GNUNET_SCHEDULER_NO_TASK)
235 GNUNET_SCHEDULER_cancel (die_task);
236 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
242 notify_ready (void *cls, size_t size, void *buf)
244 struct PeerContext *p = cls;
245 struct GNUNET_MessageHeader *hdr;
251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
252 "Timeout occurred while waiting for transmit_ready\n");
253 if (GNUNET_SCHEDULER_NO_TASK != die_task)
254 GNUNET_SCHEDULER_cancel (die_task);
255 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
260 GNUNET_assert (size >= 256);
265 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
266 hdr->type = htons (MTYPE);
268 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
271 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
272 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
273 GNUNET_i2s (&p->id));
275 return sizeof (struct GNUNET_MessageHeader);
280 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
282 send_task = GNUNET_SCHEDULER_NO_TASK;
284 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
286 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
290 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
291 GNUNET_free (receiver_s);
293 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256, 0,
294 TIMEOUT_TRANSMIT, ¬ify_ready,
300 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
301 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
306 struct PeerContext *p = cls;
307 struct PeerContext *t = NULL;
309 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
311 p1_connected = GNUNET_YES;
314 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
316 p2_connected = GNUNET_YES;
319 GNUNET_assert (t != NULL);
321 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
325 t->no, GNUNET_i2s (peer));
328 if ((restarted == GNUNET_YES) && ((p1_connected == GNUNET_YES) && (p2_connected == GNUNET_YES)))
330 /* Peer was restarted and we received 3 connect messages (2 from first connect, 1 from reconnect) */
331 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
337 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
339 struct PeerContext *p = cls;
341 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
343 p1_connected = GNUNET_NO;
345 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
347 p2_connected = GNUNET_NO;
351 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
359 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
364 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
367 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
370 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
373 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
379 start_cb (struct PeerContext *p, void *cls)
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
386 GNUNET_i2s (&p->id));
391 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
395 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
396 GNUNET_free (sender_c);
398 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
404 run (void *cls, char *const *args, const char *cfgfile,
405 const struct GNUNET_CONFIGURATION_Handle *cfg)
407 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
408 p1_connected = GNUNET_NO;
409 p2_connected = GNUNET_NO;
410 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
411 ¬ify_receive, ¬ify_connect,
412 ¬ify_disconnect, &start_cb,
415 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
416 ¬ify_receive, ¬ify_connect,
417 ¬ify_disconnect, &start_cb,
420 if ((p1 == NULL) || (p2 == NULL))
422 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
423 if (die_task != GNUNET_SCHEDULER_NO_TASK)
424 GNUNET_SCHEDULER_cancel (die_task);
425 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
434 static char *const argv[] = { "test-transport-api",
436 "test_transport_api_data.conf",
442 static struct GNUNET_GETOPT_CommandLineOption options[] = {
443 GNUNET_GETOPT_OPTION_END
447 setTransportOptions ("test_transport_api_data.conf");
449 send_task = GNUNET_SCHEDULER_NO_TASK;
452 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
453 "nohelp", options, &run, &ok);
459 main (int argc, char *argv[])
463 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
466 GNUNET_log_setup (test_name,
474 tth = GNUNET_TRANSPORT_TESTING_init ();
476 GNUNET_asprintf (&cfg_file_p1, "test_transport_api_tcp_peer1.conf");
477 GNUNET_asprintf (&cfg_file_p2, "test_transport_api_tcp_peer2.conf");
481 GNUNET_free (cfg_file_p1);
482 GNUNET_free (cfg_file_p2);
484 GNUNET_free (test_name);
486 GNUNET_TRANSPORT_TESTING_done (tth);
491 /* end of test_transport_api_restart_1peer.c */