2 This file is part of GNUnet.
3 (C) 2009, 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file revocation/test_revocation.c
22 * @brief base testcase for revocation exchange
25 #include "gnunet_core_service.h"
26 #include "gnunet_identity_service.h"
27 #include "gnunet_revocation_service.h"
28 #include "gnunet_testbed_service.h"
30 #define NUM_TEST_PEERS 2
34 struct GNUNET_TESTBED_Peer *p;
35 struct GNUNET_TESTBED_Operation *identity_op;
36 struct GNUNET_TESTBED_Operation *core_op;
37 struct GNUNET_IDENTITY_Handle *idh;
38 const struct GNUNET_CONFIGURATION_Handle *cfg;
39 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
40 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
41 struct GNUNET_CRYPTO_EcdsaSignature sig;
42 struct GNUNET_IDENTITY_Operation *create_id_op;
43 struct GNUNET_IDENTITY_EgoLookup *ego_lookup;
44 struct GNUNET_REVOCATION_Handle *revok_handle;
45 struct GNUNET_CORE_Handle *ch;
49 static struct TestPeer testpeers[2];
51 static GNUNET_SCHEDULER_TaskIdentifier die_task;
54 * Return value from main, set to 0 on success.
59 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
63 if (GNUNET_SCHEDULER_NO_TASK != die_task)
65 GNUNET_SCHEDULER_cancel (die_task);
66 die_task = GNUNET_SCHEDULER_NO_TASK;
69 for (c = 0; c < NUM_TEST_PEERS; c++)
71 if (NULL != testpeers[c].create_id_op)
73 GNUNET_IDENTITY_cancel (testpeers[c].create_id_op);
74 testpeers[c].create_id_op = NULL;
76 if (NULL != testpeers[c].ego_lookup)
78 GNUNET_IDENTITY_ego_lookup_cancel (testpeers[c].ego_lookup);
79 testpeers[c].ego_lookup = NULL;
82 if (NULL != testpeers[c].revok_handle)
84 GNUNET_REVOCATION_revoke_cancel (testpeers[c].revok_handle);
85 testpeers[c].revok_handle = NULL;
87 if (NULL != testpeers[c].identity_op)
89 GNUNET_TESTBED_operation_done (testpeers[c].identity_op);
90 testpeers[c].identity_op = NULL;
92 if (NULL != testpeers[c].core_op)
94 GNUNET_TESTBED_operation_done (testpeers[c].core_op);
95 testpeers[c].core_op = NULL;
98 GNUNET_SCHEDULER_shutdown ();
103 do_shutdown_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
105 if (GNUNET_SCHEDULER_NO_TASK != die_task)
106 die_task = GNUNET_SCHEDULER_NO_TASK;
107 do_shutdown (NULL, NULL );
113 identity_connect_adapter (void *cls,
114 const struct GNUNET_CONFIGURATION_Handle *cfg)
116 struct TestPeer *me = cls;
118 me->idh = GNUNET_IDENTITY_connect (cfg, NULL, NULL );
120 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create IDENTITY handle \n");
125 identity_disconnect_adapter (void *cls, void *op_result)
127 struct TestPeer *me = cls;
128 GNUNET_IDENTITY_disconnect (me->idh);
136 revocation_remote_cb (void *cls, int is_valid)
138 static int repeat = 0;
139 if (GNUNET_NO == is_valid)
141 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Local revocation successful\n");
142 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL );
144 else if (repeat < 10)
146 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &check_revocation,
151 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Flooding of revocation failed\n");
152 if (GNUNET_SCHEDULER_NO_TASK != die_task)
154 GNUNET_SCHEDULER_cancel (die_task);
155 die_task = GNUNET_SCHEDULER_NO_TASK;
157 GNUNET_SCHEDULER_add_now (&do_shutdown_badly, NULL );
165 GNUNET_REVOCATION_query (testpeers[0].cfg, &testpeers[1].pubkey,
166 &revocation_remote_cb, NULL );
170 revocation_cb (void *cls, int is_valid)
172 testpeers[1].revok_handle = NULL;
173 if (GNUNET_NO == is_valid)
175 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Revocation successful\n");
181 ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
183 static int completed = 0;
184 if ((NULL != ego) && (cls == &testpeers[0]))
186 testpeers[0].ego_lookup = NULL;
187 testpeers[0].privkey = GNUNET_IDENTITY_ego_get_private_key (ego);
188 GNUNET_IDENTITY_ego_get_public_key (ego, &testpeers[0].pubkey);
191 if ((NULL != ego) && (cls == &testpeers[1]))
193 testpeers[1].ego_lookup = NULL;
194 testpeers[1].privkey = GNUNET_IDENTITY_ego_get_private_key (ego);
195 GNUNET_IDENTITY_ego_get_public_key (ego, &testpeers[1].pubkey);
196 GNUNET_REVOCATION_sign_revocation (testpeers[1].privkey, &testpeers[1].sig);
198 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Calculating proof of work...\n");
199 testpeers[1].pow = 0;
200 int res = GNUNET_REVOCATION_check_pow (&testpeers[1].pubkey,
201 testpeers[1].pow, 5);
202 while (GNUNET_OK != res)
205 res = GNUNET_REVOCATION_check_pow (&testpeers[1].pubkey, testpeers[1].pow,
208 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Done calculating proof of work\n");
213 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Egos retrieved\n");
214 testpeers[1].revok_handle = GNUNET_REVOCATION_revoke (testpeers[1].cfg,
215 &testpeers[1].pubkey, &testpeers[1].sig, testpeers[1].pow,
216 revocation_cb, NULL );
221 identity_create_cb (void *cls, const char *emsg)
223 static int completed = 0;
224 if ((NULL == emsg) && (cls == &testpeers[0]))
226 testpeers[0].create_id_op = NULL;
229 if ((NULL == emsg) && (cls == &testpeers[1]))
231 testpeers[1].create_id_op = NULL;
236 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Identities created\n");
237 testpeers[0].ego_lookup = GNUNET_IDENTITY_ego_lookup (testpeers[0].cfg,
238 "client", ego_cb, &testpeers[0]);
239 testpeers[1].ego_lookup = GNUNET_IDENTITY_ego_lookup (testpeers[1].cfg,
240 "toberevoked", ego_cb, &testpeers[1]);
245 identity_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
246 void *ca_result, const char *emsg)
248 static int completed = 0;
250 if (NUM_TEST_PEERS == completed)
252 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to identity\n");
253 testpeers[0].create_id_op = GNUNET_IDENTITY_create (testpeers[0].idh,
254 "client", identity_create_cb, &testpeers[0]);
255 testpeers[1].create_id_op = GNUNET_IDENTITY_create (testpeers[1].idh,
256 "toberevoked", identity_create_cb, &testpeers[1]);
260 void static connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer)
262 static int connects = 0;
266 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "All peers connected ...\n");
268 /* Connect to identity service */
269 testpeers[0].identity_op = GNUNET_TESTBED_service_connect (NULL,
270 testpeers[0].p, "identity", identity_completion_cb, NULL,
271 &identity_connect_adapter, &identity_disconnect_adapter,
273 testpeers[1].identity_op = GNUNET_TESTBED_service_connect (NULL,
274 testpeers[1].p, "identity", identity_completion_cb, NULL,
275 &identity_connect_adapter, &identity_disconnect_adapter,
282 core_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
283 void *ca_result, const char *emsg)
285 static int completed = 0;
287 if (NUM_TEST_PEERS == completed)
289 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to CORE\n");
295 core_connect_adapter (void *cls,
296 const struct GNUNET_CONFIGURATION_Handle *cfg)
298 struct TestPeer *me = cls;
300 me->ch = GNUNET_CORE_connect (cfg, me, NULL, &connect_cb, NULL, NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
302 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create CORE handle \n");
308 core_disconnect_adapter (void *cls, void *op_result)
310 struct TestPeer *me = cls;
311 GNUNET_CORE_disconnect (me->ch);
316 test_connection (void *cls, struct GNUNET_TESTBED_RunHandle *h,
317 unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers,
318 unsigned int links_succeeded, unsigned int links_failed)
322 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
323 &do_shutdown_badly, NULL );
324 if (NUM_TEST_PEERS != num_peers)
327 fprintf (stderr, "Only %u out of 2 peers were started ...\n", num_peers);
330 if (0 == links_failed)
332 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Testbed connected peers\n");
333 for (c = 0; c < num_peers; c++)
335 testpeers[c].p = peers[c];
337 testpeers[c].core_op = GNUNET_TESTBED_service_connect (NULL,
338 testpeers[c].p, "core", &core_completion_cb, NULL,
339 &core_connect_adapter, &core_disconnect_adapter,
346 main (int argc, char *argv[])
349 /* Connecting initial topology */
350 (void) GNUNET_TESTBED_test_run ("test-revocation", "test_revocation.conf",
351 NUM_TEST_PEERS, 0, NULL, NULL, &test_connection, NULL );
355 /* end of test_revocation.c */