aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_address_switch.c
blob: 96e11b7de9a528c71de15e72b8552e32b6dd488b (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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
 This file is part of GNUnet.
 Copyright (C) 2009, 2010, 2011, 2016 GNUnet e.V.

 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., 51 Franklin Street, Fifth Floor,
 Boston, MA 02110-1301, USA.
 */
/**
 * @file transport/test_transport_address_switch.c
 * @brief base test case for transport implementations
 *
 * This test case tests if peers can successfully switch addresses when
 * connected for plugins supporting multiple addresses by monitoring transport's
 * statistic values.
 *
 * This test starts 2 peers and connects them. When connected test messages
 * are transmitted from peer 2 to peer 1. The test monitors transport's
 * statistics values for information about address switch attempts.
 *
 * The test passes with success if one of the peers could successfully switch
 * addresses in connected state and a test message was successfully transmitted
 * after this switch.
 *
 * Since it is not possible to trigger an address switch from outside,
 * the test returns "77" (skipped) when no address switching attempt
 * takes place. It fails if an address switch attempt fails.
 *
 * NOTE: The test seems largely useless right now, as we simply NEVER
 * switch addresses under the test conditions.  However, it may be a
 * good starting point for a future test.  For now, it always times
 * out and returns "77" (skipped), so we set the timeout suitably low.
 */
#include "platform.h"
#include "gnunet_transport_service.h"
#include "gnunet_ats_service.h"
#include "transport-testing.h"


GNUNET_NETWORK_STRUCT_BEGIN
struct TestMessage
{
  struct GNUNET_MessageHeader header;
  uint32_t num GNUNET_PACKED;
};
GNUNET_NETWORK_STRUCT_END

/**
 * Message type for test messages
 */
#define MTYPE 12345

/**
 * Message size for test messages
 */
#define MSIZE 2048

/**
 * Testcase timeout
 */
#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)

/**
 * How long until we give up on transmitting the message?
 */
#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)


static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;

static struct GNUNET_SCHEDULER_Task *measure_task;

static struct GNUNET_TRANSPORT_TransmitHandle *th;

/**
 * Statistics we track per peer.
 */
struct PeerStats
{
  struct GNUNET_STATISTICS_Handle *stat;

  unsigned int addresses_avail;

  unsigned int switch_attempts;

  unsigned int switch_success;

  unsigned int switch_fail;
};

static struct PeerStats stats[2];

/* Amount of data transfered since last switch attempt */
static unsigned long long bytes_sent_after_switch;

static unsigned long long bytes_recv_after_switch;


static int
stat_start_attempt_cb (void *cls,
                       const char *subsystem,
                       const char *name,
                       uint64_t value,
                       int is_persistent)
{
  struct PeerStats *stat = cls;

  stat->switch_attempts++;
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Switch attempted (%p)",
              stat);
  bytes_recv_after_switch = 0;
  bytes_sent_after_switch = 0;

  return GNUNET_OK;
}


static int
stat_success_attempt_cb (void *cls,
                         const char *subsystem,
                         const char *name,
                         uint64_t value,
                         int is_persistent)
{
  struct PeerStats *stat = cls;

  stat->switch_success++;
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Switch succeeded (%p)",
              stat);
  return GNUNET_OK;
}


static int
stat_fail_attempt_cb (void *cls,
                      const char *subsystem,
                      const char *name,
                      uint64_t value,
                      int is_persistent)
{
  struct PeerStats *stat = cls;

  if (value == 0)
    return GNUNET_OK;

  stat->switch_fail++;
  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
              "Switch failed (%p)",
              stat);
  return GNUNET_OK;
}


static int
stat_addresses_available (void *cls,
                          const char *subsystem,
                          const char *name,
                          uint64_t value,
                          int is_persistent)
{
  struct PeerStats *stat = cls;

  stat->addresses_avail++;
  return GNUNET_OK;
}


/**
 * List of statistics entries we care about.
 */
static struct WatchEntry {

  /**
   * Name of the statistic we watch.
   */
  const char *stat_name;

  /**
   * Handler to register;
   */
  GNUNET_STATISTICS_Iterator stat_handler;
} watches[] = {
  { "# Attempts to switch addresses", &stat_start_attempt_cb },
  { "# Successful attempts to switch addresses", &stat_success_attempt_cb },
  { "# Failed attempts to switch addresses (failed to send CONNECT CONT)", &stat_fail_attempt_cb },
  { "# Failed attempts to switch addresses (failed to send CONNECT)", &stat_fail_attempt_cb },
  { "# Failed attempts to switch addresses (no response)", &stat_fail_attempt_cb },
  { "# transport addresses", &stat_addresses_available },
  { NULL, NULL }
};


