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