aboutsummaryrefslogtreecommitdiff
path: root/src/cadet/cadet_test_lib.c
blob: bbd0284334160fa7392154aaeb60075e7b059484 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
     This file is part of GNUnet.
     Copyright (C) 2012 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/
/**
 * @file cadet/cadet_test_lib.c
 * @author Bartlomiej Polot
 * @brief library for writing CADET tests
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "cadet_test_lib.h"
#include "gnunet_cadet_service.h"

/**
 * Test context for a CADET Test.
 */
struct GNUNET_CADET_TEST_Context
{
  /**
   * Array of running peers.
   */
  struct GNUNET_TESTBED_Peer **peers;

  /**
   * Array of handles to the CADET for each peer.
   */
  struct GNUNET_CADET_Handle **cadetes;

  /**
   * Operation associated with the connection to the CADET.
   */
  struct GNUNET_TESTBED_Operation **ops;

  /**
   * Main function of the test to run once all CADETs are available.
   */
  GNUNET_CADET_TEST_AppMain app_main;

  /**
   * Closure for 'app_main'.
   */
  void *app_main_cls;

  /**
   * Number of peers running, size of the arrays above.
   */
  unsigned int num_peers;

  /**
   * Handler for incoming tunnels.
   */
  GNUNET_CADET_InboundChannelNotificationHandler *new_channel;

  /**
   * Cleaner for destroyed incoming tunnels.
   */
  GNUNET_CADET_ChannelEndHandler *cleaner;

  /**
   * Message handlers.
   */
  struct GNUNET_CADET_MessageHandler* handlers;

  /**
   * Application ports.
   */
  const uint32_t *ports;

};


/**
 * Context for a cadet adapter callback.
 */
struct GNUNET_CADET_TEST_AdapterContext
{
  /**
   * Peer number for the particular peer.
   */
  unsigned int peer;

  /**
   * General context.
   */
  struct GNUNET_CADET_TEST_Context *ctx;
};


/**
 * Adapter function called to establish a connection to
 * the CADET service.
 *
 * @param cls closure
 * @param cfg configuration of the peer to connect to; will be available until
 *          GNUNET_TESTBED_operation_done() is called on the operation returned
 *          from GNUNET_TESTBED_service_connect()
 * @return service handle to return in 'op_result', NULL on error
 */
static void *
cadet_connect_adapter (void *cls,
                      const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  struct GNUNET_CADET_TEST_AdapterContext *actx = cls;
  struct GNUNET_CADET_TEST_Context *ctx = actx->ctx;
  struct GNUNET_CADET_Handle *h;

  h = GNUNET_CADET_connect (cfg,
                           (void *) (long) actx->peer,
                           ctx->new_channel,
                           ctx->cleaner,
                           ctx->handlers,
                           ctx->ports);
  return h;
}


/**
 * Adapter function called to destroy a connection to
 * the CADET service.
 *
 * @param cls closure
 * @param op_result service handle returned from the connect adapter
 */
static void
cadet_disconnect_adapter (void *cls,
                         void *op_result)
{
  struct GNUNET_CADET_Handle *cadet = op_result;
  struct GNUNET_CADET_TEST_AdapterContext *actx = cls;

  GNUNET_free (actx);
  GNUNET_CADET_disconnect (cadet);
}


/**
 * Callback to be called when a service connect operation is completed.
 *
 * @param cls The callback closure from functions generating an operation.
 * @param op The operation that has been finished.
 * @param ca_result The service handle returned from
 *                  GNUNET_TESTBED_ConnectAdapter() (cadet handle).
 * @param emsg Error message in case the operation has failed.
 *             NULL if operation has executed successfully.
 */
