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_api.c
23 * @brief testcase for set_api.c
24 * @author Florian Dold
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_set_service.h"
32 static struct GNUNET_PeerIdentity local_id;
34 static struct GNUNET_HashCode app_id;
36 static struct GNUNET_SET_Handle *set1;
38 static struct GNUNET_SET_Handle *set2;
40 static struct GNUNET_SET_ListenHandle *listen_handle;
42 static struct GNUNET_SET_OperationHandle *oh1;
44 static struct GNUNET_SET_OperationHandle *oh2;
46 static const struct GNUNET_CONFIGURATION_Handle *config;
48 static unsigned int iter_count;
52 static struct GNUNET_SCHEDULER_Task *tt;
56 result_cb_set1 (void *cls,
57 const struct GNUNET_SET_Element *element,
59 enum GNUNET_SET_Status status)
63 case GNUNET_SET_STATUS_OK:
64 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
65 "set 1: got element\n");
67 case GNUNET_SET_STATUS_FAILURE:
71 "set 1: received failure status!\n");
75 GNUNET_SCHEDULER_cancel (tt);
78 GNUNET_SCHEDULER_shutdown ();
80 case GNUNET_SET_STATUS_DONE:
81 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86 GNUNET_SET_destroy (set1);
91 GNUNET_SCHEDULER_cancel (tt);
93 GNUNET_SCHEDULER_shutdown ();
103 result_cb_set2 (void *cls,
104 const struct GNUNET_SET_Element *element,
106 enum GNUNET_SET_Status status)
110 case GNUNET_SET_STATUS_OK:
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
112 "set 2: got element\n");
114 case GNUNET_SET_STATUS_FAILURE:
118 "set 2: received failure status\n");
119 GNUNET_SCHEDULER_shutdown ();
122 case GNUNET_SET_STATUS_DONE:
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
126 GNUNET_SET_destroy (set2);
130 GNUNET_SCHEDULER_cancel (tt);
132 GNUNET_SCHEDULER_shutdown ();
142 listen_cb (void *cls,
143 const struct GNUNET_PeerIdentity *other_peer,
144 const struct GNUNET_MessageHeader *context_msg,
145 struct GNUNET_SET_Request *request)
147 GNUNET_assert (NULL != context_msg);
148 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150 "listen cb called\n");
151 oh2 = GNUNET_SET_accept (request,
152 GNUNET_SET_RESULT_ADDED,
153 (struct GNUNET_SET_Option[]) { 0 },
156 GNUNET_SET_commit (oh2,
162 * Start the set operation.
164 * @param cls closure, unused
169 struct GNUNET_MessageHeader context_msg;
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172 "Starting reconciliation\n");
173 context_msg.size = htons (sizeof context_msg);
174 context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
175 listen_handle = GNUNET_SET_listen (config,
176 GNUNET_SET_OPERATION_UNION,
180 oh1 = GNUNET_SET_prepare (&local_id,
183 GNUNET_SET_RESULT_ADDED,
184 (struct GNUNET_SET_Option[]) { 0 },
187 GNUNET_SET_commit (oh1,
193 * Initialize the second set, continue
195 * @param cls closure, unused
198 init_set2 (void *cls)
200 struct GNUNET_SET_Element element;
202 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
203 "initializing set 2\n");
205 element.element_type = 0;
206 element.data = "hello";
207 element.size = strlen(element.data);
208 GNUNET_SET_add_element (set2,
211 element.data = "quux";
212 element.size = strlen(element.data);
213 GNUNET_SET_add_element (set2,
216 element.data = "baz";
217 element.size = strlen(element.data);
218 GNUNET_SET_add_element (set2,
225 * Initialize the first set, continue.
230 struct GNUNET_SET_Element element;
232 element.element_type = 0;
233 element.data = "hello";
234 element.size = strlen(element.data);
235 GNUNET_SET_add_element (set1,
238 element.data = "bar";
239 element.size = strlen(element.data);
240 GNUNET_SET_add_element (set1,
244 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
245 "initialized set 1\n");
251 const struct GNUNET_SET_Element *element)
253 struct GNUNET_SET_Handle *set = cls;
257 GNUNET_assert (3 == iter_count);
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "Iteration finished, destroying set %p\n",
261 GNUNET_SET_destroy (set);
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266 "iter: got element %u\n",
275 struct GNUNET_SET_Element element;
276 struct GNUNET_SET_Handle *iter_set;
278 iter_set = GNUNET_SET_create (config,
279 GNUNET_SET_OPERATION_UNION);
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Testing iteration over 3 elements on set %p\n",
283 element.element_type = 0;
285 element.data = "hello";
286 element.size = strlen(element.data);
287 GNUNET_SET_add_element (iter_set,
290 element.data = "bar";
291 element.size = strlen(element.data);
292 GNUNET_SET_add_element (iter_set,
295 element.data = "quux";
296 element.size = strlen(element.data);
297 GNUNET_SET_add_element (iter_set,
300 GNUNET_SET_iterate (iter_set,
307 * Function run on timeout.
312 timeout_fail (void *cls)
315 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
316 "Testcase failed with timeout\n");
317 GNUNET_SCHEDULER_shutdown ();
323 * Function run on shutdown.
328 do_shutdown (void *cls)
332 GNUNET_SCHEDULER_cancel (tt);
337 GNUNET_SET_operation_cancel (oh1);
342 GNUNET_SET_operation_cancel (oh2);
347 GNUNET_SET_destroy (set1);
352 GNUNET_SET_destroy (set2);
355 if (NULL != listen_handle)
357 GNUNET_SET_listen_cancel (listen_handle);
358 listen_handle = NULL;
364 * Signature of the 'main' function for a (single-peer) testcase that
365 * is run using 'GNUNET_TESTING_peer_run'.
368 * @param cfg configuration of the peer that was started
369 * @param peer identity of the peer that was created
373 const struct GNUNET_CONFIGURATION_Handle *cfg,
374 struct GNUNET_TESTING_Peer *peer)
377 struct GNUNET_SET_OperationHandle *my_oh;
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380 "Running preparatory tests\n");
381 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
384 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
388 GNUNET_CRYPTO_get_peer_identity (cfg, &local_id);
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
390 "my id (from CRYPTO): %s\n",
391 GNUNET_i2s (&local_id));
392 GNUNET_TESTING_peer_get_identity (peer, &local_id);
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "my id (from TESTING): %s\n",
395 GNUNET_i2s (&local_id));
398 set1 = GNUNET_SET_create (cfg,
399 GNUNET_SET_OPERATION_UNION);
400 set2 = GNUNET_SET_create (cfg,
401 GNUNET_SET_OPERATION_UNION);
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403 "Created sets %p and %p for union operation\n",
406 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
409 /* test if canceling an uncommited request works! */
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "Launching and instantly stopping set operation\n");
412 my_oh = GNUNET_SET_prepare (&local_id,
415 GNUNET_SET_RESULT_ADDED,
416 (struct GNUNET_SET_Option[]) { 0 },
419 GNUNET_SET_operation_cancel (my_oh);
421 /* test the real set reconciliation */
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 "Running real set-reconciliation\n");
429 main (int argc, char **argv)
431 GNUNET_log_setup ("test_set_api",
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436 if (0 != GNUNET_TESTING_peer_run ("test_set_api",