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