aboutsummaryrefslogtreecommitdiff
path: root/src/revocation/test_revocation.c
blob: e9be5df84cf28ffc3754296288182659d840e4d3 (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
/*
   This file is part of GNUnet.
   Copyright (C) 2009, 2013 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., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/
/**
 * @file revocation/test_revocation.c
 * @brief base testcase for revocation exchange
 * @author Matthias Wachs
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_core_service.h"
#include "gnunet_identity_service.h"
#include "gnunet_revocation_service.h"
#include "gnunet_testbed_service.h"

#define NUM_TEST_PEERS 2

struct TestPeer
{
  struct GNUNET_TESTBED_Peer *p;
  struct GNUNET_TESTBED_Operation *identity_op;
  struct GNUNET_TESTBED_Operation *core_op;
  struct GNUNET_IDENTITY_Handle *idh;
  const struct GNUNET_CONFIGURATION_Handle *cfg;
  const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
  struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
  struct GNUNET_CRYPTO_EcdsaSignature sig;
  struct GNUNET_IDENTITY_Operation *create_id_op;
  struct GNUNET_IDENTITY_EgoLookup *ego_lookup;
  struct GNUNET_REVOCATION_Handle *revok_handle;
  struct GNUNET_CORE_Handle *ch;
  uint64_t pow;
};

static struct TestPeer testpeers[2];

/**
 * Return value from main, set to 0 on success.
 */
static int ok;


static void
do_shutdown (void *cls,
             const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  unsigned int c;

  for (c = 0; c < NUM_TEST_PEERS; c++)
  {
    if (NULL != testpeers[c].create_id_op)
    {
      GNUNET_IDENTITY_cancel (testpeers[c].create_id_op);
      testpeers[c].create_id_op = NULL;
    }
    if (NULL != testpeers[c].ego_lookup)
    {
      GNUNET_IDENTITY_ego_lookup_cancel (testpeers[c].ego_lookup);
      testpeers[c].ego_lookup = NULL;
    }
    if (NULL != testpeers[c].revok_handle)
    {
      GNUNET_REVOCATION_revoke_cancel (testpeers[c].revok_handle);
      testpeers[c].revok_handle = NULL;
    }
    if (NULL != testpeers[c].identity_op)
    {
      GNUNET_TESTBED_operation_done (testpeers[c].identity_op);
      testpeers[c].identity_op = NULL;
    }
    if (NULL != testpeers[c].core_op)
    {
      GNUNET_TESTBED_operation_done (testpeers[c].core_op);
      testpeers[c].core_op = NULL;
    }
  }
}


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


static void
revocation_remote_cb (void *cls,
                      int is_valid)
{
  static int repeat = 0;

  if (GNUNET_NO == is_valid)
  {
    fprintf (stderr,
             "Local revocation successful\n");
    ok = 0;
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
  if (repeat < 10)
  {
    repeat++;
    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
                                  &check_revocation,
                                  NULL);
    return;
  }
  fprintf (stderr,
           "Flooding of revocation failed\n");
  ok = 2;
  GNUNET_SCHEDULER_shutdown ();
}


static void
check_revocation (void *cls,
                  const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  GNUNET_REVOCATION_query (testpeers[0].cfg,
                           &testpeers[1].pubkey,
                           &revocation_remote_cb, NULL);
}


static void
revocation_cb (void *cls,
               int is_valid)
{
  testpeers[1].revok_handle = NULL;
  if (GNUNET_NO == is_valid)
  {
    fprintf (stderr,
             "Revocation successful\n");
    check_revocation (NULL, NULL);
  }
}


static void
ego_cb (void *cls,
        const struct GNUNET_IDENTITY_Ego *ego)
{
  static int completed = 0;

  if ((NULL != ego) && (cls == &testpeers[0]))
  {
    testpeers[0].ego_lookup = NULL;
    testpeers[0].privkey = GNUNET_IDENTITY_ego_get_private_key (ego);
    GNUNET_IDENTITY_ego_get_public_key (ego, &testpeers[0].pubkey);
    completed++;
  }
  if ((NULL != ego) && (cls == &testpeers[1]))
  {
    testpeers[1].ego_lookup = NULL;
    testpeers[1].privkey = GNUNET_IDENTITY_ego_get_private_key (ego);
    GNUNET_IDENTITY_ego_get_public_key (ego, &testpeers[1].pubkey);
    GNUNET_REVOCATION_sign_revocation (testpeers[1].privkey, &testpeers[1].sig);

    GNUNET_log(GNUNET_ERROR_TYPE_INFO,
               "Calculating proof of work...\n");
    testpeers[1].pow = 0;
    int res = GNUNET_REVOCATION_check_pow (&testpeers[1].pubkey,
        testpeers[1].pow, 5);
    while (GNUNET_OK != res)
    {
      testpeers[1].pow++;
      res = GNUNET_REVOCATION_check_pow (&testpeers[1].pubkey,
                                         testpeers[1].pow,
                                         5);
    }
    fprintf (stderr,
             "Done calculating proof of work\n");
    completed++;
  }
  if (2 == completed)
  {
    GNUNET_log(GNUNET_ERROR_TYPE_INFO,
               "Egos retrieved\n");
    testpeers[1].revok_handle
      = GNUNET_REVOCATION_revoke (testpeers[1].cfg,
                                  &testpeers[1].pubkey,
                                  &testpeers[1].sig,
                                  testpeers[1].pow,
                                  &revocation_cb, NULL);
  }
}


static void
identity_create_cb (void *cls,
                    const char *emsg)
{
  static int completed = 0;

  if ((NULL == emsg) && (cls == &testpeers[0]))
  {
    testpeers[0].create_id_op = NULL;
    completed++;
  }
  if ((NULL == emsg) && (cls == &testpeers[1]))
  {
    testpeers[1].create_id_op = NULL;
    completed++;
  }
  if (2 != completed)
    return;
  fprintf (stderr,
           "Identities created\n");
  testpeers[0].ego_lookup = GNUNET_IDENTITY_ego_lookup (testpeers[0].cfg,
                                                        "client",
                                                        &ego_cb,
                                                        &testpeers[0]);
  testpeers[1].ego_lookup = GNUNET_IDENTITY_ego_lookup (testpeers[1].cfg,
                                                        "toberevoked",
                                                        &ego_cb,
                                                        &testpeers[1]);
}


static void
identity_completion_cb (void *cls,
                        struct GNUNET_TESTBED_Operation *op,
                        void *ca_result,
                        const char *emsg)
{
  static int completed = 0;

  completed++;
  if (NUM_TEST_PEERS != completed)
    return;
  fprintf (stderr,
           "All peers connected @ IDENTITY ...\n");
  testpeers[0].create_id_op
    = GNUNET_IDENTITY_create (testpeers[0].idh,
                              "client",
                              &identity_create_cb,
                              &testpeers[0]);
  testpeers[1].create_id_op
    = GNUNET_IDENTITY_create (testpeers[1].idh,
                              "toberevoked",
                              &identity_create_cb,
                              &testpeers[1]);
}


static void *
identity_connect_adapter (void *cls,
                          const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  struct TestPeer *me = cls;
  me->cfg = cfg;
  me->idh = GNUNET_IDENTITY_connect (cfg, NULL, NULL );
  if (NULL == me->idh)
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
               "Failed to create IDENTITY handle \n");
  return me->idh;
}


