2 This file is part of GNUnet.
3 Copyright (C) 2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file cadet/test_cadet_local_mq.c
23 * @brief test cadet local: test of cadet channels with just one peer
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_cadet_service.h"
33 #define TEST_MESSAGE_TYPE 1
34 #define TEST_PORT_ID 1
37 * Test message structure.
39 struct GNUNET_CADET_TestMsg
42 * Type: #TEST_MESSAGE_TYPE
44 * Size: sizeof(struct GNUNET_CADET_TestMsg)
46 struct GNUNET_MessageHeader header;
54 struct GNUNET_TESTING_Peer *me;
56 static struct GNUNET_CADET_Handle *cadet_peer_1;
58 static struct GNUNET_CADET_Handle *cadet_peer_2;
60 static struct GNUNET_CADET_Channel *ch;
62 static int result = GNUNET_OK;
64 static int got_data = GNUNET_NO;
66 static struct GNUNET_SCHEDULER_Task *abort_task;
68 static struct GNUNET_SCHEDULER_Task *connect_task;
72 * Connect to other client and send data
74 * @param cls Closue (unused).
77 do_connect (void *cls);
84 do_shutdown (void *cls)
86 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
88 if (NULL != abort_task)
90 GNUNET_SCHEDULER_cancel (abort_task);
95 GNUNET_CADET_channel_destroy (ch);
98 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99 "Disconnect client 1\n");
100 if (NULL != cadet_peer_1)
102 GNUNET_CADET_disconnect (cadet_peer_1);
105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
106 "Disconnect client 2\n");
107 if (NULL != cadet_peer_2)
109 GNUNET_CADET_disconnect (cadet_peer_2);
112 if (NULL != connect_task)
114 GNUNET_SCHEDULER_cancel (connect_task);
121 * Something went wrong and timed out. Kill everything and set error flag
126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
127 "ABORT from line %ld\n",
129 result = GNUNET_SYSERR;
131 GNUNET_SCHEDULER_shutdown ();
136 * Method called whenever a peer connects to a port in MQ-based CADET.
138 * @param cls Closure from #GNUNET_CADET_open_port.
139 * @param channel New handle to the channel.
140 * @param source Peer that started this channel.
141 * @return Closure for the incoming @a channel. It's given to:
142 * - The #GNUNET_CADET_DisconnectEventHandler (given to
143 * #GNUNET_CADET_open_port) when the channel dies.
144 * - Each the #GNUNET_MQ_MessageCallback handlers for each message
145 * received on the @a channel.
148 connected (void *cls,
149 struct GNUNET_CADET_Channel *channel,
150 const struct GNUNET_PeerIdentity *source)
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153 "connected %s, cls: %p\n",
161 * Function called whenever an MQ-channel is destroyed, even if the destruction
162 * was requested by #GNUNET_CADET_channel_destroy.
163 * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
165 * It should clean up any associated state, including cancelling any pending
166 * transmission on this channel.
168 * @param cls Channel closure.
169 * @param channel Connection to the other end (henceforth invalid).
172 disconnected (void *cls,
173 const struct GNUNET_CADET_Channel *channel)
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "disconnected channel %p, cls: %p\n",
186 * @param h The cadet handle
187 * @param msg A message with the details of the new incoming channel
190 handle_data_received (void *cls,
191 const struct GNUNET_CADET_TestMsg *msg)
195 payload = GNUNET_ntohll (msg->payload);
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 "Data callback payload %llu with cls: %p! Shutting down.\n",
198 (unsigned long long) payload,
200 GNUNET_assert (42 == payload);
201 got_data = GNUNET_YES;
202 GNUNET_SCHEDULER_shutdown ();
207 * Signature of the main function of a task.
209 * @param cls Closure (unused).
212 message_sent (void *cls)
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220 * Connect to other client and send data
222 * @param cls Closure (unused).
225 do_connect (void *cls)
227 struct GNUNET_PeerIdentity id;
228 struct GNUNET_MQ_Handle *mq;
229 struct GNUNET_MQ_Envelope *env;
230 struct GNUNET_CADET_TestMsg *msg;
232 struct GNUNET_MQ_MessageHandler handlers[] = {
233 GNUNET_MQ_hd_fixed_size (data_received,
235 struct GNUNET_CADET_TestMsg,
237 GNUNET_MQ_handler_end ()
241 GNUNET_TESTING_peer_get_identity (me, &id);
242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243 "creating channel\n");
244 ch = GNUNET_CADET_channel_create (cadet_peer_1, /* cadet handle */
245 NULL, /* channel cls */
246 &id, /* destination */
247 GC_u2h (TEST_MESSAGE_TYPE), /* port */
248 NULL, /* window change */
249 &disconnected, /* disconnect handler */
250 handlers /* traffic handlers */
252 env = GNUNET_MQ_msg (msg, TEST_MESSAGE_TYPE);
253 msg->payload = GNUNET_htonll (42);
254 mq = GNUNET_CADET_get_mq (ch);
255 GNUNET_MQ_notify_sent (env, &message_sent, NULL);
256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
257 "sending message\n");
258 GNUNET_MQ_send (mq, env);
263 * Initialize framework and start test
265 * @param cls Closure (unused).
266 * @param cfg Configuration handle.
267 * @param peer Testing peer handle.
271 const struct GNUNET_CONFIGURATION_Handle *cfg,
272 struct GNUNET_TESTING_Peer *peer)
274 struct GNUNET_MQ_MessageHandler handlers[] = {
275 GNUNET_MQ_hd_fixed_size (data_received,
277 struct GNUNET_CADET_TestMsg,
279 GNUNET_MQ_handler_end ()
281 struct GNUNET_TIME_Relative delay;
284 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
286 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15);
287 abort_task = GNUNET_SCHEDULER_add_delayed (delay,
289 (void *) (long) __LINE__);
290 cadet_peer_1 = GNUNET_CADET_connect (cfg);
291 cadet_peer_2 = GNUNET_CADET_connect (cfg);
293 if ((NULL == cadet_peer_1) ||
294 (NULL == cadet_peer_2))
296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
297 "Couldn't connect to cadet\n");
298 result = GNUNET_SYSERR;
299 GNUNET_SCHEDULER_shutdown ();
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CADET 1: %p\n", cadet_peer_1);
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CADET 2: %p\n", cadet_peer_2);
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "handlers 2: %p\n", handlers);
305 GNUNET_CADET_open_port (cadet_peer_2, /* cadet handle */
306 GC_u2h (TEST_PORT_ID), /* port id */
307 &connected, /* connect handler */
308 (void *) 2L, /* handle for #connected */
309 NULL, /* window size handler */
310 &disconnected, /* disconnect handler */
311 handlers); /* traffic handlers */
312 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2);
313 if (NULL == connect_task)
314 connect_task = GNUNET_SCHEDULER_add_delayed (delay,
324 main (int argc, char *argv[])
326 if (0 != GNUNET_TESTING_peer_run ("test-cadet-local",
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run failed\n");
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Final result: %d\n", result);
334 return (result == GNUNET_OK) ? 0 : 1;
338 /* end of test_cadet_local_1.c */