summaryrefslogtreecommitdiff
path: root/src/psycstore/test_psycstore.c
blob: 76494a85d0c21977d7a25fbfa630d19bec81056e (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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
 * This file is part of GNUnet
 * Copyright (C) 2013 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/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file psycstore/test_psycstore.c
 * @brief Test for the PSYCstore service.
 * @author Gabor X Toth
 * @author Christian Grothoff
 */

#include <inttypes.h>

#include <gnunet/platform.h>
#include <gnunet/gnunet_util_lib.h>
#include <gnunet/gnunet_common.h>
#include <gnunet/gnunet_testing_lib.h>
#include "gnunet_psycstore_service.h"

#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)


/**
 * Return value from 'main'.
 */
static int res;

/**
 * Handle to PSYCstore service.
 */
static struct GNUNET_PSYCSTORE_Handle *h;

/**
 * Handle to PSYCstore operation.
 */
static struct GNUNET_PSYCSTORE_OperationHandle *op;

/**
 * Handle for task for timeout termination.
 */
static struct GNUNET_SCHEDULER_Task *end_badly_task;

static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
static struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;

static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
static struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;

static struct FragmentClosure
{
  uint8_t n;
  uint8_t n_expected;
  uint64_t flags[16];
  struct GNUNET_MULTICAST_MessageHeader *msg[16];
} fcls;

struct StateClosure {
  size_t n;
  char *name[16];
  void *value[16];
  size_t value_size[16];
} scls;

static struct GNUNET_PSYC_Modifier modifiers[16];

/**
 * Clean up all resources used.
 */
static void
cleanup ()
{
  if (NULL != op)
  {
    GNUNET_PSYCSTORE_operation_cancel (op);
    op = NULL;
  }
  if (NULL != h)
  {
    GNUNET_PSYCSTORE_disconnect (h);
    h = NULL;
  }
  if (NULL != channel_key)
  {
    GNUNET_free (channel_key);
    channel_key = NULL;
  }
  if (NULL != slave_key)
  {
    GNUNET_free (slave_key);
    slave_key = NULL;
  }
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * Terminate the testcase (failure).
 *
 * @param cls NULL
 */
static void
end_badly (void *cls)
{
  res = 1;
  cleanup ();
}


/**
 * Terminate the testcase (success).
 *
 * @param cls NULL
 */
static void
end_normally (void *cls)
{
  res = 0;
  cleanup ();
}


/**
 * Finish the testcase (successfully).
 */
static void
end ()
{
  if (NULL != end_badly_task)
  {
    GNUNET_SCHEDULER_cancel (end_badly_task);
    end_badly_task = NULL;
  }
  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
				&end_normally, NULL);
}


static void
state_reset_result (void *cls,
                    int64_t result,
                    const char *err_msg,
                    uint16_t err_msg_size)
{
  op = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
              "state_reset_result:\t%d\n",
              (int) result);
  GNUNET_assert (GNUNET_OK == result);

  op = GNUNET_PSYCSTORE_state_reset (h, &channel_pub_key,
                                     &state_reset_result, cls);
  GNUNET_PSYCSTORE_operation_cancel (op);
  op = NULL;
  end ();
}


static int
state_result (void *cls,
              const char *name,
              const void *value,
              uint32_t value_size)
{
  struct StateClosure *scls = cls;
  const char *nam = scls->name[scls->n];
  const void *val = scls->value[scls->n];
  size_t val_size = scls->value_size[scls->n++];

  if (value_size == val_size
      && 0 == memcmp (value, val, val_size)
      && 0 == strcmp (name, nam))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "  variable %s matches\n",
                name);
    return GNUNET_YES;
  }
  else
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "  variable %s differs\nReceived: %.*s\nExpected: %.*s\n",
                name, (int) value_size, (char*) value, (int) val_size, (char*) val);
    GNUNET_assert (0);
    return GNUNET_SYSERR;
  }
}


static void
state_get_prefix_result (void *cls, int64_t result,
                         const char *err_msg, uint16_t err_msg_size)
{
  struct StateClosure *scls = cls;
  op = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_prefix_result:\t%ld\n", (long int) result);
  GNUNET_assert (GNUNET_OK == result && 2 == scls->n);

  op = GNUNET_PSYCSTORE_state_reset (h, &channel_pub_key,
                                     &state_reset_result, cls);
}


