2 This file is part of GNUnet.
3 Copyright (C) 2012 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
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_set_service.h"
32 * Value to return from #main().
36 static struct GNUNET_PeerIdentity local_id;
38 static struct GNUNET_HashCode app_id;
39 static struct GNUNET_SET_Handle *set1;
41 static struct GNUNET_SET_Handle *set2;
43 static struct GNUNET_SET_ListenHandle *listen_handle;
45 static const struct GNUNET_CONFIGURATION_Handle *config;
47 static int iter_count;
50 * Are we testing correctness for the empty set union?
55 * Number of elements found in set 1
57 static unsigned int count_set1;
60 * Number of elements found in set 2
62 static unsigned int count_set2;
65 * Task that is run when the test times out.
67 static struct GNUNET_SCHEDULER_Task *timeout_task;
71 result_cb_set1 (void *cls,
72 const struct GNUNET_SET_Element *element,
73 enum GNUNET_SET_Status status)
77 case GNUNET_SET_STATUS_ADD_LOCAL:
79 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
80 "set 1: got element\n");
82 case GNUNET_SET_STATUS_FAILURE:
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86 if (NULL != timeout_task)
88 GNUNET_SCHEDULER_cancel (timeout_task);
91 GNUNET_SCHEDULER_shutdown ();
93 case GNUNET_SET_STATUS_DONE:
94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96 GNUNET_SET_destroy (set1);
100 if (NULL != timeout_task)
102 GNUNET_SCHEDULER_cancel (timeout_task);
105 GNUNET_SCHEDULER_shutdown ();
108 case GNUNET_SET_STATUS_ADD_REMOTE:
117 result_cb_set2 (void *cls,
118 const struct GNUNET_SET_Element *element,
119 enum GNUNET_SET_Status status)
123 case GNUNET_SET_STATUS_ADD_LOCAL:
125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
126 "set 2: got element\n");
128 case GNUNET_SET_STATUS_FAILURE:
129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132 if (NULL != timeout_task)
134 GNUNET_SCHEDULER_cancel (timeout_task);
137 GNUNET_SCHEDULER_shutdown ();
139 case GNUNET_SET_STATUS_DONE:
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
142 GNUNET_SET_destroy (set2);
146 if (NULL != timeout_task)
148 GNUNET_SCHEDULER_cancel (timeout_task);
151 GNUNET_SCHEDULER_shutdown ();
154 case GNUNET_SET_STATUS_ADD_REMOTE:
163 listen_cb (void *cls,
164 const struct GNUNET_PeerIdentity *other_peer,
165 const struct GNUNET_MessageHeader *context_msg,
166 struct GNUNET_SET_Request *request)
168 struct GNUNET_SET_OperationHandle *oh;
170 GNUNET_assert (NULL != context_msg);
171 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_TEST);
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "listen cb called\n");
174 GNUNET_SET_listen_cancel (listen_handle);
175 oh = GNUNET_SET_accept (request,
176 GNUNET_SET_RESULT_SYMMETRIC,
179 GNUNET_SET_commit (oh, set2);
184 * Start the set operation.
186 * @param cls closure, unused
191 struct GNUNET_SET_OperationHandle *oh;
192 struct GNUNET_MessageHeader context_msg;
194 context_msg.size = htons (sizeof context_msg);
195 context_msg.type = htons (GNUNET_MESSAGE_TYPE_TEST);
197 listen_handle = GNUNET_SET_listen (config,
198 GNUNET_SET_OPERATION_UNION,
201 oh = GNUNET_SET_prepare (&local_id,
204 GNUNET_SET_RESULT_SYMMETRIC,
205 &result_cb_set1, NULL);
206 GNUNET_SET_commit (oh, set1);
211 * Initialize the second set, continue
213 * @param cls closure, unused
216 init_set2 (void *cls)
218 struct GNUNET_SET_Element element;
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221 "initializing set 2\n");
227 element.element_type = 0;
228 element.data = "hello";
229 element.size = strlen(element.data);
230 GNUNET_SET_add_element (set2,
234 element.data = "quux";
235 element.size = strlen(element.data);
236 GNUNET_SET_add_element (set2,
240 element.data = "baz";
241 element.size = strlen(element.data);
242 GNUNET_SET_add_element (set2,
249 * Initialize the first set, continue.
254 struct GNUNET_SET_Element element;
261 element.element_type = 0;
262 element.data = "hello";
263 element.size = strlen(element.data);
264 GNUNET_SET_add_element (set1,
268 element.data = "bar";
269 element.size = strlen(element.data);
270 GNUNET_SET_add_element (set1,
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275 "initialized set 1\n");
281 const struct GNUNET_SET_Element *element)
285 GNUNET_assert (iter_count == 3);
286 GNUNET_SET_destroy (cls);
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290 "iter: got element\n");
299 struct GNUNET_SET_Element element;
300 struct GNUNET_SET_Handle *iter_set;
303 iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
304 element.element_type = 0;
305 element.data = "hello";
306 element.size = strlen(element.data);
307 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
308 element.data = "bar";
309 element.size = strlen(element.data);
310 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
311 element.data = "quux";
312 element.size = strlen(element.data);
313 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
315 GNUNET_SET_iterate (iter_set,
322 * Signature of the main function of a task.
327 timeout_fail (void *cls)
330 GNUNET_SCHEDULER_shutdown ();
331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
338 * Signature of the 'main' function for a (single-peer) testcase that
339 * is run using 'GNUNET_TESTING_peer_run'.
342 * @param cfg configuration of the peer that was started
343 * @param peer identity of the peer that was created
347 const struct GNUNET_CONFIGURATION_Handle *cfg,
348 struct GNUNET_TESTING_Peer *peer)
350 timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
355 GNUNET_TESTING_peer_get_identity (peer,
360 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
361 set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
362 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
364 /* test the real set reconciliation */
370 main (int argc, char **argv)
373 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
379 GNUNET_assert (0 == count_set1);
380 GNUNET_assert (0 == count_set2);
382 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
388 GNUNET_assert (2 == count_set1);
389 GNUNET_assert (1 == count_set2);