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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file set/test_set_api.c
21 * @brief testcase for set_api.c
22 * @author Florian Dold
25 #include "gnunet_util_lib.h"
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_set_service.h"
30 static struct GNUNET_PeerIdentity local_id;
32 static struct GNUNET_HashCode app_id;
34 static struct GNUNET_SET_Handle *set1;
36 static struct GNUNET_SET_Handle *set2;
38 static struct GNUNET_SET_ListenHandle *listen_handle;
40 static struct GNUNET_SET_OperationHandle *oh1;
42 static struct GNUNET_SET_OperationHandle *oh2;
44 static const struct GNUNET_CONFIGURATION_Handle *config;
46 static unsigned int iter_count;
50 static struct GNUNET_SCHEDULER_Task *tt;
54 result_cb_set1 (void *cls,
55 const struct GNUNET_SET_Element *element,
57 enum GNUNET_SET_Status status)
61 case GNUNET_SET_STATUS_OK:
62 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
63 "set 1: got element\n");
65 case GNUNET_SET_STATUS_FAILURE:
69 "set 1: received failure status!\n");
73 GNUNET_SCHEDULER_cancel (tt);
76 GNUNET_SCHEDULER_shutdown ();
78 case GNUNET_SET_STATUS_DONE:
79 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84 GNUNET_SET_destroy (set1);
89 GNUNET_SCHEDULER_cancel (tt);
91 GNUNET_SCHEDULER_shutdown ();
101 result_cb_set2 (void *cls,
102 const struct GNUNET_SET_Element *element,
104 enum GNUNET_SET_Status status)
108 case GNUNET_SET_STATUS_OK:
109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
110 "set 2: got element\n");
112 case GNUNET_SET_STATUS_FAILURE:
116 "set 2: received failure status\n");
117 GNUNET_SCHEDULER_shutdown ();
120 case GNUNET_SET_STATUS_DONE:
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
124 GNUNET_SET_destroy (set2);
128 GNUNET_SCHEDULER_cancel (tt);
130 GNUNET_SCHEDULER_shutdown ();
140 listen_cb (void *cls,
141 const struct GNUNET_PeerIdentity *other_peer,
142 const struct GNUNET_MessageHeader *context_msg,
143 struct GNUNET_SET_Request *request)
145 GNUNET_assert (NULL != context_msg);
146 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148 "listen cb called\n");
149 oh2 = GNUNET_SET_accept (request,
150 GNUNET_SET_RESULT_ADDED,
151 (struct GNUNET_SET_Option[]) { 0 },
154 GNUNET_SET_commit (oh2,
160 * Start the set operation.
162 * @param cls closure, unused
167 struct GNUNET_MessageHeader context_msg;
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170 "Starting reconciliation\n");
171 context_msg.size = htons (sizeof context_msg);
172 context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
173 listen_handle = GNUNET_SET_listen (config,
174 GNUNET_SET_OPERATION_UNION,
178 oh1 = GNUNET_SET_prepare (&local_id,
181 GNUNET_SET_RESULT_ADDED,
182 (struct GNUNET_SET_Option[]) { 0 },
185 GNUNET_SET_commit (oh1,
191 * Initialize the second set, continue
193 * @param cls closure, unused
196 init_set2 (void *cls)
198 struct GNUNET_SET_Element element;
200 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
201 "initializing set 2\n");
203 element.element_type = 0;
204 element.data = "hello";
205 element.size = strlen(element.data);
206 GNUNET_SET_add_element (set2,
209 element.data = "quux";
210 element.size = strlen(element.data);
211 GNUNET_SET_add_element (set2,
214 element.data = "baz";
215 element.size = strlen(element.data);
216 GNUNET_SET_add_element (set2,
223 * Initialize the first set, continue.
228 struct GNUNET_SET_Element element;
230 element.element_type = 0;
231 element.data = "hello";
232 element.size = strlen(element.data);
233 GNUNET_SET_add_element (set1,
236 element.data = "bar";
237 element.size = strlen(element.data);
238 GNUNET_SET_add_element (set1,
242 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
243 "initialized set 1\n");
249 const struct GNUNET_SET_Element *element)
251 struct GNUNET_SET_Handle *set = cls;
255 GNUNET_assert (3 == iter_count);
256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
257 "Iteration finished, destroying set %p\n",
259 GNUNET_SET_destroy (set);
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264 "iter: got element %u\n",
273 struct GNUNET_SET_Element element;
274 struct GNUNET_SET_Handle *iter_set;
276 iter_set = GNUNET_SET_create (config,
277 GNUNET_SET_OPERATION_UNION);
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
279 "Testing iteration over 3 elements on set %p\n",
281 element.element_type = 0;
283 element.data = "hello";
284 element.size = strlen(element.data);
285 GNUNET_SET_add_element (iter_set,
288 element.data = "bar";
289 element.size = strlen(element.data);
290 GNUNET_SET_add_element (iter_set,
293 element.data = "quux";
294 element.size = strlen(element.data);
295 GNUNET_SET_add_element (iter_set,
298 GNUNET_SET_iterate (iter_set,
305 * Function run on timeout.
310 timeout_fail (void *cls)
313 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
314 "Testcase failed with timeout\n");
315 GNUNET_SCHEDULER_shutdown ();
321 * Function run on shutdown.
326 do_shutdown (void *cls)
330 GNUNET_SCHEDULER_cancel (tt);
335 GNUNET_SET_operation_cancel (oh1);
340 GNUNET_SET_operation_cancel (oh2);
345 GNUNET_SET_destroy (set1);
350 GNUNET_SET_destroy (set2);
353 if (NULL != listen_handle)
355 GNUNET_SET_listen_cancel (listen_handle);
356 listen_handle = NULL;
362 * Signature of the 'main' function for a (single-peer) testcase that
363 * is run using 'GNUNET_TESTING_peer_run'.
366 * @param cfg configuration of the peer that was started
367 * @param peer identity of the peer that was created
371 const struct GNUNET_CONFIGURATION_Handle *cfg,
372 struct GNUNET_TESTING_Peer *peer)
375 struct GNUNET_SET_OperationHandle *my_oh;
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "Running preparatory tests\n");
379 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
382 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
386 GNUNET_CRYPTO_get_peer_identity (cfg, &local_id);
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "my id (from CRYPTO): %s\n",
389 GNUNET_i2s (&local_id));
390 GNUNET_TESTING_peer_get_identity (peer, &local_id);
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392 "my id (from TESTING): %s\n",
393 GNUNET_i2s (&local_id));
396 set1 = GNUNET_SET_create (cfg,
397 GNUNET_SET_OPERATION_UNION);
398 set2 = GNUNET_SET_create (cfg,
399 GNUNET_SET_OPERATION_UNION);
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
401 "Created sets %p and %p for union operation\n",
404 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
407 /* test if canceling an uncommited request works! */
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "Launching and instantly stopping set operation\n");
410 my_oh = GNUNET_SET_prepare (&local_id,
413 GNUNET_SET_RESULT_ADDED,
414 (struct GNUNET_SET_Option[]) { 0 },
417 GNUNET_SET_operation_cancel (my_oh);
419 /* test the real set reconciliation */
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "Running real set-reconciliation\n");
427 main (int argc, char **argv)
429 GNUNET_log_setup ("test_set_api",
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434 if (0 != GNUNET_TESTING_peer_run ("test_set_api",