aboutsummaryrefslogtreecommitdiff
path: root/src/cadet/test_cadet_local_mq.c
blob: 2e9cfa918a34e068c09cc89f075099ef87c7ded2 (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
     This file is part of GNUnet.
     Copyright (C) 2017 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     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
     Affero General Public License for more details.
    
     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file cadet/test_cadet_local_mq.c
 * @brief test cadet local: test of cadet channels with just one peer
 * @author Bartlomiej Polot
 */

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_dht_service.h"
#include "gnunet_testing_lib.h"
#include "gnunet_cadet_service.h"

#define TEST_MESSAGE_TYPE 1
#define TEST_PORT_ID 1

/**
 * Test message structure.
 */
struct GNUNET_CADET_TestMsg
{
  /**
   * Type: #TEST_MESSAGE_TYPE
   *
   * Size: sizeof(struct GNUNET_CADET_TestMsg)
   */
  struct GNUNET_MessageHeader header;

  /**
   * Test payload.
   */
  uint64_t payload;
};

struct GNUNET_TESTING_Peer *me;

static struct GNUNET_CADET_Handle *cadet_peer_1;

static struct GNUNET_CADET_Handle *cadet_peer_2;

static struct GNUNET_CADET_Channel *ch;

static int result = GNUNET_OK;

static int got_data = GNUNET_NO;

static struct GNUNET_SCHEDULER_Task *abort_task;

static struct GNUNET_SCHEDULER_Task *connect_task;


/**
 * Connect to other client and send data
 *
 * @param cls Closue (unused).
 */
static void
do_connect (void *cls);


/**
 * Shutdown nicely
 */
static void
do_shutdown (void *cls)
{
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "shutdown\n");
  if (NULL != abort_task)
  {
    GNUNET_SCHEDULER_cancel (abort_task);
    abort_task = NULL;
  }
  if (NULL != ch)
  {
    GNUNET_CADET_channel_destroy (ch);
    ch = NULL;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Disconnect client 1\n");
  if (NULL != cadet_peer_1)
  {
    GNUNET_CADET_disconnect (cadet_peer_1);
    cadet_peer_1 = NULL;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Disconnect client 2\n");
  if (NULL != cadet_peer_2)
  {
    GNUNET_CADET_disconnect (cadet_peer_2);
    cadet_peer_2 = NULL;
  }
  if (NULL != connect_task)
  {
    GNUNET_SCHEDULER_cancel (connect_task);
    connect_task = NULL;
  }
}


/**
 * Something went wrong and timed out. Kill everything and set error flag
 */
static void
do_abort (void *cls)
{
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
              "ABORT from line %ld\n",
              (long) cls);
  result = GNUNET_SYSERR;
  abort_task = NULL;
  GNUNET_SCHEDULER_shutdown ();
}

/**
 * Method called whenever a peer connects to a port in MQ-based CADET.
 *
 * @param cls Closure from #GNUNET_CADET_open_port.
 * @param channel New handle to the channel.
 * @param source Peer that started this channel.
 * @return Closure for the incoming @a channel. It's given to:
 *         - The #GNUNET_CADET_DisconnectEventHandler (given to
 *           #GNUNET_CADET_open_port) when the channel dies.
 *         - Each the #GNUNET_MQ_MessageCallback handlers for each message
 *           received on the @a channel.
 */
static void *
connected (void *cls,
           struct GNUNET_CADET_Channel *channel,
           const struct GNUNET_PeerIdentity *source)
{
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "connected %s, cls: %p\n",
              GNUNET_i2s(source),
              cls);
  return channel;
}

/**
 * Function called whenever an MQ-channel is destroyed, even if the destruction
 * was requested by #GNUNET_CADET_channel_destroy.
 * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
 *
 * It should clean up any associated state, including cancelling any pending
 * transmission on this channel.
 *
 * @param cls Channel closure.
 * @param channel Connection to the other end (henceforth invalid).
 */
static void
disconnected (void *cls,
              const struct GNUNET_CADET_Channel *channel)
{
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "disconnected channel %p, cls: %p\n",
              channel, cls);
  if (channel == ch)
    ch = NULL;
}


/**
 * Handle test data
 *
 * @param h     The cadet handle
 * @param msg   A message with the details of the new incoming channel
 */
