guix-env: some update.
[oweals/gnunet.git] / src / datastore / test_datastore_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2004, 2005, 2006, 2007, 2009, 2015 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.c
22  * @brief Test for the basic datastore API.
23  * @author Christian Grothoff
24  *
25  * TODO:
26  * - test reservation failure
27  */
28
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_datastore_service.h"
33 #include "gnunet_datastore_plugin.h"
34 #include "gnunet_testing_lib.h"
35
36
37 /**
38  * How long until we give up on transmitting the message?
39  */
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
41
42 #define ITERATIONS 256
43
44 /**
45  * Handle to the datastore.
46  */
47 static struct GNUNET_DATASTORE_Handle *datastore;
48
49 static struct GNUNET_TIME_Absolute now;
50
51 /**
52  * Value we return from #main().
53  */
54 static int ok;
55
56 /**
57  * Name of plugin under test.
58  */
59 static const char *plugin_name;
60
61
62 static size_t
63 get_size (int i)
64 {
65   return 8 * i;
66 }
67
68
69 static const void *
70 get_data (int i)
71 {
72   static char buf[60000];
73
74   memset (buf, i, 8 * i);
75   return buf;
76 }
77
78
79 static int
80 get_type (int i)
81 {
82   return i + 1;
83 }
84
85
86 static int
87 get_priority (int i)
88 {
89   return i + 1;
90 }
91
92
93 static int
94 get_anonymity (int i)
95 {
96   return i;
97 }
98
99
100 static struct GNUNET_TIME_Absolute
101 get_expiration (int i)
102 {
103   struct GNUNET_TIME_Absolute av;
104
105   av.abs_value_us = now.abs_value_us + 20000000000LL - i * 1000 * 1000LL;
106   return av;
107 }
108
109
110 /**
111  * Which phase of the process are we in?
112  */
113 enum RunPhase
114 {
115   /**
116    * We are done (shutting down normally).
117    */
118   RP_DONE = 0,
119
120   /**
121    * We are adding new entries to the datastore.
122    */
123   RP_PUT = 1,
124   RP_GET = 2,
125   RP_DEL = 3,
126   RP_DO_DEL = 4,
127   RP_DELVALIDATE = 5,
128   RP_RESERVE = 6,
129   RP_PUT_MULTIPLE = 7,
130   RP_PUT_MULTIPLE_NEXT = 8,
131   RP_GET_MULTIPLE = 9,
132   RP_GET_MULTIPLE_NEXT = 10,
133
134   /**
135    * Execution failed with some kind of error.
136    */
137   RP_ERROR
138 };
139
140
141 /**
142  * Closure we give to all of the functions executing the
143  * benchmark.  Could right now be global, but this allows
144  * us to theoretically run multiple clients "in parallel".
145  */
146 struct CpsRunContext
147 {
148   /**
149    * Execution phase we are in.
150    */
151   enum RunPhase phase;
152
153   struct GNUNET_HashCode key;
154   int i;
155   int rid;
156   void *data;
157   size_t size;
158
159   uint64_t first_uid;
160 };
161
162
163 /**
164  * Main state machine.  Executes the next step of the test
165  * depending on the current state.
166  *
167  * @param cls the `struct CpsRunContext`
168  */
169 static void
170 run_continuation (void *cls);
171
172
173 /**
174  * Continuation called to notify client about result of an
175  * operation.  Checks for errors, updates our iteration counters and
176  * continues execution with #run_continuation().
177  *
178  * @param cls the `struct CpsRunContext`
179  * @param success #GNUNET_SYSERR on failure
180  * @param min_expiration minimum expiration time required for content to be stored
181  *                by the datacache at this time, zero for unknown
182  * @param msg NULL on success, otherwise an error message
183  */
184 static void
185 check_success (void *cls,
186                int success,
187                struct GNUNET_TIME_Absolute min_expiration,
188                const char *msg)
189 {
190   struct CpsRunContext *crc = cls;
191
192   if (GNUNET_OK != success)
193   {
194     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
195                 "Operation %d/%d not successful: `%s'\n",
196                 crc->phase,
197                 crc->i,
198                 msg);
199     crc->phase = RP_ERROR;
200   }
201   GNUNET_free_non_null (crc->data);
202   crc->data = NULL;
203   GNUNET_SCHEDULER_add_now (&run_continuation, crc);
204 }
205
206
207 static void
208 get_reserved (void *cls,
209               int success,
210               struct GNUNET_TIME_Absolute min_expiration,
211               const char *msg)
212 {
213   struct CpsRunContext *crc = cls;
214
215   if (0 >= success)
216     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
217                 "Error obtaining reservation: `%s'\n",
218                 msg);
219   GNUNET_assert (0 < success);
220   crc->rid = success;
221   GNUNET_SCHEDULER_add_now (&run_continuation,
222                             crc);
223 }
224
225
226 static void
227 check_value (void *cls,
228              const struct GNUNET_HashCode *key,
229              size_t size,
230              const void *data,
231              enum GNUNET_BLOCK_Type type,
232              uint32_t priority,
233              uint32_t anonymity,
234              uint32_t replication,
235              struct GNUNET_TIME_Absolute expiration,
236              uint64_t uid)
237 {
238   struct CpsRunContext *crc = cls;
239   int i;
240
241   i = crc->i;
242   if (NULL == key)
243   {
244     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
245                 "Value check failed (got NULL key) in %d/%d\n",
246                 crc->phase,
247                 crc->i);
248     crc->phase = RP_ERROR;
249     GNUNET_SCHEDULER_add_now (&run_continuation,
250                               crc);
251     return;
252   }
253 #if 0
254   FPRINTF (stderr,
255            "Check value got `%s' of size %u, type %d, expire %s\n",
256            GNUNET_h2s (key), (unsigned int) size, type,
257            GNUNET_STRINGS_absolute_time_to_string (expiration));
258   FPRINTF (stderr,
259            "Check value iteration %d wants size %u, type %d, expire %s\n", i,
260            (unsigned int) get_size (i), get_type (i),
261            GNUNET_STRINGS_absolute_time_to_string (get_expiration(i)));
262 #endif
263   GNUNET_assert (size == get_size (i));
264   GNUNET_assert (0 == memcmp (data, get_data (i), size));
265   GNUNET_assert (type == get_type (i));
266   GNUNET_assert (priority == get_priority (i));
267   GNUNET_assert (anonymity == get_anonymity (i));
268   GNUNET_assert (expiration.abs_value_us == get_expiration (i).abs_value_us);
269   if (crc->i == 0)
270   {
271     crc->phase = RP_DEL;
272     crc->i = ITERATIONS;
273   }
274   GNUNET_SCHEDULER_add_now (&run_continuation,
275                             crc);
276 }
277
278
279 static void
280 delete_value (void *cls,
281               const struct GNUNET_HashCode *key,
282               size_t size,
283               const void *data,
284               enum GNUNET_BLOCK_Type type,
285               uint32_t priority,
286               uint32_t anonymity,
287               uint32_t replication,
288               struct GNUNET_TIME_Absolute expiration,
289               uint64_t uid)
290 {
291   struct CpsRunContext *crc = cls;
292
293   GNUNET_assert (NULL == crc->data);
294   GNUNET_assert (NULL != key);
295   crc->size = size;
296   crc->key = *key;
297   crc->data = GNUNET_malloc (size);
298   GNUNET_memcpy (crc->data, data, size);
299   crc->phase = RP_DO_DEL;
300   GNUNET_SCHEDULER_add_now (&run_continuation,
301                             crc);
302 }
303
304
305 static void
306 check_nothing (void *cls,
307                const struct GNUNET_HashCode *key,
308                size_t size,
309                const void *data,
310                enum GNUNET_BLOCK_Type type,
311                uint32_t priority,
312                uint32_t anonymity,
313                uint32_t replication,
314                struct GNUNET_TIME_Absolute expiration,
315                uint64_t uid)
316 {
317   struct CpsRunContext *crc = cls;
318
319   GNUNET_assert (key == NULL);
320   if (crc->i == 0)
321     crc->phase = RP_RESERVE;
322   GNUNET_SCHEDULER_add_now (&run_continuation,
323                             crc);
324 }
325
326
327 static void
328 check_multiple (void *cls,
329                 const struct GNUNET_HashCode *key,
330                 size_t size,
331                 const void *data,
332                 enum GNUNET_BLOCK_Type type,
333                 uint32_t priority,
334                 uint32_t anonymity,
335                 uint32_t replication,
336                 struct GNUNET_TIME_Absolute expiration,
337                 uint64_t uid)
338 {
339   struct CpsRunContext *crc = cls;
340
341   GNUNET_assert (key != NULL);
342   switch (crc->phase)
343   {
344   case RP_GET_MULTIPLE:
345     crc->phase = RP_GET_MULTIPLE_NEXT;
346     crc->first_uid = uid;
347     break;
348   case RP_GET_MULTIPLE_NEXT:
349     GNUNET_assert (uid != crc->first_uid);
350     crc->phase = RP_DONE;
351     break;
352   default:
353     GNUNET_break (0);
354     crc->phase = RP_ERROR;
355     break;
356   }
357   GNUNET_SCHEDULER_add_now (&run_continuation, crc);
358 }
359
360
361 /**
362  * Main state machine.  Executes the next step of the test
363  * depending on the current state.
364  *
365  * @param cls the `struct CpsRunContext`
366  */
367 static void
368 run_continuation (void *cls)
369 {
370   struct CpsRunContext *crc = cls;
371
372   ok = (int) crc->phase;
373   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374               "Test in phase %u\n",
375               crc->phase);
376   switch (crc->phase)
377   {
378   case RP_PUT:
379     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380                 "Executing PUT number %u\n",
381                 crc->i);
382     GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
383     GNUNET_DATASTORE_put (datastore, 0, &crc->key, get_size (crc->i),
384                           get_data (crc->i), get_type (crc->i),
385                           get_priority (crc->i), get_anonymity (crc->i), 0,
386                           get_expiration (crc->i), 1, 1,
387                           &check_success, crc);
388     crc->i++;
389     if (crc->i == ITERATIONS)
390       crc->phase = RP_GET;
391     break;
392   case RP_GET:
393     crc->i--;
394     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395                 "Executing GET number %u\n",
396                 crc->i);
397     GNUNET_CRYPTO_hash (&crc->i,
398                         sizeof (int),
399                         &crc->key);
400     GNUNET_DATASTORE_get_key (datastore,
401                               0,
402                               false,
403                               &crc->key,
404                               get_type (crc->i),
405                               1,
406                               1,
407                               &check_value,
408                               crc);
409     break;
410   case RP_DEL:
411     crc->i--;
412     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413                 "Executing DEL number %u\n",
414                 crc->i);
415     crc->data = NULL;
416     GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
417     GNUNET_assert (NULL !=
418                    GNUNET_DATASTORE_get_key (datastore,
419                                              0,
420                                              false,
421                                              &crc->key,
422                                              get_type (crc->i),
423                                              1,
424                                              1,
425                                              &delete_value,
426                                              crc));
427     break;
428   case RP_DO_DEL:
429     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
430                 "Executing DO_DEL number %u\n",
431                 crc->i);
432     if (crc->i == 0)
433     {
434       crc->i = ITERATIONS;
435       crc->phase = RP_DELVALIDATE;
436     }
437     else
438     {
439       crc->phase = RP_DEL;
440     }
441     GNUNET_assert (NULL !=
442                    GNUNET_DATASTORE_remove (datastore, &crc->key, crc->size,
443                                             crc->data, 1, 1,
444                                             &check_success, crc));
445     break;
446   case RP_DELVALIDATE:
447     crc->i--;
448     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
449                 "Executing DELVALIDATE number %u\n",
450                 crc->i);
451     GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
452     GNUNET_assert (NULL !=
453                    GNUNET_DATASTORE_get_key (datastore,
454                                              0,
455                                              false,
456                                              &crc->key,
457                                              get_type (crc->i),
458                                              1,
459                                              1,
460                                              &check_nothing,
461                                              crc));
462     break;
463   case RP_RESERVE:
464     crc->phase = RP_PUT_MULTIPLE;
465     GNUNET_DATASTORE_reserve (datastore, 128 * 1024, 2,
466                               &get_reserved, crc);
467     break;
468   case RP_PUT_MULTIPLE:
469     crc->phase = RP_PUT_MULTIPLE_NEXT;
470     GNUNET_DATASTORE_put (datastore, crc->rid, &crc->key, get_size (42),
471                           get_data (42), get_type (42), get_priority (42),
472                           get_anonymity (42), 0, get_expiration (42), 1, 1,
473                           &check_success, crc);
474     break;
475   case RP_PUT_MULTIPLE_NEXT:
476     crc->phase = RP_GET_MULTIPLE;
477     GNUNET_DATASTORE_put (datastore, crc->rid,
478                           &crc->key,
479                           get_size (43),
480                           get_data (43),
481                           get_type (42),
482                           get_priority (43),
483                           get_anonymity (43),
484                           0,
485                           get_expiration (43),
486                           1, 1,
487                           &check_success, crc);
488     break;
489   case RP_GET_MULTIPLE:
490     GNUNET_assert (NULL !=
491                    GNUNET_DATASTORE_get_key (datastore,
492                                              0,
493                                              false,
494                                              &crc->key,
495                                              get_type (42),
496                                              1,
497                                              1,
498                                              &check_multiple,
499                                              crc));
500     break;
501   case RP_GET_MULTIPLE_NEXT:
502     GNUNET_assert (NULL !=
503                    GNUNET_DATASTORE_get_key (datastore,
504                                              crc->first_uid + 1,
505                                              false,
506                                              &crc->key,
507                                              get_type (42),
508                                              1,
509                                              1,
510                                              &check_multiple,
511                                              crc));
512     break;
513   case RP_DONE:
514     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
515                 "Finished, disconnecting\n");
516     GNUNET_DATASTORE_disconnect (datastore,
517                                  GNUNET_YES);
518     GNUNET_free (crc);
519     ok = 0;
520     break;
521   case RP_ERROR:
522     GNUNET_DATASTORE_disconnect (datastore,
523                                  GNUNET_YES);
524     GNUNET_free (crc);
525     ok = 43;
526     break;
527   }
528 }
529
530
531 /**
532  * Function called with the result of the initial PUT operation.  If
533  * the PUT succeeded, we start the actual benchmark loop, otherwise we
534  * bail out with an error.
535  *
536  *
537  * @param cls closure
538  * @param success #GNUNET_SYSERR on failure
539  * @param min_expiration minimum expiration time required for content to be stored
540  *                by the datacache at this time, zero for unknown
541  * @param msg NULL on success, otherwise an error message
542  */
543 static void
544 run_tests (void *cls,
545            int32_t success,
546            struct GNUNET_TIME_Absolute min_expiration,
547            const char *msg)
548 {
549   struct CpsRunContext *crc = cls;
550
551   switch (success)
552   {
553   case GNUNET_YES:
554     GNUNET_SCHEDULER_add_now (&run_continuation,
555                               crc);
556     return;
557   case GNUNET_NO:
558     FPRINTF (stderr,
559              "%s", "Test 'put' operation failed, key already exists (!?)\n");
560     GNUNET_DATASTORE_disconnect (datastore,
561                                  GNUNET_YES);
562     GNUNET_free (crc);
563     return;
564   case GNUNET_SYSERR:
565     FPRINTF (stderr,
566              "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
567              msg);
568     GNUNET_DATASTORE_disconnect (datastore,
569                                  GNUNET_YES);
570     GNUNET_free (crc);
571     return;
572   default:
573     GNUNET_assert (0);
574   }
575 }
576
577
578 /**
579  * Beginning of the actual execution of the benchmark.
580  * Performs a first test operation (PUT) to verify that
581  * the plugin works at all.
582  *
583  * @param cls NULL
584  * @param cfg configuration to use
585  * @param peer peer handle (unused)
586  */
587 static void
588 run (void *cls,
589      const struct GNUNET_CONFIGURATION_Handle *cfg,
590      struct GNUNET_TESTING_Peer *peer)
591 {
592   struct CpsRunContext *crc;
593   static struct GNUNET_HashCode zkey;
594
595   crc = GNUNET_new (struct CpsRunContext);
596   crc->phase = RP_PUT;
597   now = GNUNET_TIME_absolute_get ();
598   datastore = GNUNET_DATASTORE_connect (cfg);
599   if (NULL ==
600       GNUNET_DATASTORE_put (datastore,
601                             0,
602                             &zkey,
603                             4,
604                             "TEST",
605                             GNUNET_BLOCK_TYPE_TEST,
606                             0, 0, 0,
607                             GNUNET_TIME_relative_to_absolute
608                             (GNUNET_TIME_UNIT_SECONDS),
609                             0, 1,
610                             &run_tests, crc))
611   {
612     FPRINTF (stderr,
613              "%s",
614              "Test 'put' operation failed.\n");
615     ok = 1;
616     GNUNET_free (crc);
617   }
618 }
619
620
621 /**
622  * Function invoked to notify service of disk utilization
623  * changes.
624  *
625  * @param cls closure
626  * @param delta change in disk utilization,
627  *        0 for "reset to empty"
628  */
629 static void
630 duc_dummy (void *cls,
631            int delta)
632 {
633   /* intentionally empty */
634 }
635
636
637 /**
638  * check if plugin is actually working 
639  */
640 static int
641 test_plugin (const char *cfg_name)
642 {
643   char libname[128];
644   struct GNUNET_CONFIGURATION_Handle *cfg;
645   struct GNUNET_DATASTORE_PluginFunctions *api;
646   struct GNUNET_DATASTORE_PluginEnvironment env;
647   
648   cfg = GNUNET_CONFIGURATION_create ();
649   if (GNUNET_OK !=
650       GNUNET_CONFIGURATION_load (cfg,
651                                  cfg_name))
652   {
653     GNUNET_CONFIGURATION_destroy (cfg);
654     fprintf (stderr,
655              "Failed to load configuration %s\n",
656              cfg_name);
657     return 1;
658   }
659   memset (&env, 0, sizeof (env));
660   env.cfg = cfg;
661   env.duc = &duc_dummy;
662   GNUNET_snprintf (libname,
663                    sizeof (libname),
664                    "libgnunet_plugin_datastore_%s",
665                    plugin_name);
666   api = GNUNET_PLUGIN_load (libname, &env);
667   if (NULL == api)
668   {
669     GNUNET_CONFIGURATION_destroy (cfg);
670     fprintf (stderr,
671              "Failed to load plugin `%s'\n",
672              libname);
673     return 77;
674   }
675   GNUNET_PLUGIN_unload (libname, api);
676   GNUNET_CONFIGURATION_destroy (cfg);
677   return 0;
678 }
679
680
681 /**
682  * Entry point into the test. Determines which configuration / plugin
683  * we are running with based on the name of the binary and starts
684  * the peer.
685  *
686  * @param argc should be 1
687  * @param argv used to determine plugin / configuration name.
688  * @return 0 on success
689  */
690 int
691 main (int argc,
692       char *argv[])
693 {
694   char cfg_name[128];
695   int ret;
696     
697   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
698   GNUNET_snprintf (cfg_name,
699                    sizeof (cfg_name),
700                    "test_datastore_api_data_%s.conf",
701                    plugin_name);
702   ret = test_plugin (cfg_name);
703   if (0 != ret)
704     return ret;
705   /* run actual test */
706   if (0 !=
707       GNUNET_TESTING_peer_run ("test-gnunet-datastore",
708                                cfg_name,
709                                &run,
710                                NULL))
711     return 1;
712   return ok;
713 }
714
715 /* end of test_datastore_api.c */