added adaptive step-intervals
[oweals/gnunet.git] / src / identity / test_identity_defaults.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_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 GNUNET_SCHEDULER_TaskIdentifier 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 != GNUNET_SCHEDULER_NO_TASK)
111   {
112     GNUNET_SCHEDULER_cancel (endbadly_task);
113     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
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   op = GNUNET_IDENTITY_set (h,
217                             "test-service",
218                             ego,
219                             &success_set_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 = NULL;
236 }
237
238
239 /**
240  * Main function of the test, run from scheduler.
241  *
242  * @param cls NULL
243  * @param cfg configuration we use (also to connect to identity service)
244  * @param peer handle to access more of the peer (not used)
245  */
246 static void
247 run_set (void *cls,
248          const struct GNUNET_CONFIGURATION_Handle *cfg,
249          struct GNUNET_TESTING_Peer *peer)
250 {
251   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
252                                                 &endbadly, NULL);
253   h = GNUNET_IDENTITY_connect (cfg, &notification_cb, NULL);
254   GNUNET_assert (NULL != h);
255   op = GNUNET_IDENTITY_create (h,
256                                "test-id",
257                                &create_cb,
258                                NULL);
259
260 }
261
262
263 int
264 main (int argc, char *argv[])
265 {
266   GNUNET_DISK_directory_remove ("/tmp/test-identity-service");
267   res = 1;
268   if (0 !=
269       GNUNET_TESTING_service_run ("test-identity-defaults",
270                                   "identity",
271                                   "test_identity.conf",
272                                   &run_set,
273                                   NULL))
274     return 1;
275   if (0 !=
276       GNUNET_TESTING_service_run ("test-identity-defaults",
277                                   "identity",
278                                   "test_identity.conf",
279                                   &run_get,
280                                   NULL))
281     return 1;
282   GNUNET_DISK_directory_remove ("/tmp/test-identity-service");
283   return res;
284 }
285
286
287 /* end of test_identity.c */