static void
handle_data_received (void *cls,
                      const struct GNUNET_CADET_TestMsg *msg)
{
  uint64_t payload;

  payload = GNUNET_ntohll (msg->payload);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Data callback payload %llu with cls: %p! Shutting down.\n",
              (unsigned long long) payload,
              cls);
  GNUNET_assert (42 == payload);
  got_data = GNUNET_YES;
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * Signature of the main function of a task.
 *
 * @param cls Closure (unused).
 */
static void
message_sent (void *cls)
{
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "message sent\n");
}


/**
 * Connect to other client and send data
 *
 * @param cls Closure (unused).
 */
static void
do_connect (void *cls)
{
  struct GNUNET_PeerIdentity id;
  struct GNUNET_MQ_Handle *mq;
  struct GNUNET_MQ_Envelope *env;
  struct GNUNET_CADET_TestMsg *msg;

  struct GNUNET_MQ_MessageHandler handlers[] = {
    GNUNET_MQ_hd_fixed_size (data_received,
                             TEST_MESSAGE_TYPE,
                             struct GNUNET_CADET_TestMsg,
                             cadet_peer_1),
    GNUNET_MQ_handler_end ()
  };

  connect_task = NULL;
  GNUNET_TESTING_peer_get_identity (me, &id);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "creating channel\n");
  ch = GNUNET_CADET_channel_create (cadet_peer_1, /* cadet handle */
                                    NULL,         /* channel cls */
                                    &id,          /* destination */
                                    GC_u2h (TEST_MESSAGE_TYPE), /* port */
                                    GNUNET_CADET_OPTION_DEFAULT, /* opt */
                                    NULL,          /* window change */
                                    &disconnected, /* disconnect handler */
                                    handlers       /* traffic handlers */
                                   );
  env = GNUNET_MQ_msg (msg, TEST_MESSAGE_TYPE);
  msg->payload = GNUNET_htonll (42);
  mq = GNUNET_CADET_get_mq (ch);
  GNUNET_MQ_notify_sent (env, &message_sent, NULL);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "sending message\n");
  GNUNET_MQ_send (mq, env);
}


/**
 * Initialize framework and start test
 *
 * @param cls Closure (unused).
 * @param cfg Configuration handle.
 * @param peer Testing peer handle.
 */
static void
run (void *cls,
     const struct GNUNET_CONFIGURATION_Handle *cfg,
     struct GNUNET_TESTING_Peer *peer)
{
  struct GNUNET_MQ_MessageHandler handlers[] = {
    GNUNET_MQ_hd_fixed_size (data_received,
                             TEST_MESSAGE_TYPE,
                             struct GNUNET_CADET_TestMsg,
                             cadet_peer_2),
    GNUNET_MQ_handler_end ()
  };
  struct GNUNET_TIME_Relative delay;

  me = peer;
  GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
                                 NULL);
  delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15);
  abort_task = GNUNET_SCHEDULER_add_delayed (delay,
                                             &do_abort,
                                             (void *) (long) __LINE__);
  cadet_peer_1 = GNUNET_CADET_connect (cfg);
  cadet_peer_2 = GNUNET_CADET_connect (cfg);

  if ( (NULL == cadet_peer_1) ||
       (NULL == cadet_peer_2) )
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Couldn't connect to cadet\n");
    result = GNUNET_SYSERR;
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CADET 1: %p\n", cadet_peer_1);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CADET 2: %p\n", cadet_peer_2);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "handlers 2: %p\n", handlers);
  GNUNET_CADET_open_port (cadet_peer_2,          /* cadet handle */
                          GC_u2h (TEST_PORT_ID), /* port id */
                          &connected,            /* connect handler */
                          (void *) 2L,           /* handle for #connected */
                          NULL,                  /* window size handler */
                          &disconnected,         /* disconnect handler */
                          handlers);             /* traffic handlers */
  delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2);
  if (NULL == connect_task)
    connect_task = GNUNET_SCHEDULER_add_delayed (delay,
                                                 &do_connect,
                                                 NULL);
}


/**
 * Main
 */
int
main (int argc, char *argv[])
{
  if (0 != GNUNET_TESTING_peer_run ("test-cadet-local",
                                    "test_cadet.conf",
                                &run, NULL))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run failed\n");
    return 2;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Final result: %d\n", result);
  return (result == GNUNET_OK) ? 0 : 1;
}

/* end of test_cadet_local_1.c */