2 This file is part of GNUnet.
3 Copyright (C) 2012, 2016 GNUnet e.V.
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.
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.
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.
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
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testing_lib.h"
30 #include "gnunet_set_service.h"
34 * Value to return from #main().
38 static struct GNUNET_PeerIdentity local_id;
40 static struct GNUNET_HashCode app_id;
42 static struct GNUNET_SET_Handle *set1;
44 static struct GNUNET_SET_Handle *set2;
46 static struct GNUNET_SET_ListenHandle *listen_handle;
48 static const struct GNUNET_CONFIGURATION_Handle *config;
50 static struct GNUNET_SET_OperationHandle *oh1;
52 static struct GNUNET_SET_OperationHandle *oh2;
54 static int iter_count;
57 * Are we testing correctness for the empty set union?
62 * Number of elements found in set 1
64 static unsigned int count_set1;
67 * Number of elements found in set 2
69 static unsigned int count_set2;
72 * Task that is run when the test times out.
74 static struct GNUNET_SCHEDULER_Task *timeout_task;
78 result_cb_set1 (void *cls,
79 const struct GNUNET_SET_Element *element,
80 uint64_t current_size,
81 enum GNUNET_SET_Status status)
85 case GNUNET_SET_STATUS_ADD_LOCAL:
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
88 "set 1: got element\n");
90 case GNUNET_SET_STATUS_FAILURE:
91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
95 if (NULL != timeout_task)
97 GNUNET_SCHEDULER_cancel (timeout_task);
100 GNUNET_SCHEDULER_shutdown ();
102 case GNUNET_SET_STATUS_DONE:
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
106 GNUNET_SET_destroy (set1);
110 if (NULL != timeout_task)
112 GNUNET_SCHEDULER_cancel (timeout_task);
115 GNUNET_SCHEDULER_shutdown ();
118 case GNUNET_SET_STATUS_ADD_REMOTE:
127 result_cb_set2 (void *cls,
128 const struct GNUNET_SET_Element *element,
129 uint64_t current_size,
130 enum GNUNET_SET_Status status)
134 case GNUNET_SET_STATUS_ADD_LOCAL:
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137 "set 2: got element\n");
139 case GNUNET_SET_STATUS_FAILURE:
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
144 if (NULL != timeout_task)
146 GNUNET_SCHEDULER_cancel (timeout_task);
149 GNUNET_SCHEDULER_shutdown ();
151 case GNUNET_SET_STATUS_DONE:
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 GNUNET_SET_destroy (set2);
159 if (NULL != timeout_task)
161 GNUNET_SCHEDULER_cancel (timeout_task);
164 GNUNET_SCHEDULER_shutdown ();
167 case GNUNET_SET_STATUS_ADD_REMOTE:
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)
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 },
192 GNUNET_SET_commit (oh2,
198 * Start the set operation.
200 * @param cls closure, unused
205 struct GNUNET_MessageHeader context_msg;
207 context_msg.size = htons (sizeof context_msg);
208 context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
210 listen_handle = GNUNET_SET_listen (config,
211 GNUNET_SET_OPERATION_UNION,
214 oh1 = GNUNET_SET_prepare (&local_id,
217 GNUNET_SET_RESULT_SYMMETRIC,
218 (struct GNUNET_SET_Option[]) { 0 },
219 &result_cb_set1, NULL);
220 GNUNET_SET_commit (oh1, set1);
225 * Initialize the second set, continue
227 * @param cls closure, unused
230 init_set2 (void *cls)
232 struct GNUNET_SET_Element element;
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235 "initializing set 2\n");
241 element.element_type = 0;
242 element.data = "hello";
243 element.size = strlen(element.data);
244 GNUNET_SET_add_element (set2,
248 element.data = "quux";
249 element.size = strlen(element.data);
250 GNUNET_SET_add_element (set2,
254 element.data = "baz";
255 element.size = strlen(element.data);
256 GNUNET_SET_add_element (set2,
263 * Initialize the first set, continue.
268 struct GNUNET_SET_Element element;
275 element.element_type = 0;
276 element.data = "hello";
277 element.size = strlen(element.data);
278 GNUNET_SET_add_element (set1,
282 element.data = "bar";
283 element.size = strlen(element.data);
284 GNUNET_SET_add_element (set1,
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 "initialized set 1\n");
295 const struct GNUNET_SET_Element *element)
299 GNUNET_assert (iter_count == 3);
300 GNUNET_SET_destroy (cls);
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304 "iter: got element\n");
313 struct GNUNET_SET_Element element;
314 struct GNUNET_SET_Handle *iter_set;
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);
329 GNUNET_SET_iterate (iter_set,
336 * Signature of the main function of a task.
341 timeout_fail (void *cls)
344 GNUNET_SCHEDULER_shutdown ();
345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
352 * Function run on shutdown.
357 do_shutdown (void *cls)
359 if (NULL != timeout_task)
361 GNUNET_SCHEDULER_cancel (timeout_task);
366 GNUNET_SET_operation_cancel (oh1);
371 GNUNET_SET_operation_cancel (oh2);
376 GNUNET_SET_destroy (set1);
381 GNUNET_SET_destroy (set2);
384 if (NULL != listen_handle)
386 GNUNET_SET_listen_cancel (listen_handle);
387 listen_handle = NULL;
393 * Signature of the 'main' function for a (single-peer) testcase that
394 * is run using 'GNUNET_TESTING_peer_run'.
397 * @param cfg configuration of the peer that was started
398 * @param peer identity of the peer that was created
402 const struct GNUNET_CONFIGURATION_Handle *cfg,
403 struct GNUNET_TESTING_Peer *peer)
405 timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
408 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
411 GNUNET_TESTING_peer_get_identity (peer,
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);
421 /* test the real set reconciliation */
427 main (int argc, char **argv)
430 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
436 GNUNET_assert (0 == count_set1);
437 GNUNET_assert (0 == count_set2);
439 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
445 GNUNET_break (2 == count_set1);
446 GNUNET_break (1 == count_set2);