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 enum GNUNET_SET_Status status)
84 case GNUNET_SET_STATUS_ADD_LOCAL:
86 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
87 "set 1: got element\n");
89 case GNUNET_SET_STATUS_FAILURE:
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94 if (NULL != timeout_task)
96 GNUNET_SCHEDULER_cancel (timeout_task);
99 GNUNET_SCHEDULER_shutdown ();
101 case GNUNET_SET_STATUS_DONE:
103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
105 GNUNET_SET_destroy (set1);
109 if (NULL != timeout_task)
111 GNUNET_SCHEDULER_cancel (timeout_task);
114 GNUNET_SCHEDULER_shutdown ();
117 case GNUNET_SET_STATUS_ADD_REMOTE:
126 result_cb_set2 (void *cls,
127 const struct GNUNET_SET_Element *element,
128 enum GNUNET_SET_Status status)
132 case GNUNET_SET_STATUS_ADD_LOCAL:
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "set 2: got element\n");
137 case GNUNET_SET_STATUS_FAILURE:
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
142 if (NULL != timeout_task)
144 GNUNET_SCHEDULER_cancel (timeout_task);
147 GNUNET_SCHEDULER_shutdown ();
149 case GNUNET_SET_STATUS_DONE:
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153 GNUNET_SET_destroy (set2);
157 if (NULL != timeout_task)
159 GNUNET_SCHEDULER_cancel (timeout_task);
162 GNUNET_SCHEDULER_shutdown ();
165 case GNUNET_SET_STATUS_ADD_REMOTE:
174 listen_cb (void *cls,
175 const struct GNUNET_PeerIdentity *other_peer,
176 const struct GNUNET_MessageHeader *context_msg,
177 struct GNUNET_SET_Request *request)
179 GNUNET_assert (NULL != context_msg);
180 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
182 "listen cb called\n");
183 GNUNET_SET_listen_cancel (listen_handle);
184 listen_handle = NULL;
185 oh2 = GNUNET_SET_accept (request,
186 GNUNET_SET_RESULT_SYMMETRIC,
189 GNUNET_SET_commit (oh2,
195 * Start the set operation.
197 * @param cls closure, unused
202 struct GNUNET_MessageHeader context_msg;
204 context_msg.size = htons (sizeof context_msg);
205 context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
207 listen_handle = GNUNET_SET_listen (config,
208 GNUNET_SET_OPERATION_UNION,
211 oh1 = GNUNET_SET_prepare (&local_id,
214 GNUNET_SET_RESULT_SYMMETRIC,
215 &result_cb_set1, NULL);
216 GNUNET_SET_commit (oh1, set1);
221 * Initialize the second set, continue
223 * @param cls closure, unused
226 init_set2 (void *cls)
228 struct GNUNET_SET_Element element;
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
231 "initializing set 2\n");
237 element.element_type = 0;
238 element.data = "hello";
239 element.size = strlen(element.data);
240 GNUNET_SET_add_element (set2,
244 element.data = "quux";
245 element.size = strlen(element.data);
246 GNUNET_SET_add_element (set2,
250 element.data = "baz";
251 element.size = strlen(element.data);
252 GNUNET_SET_add_element (set2,
259 * Initialize the first set, continue.
264 struct GNUNET_SET_Element element;
271 element.element_type = 0;
272 element.data = "hello";
273 element.size = strlen(element.data);
274 GNUNET_SET_add_element (set1,
278 element.data = "bar";
279 element.size = strlen(element.data);
280 GNUNET_SET_add_element (set1,
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "initialized set 1\n");
291 const struct GNUNET_SET_Element *element)
295 GNUNET_assert (iter_count == 3);
296 GNUNET_SET_destroy (cls);
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "iter: got element\n");
309 struct GNUNET_SET_Element element;
310 struct GNUNET_SET_Handle *iter_set;
313 iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
314 element.element_type = 0;
315 element.data = "hello";
316 element.size = strlen(element.data);
317 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
318 element.data = "bar";
319 element.size = strlen(element.data);
320 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
321 element.data = "quux";
322 element.size = strlen(element.data);
323 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
325 GNUNET_SET_iterate (iter_set,
332 * Signature of the main function of a task.
337 timeout_fail (void *cls)
340 GNUNET_SCHEDULER_shutdown ();
341 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
348 * Function run on shutdown.
353 do_shutdown (void *cls)
355 if (NULL != timeout_task)
357 GNUNET_SCHEDULER_cancel (timeout_task);
362 GNUNET_SET_operation_cancel (oh1);
367 GNUNET_SET_operation_cancel (oh2);
372 GNUNET_SET_destroy (set1);
377 GNUNET_SET_destroy (set2);
380 if (NULL != listen_handle)
382 GNUNET_SET_listen_cancel (listen_handle);
383 listen_handle = NULL;
389 * Signature of the 'main' function for a (single-peer) testcase that
390 * is run using 'GNUNET_TESTING_peer_run'.
393 * @param cfg configuration of the peer that was started
394 * @param peer identity of the peer that was created
398 const struct GNUNET_CONFIGURATION_Handle *cfg,
399 struct GNUNET_TESTING_Peer *peer)
401 timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
404 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
407 GNUNET_TESTING_peer_get_identity (peer,
413 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
414 set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
415 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
417 /* test the real set reconciliation */
423 main (int argc, char **argv)
426 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
432 GNUNET_assert (0 == count_set1);
433 GNUNET_assert (0 == count_set2);
435 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
441 GNUNET_break (2 == count_set1);
442 GNUNET_break (1 == count_set2);