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
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_set_service.h"
31 static struct GNUNET_PeerIdentity local_id;
33 static struct GNUNET_HashCode app_id;
35 static struct GNUNET_SET_Handle *set1;
37 static struct GNUNET_SET_Handle *set2;
39 static struct GNUNET_SET_ListenHandle *listen_handle;
41 static const struct GNUNET_CONFIGURATION_Handle *config;
43 static unsigned int iter_count;
47 static struct GNUNET_SCHEDULER_Task *tt;
51 result_cb_set1 (void *cls,
52 const struct GNUNET_SET_Element *element,
53 enum GNUNET_SET_Status status)
57 case GNUNET_SET_STATUS_OK:
58 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
59 "set 1: got element\n");
61 case GNUNET_SET_STATUS_FAILURE:
64 "set 1: received failure status!\n");
66 GNUNET_SCHEDULER_cancel (tt);
68 GNUNET_SCHEDULER_shutdown ();
70 case GNUNET_SET_STATUS_DONE:
71 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
73 GNUNET_SET_destroy (set1);
77 GNUNET_SCHEDULER_cancel (tt);
79 GNUNET_SCHEDULER_shutdown ();
89 result_cb_set2 (void *cls,
90 const struct GNUNET_SET_Element *element,
91 enum GNUNET_SET_Status status)
95 case GNUNET_SET_STATUS_OK:
96 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
97 "set 2: got element\n");
99 case GNUNET_SET_STATUS_FAILURE:
102 "set 2: received failure status\n");
105 case GNUNET_SET_STATUS_DONE:
106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
108 GNUNET_SET_destroy (set2);
112 GNUNET_SCHEDULER_cancel (tt);
114 GNUNET_SCHEDULER_shutdown ();
124 listen_cb (void *cls,
125 const struct GNUNET_PeerIdentity *other_peer,
126 const struct GNUNET_MessageHeader *context_msg,
127 struct GNUNET_SET_Request *request)
129 struct GNUNET_SET_OperationHandle *oh;
131 GNUNET_assert (NULL != context_msg);
133 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_TEST);
135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136 "listen cb called\n");
137 GNUNET_SET_listen_cancel (listen_handle);
138 oh = GNUNET_SET_accept (request,
139 GNUNET_SET_RESULT_ADDED,
140 &result_cb_set2, NULL);
141 GNUNET_SET_commit (oh, set2);
146 * Start the set operation.
148 * @param cls closure, unused
153 struct GNUNET_SET_OperationHandle *oh;
154 struct GNUNET_MessageHeader context_msg;
156 context_msg.size = htons (sizeof context_msg);
157 context_msg.type = htons (GNUNET_MESSAGE_TYPE_TEST);
159 listen_handle = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
160 &app_id, listen_cb, NULL);
161 oh = GNUNET_SET_prepare (&local_id, &app_id, &context_msg,
162 GNUNET_SET_RESULT_ADDED,
163 result_cb_set1, NULL);
164 GNUNET_SET_commit (oh, set1);
169 * Initialize the second set, continue
171 * @param cls closure, unused
174 init_set2 (void *cls)
176 struct GNUNET_SET_Element element;
178 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initializing set 2\n");
180 element.element_type = 0;
182 element.data = "hello";
183 element.size = strlen(element.data);
184 GNUNET_SET_add_element (set2, &element, NULL, NULL);
185 element.data = "quux";
186 element.size = strlen(element.data);
187 GNUNET_SET_add_element (set2, &element, NULL, NULL);
188 element.data = "baz";
189 element.size = strlen(element.data);
190 GNUNET_SET_add_element (set2, &element, start, NULL);
195 * Initialize the first set, continue.
200 struct GNUNET_SET_Element element;
202 element.element_type = 0;
204 element.data = "hello";
205 element.size = strlen(element.data);
206 GNUNET_SET_add_element (set1, &element, NULL, NULL);
207 element.data = "bar";
208 element.size = strlen(element.data);
209 GNUNET_SET_add_element (set1, &element, init_set2, NULL);
211 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initialized set 1\n");
217 const struct GNUNET_SET_Element *element)
221 GNUNET_assert (3 == iter_count);
222 GNUNET_SET_destroy (cls);
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227 "iter: got element %u\n",
236 struct GNUNET_SET_Element element;
237 struct GNUNET_SET_Handle *iter_set;
239 iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
241 element.element_type = 0;
243 element.data = "hello";
244 element.size = strlen(element.data);
245 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
246 element.data = "bar";
247 element.size = strlen(element.data);
248 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
249 element.data = "quux";
250 element.size = strlen(element.data);
251 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
253 GNUNET_SET_iterate (iter_set, iter_cb, iter_set);
258 * Signature of the main function of a task.
263 timeout_fail (void *cls)
266 GNUNET_SCHEDULER_shutdown ();
272 * Signature of the 'main' function for a (single-peer) testcase that
273 * is run using 'GNUNET_TESTING_peer_run'.
276 * @param cfg configuration of the peer that was started
277 * @param peer identity of the peer that was created
281 const struct GNUNET_CONFIGURATION_Handle *cfg,
282 struct GNUNET_TESTING_Peer *peer)
285 struct GNUNET_SET_OperationHandle *my_oh;
287 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
288 &timeout_fail, NULL);
291 GNUNET_CRYPTO_get_peer_identity (cfg, &local_id);
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "my id (from CRYPTO): %s\n",
294 GNUNET_i2s (&local_id));
295 GNUNET_TESTING_peer_get_identity (peer, &local_id);
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
297 "my id (from TESTING): %s\n",
298 GNUNET_i2s (&local_id));
301 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
302 set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
303 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
306 ///* test if canceling an uncommited request works! */
307 my_oh = GNUNET_SET_prepare (&local_id, &app_id, NULL,
308 GNUNET_SET_RESULT_ADDED, NULL, NULL);
310 GNUNET_SET_operation_cancel (my_oh);
312 /* test the real set reconciliation */
318 main (int argc, char **argv)
320 if (0 != GNUNET_TESTING_peer_run ("test_set_api",