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