summaryrefslogtreecommitdiff
path: root/src/dht/test_dht_twopeer.c
blob: 2adfda0ef6f0d06c0271fcad7408c853cb6257a7 (plain)
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
509
510
511
512
513
514
515
516
517
/*
     This file is part of GNUnet.
     (C) 2009 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 dht/test_dht_twopeer.c
 * @brief base testcase for testing DHT service with
 *        two running peers
 */
#include "platform.h"
#include "gnunet_testing_lib.h"
#include "gnunet_core_service.h"
#include "gnunet_dht_service.h"

/* DEFINES */
#define VERBOSE GNUNET_EXTRA_LOGGING

#define MAX_GET_ATTEMPTS 10

#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)

#define DEFAULT_NUM_PEERS 2

/* Structs */

struct PeerGetContext
{
  struct GNUNET_PeerIdentity *peer;

  struct GNUNET_DHT_Handle *dht_handle;

  struct GNUNET_DHT_GetHandle *get_handle;

  unsigned int get_attempts;

  GNUNET_SCHEDULER_TaskIdentifier retry_task;
};

/* Globals */
static char *test_directory;

static struct PeerGetContext curr_get_ctx;

static unsigned int expected_connections;

static unsigned long long peers_left;

static struct GNUNET_TESTING_PeerGroup *pg;

static unsigned long long num_peers;

static unsigned int total_gets;

static unsigned int gets_succeeded;

static unsigned int total_connections;

static unsigned int failed_connections;

static GNUNET_SCHEDULER_TaskIdentifier die_task;

static int ok;

static struct GNUNET_PeerIdentity peer1id;

static struct GNUNET_PeerIdentity peer2id;

static struct GNUNET_DHT_Handle *peer1dht;

static struct GNUNET_DHT_Handle *peer2dht;

/**
 * Check whether peers successfully shut down.
 */
static void
shutdown_callback (void *cls, const char *emsg)
{
  if (emsg != NULL)
  {
    if (ok == 0)
      ok = 2;
  }
}

static void
finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  GNUNET_assert (pg != NULL);
  GNUNET_assert (peer1dht != NULL);
  GNUNET_assert (peer2dht != NULL);
  GNUNET_DHT_disconnect (peer1dht);
  GNUNET_DHT_disconnect (peer2dht);
  GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
  pg = NULL;
  ok = 0;
}

static void
end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  if (peer1dht != NULL)
    GNUNET_DHT_disconnect (peer1dht);

  if (peer2dht != NULL)
    GNUNET_DHT_disconnect (peer2dht);

  if (pg != NULL)
  {
    GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
    pg = NULL;
  }

  if (curr_get_ctx.retry_task != GNUNET_SCHEDULER_NO_TASK)
  {
    GNUNET_SCHEDULER_cancel (curr_get_ctx.retry_task);
    curr_get_ctx.retry_task = GNUNET_SCHEDULER_NO_TASK;
  }
}


static void
end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  const char *emsg = cls;

  FPRINTF (stderr, "Error: %s\n", emsg);
  if (curr_get_ctx.retry_task != GNUNET_SCHEDULER_NO_TASK)
  {
    GNUNET_SCHEDULER_cancel (curr_get_ctx.retry_task);
    curr_get_ctx.retry_task = GNUNET_SCHEDULER_NO_TASK;
  }
  if (curr_get_ctx.get_handle != NULL)
  {
    GNUNET_DHT_get_stop (curr_get_ctx.get_handle);
  }

  GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
  ok = 1;
}


/* Forward declaration */
static void
do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);

/**
 * Iterator called on each result obtained for a DHT
 * operation that expects a reply
 *
 * @param cls closure
 * @param exp when will this value expire
 * @param key key of the result
 * @param type type of the result
 * @param size number of bytes in data
 * @param data pointer to the result data
 */
