More API function tests...
[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_datastore_plugin.h"
30 #include "gnunet_testing_lib.h"
31
32
33 /**
34  * How long until we give up on transmitting the message?
35  */
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
37
38 /**
39  * Number of iterations to run; must be large enough
40  * so that the quota will be exceeded!
41  */
42 #define ITERATIONS 5000
43
44 enum RunPhase
45 {
46   RP_PUT,
47   RP_GET,
48   RP_DONE,
49   RP_GET_FAIL
50 };
51
52
53 struct CpsRunContext
54 {
55   struct GNUNET_HashCode key;
56   int i;
57   int found;
58   const struct GNUNET_CONFIGURATION_Handle *cfg;
59   void *data;
60   enum RunPhase phase;
61   uint64_t offset;
62 };
63
64
65 static struct GNUNET_DATASTORE_Handle *datastore;
66
67 static struct GNUNET_TIME_Absolute now;
68
69 static int ok;
70
71 static const char *plugin_name;
72
73
74 static size_t
75 get_size (int i)
76 {
77   return 8 + 8 * (i % 256);
78 }
79
80
81 static const void *
82 get_data (int i)
83 {
84   static char buf[60000];
85
86   memset (buf, i, 8 + 8 * (i % 256));
87   return buf;
88 }
89
90
91 static int
92 get_type (int i)
93 {
94   return 1;
95 }
96
97
98 static int
99 get_priority (int i)
100 {
101   return i + 1;
102 }
103
104
105 static int
106 get_anonymity (int i)
107 {
108   return i;
109 }
110
111
112 static struct GNUNET_TIME_Absolute
113 get_expiration (int i)
114 {
115   struct GNUNET_TIME_Absolute av;
116
117   av.abs_value_us = now.abs_value_us + i * 1000 * 1000LL;
118   return av;
119 }
120
121
122 static void
123 run_continuation (void *cls);
124
125
126 static void
127 check_success (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
128 {
129   struct CpsRunContext *crc = cls;
130
131   if (GNUNET_OK != success)
132     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", msg);
133   GNUNET_assert (GNUNET_OK == success);
134   GNUNET_free_non_null (crc->data);
135   crc->data = NULL;
136   GNUNET_SCHEDULER_add_now (&run_continuation, crc);
137 }
138
139
140 static void
141 check_value (void *cls, const struct GNUNET_HashCode * key, size_t size,
142              const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
143              uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
144              uint64_t uid)
145 {
146   struct CpsRunContext *crc = cls;
147   int i;
148
149   if (NULL == key)
150   {
151     crc->phase = RP_GET_FAIL;
152     GNUNET_SCHEDULER_add_now (&run_continuation, crc);
153     return;
154   }
155   i = crc->i;
156   GNUNET_assert (size == get_size (i));
157   GNUNET_assert (0 == memcmp (data, get_data (i), size));
158   GNUNET_assert (type == get_type (i));
159   GNUNET_assert (priority == get_priority (i));
160   GNUNET_assert (anonymity == get_anonymity (i));
161   GNUNET_assert (expiration.abs_value_us == get_expiration (i).abs_value_us);
162   crc->offset++;
163   crc->i--;
164   if (crc->i == 0)
165     crc->phase = RP_DONE;
166   GNUNET_SCHEDULER_add_now (&run_continuation, crc);
167 }
168
169
170 static void
171 check_nothing (void *cls, const struct GNUNET_HashCode * key, size_t size,
172                const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
173                uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
174                uint64_t uid)
175 {
176   struct CpsRunContext *crc = cls;
177
178   GNUNET_assert (key == NULL);
179   if (0 == --crc->i)
180     crc->phase = RP_DONE;
181   GNUNET_SCHEDULER_add_now (&run_continuation, crc);
182 }
183
184
185 static void
186 run_continuation (void *cls)
187 {
188   struct CpsRunContext *crc = cls;
189
190   ok = (int) crc->phase;
191   switch (crc->phase)
192   {
193   case RP_PUT:
194     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "PUT",
195                 crc->i);
196     GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
197     GNUNET_DATASTORE_put (datastore,
198                           0,
199                           &crc->key,
200                           get_size (crc->i),
201                           get_data (crc->i),
202                           get_type (crc->i),
203                           get_priority (crc->i),
204                           get_anonymity (crc->i),
205                           0,
206                           get_expiration (crc->i),
207                           1,
208                           1,
209                           &check_success, crc);
210     crc->i++;
211     if (crc->i == ITERATIONS)
212     {
213       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
214                   "Sleeping to give datastore time to clean up\n");
215       sleep (1);
216       crc->phase = RP_GET;
217       crc->i--;
218     }
219     break;
220   case RP_GET:
221     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET",
222                 crc->i);
223     GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
224     GNUNET_DATASTORE_get_key (datastore, crc->offset++, &crc->key,
225                               get_type (crc->i), 1, 1,
226                               &check_value,
227                               crc);
228     break;
229   case RP_GET_FAIL:
230     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET(f)",
231                 crc->i);
232     GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
233     GNUNET_DATASTORE_get_key (datastore, crc->offset++, &crc->key,
234                               get_type (crc->i), 1, 1,
235                               &check_nothing,
236                               crc);
237     break;
238   case RP_DONE:
239     GNUNET_assert (0 == crc->i);
240     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n");
241     GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
242     GNUNET_free (crc);
243     ok = 0;
244   }
245 }
246
247
248 static void
249 run_tests (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
250 {
251   struct CpsRunContext *crc = cls;
252
253   if (success != GNUNET_YES)
254   {
255     FPRINTF (stderr,
256              "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
257              msg);
258     GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
259     GNUNET_free (crc);
260     return;
261   }
262   GNUNET_SCHEDULER_add_now (&run_continuation, crc);
263 }
264
265
266 static void
267 run (void *cls,
268      const struct GNUNET_CONFIGURATION_Handle *cfg,
269      struct GNUNET_TESTING_Peer *peer)
270 {
271   struct CpsRunContext *crc;
272   static struct GNUNET_HashCode zkey;
273
274   crc = GNUNET_new (struct CpsRunContext);
275   crc->cfg = cfg;
276   crc->phase = RP_PUT;
277   now = GNUNET_TIME_absolute_get ();
278   datastore = GNUNET_DATASTORE_connect (cfg);
279   if (NULL ==
280       GNUNET_DATASTORE_put (datastore,
281                             0,
282                             &zkey,
283                             4,
284                             "TEST",
285                             GNUNET_BLOCK_TYPE_TEST,
286                             0, 0, 0,
287                             GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
288                             0,
289                             1,
290                             &run_tests,
291                             crc))
292   {
293     FPRINTF (stderr, "%s",  "Test 'put' operation failed.\n");
294     GNUNET_free (crc);
295     ok = 1;
296   }
297 }
298
299
300 /**
301  * check if plugin is actually working 
302  */
303 static int
304 test_plugin (const char *cfg_name)
305 {
306   char libname[128];
307   struct GNUNET_CONFIGURATION_Handle *cfg;
308   struct GNUNET_DATASTORE_PluginFunctions *api;
309   struct GNUNET_DATASTORE_PluginEnvironment env;
310   
311   cfg = GNUNET_CONFIGURATION_create ();
312   if (GNUNET_OK !=
313       GNUNET_CONFIGURATION_load (cfg,
314                                  cfg_name))
315   {
316     GNUNET_CONFIGURATION_destroy (cfg);
317     fprintf (stderr,
318              "Failed to load configuration %s\n",
319              cfg_name);
320     return 1;
321   }
322   memset (&env, 0, sizeof (env));
323   env.cfg = cfg;
324   GNUNET_snprintf (libname,
325                    sizeof (libname),
326                    "libgnunet_plugin_datastore_%s",
327                    plugin_name);
328   api = GNUNET_PLUGIN_load (libname, &env);
329   if (NULL == api)
330   {
331     GNUNET_CONFIGURATION_destroy (cfg);
332     fprintf (stderr,
333              "Failed to load plugin `%s'\n",
334              libname);
335     return 77;
336   }
337   GNUNET_PLUGIN_unload (libname, api);
338   GNUNET_CONFIGURATION_destroy (cfg);
339   return 0;
340 }
341
342
343 int
344 main (int argc, char *argv[])
345 {
346   char cfg_name[128];
347   int ret;
348
349   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
350   GNUNET_snprintf (cfg_name,
351                    sizeof (cfg_name),
352                    "test_datastore_api_data_%s.conf",
353                    plugin_name);
354   ret = test_plugin (cfg_name);
355   if (0 != ret)
356     return ret;
357   if (0 !=
358       GNUNET_TESTING_peer_run ("test-gnunet-datastore-management",
359                                cfg_name,
360                                &run,
361                                NULL))
362     return 1;
363   return ok;
364 }
365
366 /* end of test_datastore_api_management.c */