3dff4a58885ef0d47460c7499206d7cc524011f2
[oweals/gnunet.git] / src / datastore / test_datastore_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2007, 2009 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 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
34 #define VERBOSE GNUNET_NO
35
36 #define START_DATASTORE GNUNET_YES
37
38 /**
39  * How long until we give up on transmitting the message?
40  */
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
42
43 #define ITERATIONS 256
44
45 static struct GNUNET_DATASTORE_Handle *datastore;
46
47 static struct GNUNET_TIME_Absolute now;
48
49 static int ok;
50
51 /**
52  * Name of plugin under test.
53  */
54 static const char *plugin_name;
55
56 static size_t
57 get_size (int i)
58 {
59   return 8 * i;
60 }
61
62
63 static const void *
64 get_data (int i)
65 {
66   static char buf[60000];
67
68   memset (buf, i, 8 * i);
69   return buf;
70 }
71
72
73 static int
74 get_type (int i)
75 {
76   return i + 1;
77 }
78
79
80 static int
81 get_priority (int i)
82 {
83   return i + 1;
84 }
85
86
87 static int
88 get_anonymity (int i)
89 {
90   return i;
91 }
92
93
94 static struct GNUNET_TIME_Absolute
95 get_expiration (int i)
96 {
97   struct GNUNET_TIME_Absolute av;
98
99   av.abs_value = now.abs_value + 20000000 - i * 1000;
100   return av;
101 }
102
103 enum RunPhase
104 {
105   RP_DONE = 0,
106   RP_PUT = 1,
107   RP_GET = 2,
108   RP_DEL = 3,
109   RP_DO_DEL = 4,
110   RP_DELVALIDATE = 5,
111   RP_RESERVE = 6,
112   RP_PUT_MULTIPLE = 7,
113   RP_PUT_MULTIPLE_NEXT = 8,
114   RP_GET_MULTIPLE = 9,
115   RP_GET_MULTIPLE_NEXT = 10,
116   RP_UPDATE = 11,
117   RP_UPDATE_VALIDATE = 12,
118   RP_ERROR
119 };
120
121
122 struct CpsRunContext
123 {
124   GNUNET_HashCode key;
125   int i;
126   int rid;
127   const struct GNUNET_CONFIGURATION_Handle *cfg;
128   void *data;
129   size_t size;
130   enum RunPhase phase;
131   uint64_t uid;
132   uint64_t offset;
133   uint64_t first_uid;
134 };
135
136
137 static void run_continuation (void *cls,
138                               const struct GNUNET_SCHEDULER_TaskContext *tc);
139
140
141 static void
142 check_success (void *cls, int success, const char *msg)
143 {
144   struct CpsRunContext *crc = cls;
145
146   if (GNUNET_OK != success)
147   {
148     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
149                 "Operation %d/%d not successfull: `%s'\n", crc->phase, crc->i,
150                 msg);
151     crc->phase = RP_ERROR;
152   }
153   GNUNET_free_non_null (crc->data);
154   crc->data = NULL;
155   GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
156                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
157 }
158
159
160 static void
161 get_reserved (void *cls, int success, const char *msg)
162 {
163   struct CpsRunContext *crc = cls;
164
165   if (0 >= success)
166     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error obtaining reservation: `%s'\n",
167                 msg);
168   GNUNET_assert (0 < success);
169   crc->rid = success;
170   GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
171                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
172 }
173
174
175 static void
176 check_value (void *cls, const GNUNET_HashCode * key, size_t size,
177              const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
178              uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
179              uint64_t uid)
180 {
181   struct CpsRunContext *crc = cls;
182   int i;
183
184   i = crc->i;
185 #if 0
186   fprintf (stderr, "Check value got `%s' of size %u, type %d, expire %llu\n",
187            GNUNET_h2s (key), (unsigned int) size, type,
188            (unsigned long long) expiration.abs_value);
189   fprintf (stderr,
190            "Check value iteration %d wants size %u, type %d, expire %llu\n", i,
191            (unsigned int) get_size (i), get_type (i),
192            (unsigned long long) get_expiration (i).abs_value);
193 #endif
194   GNUNET_assert (size == get_size (i));
195   GNUNET_assert (0 == memcmp (data, get_data (i), size));
196   GNUNET_assert (type == get_type (i));
197   GNUNET_assert (priority == get_priority (i));
198   GNUNET_assert (anonymity == get_anonymity (i));
199   GNUNET_assert (expiration.abs_value == get_expiration (i).abs_value);
200   crc->offset++;
201   if (crc->i == 0)
202   {
203     crc->phase = RP_DEL;
204     crc->i = ITERATIONS;
205   }
206   GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
207                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
208 }
209
210
211 static void
212 delete_value (void *cls, const GNUNET_HashCode * key, size_t size,
213               const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
214               uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
215               uint64_t uid)
216 {
217   struct CpsRunContext *crc = cls;
218
219   GNUNET_assert (crc->data == NULL);
220   GNUNET_assert (NULL != key);
221   crc->size = size;
222   crc->key = *key;
223   crc->data = GNUNET_malloc (size);
224   memcpy (crc->data, data, size);
225   crc->phase = RP_DO_DEL;
226   GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
227                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
228 }
229
230
231 static void
232 check_nothing (void *cls, const GNUNET_HashCode * key, size_t size,
233                const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
234                uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
235                uint64_t uid)
236 {
237   struct CpsRunContext *crc = cls;
238
239   GNUNET_assert (key == NULL);
240   if (crc->i == 0)
241     crc->phase = RP_RESERVE;
242   GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
243                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
244 }
245
246
247 static void
248 check_multiple (void *cls, const GNUNET_HashCode * key, size_t size,
249                 const void *data, enum GNUNET_BLOCK_Type type,
250                 uint32_t priority, uint32_t anonymity,
251                 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
252 {
253   struct CpsRunContext *crc = cls;
254
255   GNUNET_assert (key != NULL);
256   switch (crc->phase)
257   {
258   case RP_GET_MULTIPLE:
259     crc->phase = RP_GET_MULTIPLE_NEXT;
260     crc->first_uid = uid;
261     crc->offset++;
262     break;
263   case RP_GET_MULTIPLE_NEXT:
264     GNUNET_assert (uid != crc->first_uid);
265     crc->phase = RP_UPDATE;
266     break;
267   default:
268     GNUNET_break (0);
269     crc->phase = RP_ERROR;
270     break;
271   }
272   if (priority == get_priority (42))
273     crc->uid = uid;
274   GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
275                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
276 }
277
278
279 static void
280 check_update (void *cls, const GNUNET_HashCode * key, size_t size,
281               const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
282               uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
283               uint64_t uid)
284 {
285   struct CpsRunContext *crc = cls;
286
287   GNUNET_assert (key != NULL);
288   if ((anonymity == get_anonymity (42)) && (size == get_size (42)) &&
289       (priority == get_priority (42) + 100))
290     crc->phase = RP_DONE;
291   else
292   {
293     GNUNET_assert (size == get_size (43));
294     crc->offset++;
295   }
296   GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
297                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
298 }
299
300
301 static void
302 run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
303 {
304   struct CpsRunContext *crc = cls;
305
306   ok = (int) crc->phase;
307 #if VERBOSE
308   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test in phase %u\n", crc->phase);
309 #endif
310   switch (crc->phase)
311   {
312   case RP_PUT:
313 #if VERBOSE
314     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "PUT",
315                 crc->i);
316 #endif
317     GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
318     GNUNET_DATASTORE_put (datastore, 0, &crc->key, get_size (crc->i),
319                           get_data (crc->i), get_type (crc->i),
320                           get_priority (crc->i), get_anonymity (crc->i), 0,
321                           get_expiration (crc->i), 1, 1, TIMEOUT,
322                           &check_success, crc);
323     crc->i++;
324     if (crc->i == ITERATIONS)
325       crc->phase = RP_GET;
326     break;
327   case RP_GET:
328     crc->i--;
329 #if VERBOSE
330     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET",
331                 crc->i);
332 #endif
333     GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
334     GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
335                               get_type (crc->i), 1, 1, TIMEOUT, &check_value,
336                               crc);
337     break;
338   case RP_DEL:
339     crc->i--;
340 #if VERBOSE
341     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "DEL",
342                 crc->i);
343 #endif
344     crc->data = NULL;
345     GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
346     GNUNET_assert (NULL !=
347                    GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
348                                              get_type (crc->i), 1, 1, TIMEOUT,
349                                              &delete_value, crc));
350     break;
351   case RP_DO_DEL:
352 #if VERBOSE
353     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "DO_DEL",
354                 crc->i);
355 #endif
356     if (crc->i == 0)
357     {
358       crc->i = ITERATIONS;
359       crc->phase = RP_DELVALIDATE;
360     }
361     else
362     {
363       crc->phase = RP_DEL;
364     }
365     GNUNET_assert (NULL !=
366                    GNUNET_DATASTORE_remove (datastore, &crc->key, crc->size,
367                                             crc->data, 1, 1, TIMEOUT,
368                                             &check_success, crc));
369     break;
370   case RP_DELVALIDATE:
371     crc->i--;
372 #if VERBOSE
373     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n",
374                 "DEL-VALIDATE", crc->i);
375 #endif
376     GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
377     GNUNET_assert (NULL !=
378                    GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
379                                              get_type (crc->i), 1, 1, TIMEOUT,
380                                              &check_nothing, crc));
381     break;
382   case RP_RESERVE:
383     crc->phase = RP_PUT_MULTIPLE;
384     GNUNET_DATASTORE_reserve (datastore, 128 * 1024, 2, 1, 1, TIMEOUT,
385                               &get_reserved, crc);
386     break;
387   case RP_PUT_MULTIPLE:
388     crc->phase = RP_PUT_MULTIPLE_NEXT;
389     GNUNET_DATASTORE_put (datastore, crc->rid, &crc->key, get_size (42),
390                           get_data (42), get_type (42), get_priority (42),
391                           get_anonymity (42), 0, get_expiration (42), 1, 1,
392                           TIMEOUT, &check_success, crc);
393     break;
394   case RP_PUT_MULTIPLE_NEXT:
395     crc->phase = RP_GET_MULTIPLE;
396     GNUNET_DATASTORE_put (datastore, crc->rid, &crc->key, get_size (43),
397                           get_data (43), get_type (42), get_priority (43),
398                           get_anonymity (43), 0, get_expiration (43), 1, 1,
399                           TIMEOUT, &check_success, crc);
400     break;
401   case RP_GET_MULTIPLE:
402     GNUNET_assert (NULL !=
403                    GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
404                                              get_type (42), 1, 1, TIMEOUT,
405                                              &check_multiple, crc));
406     break;
407   case RP_GET_MULTIPLE_NEXT:
408     GNUNET_assert (NULL !=
409                    GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
410                                              get_type (42), 1, 1, TIMEOUT,
411                                              &check_multiple, crc));
412     break;
413   case RP_UPDATE:
414     GNUNET_assert (crc->uid > 0);
415     crc->phase = RP_UPDATE_VALIDATE;
416     GNUNET_DATASTORE_update (datastore, crc->uid, 100, get_expiration (42), 1,
417                              1, TIMEOUT, &check_success, crc);
418     break;
419   case RP_UPDATE_VALIDATE:
420     GNUNET_assert (NULL !=
421                    GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
422                                              get_type (42), 1, 1, TIMEOUT,
423                                              &check_update, crc));
424     break;
425   case RP_DONE:
426 #if VERBOSE
427     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n");
428 #endif
429     GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
430     GNUNET_free (crc);
431     ok = 0;
432     break;
433   case RP_ERROR:
434     GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
435     GNUNET_free (crc);
436     ok = 43;
437     break;
438   }
439 }
440
441
442 static void
443 run_tests (void *cls, int32_t success, const char *msg)
444 {
445   struct CpsRunContext *crc = cls;
446
447   switch (success)
448   {
449   case GNUNET_YES:
450     GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
451                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
452     return;
453   case GNUNET_NO:
454     fprintf (stderr, "Test 'put' operation failed, key already exists (!?)\n");
455     GNUNET_free (crc);
456     return;
457   case GNUNET_SYSERR:
458     fprintf (stderr,
459              "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
460              msg);
461     GNUNET_free (crc);
462     return;
463   default:
464     GNUNET_assert (0);
465   }
466 }
467
468
469 static void
470 run (void *cls, char *const *args, const char *cfgfile,
471      const struct GNUNET_CONFIGURATION_Handle *cfg)
472 {
473   struct CpsRunContext *crc;
474   static GNUNET_HashCode zkey;
475
476   crc = GNUNET_malloc (sizeof (struct CpsRunContext));
477   crc->cfg = cfg;
478   crc->phase = RP_PUT;
479   now = GNUNET_TIME_absolute_get ();
480   datastore = GNUNET_DATASTORE_connect (cfg);
481   if (NULL ==
482       GNUNET_DATASTORE_put (datastore, 0, &zkey, 4, "TEST",
483                             GNUNET_BLOCK_TYPE_TEST, 0, 0, 0,
484                             GNUNET_TIME_relative_to_absolute
485                             (GNUNET_TIME_UNIT_SECONDS), 0, 1,
486                             GNUNET_TIME_UNIT_MINUTES, &run_tests, crc))
487   {
488     fprintf (stderr, "Test 'put' operation failed.\n");
489     ok = 1;
490     GNUNET_free (crc);
491   }
492 }
493
494
495 static int
496 check ()
497 {
498   char cfg_name[128];
499
500 #if START_DATASTORE
501   struct GNUNET_OS_Process *proc;
502 #endif
503   char *const argv[] = {
504     "test-datastore-api",
505     "-c",
506     cfg_name,
507 #if VERBOSE
508     "-L", "DEBUG",
509 #endif
510     NULL
511   };
512   struct GNUNET_GETOPT_CommandLineOption options[] = {
513     GNUNET_GETOPT_OPTION_END
514   };
515   GNUNET_snprintf (cfg_name, sizeof (cfg_name),
516                    "test_datastore_api_data_%s.conf", plugin_name);
517 #if START_DATASTORE
518   proc =
519       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
520                                "gnunet-service-arm",
521 #if VERBOSE
522                                "-L", "DEBUG",
523 #endif
524                                "-c", cfg_name, NULL);
525 #endif
526   GNUNET_assert (NULL != proc);
527   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
528                       "test-datastore-api", "nohelp", options, &run, NULL);
529 #if START_DATASTORE
530   sleep (1);                    /* give datastore chance to receive 'DROP' request */
531   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
532   {
533     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
534     ok = 1;
535   }
536   GNUNET_OS_process_wait (proc);
537   GNUNET_OS_process_close (proc);
538   proc = NULL;
539 #endif
540   if (ok != 0)
541     fprintf (stderr, "Missed some testcases: %u\n", ok);
542   return ok;
543 }
544
545 int
546 main (int argc, char *argv[])
547 {
548   int ret;
549   char *pos;
550   char dir_name[128];
551
552   sleep (1);
553   /* determine name of plugin to use */
554   plugin_name = argv[0];
555   while (NULL != (pos = strstr (plugin_name, "_")))
556     plugin_name = pos + 1;
557   if (NULL != (pos = strstr (plugin_name, ".")))
558     pos[0] = 0;
559   else
560     pos = (char *) plugin_name;
561
562   GNUNET_snprintf (dir_name, sizeof (dir_name), "/tmp/test-gnunet-datastore-%s",
563                    plugin_name);
564   GNUNET_DISK_directory_remove (dir_name);
565   GNUNET_log_setup ("test-datastore-api",
566 #if VERBOSE
567                     "DEBUG",
568 #else
569                     "WARNING",
570 #endif
571                     NULL);
572   ret = check ();
573   if (pos != plugin_name)
574     pos[0] = '.';
575   GNUNET_DISK_directory_remove (dir_name);
576   return ret;
577 }
578
579 /* end of test_datastore_api.c */