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");
121 case GNUNET_SET_STATUS_DONE:
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125 GNUNET_SET_destroy (set2);
129 GNUNET_SCHEDULER_cancel (tt);
131 GNUNET_SCHEDULER_shutdown ();
141 listen_cb (void *cls,
142 const struct GNUNET_PeerIdentity *other_peer,
143 const struct GNUNET_MessageHeader *context_msg,
144 struct GNUNET_SET_Request *request)
146 GNUNET_assert (NULL != context_msg);
147 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "listen cb called\n");
150 GNUNET_SET_listen_cancel (listen_handle);
151 listen_handle = NULL;
152 oh2 = GNUNET_SET_accept (request,
153 GNUNET_SET_RESULT_ADDED,
154 (struct GNUNET_SET_Option[]) { 0 },
157 GNUNET_SET_commit (oh2,
163 * Start the set operation.
165 * @param cls closure, unused
170 struct GNUNET_MessageHeader context_msg;
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "Starting reconciliation\n");
174 context_msg.size = htons (sizeof context_msg);
175 context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
176 listen_handle = GNUNET_SET_listen (config,
177 GNUNET_SET_OPERATION_UNION,
181 oh1 = GNUNET_SET_prepare (&local_id,
184 GNUNET_SET_RESULT_ADDED,
185 (struct GNUNET_SET_Option[]) { 0 },
188 GNUNET_SET_commit (oh1,
194 * Initialize the second set, continue
196 * @param cls closure, unused
199 init_set2 (void *cls)
201 struct GNUNET_SET_Element element;
203 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initializing set 2\n");
205 element.element_type = 0;
207 element.data = "hello";
208 element.size = strlen(element.data);
209 GNUNET_SET_add_element (set2, &element, NULL, NULL);
210 element.data = "quux";
211 element.size = strlen(element.data);
212 GNUNET_SET_add_element (set2, &element, NULL, NULL);
213 element.data = "baz";
214 element.size = strlen(element.data);
215 GNUNET_SET_add_element (set2, &element, &start, NULL);
220 * Initialize the first set, continue.
225 struct GNUNET_SET_Element element;
227 element.element_type = 0;
229 element.data = "hello";
230 element.size = strlen(element.data);
231 GNUNET_SET_add_element (set1, &element, NULL, NULL);
232 element.data = "bar";
233 element.size = strlen(element.data);
234 GNUNET_SET_add_element (set1, &element, init_set2, NULL);
236 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
237 "initialized set 1\n");
243 const struct GNUNET_SET_Element *element)
247 GNUNET_assert (3 == iter_count);
248 GNUNET_SET_destroy (cls);
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 "iter: got element %u\n",
262 struct GNUNET_SET_Element element;
263 struct GNUNET_SET_Handle *iter_set;
265 iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
267 element.element_type = 0;
269 element.data = "hello";
270 element.size = strlen(element.data);
271 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
272 element.data = "bar";
273 element.size = strlen(element.data);
274 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
275 element.data = "quux";
276 element.size = strlen(element.data);
277 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
279 GNUNET_SET_iterate (iter_set, iter_cb, iter_set);
284 * Function run on timeout.
289 timeout_fail (void *cls)
292 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
293 "Testcase failed with timeout\n");
294 GNUNET_SCHEDULER_shutdown ();
300 * Function run on shutdown.
305 do_shutdown (void *cls)
309 GNUNET_SCHEDULER_cancel (tt);
314 GNUNET_SET_operation_cancel (oh1);
319 GNUNET_SET_operation_cancel (oh2);
324 GNUNET_SET_destroy (set1);
329 GNUNET_SET_destroy (set2);
332 if (NULL != listen_handle)
334 GNUNET_SET_listen_cancel (listen_handle);
335 listen_handle = NULL;
341 * Signature of the 'main' function for a (single-peer) testcase that
342 * is run using 'GNUNET_TESTING_peer_run'.
345 * @param cfg configuration of the peer that was started
346 * @param peer identity of the peer that was created
350 const struct GNUNET_CONFIGURATION_Handle *cfg,
351 struct GNUNET_TESTING_Peer *peer)
354 struct GNUNET_SET_OperationHandle *my_oh;
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
357 "Running preparatory tests\n");
358 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
361 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
365 GNUNET_CRYPTO_get_peer_identity (cfg, &local_id);
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367 "my id (from CRYPTO): %s\n",
368 GNUNET_i2s (&local_id));
369 GNUNET_TESTING_peer_get_identity (peer, &local_id);
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "my id (from TESTING): %s\n",
372 GNUNET_i2s (&local_id));
375 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
376 set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
377 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
380 ///* test if canceling an uncommited request works! */
381 my_oh = GNUNET_SET_prepare (&local_id,
384 GNUNET_SET_RESULT_ADDED,
385 (struct GNUNET_SET_Option[]) { 0 },
389 GNUNET_SET_operation_cancel (my_oh);
391 /* test the real set reconciliation */
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
393 "Running real set-reconciliation\n");
399 main (int argc, char **argv)
401 GNUNET_log_setup ("test_set_api",
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 if (0 != GNUNET_TESTING_peer_run ("test_set_api",