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 oh2 = GNUNET_SET_accept (request,
186 GNUNET_SET_RESULT_SYMMETRIC,
187 (struct GNUNET_SET_Option[]) { 0 },
190 GNUNET_SET_commit (oh2,
196 * Start the set operation.
198 * @param cls closure, unused
203 struct GNUNET_MessageHeader context_msg;
205 context_msg.size = htons (sizeof context_msg);
206 context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
208 listen_handle = GNUNET_SET_listen (config,
209 GNUNET_SET_OPERATION_UNION,
212 oh1 = GNUNET_SET_prepare (&local_id,
215 GNUNET_SET_RESULT_SYMMETRIC,
216 (struct GNUNET_SET_Option[]) { 0 },
217 &result_cb_set1, NULL);
218 GNUNET_SET_commit (oh1, set1);
223 * Initialize the second set, continue
225 * @param cls closure, unused
228 init_set2 (void *cls)
230 struct GNUNET_SET_Element element;
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "initializing set 2\n");
239 element.element_type = 0;
240 element.data = "hello";
241 element.size = strlen(element.data);
242 GNUNET_SET_add_element (set2,
246 element.data = "quux";
247 element.size = strlen(element.data);
248 GNUNET_SET_add_element (set2,
252 element.data = "baz";
253 element.size = strlen(element.data);
254 GNUNET_SET_add_element (set2,
261 * Initialize the first set, continue.
266 struct GNUNET_SET_Element element;
273 element.element_type = 0;
274 element.data = "hello";
275 element.size = strlen(element.data);
276 GNUNET_SET_add_element (set1,
280 element.data = "bar";
281 element.size = strlen(element.data);
282 GNUNET_SET_add_element (set1,
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
287 "initialized set 1\n");
293 const struct GNUNET_SET_Element *element)
297 GNUNET_assert (iter_count == 3);
298 GNUNET_SET_destroy (cls);
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "iter: got element\n");
311 struct GNUNET_SET_Element element;
312 struct GNUNET_SET_Handle *iter_set;
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);
327 GNUNET_SET_iterate (iter_set,
334 * Signature of the main function of a task.
339 timeout_fail (void *cls)
342 GNUNET_SCHEDULER_shutdown ();
343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
350 * Function run on shutdown.
355 do_shutdown (void *cls)
357 if (NULL != timeout_task)
359 GNUNET_SCHEDULER_cancel (timeout_task);
364 GNUNET_SET_operation_cancel (oh1);
369 GNUNET_SET_operation_cancel (oh2);
374 GNUNET_SET_destroy (set1);
379 GNUNET_SET_destroy (set2);
382 if (NULL != listen_handle)
384 GNUNET_SET_listen_cancel (listen_handle);
385 listen_handle = NULL;
391 * Signature of the 'main' function for a (single-peer) testcase that
392 * is run using 'GNUNET_TESTING_peer_run'.
395 * @param cfg configuration of the peer that was started
396 * @param peer identity of the peer that was created
400 const struct GNUNET_CONFIGURATION_Handle *cfg,
401 struct GNUNET_TESTING_Peer *peer)
403 timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
406 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
409 GNUNET_TESTING_peer_get_identity (peer,
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);
419 /* test the real set reconciliation */
425 main (int argc, char **argv)
428 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
434 GNUNET_assert (0 == count_set1);
435 GNUNET_assert (0 == count_set2);
437 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
443 GNUNET_break (2 == count_set1);
444 GNUNET_break (1 == count_set2);