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