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