cd9e47b0b860c90eff715c65ae0bc74781b89c07
[oweals/gnunet.git] / src / cadet / cadet_test_lib.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 GNUnet e.V.
4
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.
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      General Public License for more details.
14
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.
19 */
20 /**
21  * @file cadet/cadet_test_lib.c
22  * @author Bartlomiej Polot
23  * @brief library for writing CADET tests
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "cadet_test_lib.h"
28 #include "gnunet_cadet_service.h"
29
30 /**
31  * Test context for a CADET Test.
32  */
33 struct GNUNET_CADET_TEST_Context
34 {
35   /**
36    * Array of running peers.
37    */
38   struct GNUNET_TESTBED_Peer **peers;
39
40   /**
41    * Array of handles to the CADET for each peer.
42    */
43   struct GNUNET_CADET_Handle **cadetes;
44
45   /**
46    * Operation associated with the connection to the CADET.
47    */
48   struct GNUNET_TESTBED_Operation **ops;
49
50   /**
51    * Main function of the test to run once all CADETs are available.
52    */
53   GNUNET_CADET_TEST_AppMain app_main;
54
55   /**
56    * Closure for 'app_main'.
57    */
58   void *app_main_cls;
59
60   /**
61    * Number of peers running, size of the arrays above.
62    */
63   unsigned int num_peers;
64
65   /**
66    * Handler for incoming tunnels.
67    */
68   GNUNET_CADET_InboundChannelNotificationHandler *new_channel;
69
70   /**
71    * Cleaner for destroyed incoming tunnels.
72    */
73   GNUNET_CADET_ChannelEndHandler *cleaner;
74
75   /**
76    * Message handlers.
77    */
78   struct GNUNET_CADET_MessageHandler* handlers;
79
80   /**
81    * Application ports.
82    */
83   const uint32_t *ports;
84
85 };
86
87
88 /**
89  * Context for a cadet adapter callback.
90  */
91 struct GNUNET_CADET_TEST_AdapterContext
92 {
93   /**
94    * Peer number for the particular peer.
95    */
96   unsigned int peer;
97
98   /**
99    * General context.
100    */
101   struct GNUNET_CADET_TEST_Context *ctx;
102 };
103
104
105 /**
106  * Adapter function called to establish a connection to
107  * the CADET service.
108  *
109  * @param cls closure
110  * @param cfg configuration of the peer to connect to; will be available until
111  *          GNUNET_TESTBED_operation_done() is called on the operation returned
112  *          from GNUNET_TESTBED_service_connect()
113  * @return service handle to return in 'op_result', NULL on error
114  */
115 static void *
116 cadet_connect_adapter (void *cls,
117                       const struct GNUNET_CONFIGURATION_Handle *cfg)
118 {
119   struct GNUNET_CADET_TEST_AdapterContext *actx = cls;
120   struct GNUNET_CADET_TEST_Context *ctx = actx->ctx;
121   struct GNUNET_CADET_Handle *h;
122
123   h = GNUNET_CADET_connect (cfg,
124                            (void *) (long) actx->peer,
125                            ctx->cleaner,
126                            ctx->handlers);
127   return h;
128 }
129
130
131 /**
132  * Adapter function called to destroy a connection to
133  * the CADET service.
134  *
135  * @param cls closure
136  * @param op_result service handle returned from the connect adapter
137  */
138 static void
139 cadet_disconnect_adapter (void *cls,
140                          void *op_result)
141 {
142   struct GNUNET_CADET_Handle *cadet = op_result;
143   struct GNUNET_CADET_TEST_AdapterContext *actx = cls;
144
145   GNUNET_free (actx);
146   GNUNET_CADET_disconnect (cadet);
147 }
148
149
150 /**
151  * Callback to be called when a service connect operation is completed.
152  *
153  * @param cls The callback closure from functions generating an operation.
154  * @param op The operation that has been finished.
155  * @param ca_result The service handle returned from
156  *                  GNUNET_TESTBED_ConnectAdapter() (cadet handle).
157  * @param emsg Error message in case the operation has failed.
158  *             NULL if operation has executed successfully.
159  */
160 static void
161 cadet_connect_cb (void *cls,
162                  struct GNUNET_TESTBED_Operation *op,
163                  void *ca_result,
164                  const char *emsg)
165 {
166   struct GNUNET_CADET_TEST_Context *ctx = cls;
167   unsigned int i;
168
169   if (NULL != emsg)
170   {
171     fprintf (stderr, "Failed to connect to CADET service: %s\n",
172              emsg);
173     GNUNET_SCHEDULER_shutdown ();
174     return;
175   }
176   for (i = 0; i < ctx->num_peers; i++)
177     if (op == ctx->ops[i])
178     {
179       ctx->cadetes[i] = ca_result;
180       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "...cadet %u connected\n", i);
181     }
182   for (i = 0; i < ctx->num_peers; i++)
183     if (NULL == ctx->cadetes[i])
184       return; /* still some CADET connections missing */
185   /* all CADET connections ready! */
186   ctx->app_main (ctx->app_main_cls,
187                  ctx,
188                  ctx->num_peers,
189                  ctx->peers,
190                  ctx->cadetes);
191 }
192
193
194 void
195 GNUNET_CADET_TEST_cleanup (struct GNUNET_CADET_TEST_Context *ctx)
196 {
197   unsigned int i;
198
199   for (i = 0; i < ctx->num_peers; i++)
200   {
201     GNUNET_assert (NULL != ctx->ops[i]);
202     GNUNET_TESTBED_operation_done (ctx->ops[i]);
203     ctx->ops[i] = NULL;
204   }
205   GNUNET_free (ctx->ops);
206   GNUNET_free (ctx->cadetes);
207   GNUNET_free (ctx);
208   GNUNET_SCHEDULER_shutdown ();
209 }
210
211
212 /**
213  * Callback run when the testbed is ready (peers running and connected to
214  * each other)
215  *
216  * @param cls Closure (context).
217  * @param h the run handle
218  * @param num_peers Number of peers that are running.
219  * @param peers Handles to each one of the @c num_peers peers.
220  * @param links_succeeded the number of overlay link connection attempts that
221  *          succeeded
222  * @param links_failed the number of overlay link connection attempts that
223  *          failed
224  */
225 static void
226 cadet_test_run (void *cls,
227                struct GNUNET_TESTBED_RunHandle *h,
228                unsigned int num_peers,
229                struct GNUNET_TESTBED_Peer **peers,
230                unsigned int links_succeeded,
231                unsigned int links_failed)
232 {
233   struct GNUNET_CADET_TEST_Context *ctx = cls;
234   unsigned int i;
235
236   if  (num_peers != ctx->num_peers)
237   {
238     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers started %u/%u, ending\n",
239                 num_peers, ctx->num_peers);
240     exit (1);
241   }
242   ctx->peers = peers;
243   for (i = 0; i < num_peers; i++)
244   {
245     struct GNUNET_CADET_TEST_AdapterContext *newctx;
246     newctx = GNUNET_new (struct GNUNET_CADET_TEST_AdapterContext);
247     newctx->peer = i;
248     newctx->ctx = ctx;
249     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting to cadet %u\n", i);
250     ctx->ops[i] = GNUNET_TESTBED_service_connect (ctx,
251                                                   peers[i],
252                                                   "cadet",
253                                                   &cadet_connect_cb,
254                                                   ctx,
255                                                   &cadet_connect_adapter,
256                                                   &cadet_disconnect_adapter,
257                                                   newctx);
258     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "op handle %p\n", ctx->ops[i]);
259   }
260 }
261
262
263 void
264 GNUNET_CADET_TEST_run (const char *testname,
265                       const char *cfgname,
266                       unsigned int num_peers,
267                       GNUNET_CADET_TEST_AppMain tmain,
268                       void *tmain_cls,
269                       GNUNET_CADET_InboundChannelNotificationHandler new_channel,
270                       GNUNET_CADET_ChannelEndHandler cleaner,
271                       struct GNUNET_CADET_MessageHandler* handlers,
272                       const uint32_t *ports)
273 {
274   struct GNUNET_CADET_TEST_Context *ctx;
275
276   ctx = GNUNET_new (struct GNUNET_CADET_TEST_Context);
277   ctx->num_peers = num_peers;
278   ctx->ops = GNUNET_malloc (num_peers * sizeof (struct GNUNET_TESTBED_Operation *));
279   ctx->cadetes = GNUNET_malloc (num_peers * sizeof (struct GNUNET_CADET_Handle *));
280   ctx->app_main = tmain;
281   ctx->app_main_cls = tmain_cls;
282   ctx->new_channel = new_channel;
283   ctx->cleaner = cleaner;
284   ctx->handlers = handlers;
285   ctx->ports = ports;
286   GNUNET_TESTBED_test_run (testname,
287                            cfgname,
288                            num_peers,
289                            0LL, NULL, NULL,
290                            &cadet_test_run, ctx);
291 }
292
293 /* end of cadet_test_lib.c */