static void
get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
                     const GNUNET_HashCode * key,
                     const struct GNUNET_PeerIdentity *get_path,
                     unsigned int get_path_length,
                     const struct GNUNET_PeerIdentity *put_path,
                     unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
                     size_t size, const void *data)
{
  struct PeerGetContext *get_context = cls;

  if (0 !=
      memcmp (&get_context->peer->hashPubKey, key, sizeof (GNUNET_HashCode)))
  {
    FPRINTF (stderr, "%s",  "??\n");
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Key returned is not the same key as was searched for!\n");
    GNUNET_SCHEDULER_cancel (die_task);
    die_task =
        GNUNET_SCHEDULER_add_now (&end_badly,
                                  "key mismatch in get response!\n");
    return;
  }
  FPRINTF (stderr, "%s",  "!\n");
  if (get_context->retry_task != GNUNET_SCHEDULER_NO_TASK)
  {
    GNUNET_SCHEDULER_cancel (get_context->retry_task);
    get_context->retry_task = GNUNET_SCHEDULER_NO_TASK;
  }

  if (get_context->peer == &peer2id)
  {
    get_context->peer = &peer1id;
    get_context->dht_handle = peer2dht;
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Received first correct GET request response!\n");
    GNUNET_DHT_get_stop (get_context->get_handle);
    GNUNET_SCHEDULER_add_now (&do_get, get_context);
  }
  else
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Received second correct GET request response!\n");
    GNUNET_SCHEDULER_cancel (die_task);
    GNUNET_DHT_get_stop (get_context->get_handle);
    die_task = GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
  }

}

static void
stop_retry_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);


static void
get_stop_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct PeerGetContext *get_context = cls;

  if (get_context->get_attempts < MAX_GET_ATTEMPTS)
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Get attempt %u failed, retrying request!\n",
                get_context->get_attempts);
  else
  {
    FPRINTF (stderr, "%s",  "?\n");
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Too many attempts failed, ending test!\n",
                get_context->get_attempts);
    GNUNET_SCHEDULER_cancel (die_task);
    die_task =
        GNUNET_SCHEDULER_add_now (&end_badly,
                                  "GET attempt failed, ending test!\n");
    return;
  }
  FPRINTF (stderr, "%s",  ".");
  get_context->get_attempts++;
  get_context->retry_task =
      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
                                    (GNUNET_TIME_UNIT_SECONDS, 10),
                                    &stop_retry_get, get_context);
  get_context->get_handle =
      GNUNET_DHT_get_start (get_context->dht_handle,
                            GNUNET_TIME_relative_multiply
                            (GNUNET_TIME_UNIT_SECONDS, 5),
                            GNUNET_BLOCK_TYPE_DHT_HELLO,
                            &get_context->peer->hashPubKey, 1,
                            GNUNET_DHT_RO_NONE, NULL, 0, &get_result_iterator,
                            get_context);
}


static void
stop_retry_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct PeerGetContext *get_context = cls;

  get_context->retry_task = GNUNET_SCHEDULER_NO_TASK;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Get attempt %u failed, canceling request!\n",
              get_context->get_attempts);
  GNUNET_DHT_get_stop (get_context->get_handle);
  get_context->get_handle = NULL;
  GNUNET_SCHEDULER_add_now (&get_stop_finished, get_context);
}


static void
do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct PeerGetContext *get_context = cls;

  get_context->retry_task =
      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
                                    (GNUNET_TIME_UNIT_SECONDS, 10),
                                    &stop_retry_get, get_context);
  get_context->get_handle =
      GNUNET_DHT_get_start (get_context->dht_handle,
                            GNUNET_TIME_relative_multiply
                            (GNUNET_TIME_UNIT_SECONDS, 5),
                            GNUNET_BLOCK_TYPE_DHT_HELLO,
                            &get_context->peer->hashPubKey, 1,
                            GNUNET_DHT_RO_FIND_PEER, NULL, 0,
                            &get_result_iterator, get_context);
}


static void
topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
                   const struct GNUNET_PeerIdentity *second, uint32_t distance,
                   const struct GNUNET_CONFIGURATION_Handle *first_cfg,
                   const struct GNUNET_CONFIGURATION_Handle *second_cfg,
                   struct GNUNET_TESTING_Daemon *first_daemon,
                   struct GNUNET_TESTING_Daemon *second_daemon,
                   const char *emsg)
{
  if (emsg == NULL)
  {
    total_connections++;
#if VERBOSE
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "connected peer %s to peer %s, distance %u\n",
                first_daemon->shortname, second_daemon->shortname, distance);
#endif
  }
#if VERBOSE
  else
  {
    failed_connections++;
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Failed to connect peer %s to peer %s with error :\n%s\n",
                first_daemon->shortname, second_daemon->shortname, emsg);
  }
#endif

  if (total_connections == expected_connections)
  {
#if VERBOSE
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Created %d total connections, which is our target number!  Starting next phase of testing.\n",
                total_connections);