static void
state_get_result (void *cls, int64_t result,
                  const char *err_msg, uint16_t err_msg_size)
{
  op = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_result:\t%ld\n", (long int) result);
  GNUNET_assert (GNUNET_OK == result);

  scls.n = 0;

  scls.name[0] = "_sync_bar";
  scls.value[0] = "ten eleven twelve";
  scls.value_size[0] = sizeof ("ten eleven twelve") - 1;

  scls.name[1] = "_sync_foo";
  scls.value[1] = "three two one";
  scls.value_size[1] = sizeof ("three two one") - 1;

  op = GNUNET_PSYCSTORE_state_get_prefix (h, &channel_pub_key, "_sync",
                                          &state_result,
                                          &state_get_prefix_result, &scls);
}


static void
counters_result (void *cls, int status, uint64_t max_fragment_id,
                 uint64_t max_message_id, uint64_t max_group_generation,
                 uint64_t max_state_message_id)
{
  struct FragmentClosure *fcls = cls;
  int result = 0;
  op = NULL;

  if (GNUNET_OK == status
      && max_fragment_id == GNUNET_ntohll (fcls->msg[2]->fragment_id)
      && max_message_id == GNUNET_ntohll (fcls->msg[2]->message_id)
      && max_group_generation == GNUNET_ntohll (fcls->msg[2]->group_generation)
      && max_state_message_id == GNUNET_ntohll (fcls->msg[0]->message_id))
    result = 1;

  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "counters_get:\t%d\n", result);
  GNUNET_assert (result == 1);

  scls.n = 0;
  scls.name[0] = "_sync_bar";
  scls.value[0] = "ten eleven twelve";
  scls.value_size[0] = sizeof ("ten eleven twelve") - 1;

  op = GNUNET_PSYCSTORE_state_get (h, &channel_pub_key, "_sync_bar_x_yy_zzz",
                                   &state_result, &state_get_result, &scls);
}


static void
state_modify_result (void *cls, int64_t result,
                     const char *err_msg, uint16_t err_msg_size)
{
  op = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_modify_result:\t%ld\n", (long int) result);
  GNUNET_assert (GNUNET_OK == result);

  op = GNUNET_PSYCSTORE_counters_get (h, &channel_pub_key,
                                      &counters_result, cls);
}


static void
state_sync_result (void *cls, int64_t result,
                   const char *err_msg, uint16_t err_msg_size)
{
  struct FragmentClosure *fcls = cls;
  op = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_sync_result:\t%ld\n", (long int) result);
  GNUNET_assert (GNUNET_OK == result);

  op = GNUNET_PSYCSTORE_state_modify (h, &channel_pub_key,
                                      GNUNET_ntohll (fcls->msg[0]->message_id),
                                      0, state_modify_result, fcls);
}


static int
fragment_result (void *cls,
                 struct GNUNET_MULTICAST_MessageHeader *msg,
                 enum GNUNET_PSYCSTORE_MessageFlags flags)
{
  struct FragmentClosure *fcls = cls;
  GNUNET_assert (fcls->n < fcls->n_expected);
  struct GNUNET_MULTICAST_MessageHeader *msg0 = fcls->msg[fcls->n];
  uint64_t flags0 = fcls->flags[fcls->n++];

  if (flags == flags0 && msg->header.size == msg0->header.size
      && 0 == memcmp (msg, msg0, ntohs (msg->header.size)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  fragment %" PRIu64 " matches\n",
                GNUNET_ntohll (msg->fragment_id));
    return GNUNET_YES;
  }
  else
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "  fragment differs: expected %" PRIu64 ", got %" PRIu64 "\n",
                GNUNET_ntohll (msg0->fragment_id),
                GNUNET_ntohll (msg->fragment_id));
    GNUNET_assert (0);
    return GNUNET_SYSERR;
  }
}


