-json decrefs
[oweals/gnunet.git] / src / identity / test_identity_defaults.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  * Continuation called from successful delete operation.
122  *
123  * @param cls NULL
124  * @param emsg (should also be NULL)
125  */
126 static void
127 delete_cont (void *cls,
128              const char *emsg)
129 {
130   op = NULL;
131   GNUNET_assert (NULL == emsg);
132   end ();
133 }
134
135
136 /**
137  * Continuation called from expected-to-fail rename operation.
138  *
139  * @param cls NULL
140  * @param emsg (should also be NULL)
141  */
142 static void
143 get_cb (void *cls,
144         struct GNUNET_IDENTITY_Ego *ego,
145         void **ctx,
146         const char *identifier)
147 {
148   GNUNET_assert (NULL != ego);
149   GNUNET_assert (NULL != identifier);
150   GNUNET_assert (0 == strcmp (identifier, "test-id"));
151   op = GNUNET_IDENTITY_delete (h,
152                                "test-id",
153                                &delete_cont,
154                                NULL);
155 }
156
157
158 /**
159  * Main function of the test, run from scheduler.
160  *
161  * @param cls NULL
162  * @param cfg configuration we use (also to connect to identity service)
163  * @param peer handle to access more of the peer (not used)
164  */
165 static void
166 run_get (void *cls,
167          const struct GNUNET_CONFIGURATION_Handle *cfg,
168          struct GNUNET_TESTING_Peer *peer)
169 {
170   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
171                                                 &endbadly, NULL);
172   h = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
173   GNUNET_assert (NULL != h);
174   op = GNUNET_IDENTITY_get (h,
175                             "test-service",
176                             &get_cb,
177                             NULL);
178 }
179
180
181 /**
182  * Continuation called from successful rename operation.
183  *
184  * @param cls NULL
185  * @param emsg (should also be NULL)
186  */
187 static void
188 success_set_cont (void *cls,
189                   const char *emsg)
190 {
191   op = NULL;
192   GNUNET_assert (NULL == emsg);
193   end ();
194 }
195
196
197 /**
198  * Called with events about egos.
199  *
200  * @param cls NULL
201  * @param ego ego handle
202  * @param ego_ctx context for application to store data for this ego
203  *                 (during the lifetime of this process, initially NULL)
204  * @param identifier identifier assigned by the user for this ego,
205  *                   NULL if the user just deleted the ego and it
206  *                   must thus no longer be used
207  */
208 static void
209 notification_cb (void *cls,
210                  struct GNUNET_IDENTITY_Ego *ego,
211                  void **ctx,
212                  const char *identifier)
213 {
214   if (NULL == ego)
215     return; /* skip first call */
216   if (NULL == identifier)
217     return; /* deletion / shutdown */
218   op = GNUNET_IDENTITY_set (h,
219                             "test-service",
220                             ego,
221                             &success_set_cont,
222                             NULL);
223 }
224
225
226 /**
227  * Called with events about created ego.
228  *
229  * @param cls NULL
230  * @param emsg error message
231  */
232 static void
233 create_cb (void *cls,
234            const char *emsg)
235 {
236   GNUNET_assert (NULL == emsg);
237   op = 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_set (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-defaults",
272                                   "identity",
273                                   "test_identity.conf",
274                                   &run_set,
275                                   NULL))
276     return 1;
277   if (0 !=
278       GNUNET_TESTING_service_run ("test-identity-defaults",
279                                   "identity",
280                                   "test_identity.conf",
281                                   &run_get,
282                                   NULL))
283     return 1;
284   GNUNET_DISK_directory_remove ("/tmp/test-identity-service");
285   return res;
286 }
287
288
289 /* end of test_identity.c */