adding configure code for --enable-benchmarks, --enable-expensive-tests, some clean up
[oweals/gnunet.git] / src / datastore / test_plugin_datastore.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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  * @file test_plugin_datastore.c
22  * @brief Test database plugin directly, calling each API function once
23  * @author Christian Grothoff
24  */
25
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_datastore_plugin.h"
30
31 #define VERBOSE GNUNET_NO
32
33 /**
34  * Number of put operations to perform.
35  */
36 #define PUT_10 10
37
38 static unsigned long long stored_bytes;
39
40 static unsigned long long stored_entries;
41
42 static unsigned long long stored_ops;
43
44 static const char *plugin_name;
45
46 static int ok;
47
48 enum RunPhase
49   {
50     RP_ERROR = 0,
51     RP_PUT,
52     RP_GET,
53     RP_UPDATE,
54     RP_ITER_ZERO,
55     RP_REPL_GET,
56     RP_EXPI_GET,
57     RP_DROP
58   };
59
60
61 struct CpsRunContext
62 {
63   const struct GNUNET_CONFIGURATION_Handle *cfg;
64   struct GNUNET_DATASTORE_PluginFunctions * api;
65   enum RunPhase phase;
66   unsigned int cnt;
67   unsigned int i;
68   uint64_t offset;
69 };
70
71
72 /**
73  * Function called by plugins to notify us about a
74  * change in their disk utilization.
75  *
76  * @param cls closure (NULL)
77  * @param delta change in disk utilization, 
78  *        0 for "reset to empty"
79  */
80 static void
81 disk_utilization_change_cb (void *cls,
82                             int delta)
83 {
84   /* do nothing */
85 }
86
87
88 static void
89 gen_key (int i,
90          GNUNET_HashCode *key)
91 {
92   memset (key, 0, sizeof (GNUNET_HashCode));
93   key->bits[0] = (unsigned int) i;
94   GNUNET_CRYPTO_hash (key, sizeof (GNUNET_HashCode), key);
95 }
96
97              
98 static void
99 put_value (struct GNUNET_DATASTORE_PluginFunctions * api,
100            int i, 
101            int k)
102 {
103   char value[65536];
104   size_t size;
105   GNUNET_HashCode key;
106   char *msg;
107   unsigned int prio;
108
109   /* most content is 32k */
110   size = 32 * 1024;
111
112   if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0)  /* but some of it is less! */
113     size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
114   size = size - (size & 7);     /* always multiple of 8 */
115
116   /* generate random key */
117   gen_key (i, &key);
118   memset (value, i, size);
119   if (i > 255)
120     memset (value, i - 255, size / 2);
121   value[0] = k;
122   msg = NULL;
123   prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
124 #if VERBOSE
125   fprintf (stderr, 
126            "putting type %u, anon %u under key %s\n",
127            i+1, i, GNUNET_h2s (&key));
128 #endif
129   if (GNUNET_OK != api->put (api->cls,
130                              &key, 
131                              size,
132                              value,
133                              i + 1 /* type */,
134                              prio,
135                              i /* anonymity */,
136                              0 /* replication */,
137                              GNUNET_TIME_relative_to_absolute 
138                              (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
139                                                              60 * 60 * 60 * 1000 +
140                                                              GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
141                              &msg))
142     {
143       fprintf (stderr, "ERROR: `%s'\n", msg);
144       GNUNET_free_non_null (msg);
145       return;
146     }
147   stored_bytes += size;
148   stored_ops++;
149   stored_entries++;
150 }
151
152
153 static void
154 test (void *cls,
155       const struct GNUNET_SCHEDULER_TaskContext *tc);
156
157
158 static uint64_t guid;
159
160
161 static int
162 iterate_one_shot (void *cls,
163                   const GNUNET_HashCode * key,
164                   uint32_t size,
165                   const void *data,
166                   enum GNUNET_BLOCK_Type type,
167                   uint32_t priority,
168                   uint32_t anonymity,
169                   struct GNUNET_TIME_Absolute
170                   expiration, 
171                   uint64_t uid)
172 {
173   struct CpsRunContext *crc = cls;
174   
175   GNUNET_assert (key != NULL);
176   guid = uid;
177   crc->phase++;
178 #if VERBOSE
179   fprintf (stderr,
180            "Found result type=%u, priority=%u, size=%u, expire=%llu, key %s\n",
181            type, priority, size,
182            (unsigned long long) expiration.abs_value,
183            GNUNET_h2s (key));
184 #endif    
185   GNUNET_SCHEDULER_add_now (&test, crc);
186   return GNUNET_OK;
187 }
188
189
190 /**
191  * Function called when the service shuts
192  * down.  Unloads our datastore plugin.
193  *
194  * @param api api to unload
195  * @param cfg configuration to use
196  */
197 static void
198 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api,
199                const struct GNUNET_CONFIGURATION_Handle *cfg)
200 {
201   char *name;
202   char *libname;
203
204   if (GNUNET_OK !=
205       GNUNET_CONFIGURATION_get_value_string (cfg,
206                                              "DATASTORE", "DATABASE", &name))
207     {
208       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
209                   _("No `%s' specified for `%s' in configuration!\n"),
210                   "DATABASE",
211                   "DATASTORE");
212       return;
213     }
214   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
215   GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
216   GNUNET_free (libname);
217   GNUNET_free (name);
218 }
219
220
221
222 /**
223  * Last task run during shutdown.  Disconnects us from
224  * the transport and core.
225  */
226 static void
227 cleaning_task (void *cls,
228                const struct GNUNET_SCHEDULER_TaskContext *tc)
229 {
230   struct CpsRunContext *crc = cls;
231
232   unload_plugin (crc->api, crc->cfg);
233   GNUNET_free (crc);
234 }
235
236
237 static void
238 test (void *cls,
239       const struct GNUNET_SCHEDULER_TaskContext *tc)
240 {  
241   struct CpsRunContext *crc = cls;
242   int j;
243   unsigned long long os;
244   unsigned long long cs;
245   GNUNET_HashCode key;
246
247   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
248     {
249       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
250                   "Test aborted.\n");
251       crc->phase = RP_ERROR;
252     }
253 #if VERBOSE
254   fprintf (stderr, "In phase %d, iteration %u\n",
255            crc->phase,
256            crc->cnt);
257 #endif
258   switch (crc->phase)
259     {
260     case RP_ERROR:
261       ok = 1;
262       GNUNET_break (0); 
263       crc->api->drop (crc->api->cls);
264       GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
265       break;
266     case RP_PUT:      
267       os = 0;
268       for (j=0;j<PUT_10;j++)
269         {
270           put_value (crc->api, j, crc->i);
271           cs = crc->api->estimate_size (crc->api->cls);
272           GNUNET_assert (os < cs);
273           os = cs;
274         }
275       crc->phase++;
276       GNUNET_SCHEDULER_add_now (&test, crc);
277       break;
278     case RP_GET:
279       if (crc->cnt == 1)
280         {
281           crc->cnt = 0;
282           crc->phase++;
283           GNUNET_SCHEDULER_add_now (&test, crc);
284           break;
285         }
286       gen_key (5, &key);
287       crc->api->get_key (crc->api->cls,
288                          crc->offset++,
289                          &key, NULL, 
290                          GNUNET_BLOCK_TYPE_ANY,
291                          &iterate_one_shot,
292                          crc);
293       break;
294     case RP_UPDATE:
295       GNUNET_assert (GNUNET_OK ==
296                      crc->api->update (crc->api->cls,
297                                        guid, 1, 
298                                        GNUNET_TIME_UNIT_ZERO_ABS,
299                                        NULL));
300       crc->phase++;
301       GNUNET_SCHEDULER_add_now (&test, crc);
302       break;
303
304     case RP_ITER_ZERO:
305       if (crc->cnt == 1)
306         {
307           crc->cnt = 0;
308           crc->phase++;
309           GNUNET_SCHEDULER_add_now (&test, crc);
310           break;
311         }
312       crc->api->get_zero_anonymity (crc->api->cls, 
313                                     0,
314                                     1, 
315                                     &iterate_one_shot,
316                                     crc);
317       break;
318     case RP_REPL_GET:
319       crc->api->get_replication (crc->api->cls, 
320                                  &iterate_one_shot,
321                                  crc);
322       break;
323     case RP_EXPI_GET:
324       crc->api->get_expiration (crc->api->cls, 
325                                 &iterate_one_shot,
326                                 crc);
327       break;
328     case RP_DROP:
329       crc->api->drop (crc->api->cls);
330       GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
331       break;
332     }
333 }
334
335
336 /**
337  * Load the datastore plugin.
338  */
339 static struct GNUNET_DATASTORE_PluginFunctions *
340 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
341 {
342   static struct GNUNET_DATASTORE_PluginEnvironment env;
343   struct GNUNET_DATASTORE_PluginFunctions * ret; 
344   char *name;
345   char *libname;
346
347   if (GNUNET_OK !=
348       GNUNET_CONFIGURATION_get_value_string (cfg,
349                                              "DATASTORE", "DATABASE", &name))
350     {
351       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
352                   _("No `%s' specified for `%s' in configuration!\n"),
353                   "DATABASE",
354                   "DATASTORE");
355       return NULL;
356     }
357   env.cfg = cfg;
358   env.duc = &disk_utilization_change_cb;
359   env.cls = NULL;
360   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
361               _("Loading `%s' datastore plugin\n"), name);
362   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
363   if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
364     {
365       fprintf (stderr,
366                "Failed to load plugin `%s'!\n",
367                name);
368       return NULL;
369     }
370   GNUNET_free (libname);
371   GNUNET_free (name);
372   return ret;
373 }
374
375
376 static void
377 run (void *cls,
378      char *const *args,
379      const char *cfgfile,
380      const struct GNUNET_CONFIGURATION_Handle *c)
381 {
382   struct GNUNET_DATASTORE_PluginFunctions *api;
383   struct CpsRunContext *crc;
384
385   api = load_plugin (c);
386   if (api == NULL)
387     {
388       fprintf (stderr, 
389                "Could not initialize plugin, assuming database not configured. Test not run!\n");
390       return;
391     }
392   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
393   crc->api = api;
394   crc->cfg = c;
395   crc->phase = RP_PUT;
396   GNUNET_SCHEDULER_add_now (&test, crc);
397 }
398
399
400 static int
401 check ()
402 {
403   char cfg_name[128];
404   char *const argv[] = { 
405     "test-plugin-datastore",
406     "-c",
407     cfg_name,
408 #if VERBOSE
409     "-L", "DEBUG",
410 #endif
411     NULL
412   };
413   struct GNUNET_GETOPT_CommandLineOption options[] = {
414     GNUNET_GETOPT_OPTION_END
415   };
416
417   GNUNET_snprintf (cfg_name,
418                    sizeof (cfg_name),
419                    "test_plugin_datastore_data_%s.conf",
420                    plugin_name);
421   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
422                       argv, "test-plugin-datastore", "nohelp",
423                       options, &run, NULL);
424   if (ok != 0)
425     fprintf (stderr, "Missed some testcases: %u\n", ok);
426   return ok;
427 }
428
429
430 int
431 main (int argc, char *argv[])
432 {
433   int ret;
434   char *pos;
435   char dir_name[128];
436
437   sleep (1);
438   /* determine name of plugin to use */
439   plugin_name = argv[0];
440   while (NULL != (pos = strstr(plugin_name, "_")))
441     plugin_name = pos+1;
442   if (NULL != (pos = strstr(plugin_name, ".")))
443     pos[0] = 0;
444   else
445     pos = (char *) plugin_name;
446
447   GNUNET_snprintf (dir_name,
448                    sizeof (dir_name),
449                    "/tmp/test-gnunet-datastore-plugin-%s",
450                    plugin_name);
451   GNUNET_DISK_directory_remove (dir_name);
452   GNUNET_log_setup ("test-plugin-datastore",
453 #if VERBOSE
454                     "DEBUG",
455 #else
456                     "WARNING",
457 #endif
458                     NULL);
459   ret = check ();
460   if (pos != plugin_name)
461     pos[0] = '.';
462   GNUNET_DISK_directory_remove (dir_name);
463
464   return ret;
465 }
466
467 /* end of test_plugin_datastore.c */
468
469