Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / cadet / test_cadet_local_mq.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2017 GNUnet e.V.
4
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.
9
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.
14     
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 /**
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
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_cadet_service.h"
32
33 #define TEST_MESSAGE_TYPE 1
34 #define TEST_PORT_ID 1
35
36 /**
37  * Test message structure.
38  */
39 struct GNUNET_CADET_TestMsg
40 {
41   /**
42    * Type: #TEST_MESSAGE_TYPE
43    *
44    * Size: sizeof(struct GNUNET_CADET_TestMsg)
45    */
46   struct GNUNET_MessageHeader header;
47
48   /**
49    * Test payload.
50    */
51   uint64_t payload;
52 };
53
54 struct GNUNET_TESTING_Peer *me;
55
56 static struct GNUNET_CADET_Handle *cadet_peer_1;
57
58 static struct GNUNET_CADET_Handle *cadet_peer_2;
59
60 static struct GNUNET_CADET_Channel *ch;
61
62 static int result = GNUNET_OK;
63
64 static int got_data = GNUNET_NO;
65
66 static struct GNUNET_SCHEDULER_Task *abort_task;
67
68 static struct GNUNET_SCHEDULER_Task *connect_task;
69
70
71 /**
72  * Connect to other client and send data
73  *
74  * @param cls Closue (unused).
75  */
76 static void
77 do_connect (void *cls);
78
79
80 /**
81  * Shutdown nicely
82  */
83 static void
84 do_shutdown (void *cls)
85 {
86   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
87               "shutdown\n");
88   if (NULL != abort_task)
89   {
90     GNUNET_SCHEDULER_cancel (abort_task);
91     abort_task = NULL;
92   }
93   if (NULL != ch)
94   {
95     GNUNET_CADET_channel_destroy (ch);
96     ch = NULL;
97   }
98   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99               "Disconnect client 1\n");
100   if (NULL != cadet_peer_1)
101   {
102     GNUNET_CADET_disconnect (cadet_peer_1);
103     cadet_peer_1 = NULL;
104   }
105   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
106               "Disconnect client 2\n");
107   if (NULL != cadet_peer_2)
108   {
109     GNUNET_CADET_disconnect (cadet_peer_2);
110     cadet_peer_2 = NULL;
111   }
112   if (NULL != connect_task)
113   {
114     GNUNET_SCHEDULER_cancel (connect_task);
115     connect_task = NULL;
116   }
117 }
118
119
120 /**
121  * Something went wrong and timed out. Kill everything and set error flag
122  */
123 static void
124 do_abort (void *cls)
125 {
126   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
127               "ABORT from line %ld\n",
128               (long) cls);
129   result = GNUNET_SYSERR;
130   abort_task = NULL;
131   GNUNET_SCHEDULER_shutdown ();
132 }
133
134 /**
135  * Method called whenever a peer connects to a port in MQ-based CADET.
136  *
137  * @param cls Closure from #GNUNET_CADET_open_port.
138  * @param channel New handle to the channel.
139  * @param source Peer that started this channel.
140  * @return Closure for the incoming @a channel. It's given to:
141  *         - The #GNUNET_CADET_DisconnectEventHandler (given to
142  *           #GNUNET_CADET_open_port) when the channel dies.
143  *         - Each the #GNUNET_MQ_MessageCallback handlers for each message
144  *           received on the @a channel.
145  */
146 static void *
147 connected (void *cls,
148            struct GNUNET_CADET_Channel *channel,
149            const struct GNUNET_PeerIdentity *source)
150 {
151   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152               "connected %s, cls: %p\n",
153               GNUNET_i2s(source),
154               cls);
155   return channel;
156 }
157
158 /**
159  * Function called whenever an MQ-channel is destroyed, even if the destruction
160  * was requested by #GNUNET_CADET_channel_destroy.
161  * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
162  *
163  * It should clean up any associated state, including cancelling any pending
164  * transmission on this channel.
165  *
166  * @param cls Channel closure.
167  * @param channel Connection to the other end (henceforth invalid).
168  */
169 static void
170 disconnected (void *cls,
171               const struct GNUNET_CADET_Channel *channel)
172 {
173   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
174               "disconnected channel %p, cls: %p\n",
175               channel, cls);
176   if (channel == ch)
177     ch = NULL;
178 }
179
180
181 /**
182  * Handle test data
183  *
184  * @param h     The cadet handle
185  * @param msg   A message with the details of the new incoming channel
186  */
187 static void
188 handle_data_received (void *cls,
189                       const struct GNUNET_CADET_TestMsg *msg)
190 {
191   uint64_t payload;
192
193   payload = GNUNET_ntohll (msg->payload);
194   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195               "Data callback payload %llu with cls: %p! Shutting down.\n",
196               (unsigned long long) payload,
197               cls);
198   GNUNET_assert (42 == payload);
199   got_data = GNUNET_YES;
200   GNUNET_SCHEDULER_shutdown ();
201 }
202
203
204 /**
205  * Signature of the main function of a task.
206  *
207  * @param cls Closure (unused).
208  */
209 static void
210 message_sent (void *cls)
211 {
212   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213               "message sent\n");
214 }
215
216
217 /**
218  * Connect to other client and send data
219  *
220  * @param cls Closure (unused).
221  */
222 static void
223 do_connect (void *cls)
224 {
225   struct GNUNET_PeerIdentity id;
226   struct GNUNET_MQ_Handle *mq;
227   struct GNUNET_MQ_Envelope *env;
228   struct GNUNET_CADET_TestMsg *msg;
229
230   struct GNUNET_MQ_MessageHandler handlers[] = {
231     GNUNET_MQ_hd_fixed_size (data_received,
232                              TEST_MESSAGE_TYPE,
233                              struct GNUNET_CADET_TestMsg,
234                              cadet_peer_1),
235     GNUNET_MQ_handler_end ()
236   };
237
238   connect_task = NULL;
239   GNUNET_TESTING_peer_get_identity (me, &id);
240   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241               "creating channel\n");
242   ch = GNUNET_CADET_channel_create (cadet_peer_1, /* cadet handle */
243                                     NULL,         /* channel cls */
244                                     &id,          /* destination */
245                                     GC_u2h (TEST_MESSAGE_TYPE), /* port */
246                                     GNUNET_CADET_OPTION_DEFAULT, /* opt */
247                                     NULL,          /* window change */
248                                     &disconnected, /* disconnect handler */
249                                     handlers       /* traffic handlers */
250                                    );
251   env = GNUNET_MQ_msg (msg, TEST_MESSAGE_TYPE);
252   msg->payload = GNUNET_htonll (42);
253   mq = GNUNET_CADET_get_mq (ch);
254   GNUNET_MQ_notify_sent (env, &message_sent, NULL);
255   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256               "sending message\n");
257   GNUNET_MQ_send (mq, env);
258 }
259
260
261 /**
262  * Initialize framework and start test
263  *
264  * @param cls Closure (unused).
265  * @param cfg Configuration handle.
266  * @param peer Testing peer handle.
267  */
268 static void
269 run (void *cls,
270      const struct GNUNET_CONFIGURATION_Handle *cfg,
271      struct GNUNET_TESTING_Peer *peer)
272 {
273   struct GNUNET_MQ_MessageHandler handlers[] = {
274     GNUNET_MQ_hd_fixed_size (data_received,
275                              TEST_MESSAGE_TYPE,
276                              struct GNUNET_CADET_TestMsg,
277                              cadet_peer_2),
278     GNUNET_MQ_handler_end ()
279   };
280   struct GNUNET_TIME_Relative delay;
281
282   me = peer;
283   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
284                                  NULL);
285   delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15);
286   abort_task = GNUNET_SCHEDULER_add_delayed (delay,
287                                              &do_abort,
288                                              (void *) (long) __LINE__);
289   cadet_peer_1 = GNUNET_CADET_connect (cfg);
290   cadet_peer_2 = GNUNET_CADET_connect (cfg);
291
292   if ( (NULL == cadet_peer_1) ||
293        (NULL == cadet_peer_2) )
294   {
295     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
296                 "Couldn't connect to cadet\n");
297     result = GNUNET_SYSERR;
298     GNUNET_SCHEDULER_shutdown ();
299     return;
300   }
301   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CADET 1: %p\n", cadet_peer_1);
302   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CADET 2: %p\n", cadet_peer_2);
303   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "handlers 2: %p\n", handlers);
304   GNUNET_CADET_open_port (cadet_peer_2,          /* cadet handle */
305                           GC_u2h (TEST_PORT_ID), /* port id */
306                           &connected,            /* connect handler */
307                           (void *) 2L,           /* handle for #connected */
308                           NULL,                  /* window size handler */
309                           &disconnected,         /* disconnect handler */
310                           handlers);             /* traffic handlers */
311   delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2);
312   if (NULL == connect_task)
313     connect_task = GNUNET_SCHEDULER_add_delayed (delay,
314                                                  &do_connect,
315                                                  NULL);
316 }
317
318
319 /**
320  * Main
321  */
322 int
323 main (int argc, char *argv[])
324 {
325   if (0 != GNUNET_TESTING_peer_run ("test-cadet-local",
326                                     "test_cadet.conf",
327                                 &run, NULL))
328   {
329     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run failed\n");
330     return 2;
331   }
332   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Final result: %d\n", result);
333   return (result == GNUNET_OK) ? 0 : 1;
334 }
335
336 /* end of test_cadet_local_1.c */