-do not run tests if disabled
[oweals/gnunet.git] / src / set / test_set_union_result_full.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file set/test_set_union_result_full.c
23  * @brief testcase for full result mode of the union set operation
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_set_service.h"
29
30
31 static int ret;
32
33 static struct GNUNET_PeerIdentity local_id;
34
35 static struct GNUNET_HashCode app_id;
36 static struct GNUNET_SET_Handle *set1;
37 static struct GNUNET_SET_Handle *set2;
38 static struct GNUNET_SET_ListenHandle *listen_handle;
39 const static struct GNUNET_CONFIGURATION_Handle *config;
40
41 static int iter_count;
42
43
44 static void
45 result_cb_set1 (void *cls, const struct GNUNET_SET_Element *element,
46                 enum GNUNET_SET_Status status)
47 {
48   switch (status)
49   {
50     case GNUNET_SET_STATUS_OK:
51       printf ("set 1: got element\n");
52       break;
53     case GNUNET_SET_STATUS_FAILURE:
54       printf ("set 1: failure\n");
55       ret = 1;
56       GNUNET_SCHEDULER_shutdown ();
57       break;
58     case GNUNET_SET_STATUS_DONE:
59       printf ("set 1: done\n");
60       GNUNET_SET_destroy (set1);
61       set1 = NULL;
62       if (NULL == set2)
63         GNUNET_SCHEDULER_shutdown ();
64       break;
65     default:
66       GNUNET_assert (0);
67   }
68 }
69
70
71 static void
72 result_cb_set2 (void *cls, const struct GNUNET_SET_Element *element,
73            enum GNUNET_SET_Status status)
74 {
75   switch (status)
76   {
77     case GNUNET_SET_STATUS_OK:
78       printf ("set 2: got element\n");
79       break;
80     case GNUNET_SET_STATUS_FAILURE:
81       printf ("set 2: failure\n");
82       ret = 1;
83       GNUNET_SCHEDULER_shutdown ();
84       break;
85     case GNUNET_SET_STATUS_DONE:
86       printf ("set 2: done\n");
87       GNUNET_SET_destroy (set2);
88       set2 = NULL;
89       if (NULL == set1)
90         GNUNET_SCHEDULER_shutdown ();
91       break;
92     default:
93       GNUNET_assert (0);
94   }
95 }
96
97
98 static void
99 listen_cb (void *cls,
100            const struct GNUNET_PeerIdentity *other_peer,
101            const struct GNUNET_MessageHeader *context_msg,
102            struct GNUNET_SET_Request *request)
103 {
104   struct GNUNET_SET_OperationHandle *oh;
105
106   GNUNET_assert (NULL != context_msg);
107
108   GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_TEST);
109
110   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "listen cb called\n");
111   GNUNET_SET_listen_cancel (listen_handle);
112
113   oh = GNUNET_SET_accept (request, GNUNET_SET_RESULT_FULL, result_cb_set2, NULL);
114   GNUNET_SET_commit (oh, set2);
115 }
116
117
118 /**
119  * Start the set operation.
120  *
121  * @param cls closure, unused
122  */
123 static void
124 start (void *cls)
125 {
126   struct GNUNET_SET_OperationHandle *oh;
127   struct GNUNET_MessageHeader context_msg;
128
129   context_msg.size = htons (sizeof context_msg);
130   context_msg.type = htons (GNUNET_MESSAGE_TYPE_TEST);
131
132   listen_handle = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
133                                      &app_id, listen_cb, NULL);
134   oh = GNUNET_SET_prepare (&local_id, &app_id, &context_msg,
135                            GNUNET_SET_RESULT_FULL,
136                            result_cb_set1, NULL);
137   GNUNET_SET_commit (oh, set1);
138 }
139
140
141 /**
142  * Initialize the second set, continue
143  *
144  * @param cls closure, unused
145  */
146 static void
147 init_set2 (void *cls)
148 {
149   struct GNUNET_SET_Element element;
150
151   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initializing set 2\n");
152
153   element.type = 0;
154
155   element.data = "hello";
156   element.size = strlen(element.data);
157   GNUNET_SET_add_element (set2, &element, NULL, NULL);
158   element.data = "quux";
159   element.size = strlen(element.data);
160   GNUNET_SET_add_element (set2, &element, NULL, NULL);
161   element.data = "baz";
162   element.size = strlen(element.data);
163   GNUNET_SET_add_element (set2, &element, start, NULL);
164 }
165
166
167 /**
168  * Initialize the first set, continue.
169  */
170 static void
171 init_set1 (void)
172 {
173   struct GNUNET_SET_Element element;
174
175   element.type = 0;
176
177   element.data = "hello";
178   element.size = strlen(element.data);
179   GNUNET_SET_add_element (set1, &element, NULL, NULL);
180   element.data = "bar";
181   element.size = strlen(element.data);
182   GNUNET_SET_add_element (set1, &element, init_set2, NULL);
183
184   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initialized set 1\n");
185 }
186
187
188 static int
189 iter_cb (void *cls,
190          const struct GNUNET_SET_Element *element)
191 {
192   if (NULL == element)
193   {
194     GNUNET_assert (iter_count == 3);
195     GNUNET_SET_destroy (cls);
196     return GNUNET_YES;
197   }
198   printf ("iter: got element\n");
199   iter_count++;
200   return GNUNET_YES;
201 }
202
203
204 static void
205 test_iter ()
206 {
207   struct GNUNET_SET_Element element;
208   struct GNUNET_SET_Handle *iter_set;
209
210   iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
211
212   element.type = 0;
213
214   element.data = "hello";
215   element.size = strlen(element.data);
216   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
217   element.data = "bar";
218   element.size = strlen(element.data);
219   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
220   element.data = "quux";
221   element.size = strlen(element.data);
222   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
223
224   GNUNET_SET_iterate (iter_set, iter_cb, iter_set);
225 }
226
227
228 /**
229  * Signature of the main function of a task.
230  *
231  * @param cls closure
232  * @param tc context information (why was this task triggered now)
233  */
234 static void
235 timeout_fail (void *cls,
236               const struct GNUNET_SCHEDULER_TaskContext *tc)
237 {
238   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
239     return;
240   GNUNET_SCHEDULER_shutdown ();
241   ret = 1;
242 }
243
244
245 /**
246  * Signature of the 'main' function for a (single-peer) testcase that
247  * is run using 'GNUNET_TESTING_peer_run'.
248  *
249  * @param cls closure
250  * @param cfg configuration of the peer that was started
251  * @param peer identity of the peer that was created
252  */
253 static void
254 run (void *cls,
255      const struct GNUNET_CONFIGURATION_Handle *cfg,
256      struct GNUNET_TESTING_Peer *peer)
257 {
258
259   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
260                                 &timeout_fail, NULL);
261
262   config = cfg;
263   GNUNET_CRYPTO_get_peer_identity (cfg, &local_id);
264   printf ("my id (from CRYPTO): %s\n", GNUNET_i2s (&local_id));
265   GNUNET_TESTING_peer_get_identity (peer, &local_id);
266   printf ("my id (from TESTING): %s\n", GNUNET_i2s (&local_id));
267
268   test_iter ();
269
270   set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
271   set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
272   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
273
274   /* test the real set reconciliation */
275   init_set1 ();
276 }
277
278 int
279 main (int argc, char **argv)
280 {
281   if (0 != GNUNET_TESTING_peer_run ("test_set_api",
282                                     "test_set.conf",
283                                     &run, NULL))
284   {
285     return 1;
286   }
287   return ret;
288 }
289