static void
custom_shutdown (void *cls)
{
  int result;

  if (NULL != measure_task)
  {
    GNUNET_SCHEDULER_cancel (measure_task);
    measure_task = NULL;
  }
  if (0 == stats[0].switch_attempts + stats[1].switch_attempts)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Test did not work, as peers didn't switch (flawed testcase)!\n");
    ccc->global_ret = 77;
  }
  else
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Fail (timeout)! No transmission after switch! Stopping peers\n");
    ccc->global_ret = GNUNET_SYSERR;
  }

  /* stop statistics */
  for (unsigned int i=0;i<2;i++)
  {
    if (NULL != stats[i].stat)
    {
      for (unsigned int j=0;NULL != watches[j].stat_name; j++)
        GNUNET_STATISTICS_watch_cancel (stats[i].stat,
                                        "transport",
                                        watches[j].stat_name,
                                        watches[j].stat_handler,
                                        &stats[i]);

      GNUNET_STATISTICS_destroy (stats[i].stat,
                                 GNUNET_NO);
      stats[i].stat = NULL;
    }
  }
  if (NULL != th)
  {
    GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
    th = NULL;
  }

  result = 0;
  FPRINTF (stderr, "\n");
  if (stats[0].switch_attempts > 0)
  {
    FPRINTF (stderr,
             "Peer 1 tried %u times to switch and succeeded %u times, failed %u times\n",
             stats[0].switch_attempts,
             stats[0].switch_success,
             stats[0].switch_fail);
    if (stats[0].switch_success != stats[0].switch_attempts)
    {
      GNUNET_break (0);
      result ++;
    }
  }
  else if (stats[0].addresses_avail > 1)
  {
    FPRINTF (stderr,
             "Peer 1 had %u addresses available, but did not try to switch\n",
             stats[0].addresses_avail);
  }
  if (stats[1].switch_attempts > 0)
  {
    FPRINTF (stderr,
             "Peer 2 tried %u times to switch and succeeded %u times, failed %u times\n",
             stats[1].switch_attempts,
             stats[1].switch_success,
             stats[1].switch_fail);
    if (stats[1].switch_success != stats[1].switch_attempts)
    {
      GNUNET_break (0);
      result++;
    }
  }
  else if (stats[1].addresses_avail > 1)
  {
    FPRINTF (stderr,
             "Peer 2 had %u addresses available, but did not try to switch\n",
             stats[1].addresses_avail);
  }

  if ( ((stats[0].switch_attempts > 0) || (stats[1].switch_attempts > 0)) &&
       (bytes_sent_after_switch == 0) )
  {
    FPRINTF (stderr, "No data sent after switching!\n");
    GNUNET_break (0);
    result++;
  }
  if ( ((stats[0].switch_attempts > 0) || (stats[1].switch_attempts > 0)) &&
       (bytes_recv_after_switch == 0) )
  {
    FPRINTF (stderr, "No data received after switching!\n");
    GNUNET_break (0);
    result++;
  }
  if (0 != result)
    ccc->global_ret = GNUNET_SYSERR;
}


static void
notify_receive (void *cls,
                struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
                const struct GNUNET_PeerIdentity *sender,
                const struct GNUNET_MessageHeader *message)
{
  const struct TestMessage *hdr;

  hdr = (const struct TestMessage *) message;
  if (MTYPE != ntohs (message->type))
    return;

  {
    char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));

    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "Peer %u (`%s') got message %u of size %u from peer (`%s')\n",
                receiver->no,
                ps,
                ntohl (hdr->num),
                ntohs (message->size),
                GNUNET_i2s (sender));
    GNUNET_free (ps);
  }
  if ( ((stats[0].switch_attempts >= 1) || (stats[1].switch_attempts >= 1)) &&
        (stats[0].switch_attempts == stats[0].switch_fail + stats[0].switch_success) &&
        (stats[1].switch_attempts == stats[1].switch_fail + stats[1].switch_success) )
  {
    bytes_recv_after_switch += ntohs(hdr->header.size);
    if ((bytes_sent_after_switch > 0) && (bytes_recv_after_switch > 0))
    {
      /* A peer switched addresses and sent and received data after the
       * switch operations */
      GNUNET_SCHEDULER_shutdown ();
    }
  }
}


