-new service API
[oweals/gnunet.git] / src / identity / test_identity.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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 (NULL != endbadly_task)
109   {
110     GNUNET_SCHEDULER_cancel (endbadly_task);
111     endbadly_task = NULL;
112   }
113   GNUNET_SCHEDULER_shutdown ();
114 }
115
116
117 /**
118  * Called with events about egos.
119  *
120  * @param cls NULL
121  * @param ego ego handle
122  * @param ego_ctx context for application to store data for this ego
123  *                 (during the lifetime of this process, initially NULL)
124  * @param identifier identifier assigned by the user for this ego,
125  *                   NULL if the user just deleted the ego and it
126  *                   must thus no longer be used
127  */
128 static void
129 notification_cb (void *cls,
130                  struct GNUNET_IDENTITY_Ego *ego,
131                  void **ctx,
132                  const char *identifier)
133 {
134   static struct GNUNET_IDENTITY_Ego *my_ego;
135   static int round;
136
137   switch (round)
138   {
139   case 0: /* end of initial iteration */
140     GNUNET_assert (NULL == ego);
141     GNUNET_assert (NULL == identifier);
142     break;
143   case 1: /* create */
144     GNUNET_assert (NULL != ego);
145     GNUNET_assert (0 == strcmp (identifier,
146                                 "test-id"));
147     my_ego = ego;
148     *ctx = &round;
149     break;
150   case 2: /* rename */
151     GNUNET_assert (my_ego == ego);
152     GNUNET_assert (0 == strcmp (identifier,
153                                 "test"));
154     GNUNET_assert (*ctx == &round);
155     break;
156   case 3: /* reconnect-down */
157     GNUNET_assert (my_ego == ego);
158     GNUNET_assert (NULL == identifier);
159     GNUNET_assert (*ctx == &round);
160     *ctx = NULL;
161     break;
162   case 4: /* reconnect-up */
163     GNUNET_assert (0 == strcmp (identifier,
164                                 "test"));
165     my_ego = ego;
166     *ctx = &round;
167     break;
168   case 5: /* end of iteration after reconnect */
169     GNUNET_assert (NULL == ego);
170     GNUNET_assert (NULL == identifier);
171     break;
172   case 6: /* delete */
173     GNUNET_assert (my_ego == ego);
174     GNUNET_assert (*ctx == &round);
175     *ctx = NULL;
176     break;
177   default:
178     GNUNET_break (0);
179   }
180   round++;
181 }
182
183
184 /**
185  * Continuation called from successful delete operation.
186  *
187  * @param cls NULL
188  * @param emsg (should also be NULL)
189  */
190 static void
191 delete_cont (void *cls,
192              const char *emsg)
193 {
194   op = NULL;
195   GNUNET_assert (NULL == emsg);
196   end ();
197 }
198
199
200 /**
201  * Continue by deleting the "test" identity.
202  *
203  * @param cls NULL
204  */
205 static void
206 finally_delete (void *cls)
207 {
208   op = GNUNET_IDENTITY_delete (h,
209                                "test",
210                                &delete_cont,
211                                NULL);
212 }
213
214
215 /**
216  * Continuation called from expected-to-fail rename operation.
217  *
218  * @param cls NULL
219  * @param emsg (should also be NULL)
220  */
221 static void
222 fail_rename_cont (void *cls,
223                   const char *emsg)
224 {
225   GNUNET_assert (NULL != emsg);
226   op = NULL;
227   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
228                                 &finally_delete,
229                                 NULL);
230 }
231
232
233 /**
234  * Continuation called from successful rename operation.
235  *
236  * @param cls NULL
237  * @param emsg (should also be NULL)
238  */
239 static void
240 success_rename_cont (void *cls,
241                      const char *emsg)
242 {
243   GNUNET_assert (NULL == emsg);
244   op = GNUNET_IDENTITY_rename (h,
245                                "test-id",
246                                "test",
247                                &fail_rename_cont,
248                                NULL);
249 }
250
251
252 /**
253  * Called with events about created ego.
254  *
255  * @param cls NULL
256  * @param emsg error message
257  */
258 static void
259 create_cb (void *cls,
260            const char *emsg)
261 {
262   GNUNET_assert (NULL == emsg);
263   op = GNUNET_IDENTITY_rename (h,
264                                "test-id",
265                                "test",
266                                &success_rename_cont,
267                                NULL);
268 }
269
270
271 /**
272  * Main function of the test, run from scheduler.
273  *
274  * @param cls NULL
275  * @param cfg configuration we use (also to connect to identity service)
276  * @param peer handle to access more of the peer (not used)
277  */
278 static void
279 run (void *cls,
280      const struct GNUNET_CONFIGURATION_Handle *cfg,
281      struct GNUNET_TESTING_Peer *peer)
282 {
283   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
284                                                 &endbadly,
285                                                 NULL);
286   GNUNET_SCHEDULER_add_shutdown (&end_normally,
287                                  NULL);
288   h = GNUNET_IDENTITY_connect (cfg,
289                                &notification_cb,
290                                NULL);
291   GNUNET_assert (NULL != h);
292   op = GNUNET_IDENTITY_create (h,
293                                "test-id",
294                                &create_cb,
295                                NULL);
296
297 }
298
299
300 int
301 main (int argc, char *argv[])
302 {
303   GNUNET_DISK_directory_remove ("/tmp/test-identity-service");
304   res = 1;
305   if (0 !=
306       GNUNET_TESTING_service_run ("test-identity",
307                                   "identity",
308                                   "test_identity.conf",
309                                   &run,
310                                   NULL))
311     return 1;
312   return res;
313 }
314
315
316 /* end of test_identity.c */