-doxygen, indentation
[oweals/gnunet.git] / src / revocation / test_revocation.c
1 /*
2  This file is part of GNUnet.
3  (C) 2009, 2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20 /**
21  * @file revocation/test_revocation.c
22  * @brief base testcase for revocation exchange
23  */
24 #include "platform.h"
25 #include "gnunet_core_service.h"
26 #include "gnunet_identity_service.h"
27 #include "gnunet_revocation_service.h"
28 #include "gnunet_testbed_service.h"
29
30 #define NUM_TEST_PEERS 2
31
32 struct TestPeer
33 {
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;
46   uint64_t pow;
47 };
48
49 static struct TestPeer testpeers[2];
50
51 static GNUNET_SCHEDULER_TaskIdentifier die_task;
52
53 /**
54  * Return value from main, set to 0 on success.
55  */
56 static int ok;
57
58 static void
59 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
60 {
61   int c;
62
63   if (GNUNET_SCHEDULER_NO_TASK != die_task)
64   {
65     GNUNET_SCHEDULER_cancel (die_task);
66     die_task = GNUNET_SCHEDULER_NO_TASK;
67   }
68
69   for (c = 0; c < NUM_TEST_PEERS; c++)
70   {
71     if (NULL != testpeers[c].create_id_op)
72     {
73       GNUNET_IDENTITY_cancel (testpeers[c].create_id_op);
74       testpeers[c].create_id_op = NULL;
75     }
76     if (NULL != testpeers[c].ego_lookup)
77     {
78       GNUNET_IDENTITY_ego_lookup_cancel (testpeers[c].ego_lookup);
79       testpeers[c].ego_lookup = NULL;
80     }
81
82     if (NULL != testpeers[c].revok_handle)
83     {
84       GNUNET_REVOCATION_revoke_cancel (testpeers[c].revok_handle);
85       testpeers[c].revok_handle = NULL;
86     }
87     if (NULL != testpeers[c].identity_op)
88     {
89       GNUNET_TESTBED_operation_done (testpeers[c].identity_op);
90       testpeers[c].identity_op = NULL;
91     }
92     if (NULL != testpeers[c].core_op)
93     {
94       GNUNET_TESTBED_operation_done (testpeers[c].core_op);
95       testpeers[c].core_op = NULL;
96     }
97   }
98   GNUNET_SCHEDULER_shutdown ();
99   ok = 0;
100 }
101
102 static void
103 do_shutdown_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 {
105   if (GNUNET_SCHEDULER_NO_TASK != die_task)
106     die_task = GNUNET_SCHEDULER_NO_TASK;
107   do_shutdown (NULL, NULL );
108   ok = 1;
109 }
110
111
112 static void *
113 identity_connect_adapter (void *cls,
114     const struct GNUNET_CONFIGURATION_Handle *cfg)
115 {
116   struct TestPeer *me = cls;
117   me->cfg = cfg;
118   me->idh = GNUNET_IDENTITY_connect (cfg, NULL, NULL );
119   if (NULL == me->idh)
120     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create IDENTITY handle \n");
121   return me->idh;
122 }
123
124 static void
125 identity_disconnect_adapter (void *cls, void *op_result)
126 {
127   struct TestPeer *me = cls;
128   GNUNET_IDENTITY_disconnect (me->idh);
129   me->idh = NULL;
130 }
131
132 static void
133 check_revocation ();
134
135 static void
136 revocation_remote_cb (void *cls, int is_valid)
137 {
138   static int repeat = 0;
139   if (GNUNET_NO == is_valid)
140   {
141     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Local revocation successful\n");
142     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL );
143   }
144   else if (repeat < 10)
145   {
146     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &check_revocation,
147         NULL );
148   }
149   else
150   {
151     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Flooding of revocation failed\n");
152     if (GNUNET_SCHEDULER_NO_TASK != die_task)
153     {
154       GNUNET_SCHEDULER_cancel (die_task);
155       die_task = GNUNET_SCHEDULER_NO_TASK;
156     }
157     GNUNET_SCHEDULER_add_now (&do_shutdown_badly, NULL );
158   }
159   repeat++;
160 }
161
162 static void
163 check_revocation ()
164 {
165   GNUNET_REVOCATION_query (testpeers[0].cfg, &testpeers[1].pubkey,
166       &revocation_remote_cb, NULL );
167 }
168
169 static void
170 revocation_cb (void *cls, int is_valid)
171 {
172   testpeers[1].revok_handle = NULL;
173   if (GNUNET_NO == is_valid)
174   {
175     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Revocation successful\n");
176     check_revocation ();
177   }
178 }
179
180 static void
181 ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
182 {
183   static int completed = 0;
184   if ((NULL != ego) && (cls == &testpeers[0]))
185   {
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);
189     completed++;
190   }
191   if ((NULL != ego) && (cls == &testpeers[1]))
192   {
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);
197
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)
203     {
204       testpeers[1].pow++;
205       res = GNUNET_REVOCATION_check_pow (&testpeers[1].pubkey, testpeers[1].pow,
206           5);
207     }
208     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Done calculating proof of work\n");
209     completed++;
210   }
211   if (2 == completed)
212   {
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 );
217   }
218 }
219
220 void
221 identity_create_cb (void *cls, const char *emsg)
222 {
223   static int completed = 0;
224   if ((NULL == emsg) && (cls == &testpeers[0]))
225   {
226     testpeers[0].create_id_op = NULL;
227     completed++;
228   }
229   if ((NULL == emsg) && (cls == &testpeers[1]))
230   {
231     testpeers[1].create_id_op = NULL;
232     completed++;
233   }
234   if (2 == completed)
235   {
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]);
241   }
242 }
243
244 static void
245 identity_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
246     void *ca_result, const char *emsg)
247 {
248   static int completed = 0;
249   completed++;
250   if (NUM_TEST_PEERS == completed)
251   {
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]);
257   }
258 }
259
260 void static connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer)
261 {
262   static int connects = 0;
263   connects++;
264   if (4 == connects)
265   {
266     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "All peers connected ...\n");
267
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,
272         &testpeers[0]);
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,
276         &testpeers[1]);
277   }
278 }
279
280
281 static void
282 core_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
283     void *ca_result, const char *emsg)
284 {
285   static int completed = 0;
286   completed++;
287   if (NUM_TEST_PEERS == completed)
288   {
289     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to CORE\n");
290   }
291 }
292
293
294 static void *
295 core_connect_adapter (void *cls,
296     const struct GNUNET_CONFIGURATION_Handle *cfg)
297 {
298   struct TestPeer *me = cls;
299   me->cfg = cfg;
300   me->ch = GNUNET_CORE_connect (cfg, me, NULL, &connect_cb, NULL, NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
301   if (NULL == me->ch)
302     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create CORE handle \n");
303   return me->ch;
304 }
305
306
307 static void
308 core_disconnect_adapter (void *cls, void *op_result)
309 {
310   struct TestPeer *me = cls;
311   GNUNET_CORE_disconnect (me->ch);
312   me->ch = NULL;
313 }
314
315 static void
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)
319 {
320   int c;
321
322   die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
323       &do_shutdown_badly, NULL );
324   if (NUM_TEST_PEERS != num_peers)
325   {
326     ok = 1;
327     fprintf (stderr, "Only %u out of 2 peers were started ...\n", num_peers);
328   }
329
330   if (0 == links_failed)
331   {
332     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Testbed connected peers\n");
333     for (c = 0; c < num_peers; c++)
334     {
335       testpeers[c].p = peers[c];
336
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,
340           &testpeers[c]);
341     }
342   }
343 }
344
345 int
346 main (int argc, char *argv[])
347 {
348   ok = 1;
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 );
352   return ok;
353 }
354
355 /* end of test_revocation.c */