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