2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2015 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_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_transport_service.h"
30 #include "transport-testing.h"
33 * How long until we give up on transmitting the message?
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
38 * How long until we give up on transmitting the message?
40 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
44 static char *test_name;
48 static struct GNUNET_SCHEDULER_Task *die_task;
50 static struct GNUNET_SCHEDULER_Task *send_task;
52 static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
54 static struct PeerContext *p1;
56 static int p1_connected;
58 static struct PeerContext *p2;
60 static int p2_connected;
62 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
64 static struct GNUNET_TRANSPORT_TransmitHandle *th;
66 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
68 static char *cfg_file_p1;
70 static char *cfg_file_p2;
78 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81 if (NULL != send_task)
83 GNUNET_SCHEDULER_cancel (send_task);
88 GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
93 GNUNET_SCHEDULER_cancel (die_task);
98 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
103 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
108 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
115 end_badly (void *cls)
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
120 "Fail! Stopping peers\n");
122 if (restarted == GNUNET_YES)
123 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
124 "Peer was restarted\n");
126 if (restarted == GNUNET_NO)
127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
128 "Peer was NOT restarted\n");
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
132 _("Fail! Could not connect peers\n"));
133 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
142 restart_cb (struct PeerContext *p, void *cls)
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145 "Restarted peer %u (`%4s'), issuing reconnect\n",
147 GNUNET_i2s (&p->id));
148 ats_sh = GNUNET_ATS_connectivity_suggest (p->ats,
155 restart (struct PeerContext *p,
156 const char *cfg_file)
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "Restarting peer %u (`%4s')\n",
161 GNUNET_i2s (&p->id));
162 GNUNET_TRANSPORT_TESTING_restart_peer (p,
170 notify_receive (void *cls,
171 const struct GNUNET_PeerIdentity *peer,
172 const struct GNUNET_MessageHeader *message)
174 struct PeerContext *p = cls;
175 struct PeerContext *t = NULL;
177 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
179 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
181 GNUNET_assert (t != NULL);
183 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
187 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
188 GNUNET_i2s (&t->id));
191 if ((MTYPE == ntohs (message->type)) &&
192 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
194 if (restarted == GNUNET_NO)
196 restarted = GNUNET_YES;
197 restart (p1, cfg_file_p1);
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 "Restarted peers connected and message was sent, stopping test...\n");
212 if (die_task != NULL)
213 GNUNET_SCHEDULER_cancel (die_task);
214 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
220 notify_ready (void *cls, size_t size, void *buf)
222 struct PeerContext *p = cls;
223 struct GNUNET_MessageHeader *hdr;
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230 "Timeout occurred while waiting for transmit_ready\n");
231 if (NULL != die_task)
232 GNUNET_SCHEDULER_cancel (die_task);
233 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
238 GNUNET_assert (size >= 256);
240 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
241 hdr->type = htons (MTYPE);
244 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
253 GNUNET_i2s (&p->id));
256 return sizeof (struct GNUNET_MessageHeader);
264 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
267 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
268 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
269 GNUNET_free (receiver_s);
271 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
281 notify_connect (void *cls,
282 const struct GNUNET_PeerIdentity *peer)
287 struct PeerContext *p = cls;
288 struct PeerContext *t = NULL;
290 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
292 p1_connected = GNUNET_YES;
295 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
297 p2_connected = GNUNET_YES;
300 GNUNET_assert (t != NULL);
302 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
306 t->no, GNUNET_i2s (peer));
309 if ((restarted == GNUNET_YES) && ((p1_connected == GNUNET_YES) && (p2_connected == GNUNET_YES)))
311 /* Peer was restarted and we received 3 connect messages (2 from first connect, 1 from reconnect) */
312 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
318 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
320 struct PeerContext *p = cls;
323 (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity))))
325 p1_connected = GNUNET_NO;
328 (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity))))
330 p2_connected = GNUNET_NO;
332 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
340 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
342 if (NULL != send_task)
343 GNUNET_SCHEDULER_cancel (send_task);
348 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
351 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
354 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
357 send_task = GNUNET_SCHEDULER_add_now (&sendtask, 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 p1_connected = GNUNET_NO;
393 p2_connected = GNUNET_NO;
394 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
395 ¬ify_receive, ¬ify_connect,
396 ¬ify_disconnect, &start_cb,
399 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
400 ¬ify_receive, ¬ify_connect,
401 ¬ify_disconnect, &start_cb,
404 if ((p1 == NULL) || (p2 == NULL))
406 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
407 if (die_task != NULL)
408 GNUNET_SCHEDULER_cancel (die_task);
409 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
418 static char *const argv[] = { "test-transport-api",
420 "test_transport_api_data.conf",
423 static struct GNUNET_GETOPT_CommandLineOption options[] = {
424 GNUNET_GETOPT_OPTION_END
430 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
431 "nohelp", options, &run, &ok);
437 main (int argc, char *argv[])
441 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
442 GNUNET_log_setup (test_name,
445 tth = GNUNET_TRANSPORT_TESTING_init ();
446 GNUNET_asprintf (&cfg_file_p1, "test_transport_api_tcp_peer1.conf");
447 GNUNET_asprintf (&cfg_file_p2, "test_transport_api_tcp_peer2.conf");
449 GNUNET_free (cfg_file_p1);
450 GNUNET_free (cfg_file_p2);
451 GNUNET_free (test_name);
452 GNUNET_TRANSPORT_TESTING_done (tth);
456 /* end of test_transport_api_restart_1peer.c */