#endif
    GNUNET_SCHEDULER_cancel (die_task);
    die_task =
        GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
                                      "Timeout trying to GET");

    curr_get_ctx.dht_handle = peer1dht;
    curr_get_ctx.peer = &peer2id;
    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
                                  (GNUNET_TIME_UNIT_SECONDS, 2), &do_get,
                                  &curr_get_ctx);
  }
  else if (total_connections + failed_connections == expected_connections)
  {
    GNUNET_SCHEDULER_cancel (die_task);
    die_task =
        GNUNET_SCHEDULER_add_now (&end_badly,
                                  "from topology_callback (too many failed connections)");
  }
}


static void
connect_topology (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  expected_connections = -1;
  if ((pg != NULL) && (peers_left == 0))
    expected_connections =
        GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_CLIQUE,
                                         GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
                                         0.0, TIMEOUT, 12, NULL, NULL);

  GNUNET_SCHEDULER_cancel (die_task);
  if (expected_connections == GNUNET_SYSERR)
    die_task =
        GNUNET_SCHEDULER_add_now (&end_badly,
                                  "from connect topology (bad return)");
  else
    die_task =
        GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
                                      "from connect topology (timeout)");
}


static void
peers_started_callback (void *cls, const struct GNUNET_PeerIdentity *id,
                        const struct GNUNET_CONFIGURATION_Handle *cfg,
                        struct GNUNET_TESTING_Daemon *d, const char *emsg)
{
  if (emsg != NULL)
  {
    FPRINTF (stderr, "Failed to start daemon: `%s'\n", emsg);
    return;
  }
  GNUNET_assert (id != NULL);
  if (peers_left == num_peers)
  {
    memcpy (&peer1id, id, sizeof (struct GNUNET_PeerIdentity));
    peer1dht = GNUNET_DHT_connect (cfg, 100);
    if (peer1dht == NULL)
    {
      GNUNET_SCHEDULER_cancel (die_task);
      die_task =
          GNUNET_SCHEDULER_add_now (&end_badly, "Failed to get dht handle!\n");
    }
  }
  else
  {
    memcpy (&peer2id, id, sizeof (struct GNUNET_PeerIdentity));
    peer2dht = GNUNET_DHT_connect (cfg, 100);
    if (peer2dht == NULL)
    {
      GNUNET_SCHEDULER_cancel (die_task);
      die_task =
          GNUNET_SCHEDULER_add_now (&end_badly, "Failed to get dht handle!\n");
    }
  }


  peers_left--;

  if (peers_left == 0)
  {
#if VERBOSE
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "All %d daemons started, now connecting peers!\n", num_peers);
#endif
    GNUNET_SCHEDULER_cancel (die_task);
    /* Set up task in case topology creation doesn't finish
     * within a reasonable amount of time */
    die_task =
        GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
                                      "from peers_started_callback");

    GNUNET_SCHEDULER_add_now (&connect_topology, NULL);
    ok = 0;
  }
}


static void
run (void *cls, char *const *args, const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{

  if (GNUNET_YES !=
      GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
                                             &test_directory))
  {
    ok = 404;
    return;
  }

  if (GNUNET_SYSERR ==
      GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
                                             &num_peers))
    num_peers = DEFAULT_NUM_PEERS;

  peers_left = num_peers;
  total_gets = num_peers;
  gets_succeeded = 0;
  /* Set up a task to end testing if peer start fails */
  die_task =
      GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
                                    "didn't start all daemons in reasonable amount of time!!!");

  pg = GNUNET_TESTING_daemons_start (cfg, num_peers, 10, num_peers, TIMEOUT,
                                     NULL, NULL, &peers_started_callback, NULL,
                                     &topology_callback, NULL, NULL);

}

static int
check ()
{
  int ret;

  char *const argv[] = { "test-dht-twopeer",
    "-c",
    "test_dht_twopeer_data.conf",
#if VERBOSE
    "-L", "DEBUG",
#endif
    NULL
  };
  struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_OPTION_END
  };
  ret =
      GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
                          "test-dht-twopeer", "nohelp", options, &run, &ok);
  if (ret != GNUNET_OK)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "`test-dht-twopeer': Failed with error code %d\n", ret);
  }
  return ok;
}

int
main (int argc, char *argv[])
{
  int ret;

  GNUNET_log_setup ("test-dht-twopeer",
#if VERBOSE
                    "DEBUG",
#else
                    "WARNING",
#endif
                    NULL);
  ret = check ();
  /**
   * Need to remove base directory, subdirectories taken care
   * of by the testing framework.
   */
  if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Failed to remove testing directory %s\n", test_directory);
  }
  return ret;
}

/* end of test_dht_twopeer.c */