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