Fix TESTS_ENVIRONMENT setting (allow GNUNET_PREFIX to be overriden, allow TESTS_ENVIR...
[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       break;
57     case GNUNET_SET_STATUS_DONE:
58       printf ("set 1: done\n");
59       GNUNET_SET_destroy (set1);
60       break;
61     default:
62       GNUNET_assert (0);
63   }
64 }
65
66
67 static void
68 result_cb_set2 (void *cls, const struct GNUNET_SET_Element *element,
69            enum GNUNET_SET_Status status)
70 {
71   switch (status)
72   {
73     case GNUNET_SET_STATUS_OK:
74       printf ("set 2: got element\n");
75       break;
76     case GNUNET_SET_STATUS_FAILURE:
77       printf ("set 2: failure\n");
78       ret = 1;
79       break;
80     case GNUNET_SET_STATUS_DONE:
81       printf ("set 2: done\n");
82       GNUNET_SET_destroy (set2);
83       break;
84     default:
85       GNUNET_assert (0);
86   }
87 }
88
89
90 static void
91 listen_cb (void *cls,
92            const struct GNUNET_PeerIdentity *other_peer,
93            const struct GNUNET_MessageHeader *context_msg,
94            struct GNUNET_SET_Request *request)
95 {
96   struct GNUNET_SET_OperationHandle *oh;
97
98   GNUNET_assert (NULL != context_msg);
99
100   GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_TEST);
101
102   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "listen cb called\n");
103   GNUNET_SET_listen_cancel (listen_handle);
104
105   oh = GNUNET_SET_accept (request, GNUNET_SET_RESULT_FULL, result_cb_set2, NULL);
106   GNUNET_SET_commit (oh, set2);
107 }
108
109
110 /**
111  * Start the set operation.
112  *
113  * @param cls closure, unused
114  */
115 static void
116 start (void *cls)
117 {
118   struct GNUNET_SET_OperationHandle *oh;
119   struct GNUNET_MessageHeader context_msg;
120
121   context_msg.size = htons (sizeof context_msg);
122   context_msg.type = htons (GNUNET_MESSAGE_TYPE_TEST);
123
124   listen_handle = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
125                                      &app_id, listen_cb, NULL);
126   oh = GNUNET_SET_prepare (&local_id, &app_id, &context_msg, 42,
127                            GNUNET_SET_RESULT_FULL,
128                            result_cb_set1, NULL);
129   GNUNET_SET_commit (oh, set1);
130 }
131
132
133 /**
134  * Initialize the second set, continue
135  *
136  * @param cls closure, unused
137  */
138 static void
139 init_set2 (void *cls)
140 {
141   struct GNUNET_SET_Element element;
142
143   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initializing set 2\n");
144
145   element.type = 0;
146
147   element.data = "hello";
148   element.size = strlen(element.data);
149   GNUNET_SET_add_element (set2, &element, NULL, NULL);
150   element.data = "quux";
151   element.size = strlen(element.data);
152   GNUNET_SET_add_element (set2, &element, NULL, NULL);
153   element.data = "baz";
154   element.size = strlen(element.data);
155   GNUNET_SET_add_element (set2, &element, start, NULL);
156 }
157
158
159 /**
160  * Initialize the first set, continue.
161  */
162 static void
163 init_set1 (void)
164 {
165   struct GNUNET_SET_Element element;
166
167   element.type = 0;
168
169   element.data = "hello";
170   element.size = strlen(element.data);
171   GNUNET_SET_add_element (set1, &element, NULL, NULL);
172   element.data = "bar";
173   element.size = strlen(element.data);
174   GNUNET_SET_add_element (set1, &element, init_set2, NULL);
175
176   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initialized set 1\n");
177 }
178
179
180 static int
181 iter_cb (void *cls,
182          const struct GNUNET_SET_Element *element)
183 {
184   if (NULL == element)
185   {
186     GNUNET_assert (iter_count == 3);
187     GNUNET_SET_destroy (cls);
188     return GNUNET_YES;
189   }
190   printf ("iter: got element\n");
191   iter_count++;
192   return GNUNET_YES;
193 }
194
195
196 static void
197 test_iter ()
198 {
199   struct GNUNET_SET_Element element;
200   struct GNUNET_SET_Handle *iter_set;
201
202   iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
203
204   element.type = 0;
205
206   element.data = "hello";
207   element.size = strlen(element.data);
208   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
209   element.data = "bar";
210   element.size = strlen(element.data);
211   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
212   element.data = "quux";
213   element.size = strlen(element.data);
214   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
215
216   GNUNET_SET_iterate (iter_set, iter_cb, iter_set);
217 }
218
219
220 /**
221  * Signature of the 'main' function for a (single-peer) testcase that
222  * is run using 'GNUNET_TESTING_peer_run'.
223  *
224  * @param cls closure
225  * @param cfg configuration of the peer that was started
226  * @param peer identity of the peer that was created
227  */
228 static void
229 run (void *cls,
230      const struct GNUNET_CONFIGURATION_Handle *cfg,
231      struct GNUNET_TESTING_Peer *peer)
232 {
233   config = cfg;
234   GNUNET_CRYPTO_get_peer_identity (cfg, &local_id);
235   printf ("my id (from CRYPTO): %s\n", GNUNET_i2s (&local_id));
236   GNUNET_TESTING_peer_get_identity (peer, &local_id);
237   printf ("my id (from TESTING): %s\n", GNUNET_i2s (&local_id));
238
239   test_iter ();
240
241   set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
242   set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
243   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
244
245   /* test the real set reconciliation */
246   init_set1 ();
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   {
256     return 0;
257   }
258   return ret;
259 }
260