static void
identity_disconnect_adapter (void *cls,
                             void *op_result)
{
  struct TestPeer *me = cls;
  GNUNET_IDENTITY_disconnect (me->idh);
  me->idh = NULL;
}


static void
connect_cb (void *cls,
            const struct GNUNET_PeerIdentity *peer)
{
  static int connects = 0;

  connects++;
  if (NUM_TEST_PEERS * NUM_TEST_PEERS == connects)
  {
    fprintf (stderr,
             "All peers connected @ CORE ...\n");

    /* Connect to identity service */
    testpeers[0].identity_op
      = GNUNET_TESTBED_service_connect (NULL,
                                        testpeers[0].p, "identity",
                                        &identity_completion_cb, NULL,
                                        &identity_connect_adapter,
                                        &identity_disconnect_adapter,
                                        &testpeers[0]);
    testpeers[1].identity_op
      = GNUNET_TESTBED_service_connect (NULL,
                                        testpeers[1].p, "identity",
                                        *identity_completion_cb, NULL,
                                        &identity_connect_adapter,
                                        &identity_disconnect_adapter,
                                        &testpeers[1]);
  }
}


static void
core_completion_cb (void *cls,
                    struct GNUNET_TESTBED_Operation *op,
                    void *ca_result,
                    const char *emsg)
{
  static int completed = 0;

  completed++;
  if (NUM_TEST_PEERS == completed)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "Connected to CORE\n");
  }
}


static void *
core_connect_adapter (void *cls,
                      const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  struct TestPeer *me = cls;

  me->cfg = cfg;
  me->ch = GNUNET_CORE_connect (cfg, me, NULL,
                                &connect_cb, NULL,
                                NULL, GNUNET_NO,
                                NULL, GNUNET_NO,
                                NULL);
  if (NULL == me->ch)
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
               "Failed to create CORE handle \n");
  return me->ch;
}


static void
core_disconnect_adapter (void *cls,
                         void *op_result)
{
  struct TestPeer *me = cls;

  GNUNET_CORE_disconnect (me->ch);
  me->ch = NULL;
}


static void
test_connection (void *cls,
                 struct GNUNET_TESTBED_RunHandle *h,
                 unsigned int num_peers,
                 struct GNUNET_TESTBED_Peer **peers,
                 unsigned int links_succeeded,
                 unsigned int links_failed)
{
  unsigned int c;

  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
                                &do_shutdown, NULL);

  if (NUM_TEST_PEERS != num_peers)
  {
    ok = 4;
    fprintf (stderr,
             "Only %u out of %u peers were started ...\n",
             num_peers,
             NUM_TEST_PEERS);
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
   /* We are generating a CLIQUE */
  if (NUM_TEST_PEERS * (NUM_TEST_PEERS -1) == links_succeeded)
  {
    fprintf (stderr,
             "Testbed connected peers, initializing test\n");
    for (c = 0; c < num_peers; c++)
    {
      testpeers[c].p = peers[c];
      testpeers[c].core_op
        = GNUNET_TESTBED_service_connect (NULL,
                                          testpeers[c].p,
                                          "core",
                                          &core_completion_cb, NULL,
                                          &core_connect_adapter,
                                          &core_disconnect_adapter,
                                          &testpeers[c]);
    }
  }
  else
  {
    fprintf (stderr,
             "Testbed failed to connect peers\n");
    ok = 5;
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
}


int
main (int argc,
      char *argv[])
{
  ok = 1;
  /* Connecting initial topology */
  (void) GNUNET_TESTBED_test_run ("test-revocation",
                                  "test_revocation.conf",
                                  NUM_TEST_PEERS, 0,
                                  NULL, NULL,
                                  &test_connection, NULL);
  return ok;
}

/* end of test_revocation.c */