2 This file is part of GNUnet.
3 Copyright (C) 2012 Christian Grothoff (and other contributing authors)
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_full.c
23 * @brief testcase for full 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;
66 result_cb_set1 (void *cls,
67 const struct GNUNET_SET_Element *element,
68 enum GNUNET_SET_Status status)
72 case GNUNET_SET_STATUS_OK:
74 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
75 "set 1: got element\n");
77 case GNUNET_SET_STATUS_FAILURE:
78 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81 GNUNET_SCHEDULER_shutdown ();
83 case GNUNET_SET_STATUS_DONE:
84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86 GNUNET_SET_destroy (set1);
89 GNUNET_SCHEDULER_shutdown ();
98 result_cb_set2 (void *cls,
99 const struct GNUNET_SET_Element *element,
100 enum GNUNET_SET_Status status)
104 case GNUNET_SET_STATUS_OK:
106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
107 "set 2: got element\n");
109 case GNUNET_SET_STATUS_FAILURE:
110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
113 GNUNET_SCHEDULER_shutdown ();
115 case GNUNET_SET_STATUS_DONE:
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
118 GNUNET_SET_destroy (set2);
121 GNUNET_SCHEDULER_shutdown ();
130 listen_cb (void *cls,
131 const struct GNUNET_PeerIdentity *other_peer,
132 const struct GNUNET_MessageHeader *context_msg,
133 struct GNUNET_SET_Request *request)
135 struct GNUNET_SET_OperationHandle *oh;
137 GNUNET_assert (NULL != context_msg);
138 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_TEST);
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
140 "listen cb called\n");
141 GNUNET_SET_listen_cancel (listen_handle);
142 oh = GNUNET_SET_accept (request,
143 GNUNET_SET_RESULT_FULL,
146 GNUNET_SET_commit (oh, set2);
151 * Start the set operation.
153 * @param cls closure, unused
158 struct GNUNET_SET_OperationHandle *oh;
159 struct GNUNET_MessageHeader context_msg;
161 context_msg.size = htons (sizeof context_msg);
162 context_msg.type = htons (GNUNET_MESSAGE_TYPE_TEST);
164 listen_handle = GNUNET_SET_listen (config,
165 GNUNET_SET_OPERATION_UNION,
168 oh = GNUNET_SET_prepare (&local_id,
171 GNUNET_SET_RESULT_FULL,
172 &result_cb_set1, NULL);
173 GNUNET_SET_commit (oh, set1);
178 * Initialize the second set, continue
180 * @param cls closure, unused
183 init_set2 (void *cls)
185 struct GNUNET_SET_Element element;
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188 "initializing set 2\n");
194 element.element_type = 0;
195 element.data = "hello";
196 element.size = strlen(element.data);
197 GNUNET_SET_add_element (set2,
201 element.data = "quux";
202 element.size = strlen(element.data);
203 GNUNET_SET_add_element (set2,
207 element.data = "baz";
208 element.size = strlen(element.data);
209 GNUNET_SET_add_element (set2,
216 * Initialize the first set, continue.
221 struct GNUNET_SET_Element element;
228 element.element_type = 0;
229 element.data = "hello";
230 element.size = strlen(element.data);
231 GNUNET_SET_add_element (set1,
235 element.data = "bar";
236 element.size = strlen(element.data);
237 GNUNET_SET_add_element (set1,
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242 "initialized set 1\n");
248 const struct GNUNET_SET_Element *element)
252 GNUNET_assert (iter_count == 3);
253 GNUNET_SET_destroy (cls);
256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
257 "iter: got element\n");
266 struct GNUNET_SET_Element element;
267 struct GNUNET_SET_Handle *iter_set;
270 iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
271 element.element_type = 0;
272 element.data = "hello";
273 element.size = strlen(element.data);
274 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
275 element.data = "bar";
276 element.size = strlen(element.data);
277 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
278 element.data = "quux";
279 element.size = strlen(element.data);
280 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
282 GNUNET_SET_iterate (iter_set,
289 * Signature of the main function of a task.
292 * @param tc context information (why was this task triggered now)
295 timeout_fail (void *cls,
296 const struct GNUNET_SCHEDULER_TaskContext *tc)
298 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
300 GNUNET_SCHEDULER_shutdown ();
306 * Signature of the 'main' function for a (single-peer) testcase that
307 * is run using 'GNUNET_TESTING_peer_run'.
310 * @param cfg configuration of the peer that was started
311 * @param peer identity of the peer that was created
315 const struct GNUNET_CONFIGURATION_Handle *cfg,
316 struct GNUNET_TESTING_Peer *peer)
318 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
323 GNUNET_TESTING_peer_get_identity (peer,
328 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
329 set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
330 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
332 /* test the real set reconciliation */
338 main (int argc, char **argv)
341 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
347 GNUNET_assert (0 == count_set1);
348 GNUNET_assert (0 == count_set2);
350 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
356 GNUNET_assert (4 == count_set1);
357 GNUNET_assert (4 == count_set2);