static void
message_get_latest_result (void *cls, int64_t result,
                           const char *err_msg, uint16_t err_msg_size)
{
  struct FragmentClosure *fcls = cls;
  op = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_latest:\t%ld\n", (long int) result);
  GNUNET_assert (0 < result && fcls->n == fcls->n_expected);

  modifiers[0] = (struct GNUNET_PSYC_Modifier) {
    .oper = '=',
    .name = "_sync_foo",
    .value = "three two one",
    .value_size = sizeof ("three two one") - 1
  };
  modifiers[1] = (struct GNUNET_PSYC_Modifier) {
    .oper = '=',
    .name = "_sync_bar",
    .value = "ten eleven twelve",
    .value_size = sizeof ("ten eleven twelve") - 1
  };

  op = GNUNET_PSYCSTORE_state_sync (h, &channel_pub_key,
                                    GNUNET_ntohll (fcls->msg[0]->message_id) + 1,
                                    GNUNET_ntohll (fcls->msg[0]->message_id) + 2,
                                    2, modifiers, state_sync_result, fcls);
}


static void
message_get_result (void *cls, int64_t result,
                    const char *err_msg, uint16_t err_msg_size)
{
  struct FragmentClosure *fcls = cls;
  op = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get:\t%ld\n", (long int) result);
  GNUNET_assert (0 < result && fcls->n == fcls->n_expected);

  fcls->n = 0;
  fcls->n_expected = 3;
  op = GNUNET_PSYCSTORE_message_get_latest (h, &channel_pub_key, &slave_pub_key,
                                            1, "", &fragment_result,
                                            &message_get_latest_result, fcls);
}


static void
message_get_fragment_result (void *cls, int64_t result,
                             const char *err_msg, uint16_t err_msg_size)
{
  struct FragmentClosure *fcls = cls;
  op = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_fragment:\t%ld\n", (long int) result);
  GNUNET_assert (0 < result && fcls->n == fcls->n_expected);

  fcls->n = 0;
  fcls->n_expected = 3;
  uint64_t message_id = GNUNET_ntohll (fcls->msg[0]->message_id);
  op = GNUNET_PSYCSTORE_message_get (h, &channel_pub_key, &slave_pub_key,
                                     message_id, message_id, 0, "",
                                     &fragment_result,
                                     &message_get_result, fcls);
}


static void
fragment_get_latest_result (void *cls, int64_t result,
                            const char *err_msg, uint16_t err_msg_size)
{
  struct FragmentClosure *fcls = cls;
  op = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get_latest:\t%ld\n", (long int) result);
  GNUNET_assert (0 < result && fcls->n == fcls->n_expected);

  fcls->n = 1;
  fcls->n_expected = 2;
  op = GNUNET_PSYCSTORE_message_get_fragment (h, &channel_pub_key, &slave_pub_key,
                                              GNUNET_ntohll (fcls->msg[1]->message_id),
                                              GNUNET_ntohll (fcls->msg[1]->fragment_offset),
                                              &fragment_result,
                                              &message_get_fragment_result, fcls);
}


static void
fragment_get_result (void *cls, int64_t result,
                     const char *err_msg, uint16_t err_msg_size)
{
  struct FragmentClosure *fcls = cls;
  op = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
              "fragment_get:\t%d\n",
              (int) result);
  GNUNET_assert (0 < result && fcls->n == fcls->n_expected);

  fcls->n = 0;
  fcls->n_expected = 3;
  op = GNUNET_PSYCSTORE_fragment_get_latest (h, &channel_pub_key,
                                             &slave_pub_key, fcls->n_expected,
                                             &fragment_result,
                                             &fragment_get_latest_result, fcls);
}


static void
fragment_store_result (void *cls, int64_t result,
                       const char *err_msg, uint16_t err_msg_size)
{
  op = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_store:\t%ld\n", (long int) result);
  GNUNET_assert (GNUNET_OK == result);

  if ((intptr_t) cls == GNUNET_YES)
  { /* last fragment */
    fcls.n = 0;
    fcls.n_expected = 1;
    uint64_t fragment_id = GNUNET_ntohll (fcls.msg[0]->fragment_id);
    op = GNUNET_PSYCSTORE_fragment_get (h, &channel_pub_key, &slave_pub_key,
                                        fragment_id, fragment_id,
                                        &fragment_result,
                                        &fragment_get_result, &fcls);
  }
}


