log error when timed out
[oweals/gnunet.git] / src / set / test_set_intersection_result_full.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012-2014 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file set/test_set_intersection_result_full.c
23  * @brief testcase for full result mode of the intersection set operation
24  * @author Christian Fuchs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testing_lib.h"
30 #include "gnunet_set_service.h"
31
32
33 static int ret;
34
35 static struct GNUNET_PeerIdentity local_id;
36
37 static struct GNUNET_HashCode app_id;
38
39 static struct GNUNET_SET_Handle *set1;
40
41 static struct GNUNET_SET_Handle *set2;
42
43 static struct GNUNET_SET_ListenHandle *listen_handle;
44
45 static const struct GNUNET_CONFIGURATION_Handle *config;
46
47 static int iter_count;
48
49
50 static void
51 result_cb_set1 (void *cls,
52                 const struct GNUNET_SET_Element *element,
53                 enum GNUNET_SET_Status status)
54 {
55   static int count;
56
57   switch (status)
58   {
59   case GNUNET_SET_STATUS_OK:
60     count++;
61     break;
62   case GNUNET_SET_STATUS_FAILURE:
63     ret = 1;
64     break;
65   case GNUNET_SET_STATUS_DONE:
66     GNUNET_assert (1 == count);
67     GNUNET_SET_destroy (set1);
68     break;
69   default:
70     GNUNET_assert (0);
71   }
72 }
73
74
75 static void
76 result_cb_set2 (void *cls,
77                 const struct GNUNET_SET_Element *element,
78                 enum GNUNET_SET_Status status)
79 {
80   static int count;
81
82   switch (status)
83   {
84   case GNUNET_SET_STATUS_OK:
85     count++;
86     break;
87   case GNUNET_SET_STATUS_FAILURE:
88     ret = 1;
89     break;
90   case GNUNET_SET_STATUS_DONE:
91     GNUNET_assert (1 == count);
92     GNUNET_SET_destroy (set2);
93     break;
94   default:
95     GNUNET_assert (0);
96   }
97 }
98
99
100 static void
101 listen_cb (void *cls,
102            const struct GNUNET_PeerIdentity *other_peer,
103            const struct GNUNET_MessageHeader *context_msg,
104            struct GNUNET_SET_Request *request)
105 {
106   struct GNUNET_SET_OperationHandle *oh;
107
108   GNUNET_assert (NULL != context_msg);
109   GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_TEST);
110   GNUNET_SET_listen_cancel (listen_handle);
111   oh = GNUNET_SET_accept (request,
112                           GNUNET_SET_RESULT_FULL,
113                           &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   listen_handle = GNUNET_SET_listen (config,
132                                      GNUNET_SET_OPERATION_INTERSECTION,
133                                      &app_id,
134                                      &listen_cb, NULL);
135   oh = GNUNET_SET_prepare (&local_id,
136                            &app_id,
137                            &context_msg,
138                            GNUNET_SET_RESULT_FULL,
139                            &result_cb_set1, NULL);
140   GNUNET_SET_commit (oh, set1);
141 }
142
143
144 /**
145  * Initialize the second set, continue
146  *
147  * @param cls closure, unused
148  */
149 static void
150 init_set2 (void *cls)
151 {
152   struct GNUNET_SET_Element element;
153
154   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
155               "initializing set 2\n");
156   element.element_type = 0;
157   element.data = "hello";
158   element.size = strlen(element.data);
159   GNUNET_SET_add_element (set2, &element, NULL, NULL);
160   element.data = "quux";
161   element.size = strlen(element.data);
162   GNUNET_SET_add_element (set2, &element, NULL, NULL);
163   element.data = "baz";
164   element.size = strlen(element.data);
165   GNUNET_SET_add_element (set2, &element, &start, NULL);
166 }
167
168
169 /**
170  * Initialize the first set, continue.
171  */
172 static void
173 init_set1 (void)
174 {
175   struct GNUNET_SET_Element element;
176
177   element.element_type = 0;
178   element.data = "hello";
179   element.size = strlen(element.data);
180   GNUNET_SET_add_element (set1, &element, NULL, NULL);
181   element.data = "bar";
182   element.size = strlen(element.data);
183   GNUNET_SET_add_element (set1, &element, &init_set2, NULL);
184 }
185
186
187 static int
188 iter_cb (void *cls,
189          const struct GNUNET_SET_Element *element)
190 {
191   if (NULL == element)
192   {
193     GNUNET_assert (iter_count == 3);
194     GNUNET_SET_destroy (cls);
195     return GNUNET_YES;
196   }
197   iter_count++;
198   return GNUNET_YES;
199 }
200
201
202 static void
203 test_iter ()
204 {
205   struct GNUNET_SET_Element element;
206   struct GNUNET_SET_Handle *iter_set;
207
208   iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_INTERSECTION);
209   element.element_type = 0;
210   element.data = "hello";
211   element.size = strlen(element.data);
212   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
213   element.data = "bar";
214   element.size = strlen(element.data);
215   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
216   element.data = "quux";
217   element.size = strlen(element.data);
218   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
219   GNUNET_SET_iterate (iter_set, &iter_cb, iter_set);
220 }
221
222
223 /**
224  * Signature of the 'main' function for a (single-peer) testcase that
225  * is run using 'GNUNET_TESTING_peer_run'.
226  *
227  * @param cls closure
228  * @param cfg configuration of the peer that was started
229  * @param peer identity of the peer that was created
230  */
231 static void
232 run (void *cls,
233      const struct GNUNET_CONFIGURATION_Handle *cfg,
234      struct GNUNET_TESTING_Peer *peer)
235 {
236   config = cfg;
237   GNUNET_TESTING_peer_get_identity (peer, &local_id);
238   if (0) test_iter ();
239
240   set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_INTERSECTION);
241   set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_INTERSECTION);
242   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
243
244   /* test the real set reconciliation */
245   init_set1 ();
246 }
247
248
249 int
250 main (int argc, char **argv)
251 {
252   if (0 != GNUNET_TESTING_peer_run ("test_set_api",
253                                     "test_set.conf",
254                                     &run, NULL))
255     return 1;
256   return ret;
257 }
258