static size_t
notify_ready (void *cls, size_t size, void *buf)
{
  static uint32_t counter;
  char *cbuf = buf;
  struct TestMessage hdr;

  th = NULL;
  if (buf == NULL)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
               "Timeout occurred while waiting for transmit_ready for message\n");
    ccc->global_ret = GNUNET_SYSERR;
    GNUNET_SCHEDULER_shutdown ();
    return 0;
  }

  GNUNET_assert(size >= MSIZE);
  GNUNET_assert(buf != NULL);
  cbuf = buf;

  hdr.header.size = htons (MSIZE);
  hdr.header.type = htons (MTYPE);
  hdr.num = htonl (counter++);
  GNUNET_memcpy (&cbuf[0], &hdr, sizeof(struct TestMessage));
  memset (&cbuf[sizeof(struct TestMessage)], '0', MSIZE - sizeof(struct TestMessage));

  {
    char *receiver_s = GNUNET_strdup (GNUNET_i2s (&ccc->p[0]->id));

    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "Sending message %u of size %u from peer %u (`%4s') -> peer %u (`%s') !\n",
                (unsigned int) (counter - 1),
                MSIZE,
                ccc->p[1]->no,
                GNUNET_i2s (&ccc->p[1]->id),
                ccc->p[0]->no,
                receiver_s);
    GNUNET_free(receiver_s);
  }

  if (th == NULL)
    th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[1]->th,
                                                 &ccc->p[0]->id,
                                                 MSIZE,
                                                 TIMEOUT_TRANSMIT,
                                                 &notify_ready,
                                                 NULL);

  if ( ( (stats[0].switch_attempts >= 1) ||
         (stats[1].switch_attempts >= 1) ) &&
       (stats[0].switch_attempts == stats[0].switch_fail + stats[0].switch_success) &&
       (stats[1].switch_attempts == stats[1].switch_fail + stats[1].switch_success) )
  {
    bytes_sent_after_switch += MSIZE;
  }
  return MSIZE;
}


static void
notify_disconnect (void *cls,
                   struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
                   const struct GNUNET_PeerIdentity *other)
{
  if (NULL != th)
  {
    GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
    th = NULL;
  }
}


static void
progress_indicator (void *cls)
{
  static int counter;

  measure_task = NULL;
  counter++;
  if ((TIMEOUT.rel_value_us / 1000 / 1000LL) < counter)
  {
    FPRINTF (stderr, "%s", ".\n");
  }
  else
  {
    FPRINTF (stderr, "%s", ".");
    measure_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
                                                 &progress_indicator,
                                                 NULL);
  }
}


static void
connected_cb (void *cls)
{
  for (unsigned int i=0;i<2;i++)
  {
    stats[i].stat = GNUNET_STATISTICS_create ("transport",
                                              ccc->p[i]->cfg);
    if (NULL == stats[i].stat)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "Fail! Could not create statistics for peers!\n");
      ccc->global_ret = GNUNET_SYSERR;
      GNUNET_SCHEDULER_shutdown ();
      return;
    }
    for (unsigned int j=0;NULL != watches[j].stat_name; j++)
    {
      GNUNET_STATISTICS_watch (stats[i].stat,
                               "transport",
                               watches[j].stat_name,
                               watches[j].stat_handler,
                               &stats[i]);
    }
  }
  /* Show progress */
  ccc->global_ret = GNUNET_OK;
  measure_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
                                               &progress_indicator,
                                               NULL);
  /* Peers are connected, start transmit test messages */
  th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[1]->th,
                                               &ccc->p[0]->id, MSIZE,
                                               TIMEOUT_TRANSMIT,
                                               &notify_ready, NULL);

}


int
main (int argc,
      char *argv[])
{
  struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
    .connect_continuation = &connected_cb,
    .config_file = "test_transport_startonly.conf",
    .rec = &notify_receive,
    .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
    .nd = &notify_disconnect,
    .shutdown_task = &custom_shutdown,
    .timeout = TIMEOUT
  };
  ccc = &my_ccc;
  int ret;

  ret = GNUNET_TRANSPORT_TESTING_main (2,
                                       &GNUNET_TRANSPORT_TESTING_connect_check,
                                       ccc);
  if (77 == ret)
    return 77;
  if (GNUNET_OK != ret)
    return 1;
  return 0;
}
/* end of test_transport_address_switch.c */