static void
fragment_store ()
{
  struct GNUNET_MULTICAST_MessageHeader *msg;
  fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
  fcls.msg[0] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
  GNUNET_assert (msg != NULL);

  msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
  msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));

  msg->hop_counter = htonl (9);
  msg->fragment_id = GNUNET_htonll (INT64_MAX - 8);
  msg->fragment_offset = GNUNET_htonll (0);
  msg->message_id = GNUNET_htonll (INT64_MAX - 10);
  msg->group_generation = GNUNET_htonll (INT64_MAX - 3);
  msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);

  GNUNET_memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));

  msg->purpose.size = htonl (ntohs (msg->header.size)
                             - sizeof (msg->header)
                             - sizeof (msg->hop_counter)
                             - sizeof (msg->signature));
  msg->purpose.purpose = htonl (234);
  GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose,
                                                        &msg->signature));

  op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[0],
                                        &fragment_store_result, GNUNET_NO);

  fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
  fcls.msg[1] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
  GNUNET_memcpy (msg, fcls.msg[0], sizeof (*msg) + sizeof (channel_pub_key));
  msg->fragment_id = GNUNET_htonll (INT64_MAX - 4);
  msg->fragment_offset = GNUNET_htonll (1024);

  op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[1],
                                        &fragment_store_result, GNUNET_NO);

  fcls.flags[2] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
  fcls.msg[2] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
  GNUNET_memcpy (msg, fcls.msg[1], sizeof (*msg) + sizeof (channel_pub_key));
  msg->fragment_id = GNUNET_htonll (INT64_MAX);
  msg->fragment_offset = GNUNET_htonll (16384);

  op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[2],
                                        &fragment_store_result, (void *) GNUNET_YES);
}


static void
membership_test_result (void *cls, int64_t result,
                        const char *err_msg, uint16_t err_msg_size)
{
  op = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_test:\t%ld\n", (long int) result);
  GNUNET_assert (GNUNET_OK == result);

  fragment_store ();
}


static void
membership_store_result (void *cls, int64_t result,
                         const char *err_msg, uint16_t err_msg_size)
{
  op = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_store:\t%ld\n", (long int) result);
  GNUNET_assert (GNUNET_OK == result);

  op = GNUNET_PSYCSTORE_membership_test (h, &channel_pub_key, &slave_pub_key,
                                         INT64_MAX - 10, 2,
                                         &membership_test_result, NULL);
}


/**
 * Main function of the test, run from scheduler.
 *
 * @param cls NULL
 * @param cfg configuration we use (also to connect to PSYCstore service)
 * @param peer handle to access more of the peer (not used)
 */
static void
#if DEBUG_TEST_PSYCSTORE
run (void *cls, char *const *args, const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
#else
run (void *cls,
     const struct GNUNET_CONFIGURATION_Handle *cfg,
     struct GNUNET_TESTING_Peer *peer)
#endif
{
  end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);

  h = GNUNET_PSYCSTORE_connect (cfg);
  GNUNET_assert (NULL != h);

  channel_key = GNUNET_CRYPTO_eddsa_key_create ();
  slave_key = GNUNET_CRYPTO_ecdsa_key_create ();

  GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
  GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);

  op = GNUNET_PSYCSTORE_membership_store (h, &channel_pub_key, &slave_pub_key,
                                          GNUNET_YES, INT64_MAX - 5,
                                          INT64_MAX - 10, 2,
                                          &membership_store_result, NULL);
}


int
main (int argc, char *argv[])
{
  res = 1;
#if DEBUG_TEST_PSYCSTORE
  const struct GNUNET_GETOPT_CommandLineOption opts[] = {
    GNUNET_GETOPT_OPTION_END
  };
  if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psycstore",
                                       "test-psycstore [options]",
                                       opts, &run, NULL))
    return 1;
#else
  if (0 != GNUNET_TESTING_service_run ("test-psycstore", "psycstore",
                                       "test_psycstore.conf", &run, NULL))
    return 1;
#endif
  return res;
}

/* end of test_psycstore.c */