make generated ego key available in continuation when creating egos
[oweals/gnunet.git] / src / identity / test_identity_defaults.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 /**
22  * @file identity/test_identity.c
23  * @brief testcase for identity service
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_identity_service.h"
29 #include "gnunet_testing_lib.h"
30
31
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
33
34
35 /**
36  * Return value from 'main'.
37  */
38 static int res;
39
40 /**
41  * Handle to identity service.
42  */
43 static struct GNUNET_IDENTITY_Handle *h;
44
45 /**
46  * Handle to identity operation.
47  */
48 static struct GNUNET_IDENTITY_Operation *op;
49
50 /**
51  * Handle for task for timeout termination.
52  */
53 static struct GNUNET_SCHEDULER_Task * endbadly_task;
54
55
56 /**
57  * Clean up all resources used.
58  */
59 static void
60 cleanup ()
61 {
62   if (NULL != op)
63   {
64     GNUNET_IDENTITY_cancel (op);
65     op = NULL;
66   }
67   if (NULL != h)
68   {
69     GNUNET_IDENTITY_disconnect (h);
70     h = NULL;
71   }
72   GNUNET_SCHEDULER_shutdown ();
73 }
74
75
76 /**
77  * Termiante the testcase (failure).
78  *
79  * @param cls NULL
80  */
81 static void
82 endbadly (void *cls)
83 {
84   cleanup ();
85   res = 1;
86 }
87
88
89 /**
90  * Termiante the testcase (success).
91  *
92  * @param cls NULL
93  */
94 static void
95 end_normally (void *cls)
96 {
97   cleanup ();
98   res = 0;
99 }
100
101
102 /**
103  * Finish the testcase (successfully).
104  */
105 static void
106 end ()
107 {
108   if (endbadly_task != NULL)
109   {
110     GNUNET_SCHEDULER_cancel (endbadly_task);
111     endbadly_task = NULL;
112   }
113   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
114                                 &end_normally, NULL);
115 }
116
117
118 /**
119  * Continuation called from successful delete operation.
120  *
121  * @param cls NULL
122  * @param emsg (should also be NULL)
123  */
124 static void
125 delete_cont (void *cls,
126              const char *emsg)
127 {
128   op = NULL;
129   GNUNET_assert (NULL == emsg);
130   end ();
131 }
132
133
134 /**
135  * Continuation called from expected-to-fail rename operation.
136  *
137  * @param cls NULL
138  * @param emsg (should also be NULL)
139  */
140 static void
141 get_cb (void *cls,
142         struct GNUNET_IDENTITY_Ego *ego,
143         void **ctx,
144         const char *identifier)
145 {
146   GNUNET_assert (NULL != ego);
147   GNUNET_assert (NULL != identifier);
148   GNUNET_assert (0 == strcmp (identifier, "test-id"));
149   op = GNUNET_IDENTITY_delete (h,
150                                "test-id",
151                                &delete_cont,
152                                NULL);
153 }
154
155
156 /**
157  * Main function of the test, run from scheduler.
158  *
159  * @param cls NULL
160  * @param cfg configuration we use (also to connect to identity service)
161  * @param peer handle to access more of the peer (not used)
162  */
163 static void
164 run_get (void *cls,
165          const struct GNUNET_CONFIGURATION_Handle *cfg,
166          struct GNUNET_TESTING_Peer *peer)
167 {
168   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
169                                                 &endbadly, NULL);
170   h = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
171   GNUNET_assert (NULL != h);
172   op = GNUNET_IDENTITY_get (h,
173                             "test-service",
174                             &get_cb,
175                             NULL);
176 }
177
178
179 /**
180  * Continuation called from successful rename operation.
181  *
182  * @param cls NULL
183  * @param emsg (should also be NULL)
184  */
185 static void
186 success_set_cont (void *cls,
187                   const char *emsg)
188 {
189   op = NULL;
190   GNUNET_assert (NULL == emsg);
191   end ();
192 }
193
194
195 /**
196  * Called with events about egos.
197  *
198  * @param cls NULL
199  * @param ego ego handle
200  * @param ego_ctx context for application to store data for this ego
201  *                 (during the lifetime of this process, initially NULL)
202  * @param identifier identifier assigned by the user for this ego,
203  *                   NULL if the user just deleted the ego and it
204  *                   must thus no longer be used
205  */
206 static void
207 notification_cb (void *cls,
208                  struct GNUNET_IDENTITY_Ego *ego,
209                  void **ctx,
210                  const char *identifier)
211 {
212   if (NULL == ego)
213     return; /* skip first call */
214   if (NULL == identifier)
215     return; /* deletion / shutdown */
216   op = GNUNET_IDENTITY_set (h,
217                             "test-service",
218                             ego,
219                             &success_set_cont,
220                             NULL);
221 }
222
223
224 /**
225  * Called with events about created ego.
226  *
227  * @param cls NULL
228  * @param pk private key of the ego, or NULL on error
229  * @param emsg error message
230  */
231 static void
232 create_cb (void *cls,
233            const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk,
234            const char *emsg)
235 {
236   GNUNET_assert (NULL == emsg);
237   GNUNET_assert (NULL != pk);
238   op = NULL;
239 }
240
241
242 /**
243  * Main function of the test, run from scheduler.
244  *
245  * @param cls NULL
246  * @param cfg configuration we use (also to connect to identity service)
247  * @param peer handle to access more of the peer (not used)
248  */
249 static void
250 run_set (void *cls,
251          const struct GNUNET_CONFIGURATION_Handle *cfg,
252          struct GNUNET_TESTING_Peer *peer)
253 {
254   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
255                                                 &endbadly, NULL);
256   h = GNUNET_IDENTITY_connect (cfg, &notification_cb, NULL);
257   GNUNET_assert (NULL != h);
258   op = GNUNET_IDENTITY_create (h,
259                                "test-id",
260                                &create_cb,
261                                NULL);
262
263 }
264
265
266 int
267 main (int argc, char *argv[])
268 {
269   GNUNET_DISK_directory_remove ("/tmp/test-identity-service");
270   res = 1;
271   if (0 !=
272       GNUNET_TESTING_service_run ("test-identity-defaults",
273                                   "identity",
274                                   "test_identity.conf",
275                                   &run_set,
276                                   NULL))
277     return 1;
278   if (0 !=
279       GNUNET_TESTING_service_run ("test-identity-defaults",
280                                   "identity",
281                                   "test_identity.conf",
282                                   &run_get,
283                                   NULL))
284     return 1;
285   GNUNET_DISK_directory_remove ("/tmp/test-identity-service");
286   return res;
287 }
288
289
290 /* end of test_identity.c */