2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2016 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.
25 * Starts two peers, has them connect, sends a message in between,
26 * stops one peer, expects the others to send a disconnect notification.
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, 120)
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 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
46 static struct GNUNET_TRANSPORT_TransmitHandle *th;
52 custom_shutdown (void *cls)
56 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
63 notify_disconnect (void *cls,
64 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
65 const struct GNUNET_PeerIdentity *other)
69 GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
74 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
77 if (shutdown_ == GNUNET_YES)
79 ccc->global_ret = GNUNET_OK;
80 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81 "Test good, shutting down...\n");
82 GNUNET_SCHEDULER_shutdown ();
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91 "Shutting down peer %u (`%s')\n",
93 GNUNET_i2s (&ccc->p[1]->id));
94 shutdown_ = GNUNET_YES;
95 GNUNET_TRANSPORT_TESTING_stop_peer (ccc->p[1]);
101 notify_receive (void *cls,
102 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
103 const struct GNUNET_PeerIdentity *sender,
104 const struct GNUNET_MessageHeader *message)
107 char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
110 "Peer %u (`%4s') received message of type %d and size %u size from peer %s!\n",
113 ntohs (message->type),
114 ntohs (message->size),
115 GNUNET_i2s (sender));
118 if ((MTYPE == ntohs (message->type)) &&
119 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
121 GNUNET_SCHEDULER_add_now (&stop_peer,
129 notify_ready (void *cls,
133 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
134 struct GNUNET_MessageHeader *hdr;
140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
141 "Transmission error occurred in transmit_ready\n");
142 GNUNET_SCHEDULER_shutdown ();
146 GNUNET_assert (size >= 256);
151 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
152 hdr->type = htons (MTYPE);
155 char *ps = GNUNET_strdup (GNUNET_i2s (&ccc->p[1]->id));
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
164 GNUNET_i2s (&p->id));
167 return sizeof (struct GNUNET_MessageHeader);
174 ccc->global_ret = GNUNET_SYSERR;
176 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&ccc->p[0]->id));
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
181 GNUNET_i2s (&ccc->p[1]->id),
184 GNUNET_free (receiver_s);
186 th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[1]->th,
195 main (int argc, char *argv[])
197 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
198 .connect_continuation = &sendtask,
199 .config_file = "test_transport_api_data.conf",
200 .rec = ¬ify_receive,
201 .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
202 .nd = ¬ify_disconnect,
203 .shutdown_task = &custom_shutdown,
209 GNUNET_TRANSPORT_TESTING_main (2,
210 &GNUNET_TRANSPORT_TESTING_connect_check,
216 /* end of test_transport_api_disconnect.c */