small API change: do no longer pass rarely needed GNUNET_SCHEDULER_TaskContext to...
[oweals/gnunet.git] / src / identity / test_identity.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
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 (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  * Called with events about egos.
120  *
121  * @param cls NULL
122  * @param ego ego handle
123  * @param ego_ctx context for application to store data for this ego
124  *                 (during the lifetime of this process, initially NULL)
125  * @param identifier identifier assigned by the user for this ego,
126  *                   NULL if the user just deleted the ego and it
127  *                   must thus no longer be used
128  */
129 static void
130 notification_cb (void *cls,
131                  struct GNUNET_IDENTITY_Ego *ego,
132                  void **ctx,
133                  const char *identifier)
134 {
135   static struct GNUNET_IDENTITY_Ego *my_ego;
136   static int round;
137
138   switch (round)
139   {
140   case 0: /* end of initial iteration */
141     GNUNET_assert (NULL == ego);
142     GNUNET_assert (NULL == identifier);
143     break;
144   case 1: /* create */
145     GNUNET_assert (NULL != ego);
146     GNUNET_assert (0 == strcmp (identifier, "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, "test"));
153     GNUNET_assert (*ctx == &round);
154     break;
155   case 3: /* delete */
156     GNUNET_assert (my_ego == ego);
157     GNUNET_assert (NULL == identifier);
158     GNUNET_assert (*ctx == &round);
159     *ctx = NULL;
160     break;
161   default:
162     GNUNET_break (0);
163   }
164   round++;
165 }
166
167
168 /**
169  * Continuation called from successful delete operation.
170  *
171  * @param cls NULL
172  * @param emsg (should also be NULL)
173  */
174 static void
175 delete_cont (void *cls,
176              const char *emsg)
177 {
178   op = NULL;
179   GNUNET_assert (NULL == emsg);
180   end ();
181 }
182
183
184 /**
185  * Continuation called from expected-to-fail rename operation.
186  *
187  * @param cls NULL
188  * @param emsg (should also be NULL)
189  */
190 static void
191 fail_rename_cont (void *cls,
192                   const char *emsg)
193 {
194   GNUNET_assert (NULL != emsg);
195   op = GNUNET_IDENTITY_delete (h,
196                                "test",
197                                &delete_cont,
198                                NULL);
199    end (); /* yepee */
200 }
201
202
203 /**
204  * Continuation called from successful rename operation.
205  *
206  * @param cls NULL
207  * @param emsg (should also be NULL)
208  */
209 static void
210 success_rename_cont (void *cls,
211                      const char *emsg)
212 {
213   GNUNET_assert (NULL == emsg);
214   op = GNUNET_IDENTITY_rename (h,
215                                "test-id",
216                                "test",
217                                &fail_rename_cont,
218                                NULL);
219 }
220
221
222 /**
223  * Called with events about created ego.
224  *
225  * @param cls NULL
226  * @param emsg error message
227  */
228 static void
229 create_cb (void *cls,
230            const char *emsg)
231 {
232   GNUNET_assert (NULL == emsg);
233   op = GNUNET_IDENTITY_rename (h,
234                                "test-id",
235                                "test",
236                                &success_rename_cont,
237                                NULL);
238 }
239
240
241 /**
242  * Main function of the test, run from scheduler.
243  *
244  * @param cls NULL
245  * @param cfg configuration we use (also to connect to identity service)
246  * @param peer handle to access more of the peer (not used)
247  */
248 static void
249 run (void *cls,
250      const struct GNUNET_CONFIGURATION_Handle *cfg,
251      struct GNUNET_TESTING_Peer *peer)
252 {
253   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
254                                                 &endbadly, NULL);
255   h = GNUNET_IDENTITY_connect (cfg, &notification_cb, NULL);
256   GNUNET_assert (NULL != h);
257   op = GNUNET_IDENTITY_create (h,
258                                "test-id",
259                                &create_cb,
260                                NULL);
261
262 }
263
264
265 int
266 main (int argc, char *argv[])
267 {
268   GNUNET_DISK_directory_remove ("/tmp/test-identity-service");
269   res = 1;
270   if (0 !=
271       GNUNET_TESTING_service_run ("test-identity",
272                                   "identity",
273                                   "test_identity.conf",
274                                   &run,
275                                   NULL))
276     return 1;
277   return res;
278 }
279
280
281 /* end of test_identity.c */