de-experimentalizing
[oweals/gnunet.git] / src / datastore / test_datastore_api_management.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_management.c
22  * @brief Test for the space management functions of the datastore implementation.
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_service.h"
30
31 #define VERBOSE GNUNET_NO
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 static struct GNUNET_DATASTORE_Handle *datastore;
45
46 static struct GNUNET_TIME_Absolute now;
47
48 static int ok;
49
50 static const char* plugin_name;
51
52 static size_t
53 get_size (int i)
54 {
55   return 8 + 8 * (i % 256);
56 }
57
58
59 static const void *
60 get_data (int i)
61 {
62   static char buf[60000]; 
63   memset (buf, i, 8 + 8 * (i % 256));
64   return buf;
65 }
66
67
68 static int
69 get_type(int i)
70 {
71   return 1;
72 }
73
74
75 static int 
76 get_priority (int i)
77 {
78   return i+1;
79 }
80
81
82 static int
83 get_anonymity(int i)
84 {
85   return i;
86 }
87
88
89 static struct GNUNET_TIME_Absolute 
90 get_expiration (int i)
91 {
92   struct GNUNET_TIME_Absolute av;
93
94   av.abs_value = now.abs_value + i * 1000;
95   return av;
96 }
97
98 enum RunPhase
99   {
100     RP_DONE = 0,
101     RP_PUT,
102     RP_GET,
103     RP_GET_FAIL
104   };
105
106
107 struct CpsRunContext
108 {
109   GNUNET_HashCode key;
110   int i;
111   int found;
112   const struct GNUNET_CONFIGURATION_Handle *cfg;
113   void *data;
114   enum RunPhase phase;
115 };
116
117
118 static void
119 run_continuation (void *cls,
120                   const struct GNUNET_SCHEDULER_TaskContext *tc);
121
122
123 static void
124 check_success (void *cls,
125                int success,
126                const char *msg)
127 {
128   struct CpsRunContext *crc = cls;
129   if (GNUNET_OK != success)
130     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
131                 "%s\n", msg);
132   GNUNET_assert (GNUNET_OK == success);
133   GNUNET_free_non_null (crc->data);
134   crc->data = NULL;
135   GNUNET_SCHEDULER_add_continuation (&run_continuation,
136                                      crc,
137                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
138 }
139
140
141 static void 
142 check_value (void *cls,
143              const GNUNET_HashCode * key,
144              size_t size,
145              const void *data,
146              enum GNUNET_BLOCK_Type type,
147              uint32_t priority,
148              uint32_t anonymity,
149              struct GNUNET_TIME_Absolute
150              expiration, uint64_t uid)
151 {
152   struct CpsRunContext *crc = cls;
153   int i;
154
155   if (key == NULL)
156     {
157       crc->i--;
158       if (crc->found == GNUNET_YES)
159         {
160           crc->phase = RP_GET;
161           crc->found = GNUNET_NO;
162         }
163       else
164         {
165           fprintf (stderr,
166                    "First not found was %u\n", crc->i);
167           crc->phase = RP_GET_FAIL;
168         }
169       if (0 == crc->i)
170         crc->phase = RP_DONE;
171       GNUNET_SCHEDULER_add_continuation (&run_continuation,
172                                          crc,
173                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
174       return;
175     }
176   i = crc->i;
177   crc->found = GNUNET_YES;
178   GNUNET_assert (size == get_size (i));
179   GNUNET_assert (0 == memcmp (data, get_data(i), size));
180   GNUNET_assert (type == get_type (i));
181   GNUNET_assert (priority == get_priority (i));
182   GNUNET_assert (anonymity == get_anonymity(i));
183   GNUNET_assert (expiration.abs_value == get_expiration(i).abs_value);
184   GNUNET_DATASTORE_iterate_get_next (datastore);
185 }
186
187
188 static void 
189 check_nothing (void *cls,
190                const GNUNET_HashCode * key,
191                size_t size,
192                const void *data,
193                enum GNUNET_BLOCK_Type type,
194                uint32_t priority,
195                uint32_t anonymity,
196                struct GNUNET_TIME_Absolute
197                expiration, uint64_t uid)
198 {
199   struct CpsRunContext *crc = cls;
200
201   GNUNET_assert (key == NULL);
202   if (0 == --crc->i)
203     crc->phase = RP_DONE;
204   GNUNET_SCHEDULER_add_continuation (&run_continuation,
205                                      crc,
206                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
207 }
208
209
210 static void
211 run_continuation (void *cls,
212                   const struct GNUNET_SCHEDULER_TaskContext *tc)
213 {
214   struct CpsRunContext *crc = cls;
215   ok = (int) crc->phase;
216   switch (crc->phase)
217     {
218     case RP_PUT:
219 #if VERBOSE
220       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221                   "Executing `%s' number %u\n",
222                   "PUT",
223                   crc->i);
224 #endif
225       GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
226       GNUNET_DATASTORE_put (datastore,
227                             0,
228                             &crc->key,
229                             get_size (crc->i),
230                             get_data (crc->i),
231                             get_type (crc->i),
232                             get_priority (crc->i),
233                             get_anonymity (crc->i),
234                             0,
235                             get_expiration (crc->i),
236                             1, 1, TIMEOUT,
237                             &check_success,
238                             crc);
239       crc->i++;
240       if (crc->i == ITERATIONS)
241         {
242           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
243                       "Sleeping to give datastore time to clean up\n");
244           sleep (5);
245           crc->phase = RP_GET;
246           crc->i--;
247         }
248       break;
249     case RP_GET:
250 #if VERBOSE
251       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252                   "Executing `%s' number %u\n",
253                   "GET",
254                   crc->i);
255 #endif
256       GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
257       GNUNET_DATASTORE_iterate_key (datastore, 
258                                     &crc->key,
259                                     get_type (crc->i),
260                                     1, 1, TIMEOUT,
261                                     &check_value,
262                                     crc);
263       break;
264     case RP_GET_FAIL:
265 #if VERBOSE
266       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
267                   "Executing `%s' number %u\n",
268                   "GET",
269                   crc->i);
270 #endif
271       GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
272       GNUNET_DATASTORE_iterate_key (datastore, 
273                                     &crc->key,
274                                     get_type (crc->i),
275                                     1, 1, TIMEOUT,
276                                     &check_nothing,
277                                     crc);
278       break;
279     case RP_DONE:
280       GNUNET_assert (0 == crc->i);
281 #if VERBOSE
282       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
283                   "Finished, disconnecting\n");
284 #endif
285       GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
286       GNUNET_free (crc);
287       ok = 0;
288     }
289 }
290
291
292 static void
293 run_tests (void *cls,
294            int success,
295            const char *msg)
296 {
297   struct CpsRunContext *crc = cls;
298
299   if (success != GNUNET_YES)
300     {
301       fprintf (stderr,
302                "Test 'put' operation failed with error `%s' database likely not setup, skipping test.",
303                msg);
304       GNUNET_free (crc);
305       return;
306     }
307   GNUNET_SCHEDULER_add_continuation (&run_continuation,
308                                      crc,
309                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
310 }
311
312
313 static void
314 run (void *cls,
315      char *const *args,
316      const char *cfgfile,
317      const struct GNUNET_CONFIGURATION_Handle *cfg)
318 {
319   struct CpsRunContext *crc;
320   static GNUNET_HashCode zkey;
321
322   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
323   crc->cfg = cfg;
324   crc->phase = RP_PUT;
325   now = GNUNET_TIME_absolute_get ();
326   datastore = GNUNET_DATASTORE_connect (cfg);
327   if (NULL ==
328       GNUNET_DATASTORE_put (datastore, 0,
329                             &zkey, 4, "TEST",
330                             GNUNET_BLOCK_TYPE_TEST,
331                             0, 0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
332                             0, 1, GNUNET_TIME_UNIT_MINUTES,
333                             &run_tests, crc))
334     {
335       fprintf (stderr,
336                "Test 'put' operation failed.\n");
337       GNUNET_free (crc);
338       ok = 1;
339     }
340 }
341
342
343
344 static int
345 check ()
346 {
347   struct GNUNET_OS_Process *proc;
348   char cfg_name[128];
349   char *const argv[] = { 
350     "test-datastore-api-management",
351     "-c",
352     cfg_name,
353 #if VERBOSE
354     "-L", "DEBUG",
355 #endif
356     NULL
357   };
358   struct GNUNET_GETOPT_CommandLineOption options[] = {
359     GNUNET_GETOPT_OPTION_END
360   };
361   GNUNET_snprintf (cfg_name,
362                    sizeof (cfg_name),
363                    "test_datastore_api_data_%s.conf",
364                    plugin_name);
365   proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
366                                  "gnunet-service-arm",
367 #if VERBOSE
368                                  "-L", "DEBUG",
369 #endif
370                                  "-c", cfg_name, NULL);
371   GNUNET_assert (NULL != proc);
372   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
373                       argv, "test-datastore-api", "nohelp",
374                       options, &run, NULL);
375   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
376     {
377       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
378       ok = 1;
379     }
380   GNUNET_OS_process_wait (proc);
381   GNUNET_OS_process_close (proc);
382   proc = NULL;
383   if (ok != 0)
384     fprintf (stderr, "Missed some testcases: %u\n", ok);
385   return ok;
386 }
387
388 int
389 main (int argc, char *argv[])
390 {
391   int ret;
392   
393   char *pos;
394   char dir_name[128];
395
396   /* determine name of plugin to use */
397   plugin_name = argv[0];
398   while (NULL != (pos = strstr(plugin_name, "_")))
399     plugin_name = pos+1;
400   if (NULL != (pos = strstr(plugin_name, ".")))
401     pos[0] = 0;
402   else
403     pos = (char *) plugin_name;
404
405   GNUNET_snprintf (dir_name,
406                    sizeof (dir_name),
407                    "/tmp/test-gnunet-datastore-%s",
408                    plugin_name);
409   GNUNET_DISK_directory_remove (dir_name);
410   GNUNET_log_setup ("test-datastore-api-management",
411 #if VERBOSE
412                     "DEBUG",
413 #else
414                     "WARNING",
415 #endif
416                     NULL);
417   ret = check ();
418   if (pos != plugin_name)
419     pos[0] = '.';
420   GNUNET_DISK_directory_remove (dir_name);
421   return ret;
422 }
423
424 /* end of test_datastore_api_management.c */