- FIX: GNUNET_SET_STATUS_HALF_DONE is never called only GNUNET_SET_STATUS_DONE
[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 dv/test_transport_dv.c
22  * @brief base testcase for testing distance vector transport
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_IDENTITY_Handle *idh;
37   const struct GNUNET_CONFIGURATION_Handle *cfg;
38   const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
39   struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
40   struct GNUNET_CRYPTO_EcdsaSignature sig;
41   uint64_t pow;
42 };
43
44 struct TestPeer testpeers[2];
45
46
47
48 /**
49  * Return value from main, set to 0 on success.
50  */
51 static int ok;
52
53 static void
54 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
55 {
56   int c;
57   for (c = 0; c < NUM_TEST_PEERS; c++)
58   {
59     GNUNET_TESTBED_operation_done (testpeers[c].identity_op);
60     testpeers[c].identity_op = NULL;
61   }
62   GNUNET_SCHEDULER_shutdown ();
63   ok = 0;
64 }
65
66 static void
67 do_shutdown_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
68 {
69   do_shutdown (NULL, NULL);
70   ok = 1;
71 }
72
73 static void *
74 identity_connect_adapter (void *cls,
75     const struct GNUNET_CONFIGURATION_Handle *cfg)
76 {
77   struct TestPeer *me = cls;
78   me->cfg = cfg;
79   me->idh = GNUNET_IDENTITY_connect(cfg, NULL, NULL);
80   if (NULL == me->idh)
81     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
82         "Failed to create IDENTITY handle \n");
83   return me->idh;
84 }
85
86 static void
87 identity_disconnect_adapter (void *cls, void *op_result)
88 {
89   struct TestPeer *me = cls;
90   GNUNET_IDENTITY_disconnect(me->idh);
91   me->idh = NULL;
92 }
93
94 static void check_revocation ();
95
96 static void revocation_remote_cb (void *cls,
97                            int is_valid)
98 {
99   static int repeat = 0;
100   if  (GNUNET_NO == is_valid)
101   {
102     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Local revocation successful\n");
103     GNUNET_SCHEDULER_add_now(&do_shutdown, NULL);
104   }
105   else if (repeat < 10)
106   {
107     GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, &check_revocation, NULL);
108   }
109   else
110   {
111     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Flooding of revocation failed\n");
112     GNUNET_SCHEDULER_add_now(&do_shutdown_badly, NULL);
113   }
114   repeat++;
115 }
116
117 static void check_revocation ()
118 {
119   GNUNET_REVOCATION_query (testpeers[0].cfg, &testpeers[1].pubkey, &revocation_remote_cb, NULL);
120 }
121
122 static void revocation_cb (void *cls,
123                            int is_valid)
124 {
125   if  (GNUNET_NO == is_valid)
126   {
127     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Revocation successful\n");
128     check_revocation();
129   }
130 }
131
132 static void ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
133 {
134   static int completed = 0;
135   if ((NULL != ego) && (cls == &testpeers[0]))
136   {
137     testpeers[0].privkey = GNUNET_IDENTITY_ego_get_private_key(ego);
138     GNUNET_IDENTITY_ego_get_public_key(ego, &testpeers[0].pubkey);
139     completed ++;
140   }
141   if ((NULL != ego) && (cls == &testpeers[1]))
142   {
143     testpeers[1].privkey = GNUNET_IDENTITY_ego_get_private_key(ego);
144     GNUNET_IDENTITY_ego_get_public_key(ego, &testpeers[1].pubkey);
145     GNUNET_REVOCATION_sign_revocation(testpeers[1].privkey, &testpeers[1].sig);
146
147     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Calculating proof of work...\n");
148     testpeers[1].pow = 0;
149     int res = GNUNET_REVOCATION_check_pow (&testpeers[1].pubkey, testpeers[1].pow, 5);
150     while (GNUNET_OK != res)
151     {
152       testpeers[1].pow++;
153       res = GNUNET_REVOCATION_check_pow (&testpeers[1].pubkey, testpeers[1].pow, 5);
154     }
155     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done calculating proof of work\n");
156     completed ++;
157   }
158   if (2 == completed)
159   {
160     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Egos retrieved\n");
161     GNUNET_REVOCATION_revoke (testpeers[1].cfg, &testpeers[1].pubkey, &testpeers[1].sig, testpeers[1].pow, revocation_cb, NULL);
162   }
163 }
164
165 void identity_create_cb (void *cls, const char *emsg)
166 {
167   static int completed = 0;
168   if ((NULL == emsg) && (cls == &testpeers[0]))
169   {
170     completed ++;
171   }
172   if ((NULL == emsg) && (cls == &testpeers[1]))
173   {
174     completed ++;
175   }
176   if (2 == completed)
177   {
178     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Identities created\n");
179     GNUNET_IDENTITY_ego_lookup (testpeers[0].cfg, "client", ego_cb, &testpeers[0]);
180     GNUNET_IDENTITY_ego_lookup (testpeers[1].cfg, "toberevoked", ego_cb, &testpeers[1]);
181   }
182 }
183
184
185 static void
186 identity_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
187     void *ca_result, const char *emsg)
188 {
189   static int completed = 0;
190   completed ++;
191   if (NUM_TEST_PEERS == completed)
192   {
193     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to identity\n");
194    GNUNET_IDENTITY_create (testpeers[0].idh, "client", identity_create_cb, &testpeers[0]);
195    GNUNET_IDENTITY_create (testpeers[1].idh, "toberevoked", identity_create_cb, &testpeers[1]);
196   }
197 }
198
199 static void
200 test_connection (void *cls,
201                  struct GNUNET_TESTBED_RunHandle *h,
202                  unsigned int num_peers,
203                  struct GNUNET_TESTBED_Peer **peers,
204                  unsigned int links_succeeded,
205                  unsigned int links_failed)
206 {
207   int c;
208   if (NUM_TEST_PEERS != num_peers)
209   {
210     ok = 1;
211     fprintf (stderr, "Only %u out of 2 peers were started ...\n",
212         num_peers);
213   }
214
215   if (0 == links_failed)
216   {
217     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Testbed connected peers\n");
218     for (c = 0; c< num_peers; c++)
219     {
220       testpeers[c].p = peers[c];
221
222       /* Connect to identity service */
223       testpeers[c].identity_op = GNUNET_TESTBED_service_connect (NULL, testpeers[c].p,
224           "identity", identity_completion_cb, NULL, &identity_connect_adapter,
225           &identity_disconnect_adapter, &testpeers[c]);
226     }
227   }
228 }
229
230
231 int
232 main (int argc, char *argv[])
233 {
234   ok = 1;
235   /* Connecting initial topology */
236   (void) GNUNET_TESTBED_test_run ("test-revocation",
237                                   "test_revocation.conf",
238                                   NUM_TEST_PEERS,
239                                   0, NULL, NULL,
240                                   &test_connection, NULL);
241   return ok;
242 }
243
244 /* end of test_transport_dv.c */