aboutsummaryrefslogtreecommitdiff
path: root/src/service/identity/test_identity.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/identity/test_identity.c')
-rw-r--r--src/service/identity/test_identity.c324
1 files changed, 324 insertions, 0 deletions
diff --git a/src/service/identity/test_identity.c b/src/service/identity/test_identity.c
new file mode 100644
index 000000000..d133e3ee4
--- /dev/null
+++ b/src/service/identity/test_identity.c
@@ -0,0 +1,324 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2013, 2016 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 */
38static int res;
39
40/**
41 * Handle to identity service.
42 */
43static struct GNUNET_IDENTITY_Handle *h;
44
45/**
46 * Handle to identity operation.
47 */
48static struct GNUNET_IDENTITY_Operation *op;
49
50/**
51 * Handle for task for timeout termination.
52 */
53static 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 */
70static void
71cleanup (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 */
92static void
93endbadly (void *cls)
94{
95 GNUNET_SCHEDULER_shutdown ();
96}
97
98
99/**
100 * Finish the testcase (successfully).
101 */
102static void
103end ()
104{
105 if (NULL != endbadly_task)
106 {
107 GNUNET_SCHEDULER_cancel (endbadly_task);
108 endbadly_task = NULL;
109 }
110 GNUNET_SCHEDULER_shutdown ();
111}
112
113
114/**
115 * Called with events about egos.
116 *
117 * @param cls NULL
118 * @param ego ego handle
119 * @param ego_ctx context for application to store data for this ego
120 * (during the lifetime of this process, initially NULL)
121 * @param identifier identifier assigned by the user for this ego,
122 * NULL if the user just deleted the ego and it
123 * must thus no longer be used
124 */
125static void
126notification_cb (void *cls,
127 struct GNUNET_IDENTITY_Ego *ego,
128 void **ctx,
129 const char *identifier)
130{
131 static struct GNUNET_IDENTITY_Ego *my_ego;
132 static int round;
133
134 switch (round)
135 {
136 case 0: /* end of initial iteration */
137 CHECK (NULL == ego);
138 CHECK (NULL == identifier);
139 break;
140
141 case 1: /* create */
142 CHECK (NULL != ego);
143 CHECK (NULL != identifier);
144 CHECK (0 == strcmp (identifier, "test-id"));
145 my_ego = ego;
146 *ctx = &round;
147 break;
148
149 case 2: /* rename */
150 CHECK (my_ego == ego);
151 CHECK (NULL != identifier);
152 CHECK (0 == strcmp (identifier, "test"));
153 CHECK (*ctx == &round);
154 break;
155
156 case 3: /* reconnect-down */
157 CHECK (my_ego == ego);
158 CHECK (NULL == identifier);
159 CHECK (*ctx == &round);
160 *ctx = NULL;
161 break;
162
163 case 4: /* reconnect-up */
164 CHECK (NULL != identifier);
165 CHECK (0 == strcmp (identifier, "test"));
166 my_ego = ego;
167 *ctx = &round;
168 break;
169
170 case 5: /* end of iteration after reconnect */
171 CHECK (NULL == ego);
172 CHECK (NULL == identifier);
173 break;
174
175 case 6: /* delete */
176 CHECK (my_ego == ego);
177 CHECK (*ctx == &round);
178 *ctx = NULL;
179 break;
180
181 default:
182 CHECK (0);
183 }
184 round++;
185}
186
187
188/**
189 * Continuation called from successful delete operation.
190 *
191 * @param cls NULL
192 * @param ec
193 */
194static void
195delete_cont (void *cls, enum GNUNET_ErrorCode ec)
196{
197 op = NULL;
198 CHECK (GNUNET_EC_NONE == ec);
199 res = 0;
200 end ();
201}
202
203
204/**
205 * Continue by deleting the "test" identity.
206 *
207 * @param cls NULL
208 */
209static void
210finally_delete (void *cls)
211{
212 op = GNUNET_IDENTITY_delete (h, "test", &delete_cont, NULL);
213}
214
215
216/**
217 * Continuation called from expected-to-fail rename operation.
218 *
219 * @param cls NULL
220 * @param ec
221 */
222static void
223fail_rename_cont (void *cls, enum GNUNET_ErrorCode ec)
224{
225 CHECK (GNUNET_EC_NONE != ec);
226 op = NULL;
227 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
228 &finally_delete,
229 NULL);
230}
231
232
233/**
234 * Continuation called from successful rename operation.
235 *
236 * @param cls NULL
237 * @param ec
238 */
239static void
240success_rename_cont (void *cls, enum GNUNET_ErrorCode ec)
241{
242 CHECK (GNUNET_EC_NONE == ec);
243 op = GNUNET_IDENTITY_rename (h, "test-id", "test", &fail_rename_cont, NULL);
244}
245
246
247/**
248 * Called with events about created ego.
249 *
250 * @param cls NULL
251 * @param pk private key of the ego, or NULL on error
252 * @param ec
253 */
254static void
255create_cb (void *cls,
256 const struct GNUNET_CRYPTO_PrivateKey *pk,
257 enum GNUNET_ErrorCode ec)
258{
259 CHECK (NULL != pk);
260 CHECK (GNUNET_EC_NONE == ec);
261 struct GNUNET_CRYPTO_PublicKey pub;
262 size_t pt_len = strlen ("test") + 1;
263 unsigned char ct[pt_len + GNUNET_CRYPTO_ENCRYPT_OVERHEAD_BYTES];
264 char pt[pt_len];
265 enum GNUNET_GenericReturnValue res;
266
267 GNUNET_CRYPTO_key_get_public (pk, &pub);
268 res = GNUNET_CRYPTO_encrypt ("test", pt_len, &pub, ct,
269 pt_len
270 + GNUNET_CRYPTO_ENCRYPT_OVERHEAD_BYTES);
271 CHECK (GNUNET_OK == res);
272 res = GNUNET_CRYPTO_decrypt (ct, pt_len
273 + GNUNET_CRYPTO_ENCRYPT_OVERHEAD_BYTES,
274 pk, pt, pt_len);
275 CHECK (GNUNET_OK == res);
276 CHECK (0 == strcmp (pt, "test"));
277 op =
278 GNUNET_IDENTITY_rename (h, "test-id", "test", &success_rename_cont, NULL);
279}
280
281
282/**
283 * Main function of the test, run from scheduler.
284 *
285 * @param cls NULL
286 * @param cfg configuration we use (also to connect to identity service)
287 * @param peer handle to access more of the peer (not used)
288 */
289static void
290run (void *cls,
291 const struct GNUNET_CONFIGURATION_Handle *cfg,
292 struct GNUNET_TESTING_Peer *peer)
293{
294 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
295 GNUNET_SCHEDULER_add_shutdown (&cleanup, NULL);
296 h = GNUNET_IDENTITY_connect (cfg, &notification_cb, NULL);
297 CHECK (NULL != h);
298 op = GNUNET_IDENTITY_create (h,
299 "test-id",
300 NULL,
301 GNUNET_PUBLIC_KEY_TYPE_ECDSA,
302 &create_cb, NULL);
303}
304
305
306int
307main (int argc, char *argv[])
308{
309 GNUNET_DISK_purge_cfg_dir ("test_identity.conf",
310 "GNUNET_TEST_HOME");
311 res = 1;
312 if (0 != GNUNET_TESTING_service_run ("test-identity",
313 "identity",
314 "test_identity.conf",
315 &run,
316 NULL))
317 return 1;
318 GNUNET_DISK_purge_cfg_dir ("test_identity.conf",
319 "GNUNET_TEST_HOME");
320 return res;
321}
322
323
324/* end of test_identity.c */