guix-env: some update.
[oweals/gnunet.git] / src / set / test_set_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 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 set/test_set_api.c
23  * @brief testcase for set_api.c
24  * @author Florian Dold
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_set_service.h"
30
31
32 static struct GNUNET_PeerIdentity local_id;
33
34 static struct GNUNET_HashCode app_id;
35
36 static struct GNUNET_SET_Handle *set1;
37
38 static struct GNUNET_SET_Handle *set2;
39
40 static struct GNUNET_SET_ListenHandle *listen_handle;
41
42 static struct GNUNET_SET_OperationHandle *oh1;
43
44 static struct GNUNET_SET_OperationHandle *oh2;
45
46 static const struct GNUNET_CONFIGURATION_Handle *config;
47
48 static unsigned int iter_count;
49
50 static int ret;
51
52 static struct GNUNET_SCHEDULER_Task *tt;
53
54
55 static void
56 result_cb_set1 (void *cls,
57                 const struct GNUNET_SET_Element *element,
58                 uint64_t size,
59                 enum GNUNET_SET_Status status)
60 {
61   switch (status)
62   {
63   case GNUNET_SET_STATUS_OK:
64     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
65                 "set 1: got element\n");
66     break;
67   case GNUNET_SET_STATUS_FAILURE:
68     GNUNET_break (0);
69     oh1 = NULL;
70     fprintf (stderr,
71              "set 1: received failure status!\n");
72     ret = 1;
73     if (NULL != tt)
74     {
75       GNUNET_SCHEDULER_cancel (tt);
76       tt = NULL;
77     }
78     GNUNET_SCHEDULER_shutdown ();
79     break;
80   case GNUNET_SET_STATUS_DONE:
81     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
82                 "set 1: done\n");
83     oh1 = NULL;
84     if (NULL != set1)
85     {
86       GNUNET_SET_destroy (set1);
87       set1 = NULL;
88     }
89     if (NULL == set2)
90     {
91       GNUNET_SCHEDULER_cancel (tt);
92       tt = NULL;
93       GNUNET_SCHEDULER_shutdown ();
94     }
95     break;
96   default:
97     GNUNET_assert (0);
98   }
99 }
100
101
102 static void
103 result_cb_set2 (void *cls,
104                 const struct GNUNET_SET_Element *element,
105                 uint64_t size,
106                 enum GNUNET_SET_Status status)
107 {
108   switch (status)
109   {
110   case GNUNET_SET_STATUS_OK:
111     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
112                 "set 2: got element\n");
113     break;
114   case GNUNET_SET_STATUS_FAILURE:
115     GNUNET_break (0);
116     oh2 = NULL;
117     fprintf (stderr,
118              "set 2: received failure status\n");
119     GNUNET_SCHEDULER_shutdown ();
120     ret = 1;
121     break;
122   case GNUNET_SET_STATUS_DONE:
123     oh2 = NULL;
124     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125                 "set 2: done\n");
126     GNUNET_SET_destroy (set2);
127     set2 = NULL;
128     if (NULL == set1)
129     {
130       GNUNET_SCHEDULER_cancel (tt);
131       tt = NULL;
132       GNUNET_SCHEDULER_shutdown ();
133     }
134     break;
135   default:
136     GNUNET_assert (0);
137   }
138 }
139
140
141 static void
142 listen_cb (void *cls,
143            const struct GNUNET_PeerIdentity *other_peer,
144            const struct GNUNET_MessageHeader *context_msg,
145            struct GNUNET_SET_Request *request)
146 {
147   GNUNET_assert (NULL != context_msg);
148   GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
149   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150               "listen cb called\n");
151   oh2 = GNUNET_SET_accept (request,
152                            GNUNET_SET_RESULT_ADDED,
153                            (struct GNUNET_SET_Option[]) { 0 },
154                            &result_cb_set2,
155                            NULL);
156   GNUNET_SET_commit (oh2,
157                      set2);
158 }
159
160
161 /**
162  * Start the set operation.
163  *
164  * @param cls closure, unused
165  */
166 static void
167 start (void *cls)
168 {
169   struct GNUNET_MessageHeader context_msg;
170
171   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172               "Starting reconciliation\n");
173   context_msg.size = htons (sizeof context_msg);
174   context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
175   listen_handle = GNUNET_SET_listen (config,
176                                      GNUNET_SET_OPERATION_UNION,
177                                      &app_id,
178                                      &listen_cb,
179                                      NULL);
180   oh1 = GNUNET_SET_prepare (&local_id,
181                             &app_id,
182                             &context_msg,
183                             GNUNET_SET_RESULT_ADDED,
184                             (struct GNUNET_SET_Option[]) { 0 },
185                             &result_cb_set1,
186                             NULL);
187   GNUNET_SET_commit (oh1,
188                      set1);
189 }
190
191
192 /**
193  * Initialize the second set, continue
194  *
195  * @param cls closure, unused
196  */
197 static void
198 init_set2 (void *cls)
199 {
200   struct GNUNET_SET_Element element;
201
202   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
203               "initializing set 2\n");
204
205   element.element_type = 0;
206   element.data = "hello";
207   element.size = strlen(element.data);
208   GNUNET_SET_add_element (set2,
209                           &element,
210                           NULL, NULL);
211   element.data = "quux";
212   element.size = strlen(element.data);
213   GNUNET_SET_add_element (set2,
214                           &element,
215                           NULL, NULL);
216   element.data = "baz";
217   element.size = strlen(element.data);
218   GNUNET_SET_add_element (set2,
219                           &element,
220                           &start, NULL);
221 }
222
223
224 /**
225  * Initialize the first set, continue.
226  */
227 static void
228 init_set1 (void)
229 {
230   struct GNUNET_SET_Element element;
231
232   element.element_type = 0;
233   element.data = "hello";
234   element.size = strlen(element.data);
235   GNUNET_SET_add_element (set1,
236                           &element,
237                           NULL, NULL);
238   element.data = "bar";
239   element.size = strlen(element.data);
240   GNUNET_SET_add_element (set1,
241                           &element,
242                           &init_set2,
243                           NULL);
244   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
245               "initialized set 1\n");
246 }
247
248
249 static int
250 iter_cb (void *cls,
251          const struct GNUNET_SET_Element *element)
252 {
253   struct GNUNET_SET_Handle *set = cls;
254
255   if (NULL == element)
256   {
257     GNUNET_assert (3 == iter_count);
258     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259                 "Iteration finished, destroying set %p\n",
260                 set);
261     GNUNET_SET_destroy (set);
262     return GNUNET_YES;
263   }
264   iter_count++;
265   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266               "iter: got element %u\n",
267               iter_count);
268   return GNUNET_YES;
269 }
270
271
272 static void
273 test_iter ()
274 {
275   struct GNUNET_SET_Element element;
276   struct GNUNET_SET_Handle *iter_set;
277
278   iter_set = GNUNET_SET_create (config,
279                                 GNUNET_SET_OPERATION_UNION);
280   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281               "Testing iteration over 3 elements on set %p\n",
282               iter_set);
283   element.element_type = 0;
284
285   element.data = "hello";
286   element.size = strlen(element.data);
287   GNUNET_SET_add_element (iter_set,
288                           &element,
289                           NULL, NULL);
290   element.data = "bar";
291   element.size = strlen(element.data);
292   GNUNET_SET_add_element (iter_set,
293                           &element,
294                           NULL, NULL);
295   element.data = "quux";
296   element.size = strlen(element.data);
297   GNUNET_SET_add_element (iter_set,
298                           &element,
299                           NULL, NULL);
300   GNUNET_SET_iterate (iter_set,
301                       &iter_cb,
302                       iter_set);
303 }
304
305
306 /**
307  * Function run on timeout.
308  *
309  * @param cls closure
310  */
311 static void
312 timeout_fail (void *cls)
313 {
314   tt = NULL;
315   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
316               "Testcase failed with timeout\n");
317   GNUNET_SCHEDULER_shutdown ();
318   ret = 1;
319 }
320
321
322 /**
323  * Function run on shutdown.
324  *
325  * @param cls closure
326  */
327 static void
328 do_shutdown (void *cls)
329 {
330   if (NULL != tt)
331   {
332     GNUNET_SCHEDULER_cancel (tt);
333     tt = NULL;
334   }
335   if (NULL != oh1)
336   {
337     GNUNET_SET_operation_cancel (oh1);
338     oh1 = NULL;
339   }
340   if (NULL != oh2)
341   {
342     GNUNET_SET_operation_cancel (oh2);
343     oh2 = NULL;
344   }
345   if (NULL != set1)
346   {
347     GNUNET_SET_destroy (set1);
348     set1 = NULL;
349   }
350   if (NULL != set2)
351   {
352     GNUNET_SET_destroy (set2);
353     set2 = NULL;
354   }
355   if (NULL != listen_handle)
356   {
357     GNUNET_SET_listen_cancel (listen_handle);
358     listen_handle = NULL;
359   }
360 }
361
362
363 /**
364  * Signature of the 'main' function for a (single-peer) testcase that
365  * is run using 'GNUNET_TESTING_peer_run'.
366  *
367  * @param cls closure
368  * @param cfg configuration of the peer that was started
369  * @param peer identity of the peer that was created
370  */
371 static void
372 run (void *cls,
373      const struct GNUNET_CONFIGURATION_Handle *cfg,
374      struct GNUNET_TESTING_Peer *peer)
375 {
376
377   struct GNUNET_SET_OperationHandle *my_oh;
378
379   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380               "Running preparatory tests\n");
381   tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
382                                      &timeout_fail,
383                                      NULL);
384   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
385                                  NULL);
386
387   config = cfg;
388   GNUNET_CRYPTO_get_peer_identity (cfg, &local_id);
389   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
390               "my id (from CRYPTO): %s\n",
391               GNUNET_i2s (&local_id));
392   GNUNET_TESTING_peer_get_identity (peer, &local_id);
393   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394               "my id (from TESTING): %s\n",
395               GNUNET_i2s (&local_id));
396   test_iter ();
397
398   set1 = GNUNET_SET_create (cfg,
399                             GNUNET_SET_OPERATION_UNION);
400   set2 = GNUNET_SET_create (cfg,
401                             GNUNET_SET_OPERATION_UNION);
402   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403               "Created sets %p and %p for union operation\n",
404               set1,
405               set2);
406   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
407                                     &app_id);
408
409   /* test if canceling an uncommited request works! */
410   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411               "Launching and instantly stopping set operation\n");
412   my_oh = GNUNET_SET_prepare (&local_id,
413                               &app_id,
414                               NULL,
415                               GNUNET_SET_RESULT_ADDED,
416                               (struct GNUNET_SET_Option[]) { 0 },
417                               NULL,
418                               NULL);
419   GNUNET_SET_operation_cancel (my_oh);
420
421   /* test the real set reconciliation */
422   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423               "Running real set-reconciliation\n");
424   init_set1 ();
425 }
426
427
428 int
429 main (int argc, char **argv)
430 {
431   GNUNET_log_setup ("test_set_api",
432                     "WARNING",
433                     NULL);
434   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435               "Launching peer\n");
436   if (0 != GNUNET_TESTING_peer_run ("test_set_api",
437                                     "test_set.conf",
438                                     &run, NULL))
439   {
440     return 1;
441   }
442   return ret;
443 }