Verify that GCD(m,n) != 1 when n is an RSA modulus
[oweals/gnunet.git] / src / identity / test_identity.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
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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 struct GNUNET_SCHEDULER_Task * 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  */
81 static void
82 endbadly (void *cls)
83 {
84   cleanup ();
85   res = 1;
86 }
87
88
89 /**
90  * Termiante the testcase (success).
91  *
92  * @param cls NULL
93  */
94 static void
95 end_normally (void *cls)
96 {
97   cleanup ();
98   res = 0;
99 }
100
101
102 /**
103  * Finish the testcase (successfully).
104  */
105 static void
106 end ()
107 {
108   if (endbadly_task != NULL)
109   {
110     GNUNET_SCHEDULER_cancel (endbadly_task);
111     endbadly_task = NULL;
112   }
113   GNUNET_SCHEDULER_add_now (&end_normally, NULL);
114 }
115
116
117 /**
118  * Called with events about egos.
119  *
120  * @param cls NULL
121  * @param ego ego handle
122  * @param ego_ctx context for application to store data for this ego
123  *                 (during the lifetime of this process, initially NULL)
124  * @param identifier identifier assigned by the user for this ego,
125  *                   NULL if the user just deleted the ego and it
126  *                   must thus no longer be used
127  */
128 static void
129 notification_cb (void *cls,
130                  struct GNUNET_IDENTITY_Ego *ego,
131                  void **ctx,
132                  const char *identifier)
133 {
134   static struct GNUNET_IDENTITY_Ego *my_ego;
135   static int round;
136
137   switch (round)
138   {
139   case 0: /* end of initial iteration */
140     GNUNET_assert (NULL == ego);
141     GNUNET_assert (NULL == identifier);
142     break;
143   case 1: /* create */
144     GNUNET_assert (NULL != ego);
145     GNUNET_assert (0 == strcmp (identifier, "test-id"));
146     my_ego = ego;
147     *ctx = &round;
148     break;
149   case 2: /* rename */
150     GNUNET_assert (my_ego == ego);
151     GNUNET_assert (0 == strcmp (identifier, "test"));
152     GNUNET_assert (*ctx == &round);
153     break;
154   case 3: /* delete */
155     GNUNET_assert (my_ego == ego);
156     GNUNET_assert (NULL == identifier);
157     GNUNET_assert (*ctx == &round);
158     *ctx = NULL;
159     break;
160   default:
161     GNUNET_break (0);
162   }
163   round++;
164 }
165
166
167 /**
168  * Continuation called from successful delete operation.
169  *
170  * @param cls NULL
171  * @param emsg (should also be NULL)
172  */
173 static void
174 delete_cont (void *cls,
175              const char *emsg)
176 {
177   op = NULL;
178   GNUNET_assert (NULL == emsg);
179   end ();
180 }
181
182
183 /**
184  * Continuation called from expected-to-fail rename operation.
185  *
186  * @param cls NULL
187  * @param emsg (should also be NULL)
188  */
189 static void
190 fail_rename_cont (void *cls,
191                   const char *emsg)
192 {
193   GNUNET_assert (NULL != emsg);
194   op = GNUNET_IDENTITY_delete (h,
195                                "test",
196                                &delete_cont,
197                                NULL);
198    end (); /* yepee */
199 }
200
201
202 /**
203  * Continuation called from successful rename operation.
204  *
205  * @param cls NULL
206  * @param emsg (should also be NULL)
207  */
208 static void
209 success_rename_cont (void *cls,
210                      const char *emsg)
211 {
212   GNUNET_assert (NULL == emsg);
213   op = GNUNET_IDENTITY_rename (h,
214                                "test-id",
215                                "test",
216                                &fail_rename_cont,
217                                NULL);
218 }
219
220
221 /**
222  * Called with events about created ego.
223  *
224  * @param cls NULL
225  * @param emsg error message
226  */
227 static void
228 create_cb (void *cls,
229            const char *emsg)
230 {
231   GNUNET_assert (NULL == emsg);
232   op = GNUNET_IDENTITY_rename (h,
233                                "test-id",
234                                "test",
235                                &success_rename_cont,
236                                NULL);
237 }
238
239
240 /**
241  * Main function of the test, run from scheduler.
242  *
243  * @param cls NULL
244  * @param cfg configuration we use (also to connect to identity service)
245  * @param peer handle to access more of the peer (not used)
246  */
247 static void
248 run (void *cls,
249      const struct GNUNET_CONFIGURATION_Handle *cfg,
250      struct GNUNET_TESTING_Peer *peer)
251 {
252   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
253                                                 &endbadly, NULL);
254   h = GNUNET_IDENTITY_connect (cfg, &notification_cb, NULL);
255   GNUNET_assert (NULL != h);
256   op = GNUNET_IDENTITY_create (h,
257                                "test-id",
258                                &create_cb,
259                                NULL);
260
261 }
262
263
264 int
265 main (int argc, char *argv[])
266 {
267   GNUNET_DISK_directory_remove ("/tmp/test-identity-service");
268   res = 1;
269   if (0 !=
270       GNUNET_TESTING_service_run ("test-identity",
271                                   "identity",
272                                   "test_identity.conf",
273                                   &run,
274                                   NULL))
275     return 1;
276   return res;
277 }
278
279
280 /* end of test_identity.c */