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