moved test files and fixed namestore
[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 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
19 /**
20  * @file identity/test_identity.c
21  * @brief testcase for identity service
22  * @author Christian Grothoff
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_identity_service.h"
27 #include "gnunet_testing_lib.h"
28
29
30 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
31
32
33 /**
34  * Return value from 'main'.
35  */
36 static int res;
37
38 /**
39  * Handle to identity service.
40  */
41 static struct GNUNET_IDENTITY_Handle *h;
42
43 /**
44  * Handle to identity operation.
45  */
46 static struct GNUNET_IDENTITY_Operation *op;
47
48 /**
49  * Handle for task for timeout termination.
50  */
51 static struct GNUNET_SCHEDULER_Task *endbadly_task;
52
53
54 /**
55  * Clean up all resources used.
56  */
57 static void
58 cleanup ()
59 {
60   if (NULL != op)
61   {
62     GNUNET_IDENTITY_cancel (op);
63     op = NULL;
64   }
65   if (NULL != h)
66   {
67     GNUNET_IDENTITY_disconnect (h);
68     h = NULL;
69   }
70   GNUNET_SCHEDULER_shutdown ();
71 }
72
73
74 /**
75  * Termiante the testcase (failure).
76  *
77  * @param cls NULL
78  */
79 static void
80 endbadly (void *cls)
81 {
82   cleanup ();
83   res = 1;
84 }
85
86
87 /**
88  * Termiante the testcase (success).
89  *
90  * @param cls NULL
91  */
92 static void
93 end_normally (void *cls)
94 {
95   cleanup ();
96   res = 0;
97 }
98
99
100 /**
101  * Finish the testcase (successfully).
102  */
103 static void
104 end ()
105 {
106   if (NULL != endbadly_task)
107   {
108     GNUNET_SCHEDULER_cancel (endbadly_task);
109     endbadly_task = NULL;
110   }
111   GNUNET_SCHEDULER_shutdown ();
112 }
113
114
115 /**
116  * Called with events about egos.
117  *
118  * @param cls NULL
119  * @param ego ego handle
120  * @param ego_ctx context for application to store data for this ego
121  *                 (during the lifetime of this process, initially NULL)
122  * @param identifier identifier assigned by the user for this ego,
123  *                   NULL if the user just deleted the ego and it
124  *                   must thus no longer be used
125  */
126 static void
127 notification_cb (void *cls,
128                  struct GNUNET_IDENTITY_Ego *ego,
129                  void **ctx,
130                  const char *identifier)
131 {
132   static struct GNUNET_IDENTITY_Ego *my_ego;
133   static int round;
134
135   switch (round)
136   {
137   case 0: /* end of initial iteration */
138     GNUNET_assert (NULL == ego);
139     GNUNET_assert (NULL == identifier);
140     break;
141   case 1: /* create */
142     GNUNET_assert (NULL != ego);
143     GNUNET_assert (0 == strcmp (identifier,
144                                 "test-id"));
145     my_ego = ego;
146     *ctx = &round;
147     break;
148   case 2: /* rename */
149     GNUNET_assert (my_ego == ego);
150     GNUNET_assert (0 == strcmp (identifier,
151                                 "test"));
152     GNUNET_assert (*ctx == &round);
153     break;
154   case 3: /* reconnect-down */
155     GNUNET_assert (my_ego == ego);
156     GNUNET_assert (NULL == identifier);
157     GNUNET_assert (*ctx == &round);
158     *ctx = NULL;
159     break;
160   case 4: /* reconnect-up */
161     GNUNET_assert (0 == strcmp (identifier,
162                                 "test"));
163     my_ego = ego;
164     *ctx = &round;
165     break;
166   case 5: /* end of iteration after reconnect */
167     GNUNET_assert (NULL == ego);
168     GNUNET_assert (NULL == identifier);
169     break;
170   case 6: /* delete */
171     GNUNET_assert (my_ego == ego);
172     GNUNET_assert (*ctx == &round);
173     *ctx = NULL;
174     break;
175   default:
176     GNUNET_break (0);
177   }
178   round++;
179 }
180
181
182 /**
183  * Continuation called from successful delete operation.
184  *
185  * @param cls NULL
186  * @param emsg (should also be NULL)
187  */
188 static void
189 delete_cont (void *cls,
190              const char *emsg)
191 {
192   op = NULL;
193   GNUNET_assert (NULL == emsg);
194   end ();
195 }
196
197
198 /**
199  * Continue by deleting the "test" identity.
200  *
201  * @param cls NULL
202  */
203 static void
204 finally_delete (void *cls)
205 {
206   op = GNUNET_IDENTITY_delete (h,
207                                "test",
208                                &delete_cont,
209                                NULL);
210 }
211
212
213 /**
214  * Continuation called from expected-to-fail rename operation.
215  *
216  * @param cls NULL
217  * @param emsg (should also be NULL)
218  */
219 static void
220 fail_rename_cont (void *cls,
221                   const char *emsg)
222 {
223   GNUNET_assert (NULL != emsg);
224   op = NULL;
225   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
226                                 &finally_delete,
227                                 NULL);
228 }
229
230
231 /**
232  * Continuation called from successful rename operation.
233  *
234  * @param cls NULL
235  * @param emsg (should also be NULL)
236  */
237 static void
238 success_rename_cont (void *cls,
239                      const char *emsg)
240 {
241   GNUNET_assert (NULL == emsg);
242   op = GNUNET_IDENTITY_rename (h,
243                                "test-id",
244                                "test",
245                                &fail_rename_cont,
246                                NULL);
247 }
248
249
250 /**
251  * Called with events about created ego.
252  *
253  * @param cls NULL
254  * @param emsg error message
255  */
256 static void
257 create_cb (void *cls,
258            const char *emsg)
259 {
260   GNUNET_assert (NULL == emsg);
261   op = GNUNET_IDENTITY_rename (h,
262                                "test-id",
263                                "test",
264                                &success_rename_cont,
265                                NULL);
266 }
267
268
269 /**
270  * Main function of the test, run from scheduler.
271  *
272  * @param cls NULL
273  * @param cfg configuration we use (also to connect to identity service)
274  * @param peer handle to access more of the peer (not used)
275  */
276 static void
277 run (void *cls,
278      const struct GNUNET_CONFIGURATION_Handle *cfg,
279      struct GNUNET_TESTING_Peer *peer)
280 {
281   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
282                                                 &endbadly,
283                                                 NULL);
284   GNUNET_SCHEDULER_add_shutdown (&end_normally,
285                                  NULL);
286   h = GNUNET_IDENTITY_connect (cfg,
287                                &notification_cb,
288                                NULL);
289   GNUNET_assert (NULL != h);
290   op = GNUNET_IDENTITY_create (h,
291                                "test-id",
292                                &create_cb,
293                                NULL);
294
295 }
296
297
298 int
299 main (int argc, char *argv[])
300 {
301   GNUNET_DISK_directory_remove ("/tmp/test-identity-service");
302   res = 1;
303   if (0 !=
304       GNUNET_TESTING_service_run ("test-identity",
305                                   "identity",
306                                   "test_identity.conf",
307                                   &run,
308                                   NULL))
309     return 1;
310   return res;
311 }
312
313
314 /* end of test_identity.c */