refactoring datastore API to use MQ API, also fixing misc. bugs in new mysql backend
[oweals/gnunet.git] / src / datastore / test_datastore_api_management.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2004, 2005, 2006, 2007, 2009, 2011 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  * @file datastore/test_datastore_api_management.c
22  * @brief Test for the space management functions of the datastore implementation.
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_protocols.h"
28 #include "gnunet_datastore_service.h"
29 #include "gnunet_testing_lib.h"
30
31
32 /**
33  * How long until we give up on transmitting the message?
34  */
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
36
37 /**
38  * Number of iterations to run; must be large enough
39  * so that the quota will be exceeded!
40  */
41 #define ITERATIONS 5000
42
43 enum RunPhase
44 {
45   RP_PUT,
46   RP_GET,
47   RP_DONE,
48   RP_GET_FAIL
49 };
50
51
52 struct CpsRunContext
53 {
54   struct GNUNET_HashCode key;
55   int i;
56   int found;
57   const struct GNUNET_CONFIGURATION_Handle *cfg;
58   void *data;
59   enum RunPhase phase;
60   uint64_t offset;
61 };
62
63
64 static struct GNUNET_DATASTORE_Handle *datastore;
65
66 static struct GNUNET_TIME_Absolute now;
67
68 static int ok;
69
70 static const char *plugin_name;
71
72
73 static size_t
74 get_size (int i)
75 {
76   return 8 + 8 * (i % 256);
77 }
78
79
80 static const void *
81 get_data (int i)
82 {
83   static char buf[60000];
84
85   memset (buf, i, 8 + 8 * (i % 256));
86   return buf;
87 }
88
89
90 static int
91 get_type (int i)
92 {
93   return 1;
94 }
95
96
97 static int
98 get_priority (int i)
99 {
100   return i + 1;
101 }
102
103
104 static int
105 get_anonymity (int i)
106 {
107   return i;
108 }
109
110
111 static struct GNUNET_TIME_Absolute
112 get_expiration (int i)
113 {
114   struct GNUNET_TIME_Absolute av;
115
116   av.abs_value_us = now.abs_value_us + i * 1000 * 1000LL;
117   return av;
118 }
119
120
121 static void
122 run_continuation (void *cls);
123
124
125 static void
126 check_success (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
127 {
128   struct CpsRunContext *crc = cls;
129
130   if (GNUNET_OK != success)
131     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", msg);
132   GNUNET_assert (GNUNET_OK == success);
133   GNUNET_free_non_null (crc->data);
134   crc->data = NULL;
135   GNUNET_SCHEDULER_add_now (&run_continuation, crc);
136 }
137
138
139 static void
140 check_value (void *cls, const struct GNUNET_HashCode * key, size_t size,
141              const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
142              uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
143              uint64_t uid)
144 {
145   struct CpsRunContext *crc = cls;
146   int i;
147
148   if (NULL == key)
149   {
150     crc->phase = RP_GET_FAIL;
151     GNUNET_SCHEDULER_add_now (&run_continuation, crc);
152     return;
153   }
154   i = crc->i;
155   GNUNET_assert (size == get_size (i));
156   GNUNET_assert (0 == memcmp (data, get_data (i), size));
157   GNUNET_assert (type == get_type (i));
158   GNUNET_assert (priority == get_priority (i));
159   GNUNET_assert (anonymity == get_anonymity (i));
160   GNUNET_assert (expiration.abs_value_us == get_expiration (i).abs_value_us);
161   crc->offset++;
162   crc->i--;
163   if (crc->i == 0)
164     crc->phase = RP_DONE;
165   GNUNET_SCHEDULER_add_now (&run_continuation, crc);
166 }
167
168
169 static void
170 check_nothing (void *cls, const struct GNUNET_HashCode * key, size_t size,
171                const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
172                uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
173                uint64_t uid)
174 {
175   struct CpsRunContext *crc = cls;
176
177   GNUNET_assert (key == NULL);
178   if (0 == --crc->i)
179     crc->phase = RP_DONE;
180   GNUNET_SCHEDULER_add_now (&run_continuation, crc);
181 }
182
183
184 static void
185 run_continuation (void *cls)
186 {
187   struct CpsRunContext *crc = cls;
188
189   ok = (int) crc->phase;
190   switch (crc->phase)
191   {
192   case RP_PUT:
193     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "PUT",
194                 crc->i);
195     GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
196     GNUNET_DATASTORE_put (datastore,
197                           0,
198                           &crc->key,
199                           get_size (crc->i),
200                           get_data (crc->i),
201                           get_type (crc->i),
202                           get_priority (crc->i),
203                           get_anonymity (crc->i),
204                           0,
205                           get_expiration (crc->i),
206                           1,
207                           1,
208                           &check_success, crc);
209     crc->i++;
210     if (crc->i == ITERATIONS)
211     {
212       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
213                   "Sleeping to give datastore time to clean up\n");
214       sleep (1);
215       crc->phase = RP_GET;
216       crc->i--;
217     }
218     break;
219   case RP_GET:
220     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET",
221                 crc->i);
222     GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
223     GNUNET_DATASTORE_get_key (datastore, crc->offset++, &crc->key,
224                               get_type (crc->i), 1, 1,
225                               &check_value,
226                               crc);
227     break;
228   case RP_GET_FAIL:
229     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET(f)",
230                 crc->i);
231     GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
232     GNUNET_DATASTORE_get_key (datastore, crc->offset++, &crc->key,
233                               get_type (crc->i), 1, 1,
234                               &check_nothing,
235                               crc);
236     break;
237   case RP_DONE:
238     GNUNET_assert (0 == crc->i);
239     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n");
240     GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
241     GNUNET_free (crc);
242     ok = 0;
243   }
244 }
245
246
247 static void
248 run_tests (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
249 {
250   struct CpsRunContext *crc = cls;
251
252   if (success != GNUNET_YES)
253   {
254     FPRINTF (stderr,
255              "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
256              msg);
257     GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
258     GNUNET_free (crc);
259     return;
260   }
261   GNUNET_SCHEDULER_add_now (&run_continuation, crc);
262 }
263
264
265 static void
266 run (void *cls,
267      const struct GNUNET_CONFIGURATION_Handle *cfg,
268      struct GNUNET_TESTING_Peer *peer)
269 {
270   struct CpsRunContext *crc;
271   static struct GNUNET_HashCode zkey;
272
273   crc = GNUNET_new (struct CpsRunContext);
274   crc->cfg = cfg;
275   crc->phase = RP_PUT;
276   now = GNUNET_TIME_absolute_get ();
277   datastore = GNUNET_DATASTORE_connect (cfg);
278   if (NULL ==
279       GNUNET_DATASTORE_put (datastore,
280                             0,
281                             &zkey,
282                             4,
283                             "TEST",
284                             GNUNET_BLOCK_TYPE_TEST,
285                             0, 0, 0,
286                             GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
287                             0,
288                             1,
289                             &run_tests,
290                             crc))
291   {
292     FPRINTF (stderr, "%s",  "Test 'put' operation failed.\n");
293     GNUNET_free (crc);
294     ok = 1;
295   }
296 }
297
298
299 int
300 main (int argc, char *argv[])
301 {
302   char cfg_name[128];
303
304   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
305   GNUNET_snprintf (cfg_name, sizeof (cfg_name),
306                    "test_datastore_api_data_%s.conf", plugin_name);
307   if (0 !=
308       GNUNET_TESTING_peer_run ("test-gnunet-datastore-management",
309                                cfg_name,
310                                &run,
311                                NULL))
312     return 1;
313   return ok;
314 }
315
316 /* end of test_datastore_api_management.c */