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