static void
cadet_connect_cb (void *cls,
                 struct GNUNET_TESTBED_Operation *op,
                 void *ca_result,
                 const char *emsg)
{
  struct GNUNET_CADET_TEST_Context *ctx = cls;
  unsigned int i;

  if (NULL != emsg)
  {
    fprintf (stderr, "Failed to connect to CADET service: %s\n",
             emsg);
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
  for (i = 0; i < ctx->num_peers; i++)
    if (op == ctx->ops[i])
    {
      ctx->cadetes[i] = ca_result;
      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "...cadet %u connected\n", i);
    }
  for (i = 0; i < ctx->num_peers; i++)
    if (NULL == ctx->cadetes[i])
      return; /* still some CADET connections missing */
  /* all CADET connections ready! */
  ctx->app_main (ctx->app_main_cls,
                 ctx,
                 ctx->num_peers,
                 ctx->peers,
                 ctx->cadetes);
}


void
GNUNET_CADET_TEST_cleanup (struct GNUNET_CADET_TEST_Context *ctx)
{
  unsigned int i;

  for (i = 0; i < ctx->num_peers; i++)
  {
    GNUNET_assert (NULL != ctx->ops[i]);
    GNUNET_TESTBED_operation_done (ctx->ops[i]);
    ctx->ops[i] = NULL;
  }
  GNUNET_free (ctx->ops);
  GNUNET_free (ctx->cadetes);
  GNUNET_free (ctx);
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * Callback run when the testbed is ready (peers running and connected to
 * each other)
 *
 * @param cls Closure (context).
 * @param h the run handle
 * @param num_peers Number of peers that are running.
 * @param peers Handles to each one of the @c num_peers peers.
 * @param links_succeeded the number of overlay link connection attempts that
 *          succeeded
 * @param links_failed the number of overlay link connection attempts that
 *          failed
 */
static void
cadet_test_run (void *cls,
               struct GNUNET_TESTBED_RunHandle *h,
               unsigned int num_peers,
               struct GNUNET_TESTBED_Peer **peers,
               unsigned int links_succeeded,
               unsigned int links_failed)
{
  struct GNUNET_CADET_TEST_Context *ctx = cls;
  unsigned int i;

  if  (num_peers != ctx->num_peers)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers started %u/%u, ending\n",
                num_peers, ctx->num_peers);
    exit (1);
  }
  ctx->peers = peers;
  for (i = 0; i < num_peers; i++)
  {
    struct GNUNET_CADET_TEST_AdapterContext *newctx;
    newctx = GNUNET_new (struct GNUNET_CADET_TEST_AdapterContext);
    newctx->peer = i;
    newctx->ctx = ctx;
    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting to cadet %u\n", i);
    ctx->ops[i] = GNUNET_TESTBED_service_connect (ctx,
                                                  peers[i],
                                                  "cadet",
                                                  &cadet_connect_cb,
                                                  ctx,
                                                  &cadet_connect_adapter,
                                                  &cadet_disconnect_adapter,
                                                  newctx);
    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "op handle %p\n", ctx->ops[i]);
  }
}


void
GNUNET_CADET_TEST_run (const char *testname,
                      const char *cfgname,
                      unsigned int num_peers,
                      GNUNET_CADET_TEST_AppMain tmain,
                      void *tmain_cls,
                      GNUNET_CADET_InboundChannelNotificationHandler new_channel,
                      GNUNET_CADET_ChannelEndHandler cleaner,
                      struct GNUNET_CADET_MessageHandler* handlers,
                      const uint32_t *ports)
{
  struct GNUNET_CADET_TEST_Context *ctx;

  ctx = GNUNET_new (struct GNUNET_CADET_TEST_Context);
  ctx->num_peers = num_peers;
  ctx->ops = GNUNET_malloc (num_peers * sizeof (struct GNUNET_TESTBED_Operation *));
  ctx->cadetes = GNUNET_malloc (num_peers * sizeof (struct GNUNET_CADET_Handle *));
  ctx->app_main = tmain;
  ctx->app_main_cls = tmain_cls;
  ctx->new_channel = new_channel;
  ctx->cleaner = cleaner;
  ctx->handlers = handlers;
  ctx->ports = ports;
  GNUNET_TESTBED_test_run (testname,
                           cfgname,
                           num_peers,
                           0LL, NULL, NULL,
                           &cadet_test_run, ctx);
}

/* end of cadet_test_lib.c */