add attestation API
[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 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 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, "set 1: got element\n");
65     break;
66
67   case GNUNET_SET_STATUS_FAILURE:
68     GNUNET_break (0);
69     oh1 = NULL;
70     fprintf (stderr, "set 1: received failure status!\n");
71     ret = 1;
72     if (NULL != tt)
73     {
74       GNUNET_SCHEDULER_cancel (tt);
75       tt = NULL;
76     }
77     GNUNET_SCHEDULER_shutdown ();
78     break;
79
80   case GNUNET_SET_STATUS_DONE:
81     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "set 1: done\n");
82     oh1 = NULL;
83     if (NULL != set1)
84     {
85       GNUNET_SET_destroy (set1);
86       set1 = NULL;
87     }
88     if (NULL == set2)
89     {
90       GNUNET_SCHEDULER_cancel (tt);
91       tt = NULL;
92       GNUNET_SCHEDULER_shutdown ();
93     }
94     break;
95
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, "set 2: got element\n");
112     break;
113
114   case GNUNET_SET_STATUS_FAILURE:
115     GNUNET_break (0);
116     oh2 = NULL;
117     fprintf (stderr, "set 2: received failure status\n");
118     GNUNET_SCHEDULER_shutdown ();
119     ret = 1;
120     break;
121
122   case GNUNET_SET_STATUS_DONE:
123     oh2 = NULL;
124     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "set 2: done\n");
125     GNUNET_SET_destroy (set2);
126     set2 = NULL;
127     if (NULL == set1)
128     {
129       GNUNET_SCHEDULER_cancel (tt);
130       tt = NULL;
131       GNUNET_SCHEDULER_shutdown ();
132     }
133     break;
134
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, "listen cb called\n");
150   oh2 = GNUNET_SET_accept (request,
151                            GNUNET_SET_RESULT_ADDED,
152                            (struct GNUNET_SET_Option[]){ 0 },
153                            &result_cb_set2,
154                            NULL);
155   GNUNET_SET_commit (oh2, set2);
156 }
157
158
159 /**
160  * Start the set operation.
161  *
162  * @param cls closure, unused
163  */
164 static void
165 start (void *cls)
166 {
167   struct GNUNET_MessageHeader context_msg;
168
169   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting reconciliation\n");
170   context_msg.size = htons (sizeof context_msg);
171   context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
172   listen_handle = GNUNET_SET_listen (config,
173                                      GNUNET_SET_OPERATION_UNION,
174                                      &app_id,
175                                      &listen_cb,
176                                      NULL);
177   oh1 = GNUNET_SET_prepare (&local_id,
178                             &app_id,
179                             &context_msg,
180                             GNUNET_SET_RESULT_ADDED,
181                             (struct GNUNET_SET_Option[]){ 0 },
182                             &result_cb_set1,
183                             NULL);
184   GNUNET_SET_commit (oh1, set1);
185 }
186
187
188 /**
189  * Initialize the second set, continue
190  *
191  * @param cls closure, unused
192  */
193 static void
194 init_set2 (void *cls)
195 {
196   struct GNUNET_SET_Element element;
197
198   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initializing set 2\n");
199
200   element.element_type = 0;
201   element.data = "hello";
202   element.size = strlen (element.data);
203   GNUNET_SET_add_element (set2, &element, NULL, NULL);
204   element.data = "quux";
205   element.size = strlen (element.data);
206   GNUNET_SET_add_element (set2, &element, NULL, NULL);
207   element.data = "baz";
208   element.size = strlen (element.data);
209   GNUNET_SET_add_element (set2, &element, &start, NULL);
210 }
211
212
213 /**
214  * Initialize the first set, continue.
215  */
216 static void
217 init_set1 (void)
218 {
219   struct GNUNET_SET_Element element;
220
221   element.element_type = 0;
222   element.data = "hello";
223   element.size = strlen (element.data);
224   GNUNET_SET_add_element (set1, &element, NULL, NULL);
225   element.data = "bar";
226   element.size = strlen (element.data);
227   GNUNET_SET_add_element (set1, &element, &init_set2, NULL);
228   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initialized set 1\n");
229 }
230
231
232 static int
233 iter_cb (void *cls, const struct GNUNET_SET_Element *element)
234 {
235   struct GNUNET_SET_Handle *set = cls;
236
237   if (NULL == element)
238   {
239     GNUNET_assert (3 == iter_count);
240     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241                 "Iteration finished, destroying set %p\n",
242                 set);
243     GNUNET_SET_destroy (set);
244     return GNUNET_YES;
245   }
246   iter_count++;
247   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "iter: got element %u\n", iter_count);
248   return GNUNET_YES;
249 }
250
251
252 static void
253 test_iter ()
254 {
255   struct GNUNET_SET_Element element;
256   struct GNUNET_SET_Handle *iter_set;
257
258   iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
259   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260               "Testing iteration over 3 elements on set %p\n",
261               iter_set);
262   element.element_type = 0;
263
264   element.data = "hello";
265   element.size = strlen (element.data);
266   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
267   element.data = "bar";
268   element.size = strlen (element.data);
269   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
270   element.data = "quux";
271   element.size = strlen (element.data);
272   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
273   GNUNET_SET_iterate (iter_set, &iter_cb, iter_set);
274 }
275
276
277 /**
278  * Function run on timeout.
279  *
280  * @param cls closure
281  */
282 static void
283 timeout_fail (void *cls)
284 {
285   tt = NULL;
286   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Testcase failed with timeout\n");
287   GNUNET_SCHEDULER_shutdown ();
288   ret = 1;
289 }
290
291
292 /**
293  * Function run on shutdown.
294  *
295  * @param cls closure
296  */
297 static void
298 do_shutdown (void *cls)
299 {
300   if (NULL != tt)
301   {
302     GNUNET_SCHEDULER_cancel (tt);
303     tt = NULL;
304   }
305   if (NULL != oh1)
306   {
307     GNUNET_SET_operation_cancel (oh1);
308     oh1 = NULL;
309   }
310   if (NULL != oh2)
311   {
312     GNUNET_SET_operation_cancel (oh2);
313     oh2 = NULL;
314   }
315   if (NULL != set1)
316   {
317     GNUNET_SET_destroy (set1);
318     set1 = NULL;
319   }
320   if (NULL != set2)
321   {
322     GNUNET_SET_destroy (set2);
323     set2 = NULL;
324   }
325   if (NULL != listen_handle)
326   {
327     GNUNET_SET_listen_cancel (listen_handle);
328     listen_handle = NULL;
329   }
330 }
331
332
333 /**
334  * Signature of the 'main' function for a (single-peer) testcase that
335  * is run using 'GNUNET_TESTING_peer_run'.
336  *
337  * @param cls closure
338  * @param cfg configuration of the peer that was started
339  * @param peer identity of the peer that was created
340  */
341 static void
342 run (void *cls,
343      const struct GNUNET_CONFIGURATION_Handle *cfg,
344      struct GNUNET_TESTING_Peer *peer)
345 {
346   struct GNUNET_SET_OperationHandle *my_oh;
347
348   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running preparatory tests\n");
349   tt = GNUNET_SCHEDULER_add_delayed (
350     GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
351     &timeout_fail,
352     NULL);
353   GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
354
355   config = cfg;
356   GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_get_peer_identity (cfg, &local_id));
357   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358               "my id (from CRYPTO): %s\n",
359               GNUNET_i2s (&local_id));
360   GNUNET_TESTING_peer_get_identity (peer, &local_id);
361   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362               "my id (from TESTING): %s\n",
363               GNUNET_i2s (&local_id));
364   test_iter ();
365
366   set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
367   set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
368   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369               "Created sets %p and %p for union operation\n",
370               set1,
371               set2);
372   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
373
374   /* test if canceling an uncommited request works! */
375   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376               "Launching and instantly stopping set operation\n");
377   my_oh = GNUNET_SET_prepare (&local_id,
378                               &app_id,
379                               NULL,
380                               GNUNET_SET_RESULT_ADDED,
381                               (struct GNUNET_SET_Option[]){ 0 },
382                               NULL,
383                               NULL);
384   GNUNET_SET_operation_cancel (my_oh);
385
386   /* test the real set reconciliation */
387   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running real set-reconciliation\n");
388   init_set1 ();
389 }
390
391
392 int
393 main (int argc, char **argv)
394 {
395   GNUNET_log_setup ("test_set_api", "WARNING", NULL);
396   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Launching peer\n");
397   if (0 !=
398       GNUNET_TESTING_peer_run ("test_set_api", "test_set.conf", &run, NULL))
399   {
400     return 1;
401   }
402   return ret;
403 }