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