- merge with master
[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   GNUNET_SET_listen_cancel (listen_handle);
186   listen_handle = NULL;
187   oh2 = GNUNET_SET_accept (request,
188                            GNUNET_SET_RESULT_SYMMETRIC,
189                            (struct GNUNET_SET_Option[]) { 0 },
190                            &result_cb_set2,
191                            NULL);
192   GNUNET_SET_commit (oh2,
193                      set2);
194 }
195
196
197 /**
198  * Start the set operation.
199  *
200  * @param cls closure, unused
201  */
202 static void
203 start (void *cls)
204 {
205   struct GNUNET_MessageHeader context_msg;
206
207   context_msg.size = htons (sizeof context_msg);
208   context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
209
210   listen_handle = GNUNET_SET_listen (config,
211                                      GNUNET_SET_OPERATION_UNION,
212                                      &app_id,
213                                      &listen_cb, NULL);
214   oh1 = GNUNET_SET_prepare (&local_id,
215                             &app_id,
216                             &context_msg,
217                             GNUNET_SET_RESULT_SYMMETRIC,
218                             (struct GNUNET_SET_Option[]) { 0 },
219                             &result_cb_set1, NULL);
220   GNUNET_SET_commit (oh1, set1);
221 }
222
223
224 /**
225  * Initialize the second set, continue
226  *
227  * @param cls closure, unused
228  */
229 static void
230 init_set2 (void *cls)
231 {
232   struct GNUNET_SET_Element element;
233
234   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235               "initializing set 2\n");
236   if (empty)
237   {
238     start (NULL);
239     return;
240   }
241   element.element_type = 0;
242   element.data = "hello";
243   element.size = strlen(element.data);
244   GNUNET_SET_add_element (set2,
245                           &element,
246                           NULL,
247                           NULL);
248   element.data = "quux";
249   element.size = strlen(element.data);
250   GNUNET_SET_add_element (set2,
251                           &element,
252                           NULL,
253                           NULL);
254   element.data = "baz";
255   element.size = strlen(element.data);
256   GNUNET_SET_add_element (set2,
257                           &element,
258                           &start, NULL);
259 }
260
261
262 /**
263  * Initialize the first set, continue.
264  */
265 static void
266 init_set1 (void)
267 {
268   struct GNUNET_SET_Element element;
269
270   if (empty)
271   {
272     init_set2 (NULL);
273     return;
274   }
275   element.element_type = 0;
276   element.data = "hello";
277   element.size = strlen(element.data);
278   GNUNET_SET_add_element (set1,
279                           &element,
280                           NULL,
281                           NULL);
282   element.data = "bar";
283   element.size = strlen(element.data);
284   GNUNET_SET_add_element (set1,
285                           &element,
286                           &init_set2,
287                           NULL);
288   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289               "initialized set 1\n");
290 }
291
292
293 static int
294 iter_cb (void *cls,
295          const struct GNUNET_SET_Element *element)
296 {
297   if (NULL == element)
298   {
299     GNUNET_assert (iter_count == 3);
300     GNUNET_SET_destroy (cls);
301     return GNUNET_YES;
302   }
303   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304               "iter: got element\n");
305   iter_count++;
306   return GNUNET_YES;
307 }
308
309
310 static void
311 test_iter ()
312 {
313   struct GNUNET_SET_Element element;
314   struct GNUNET_SET_Handle *iter_set;
315
316   iter_count = 0;
317   iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
318   element.element_type = 0;
319   element.data = "hello";
320   element.size = strlen(element.data);
321   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
322   element.data = "bar";
323   element.size = strlen(element.data);
324   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
325   element.data = "quux";
326   element.size = strlen(element.data);
327   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
328
329   GNUNET_SET_iterate (iter_set,
330                       &iter_cb,
331                       iter_set);
332 }
333
334
335 /**
336  * Signature of the main function of a task.
337  *
338  * @param cls closure
339  */
340 static void
341 timeout_fail (void *cls)
342 {
343   timeout_task = NULL;
344   GNUNET_SCHEDULER_shutdown ();
345   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
346               "test timed out\n");
347   ret = 1;
348 }
349
350
351 /**
352  * Function run on shutdown.
353  *
354  * @param cls closure
355  */
356 static void
357 do_shutdown (void *cls)
358 {
359   if (NULL != timeout_task)
360   {
361     GNUNET_SCHEDULER_cancel (timeout_task);
362     timeout_task = NULL;
363   }
364   if (NULL != oh1)
365   {
366     GNUNET_SET_operation_cancel (oh1);
367     oh1 = NULL;
368   }
369   if (NULL != oh2)
370   {
371     GNUNET_SET_operation_cancel (oh2);
372     oh2 = NULL;
373   }
374   if (NULL != set1)
375   {
376     GNUNET_SET_destroy (set1);
377     set1 = NULL;
378   }
379   if (NULL != set2)
380   {
381     GNUNET_SET_destroy (set2);
382     set2 = NULL;
383   }
384   if (NULL != listen_handle)
385   {
386     GNUNET_SET_listen_cancel (listen_handle);
387     listen_handle = NULL;
388   }
389 }
390
391
392 /**
393  * Signature of the 'main' function for a (single-peer) testcase that
394  * is run using 'GNUNET_TESTING_peer_run'.
395  *
396  * @param cls closure
397  * @param cfg configuration of the peer that was started
398  * @param peer identity of the peer that was created
399  */
400 static void
401 run (void *cls,
402      const struct GNUNET_CONFIGURATION_Handle *cfg,
403      struct GNUNET_TESTING_Peer *peer)
404 {
405   timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
406                                                &timeout_fail,
407                                                NULL);
408   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
409                                  NULL);
410   config = cfg;
411   GNUNET_TESTING_peer_get_identity (peer,
412                                     &local_id);
413
414   if (0)
415     test_iter ();
416
417   set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
418   set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
419   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
420
421   /* test the real set reconciliation */
422   init_set1 ();
423 }
424
425
426 int
427 main (int argc, char **argv)
428 {
429   empty = 1;
430   if (0 != GNUNET_TESTING_peer_run ("test_set_api",
431                                     "test_set.conf",
432                                     &run, NULL))
433   {
434     return 1;
435   }
436   GNUNET_assert (0 == count_set1);
437   GNUNET_assert (0 == count_set2);
438   empty = 0;
439   if (0 != GNUNET_TESTING_peer_run ("test_set_api",
440                                     "test_set.conf",
441                                     &run, NULL))
442   {
443     return 1;
444   }
445   GNUNET_break (2 == count_set1);
446   GNUNET_break (1 == count_set2);
447   return ret;
448 }