remove 'illegal' (non-reentrant) log logic from signal handler
[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      SPDX-License-Identifier: AGPL3.0-or-later
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 #define CHECK(cond)     \
56   do                    \
57   {                     \
58     if (! (cond))       \
59     {                   \
60       GNUNET_break (0); \
61       end ();           \
62       return;           \
63     }                   \
64   } while (0)
65
66
67 /**
68  * Clean up all resources used.
69  */
70 static void
71 cleanup (void *cls)
72 {
73   (void) cls;
74   if (NULL != op)
75   {
76     GNUNET_IDENTITY_cancel (op);
77     op = NULL;
78   }
79   if (NULL != h)
80   {
81     GNUNET_IDENTITY_disconnect (h);
82     h = NULL;
83   }
84 }
85
86
87 /**
88  * Termiante the testcase (failure).
89  *
90  * @param cls NULL
91  */
92 static void
93 endbadly (void *cls)
94 {
95   GNUNET_SCHEDULER_shutdown ();
96   res = 1;
97 }
98
99
100 /**
101  * Termiante the testcase.
102  */
103 static void
104 end ()
105 {
106   if (NULL != endbadly_task)
107   {
108     GNUNET_SCHEDULER_cancel (endbadly_task);
109     endbadly_task = NULL;
110   }
111   GNUNET_SCHEDULER_shutdown ();
112 }
113
114
115 /**
116  * Continuation called from successful delete operation.
117  *
118  * @param cls NULL
119  * @param emsg (should also be NULL)
120  */
121 static void
122 delete_cont (void *cls, const char *emsg)
123 {
124   op = NULL;
125   CHECK (NULL == emsg);
126   res = 0;
127   end ();
128 }
129
130
131 /**
132  * Continuation called from expected-to-fail rename operation.
133  *
134  * @param cls NULL
135  * @param emsg (should also be NULL)
136  */
137 static void
138 get_cb (void *cls,
139         struct GNUNET_IDENTITY_Ego *ego,
140         void **ctx,
141         const char *identifier)
142 {
143   op = NULL;
144   CHECK (NULL != ego);
145   CHECK (NULL != identifier);
146   CHECK (0 == strcmp (identifier, "test-id"));
147   op = GNUNET_IDENTITY_delete (h, "test-id", &delete_cont, NULL);
148 }
149
150
151 /**
152  * Called with events about egos.
153  *
154  * @param cls NULL
155  * @param ego ego handle
156  * @param ego_ctx context for application to store data for this ego
157  *                 (during the lifetime of this process, initially NULL)
158  * @param identifier identifier assigned by the user for this ego,
159  *                   NULL if the user just deleted the ego and it
160  *                   must thus no longer be used
161  */
162 static void
163 dummy_cb (void *cls,
164           struct GNUNET_IDENTITY_Ego *ego,
165           void **ctx,
166           const char *identifier)
167 {
168   (void) cls;
169   (void) ego;
170   (void) ctx;
171   (void) identifier;
172 }
173
174
175 /**
176  * Main function of the test, run from scheduler.
177  *
178  * @param cls NULL
179  * @param cfg configuration we use (also to connect to identity service)
180  * @param peer handle to access more of the peer (not used)
181  */
182 static void
183 run_get (void *cls,
184          const struct GNUNET_CONFIGURATION_Handle *cfg,
185          struct GNUNET_TESTING_Peer *peer)
186 {
187   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
188   GNUNET_SCHEDULER_add_shutdown (&cleanup, NULL);
189   h = GNUNET_IDENTITY_connect (cfg, &dummy_cb, NULL);
190   CHECK (NULL != h);
191   op = GNUNET_IDENTITY_get (h, "test-service", &get_cb, NULL);
192 }
193
194
195 /**
196  * Continuation called from successful rename operation.
197  *
198  * @param cls NULL
199  * @param emsg (should also be NULL)
200  */
201 static void
202 success_set_cont (void *cls, const char *emsg)
203 {
204   op = NULL;
205   CHECK (NULL == emsg);
206   end ();
207 }
208
209
210 /**
211  * Called with events about egos.
212  *
213  * @param cls NULL
214  * @param ego ego handle
215  * @param ego_ctx context for application to store data for this ego
216  *                 (during the lifetime of this process, initially NULL)
217  * @param identifier identifier assigned by the user for this ego,
218  *                   NULL if the user just deleted the ego and it
219  *                   must thus no longer be used
220  */
221 static void
222 notification_cb (void *cls,
223                  struct GNUNET_IDENTITY_Ego *ego,
224                  void **ctx,
225                  const char *identifier)
226 {
227   if (NULL == ego)
228     return; /* skip first call */
229   if (NULL == identifier)
230     return; /* deletion / shutdown */
231   op = GNUNET_IDENTITY_set (h, "test-service", ego, &success_set_cont, NULL);
232 }
233
234
235 /**
236  * Called with events about created ego.
237  *
238  * @param cls NULL
239  * @param pk private key of the ego, or NULL on error
240  * @param emsg error message
241  */
242 static void
243 create_cb (void *cls,
244            const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk,
245            const char *emsg)
246 {
247   CHECK (NULL == emsg);
248   CHECK (NULL != pk);
249   op = NULL;
250 }
251
252
253 /**
254  * Main function of the test, run from scheduler.
255  *
256  * @param cls NULL
257  * @param cfg configuration we use (also to connect to identity service)
258  * @param peer handle to access more of the peer (not used)
259  */
260 static void
261 run_set (void *cls,
262          const struct GNUNET_CONFIGURATION_Handle *cfg,
263          struct GNUNET_TESTING_Peer *peer)
264 {
265   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
266   GNUNET_SCHEDULER_add_shutdown (&cleanup, NULL);
267   h = GNUNET_IDENTITY_connect (cfg, &notification_cb, NULL);
268   CHECK (NULL != h);
269   op = GNUNET_IDENTITY_create (h, "test-id", &create_cb, NULL);
270 }
271
272
273 int
274 main (int argc, char *argv[])
275 {
276   GNUNET_DISK_directory_remove ("/tmp/gnunet/test-identity-service");
277   res = 1;
278   if (0 != GNUNET_TESTING_service_run ("test-identity-defaults",
279                                        "identity",
280                                        "test_identity.conf",
281                                        &run_set,
282                                        NULL))
283     return 1;
284   if (0 != GNUNET_TESTING_service_run ("test-identity-defaults",
285                                        "identity",
286                                        "test_identity.conf",
287                                        &run_get,
288                                        NULL))
289     return 1;
290   GNUNET_DISK_directory_remove ("/tmp/gnunet/test-identity-service");
291   return res;
292 }
293
294
295 /* end of test_identity_defaults.c */