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