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