2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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;
34 static struct GNUNET_SET_Handle *set1;
35 static struct GNUNET_SET_Handle *set2;
36 static struct GNUNET_SET_ListenHandle *listen_handle;
37 const static struct GNUNET_CONFIGURATION_Handle *config;
39 static int iter_count;
45 result_cb_set1 (void *cls, const struct GNUNET_SET_Element *element,
46 enum GNUNET_SET_Status status)
50 case GNUNET_SET_STATUS_OK:
51 printf ("set 1: got element\n");
53 case GNUNET_SET_STATUS_FAILURE:
54 printf ("set 1: failure\n");
56 GNUNET_SCHEDULER_shutdown ();
58 case GNUNET_SET_STATUS_DONE:
59 printf ("set 1: done\n");
60 GNUNET_SET_destroy (set1);
63 GNUNET_SCHEDULER_shutdown ();
72 result_cb_set2 (void *cls, const struct GNUNET_SET_Element *element,
73 enum GNUNET_SET_Status status)
77 case GNUNET_SET_STATUS_OK:
78 printf ("set 2: got element\n");
80 case GNUNET_SET_STATUS_FAILURE:
81 printf ("set 2: failure\n");
84 case GNUNET_SET_STATUS_DONE:
85 printf ("set 2: done\n");
86 GNUNET_SET_destroy (set2);
89 GNUNET_SCHEDULER_shutdown ();
99 const struct GNUNET_PeerIdentity *other_peer,
100 const struct GNUNET_MessageHeader *context_msg,
101 struct GNUNET_SET_Request *request)
103 struct GNUNET_SET_OperationHandle *oh;
105 GNUNET_assert (NULL != context_msg);
107 GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_TEST);
109 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "listen cb called\n");
110 GNUNET_SET_listen_cancel (listen_handle);
112 oh = GNUNET_SET_accept (request, GNUNET_SET_RESULT_ADDED, result_cb_set2, NULL);
113 GNUNET_SET_commit (oh, set2);
118 * Start the set operation.
120 * @param cls closure, unused
125 struct GNUNET_SET_OperationHandle *oh;
126 struct GNUNET_MessageHeader context_msg;
128 context_msg.size = htons (sizeof context_msg);
129 context_msg.type = htons (GNUNET_MESSAGE_TYPE_TEST);
131 listen_handle = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
132 &app_id, listen_cb, NULL);
133 oh = GNUNET_SET_prepare (&local_id, &app_id, &context_msg,
134 GNUNET_SET_RESULT_ADDED,
135 result_cb_set1, NULL);
136 GNUNET_SET_commit (oh, set1);
141 * Initialize the second set, continue
143 * @param cls closure, unused
146 init_set2 (void *cls)
148 struct GNUNET_SET_Element element;
150 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initializing set 2\n");
152 element.element_type = 0;
154 element.data = "hello";
155 element.size = strlen(element.data);
156 GNUNET_SET_add_element (set2, &element, NULL, NULL);
157 element.data = "quux";
158 element.size = strlen(element.data);
159 GNUNET_SET_add_element (set2, &element, NULL, NULL);
160 element.data = "baz";
161 element.size = strlen(element.data);
162 GNUNET_SET_add_element (set2, &element, start, NULL);
167 * Initialize the first set, continue.
172 struct GNUNET_SET_Element element;
174 element.element_type = 0;
176 element.data = "hello";
177 element.size = strlen(element.data);
178 GNUNET_SET_add_element (set1, &element, NULL, NULL);
179 element.data = "bar";
180 element.size = strlen(element.data);
181 GNUNET_SET_add_element (set1, &element, init_set2, NULL);
183 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initialized set 1\n");
189 const struct GNUNET_SET_Element *element)
193 GNUNET_assert (iter_count == 3);
194 GNUNET_SET_destroy (cls);
197 printf ("iter: got element\n");
206 struct GNUNET_SET_Element element;
207 struct GNUNET_SET_Handle *iter_set;
209 iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
211 element.element_type = 0;
213 element.data = "hello";
214 element.size = strlen(element.data);
215 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
216 element.data = "bar";
217 element.size = strlen(element.data);
218 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
219 element.data = "quux";
220 element.size = strlen(element.data);
221 GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
223 GNUNET_SET_iterate (iter_set, iter_cb, iter_set);
228 * Signature of the main function of a task.
231 * @param tc context information (why was this task triggered now)
234 timeout_fail (void *cls,
235 const struct GNUNET_SCHEDULER_TaskContext *tc)
237 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
239 GNUNET_SCHEDULER_shutdown ();
245 * Signature of the 'main' function for a (single-peer) testcase that
246 * is run using 'GNUNET_TESTING_peer_run'.
249 * @param cfg configuration of the peer that was started
250 * @param peer identity of the peer that was created
254 const struct GNUNET_CONFIGURATION_Handle *cfg,
255 struct GNUNET_TESTING_Peer *peer)
258 struct GNUNET_SET_OperationHandle *my_oh;
260 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
261 &timeout_fail, NULL);
264 GNUNET_CRYPTO_get_peer_identity (cfg, &local_id);
265 printf ("my id (from CRYPTO): %s\n", GNUNET_i2s (&local_id));
266 GNUNET_TESTING_peer_get_identity (peer, &local_id);
267 printf ("my id (from TESTING): %s\n", GNUNET_i2s (&local_id));
271 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
272 set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
273 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
277 ///* test if canceling an uncommited request works! */
278 my_oh = GNUNET_SET_prepare (&local_id, &app_id, NULL,
279 GNUNET_SET_RESULT_ADDED, NULL, NULL);
281 GNUNET_SET_operation_cancel (my_oh);
283 /* test the real set reconciliation */
288 main (int argc, char **argv)
290 if (0 != GNUNET_TESTING_peer_run ("test_set_api",