22d4aff25c40f74273e413b00225804fdf057d1e
[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 2, 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, 15)
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
51 static size_t
52 get_size (int i)
53 {
54   return 8 + 8 * (i % 256);
55 }
56
57
58 static const void *
59 get_data (int i)
60 {
61   static char buf[60000]; 
62   memset (buf, i, 8 + 8 * (i % 256));
63   return buf;
64 }
65
66
67 static int
68 get_type(int i)
69 {
70   return 1;
71 }
72
73
74 static int 
75 get_priority (int i)
76 {
77   return i+1;
78 }
79
80
81 static int
82 get_anonymity(int i)
83 {
84   return i;
85 }
86
87
88 static struct GNUNET_TIME_Absolute 
89 get_expiration (int i)
90 {
91   struct GNUNET_TIME_Absolute av;
92
93   av.value = now.value + i * 1000;
94   return av;
95 }
96
97 enum RunPhase
98   {
99     RP_DONE = 0,
100     RP_PUT,
101     RP_GET,
102     RP_GET_FAIL
103   };
104
105
106 struct CpsRunContext
107 {
108   GNUNET_HashCode key;
109   int i;
110   int found;
111   struct GNUNET_SCHEDULER_Handle *sched;
112   const struct GNUNET_CONFIGURATION_Handle *cfg;
113   void *data;
114   size_t size;
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                                      GNUNET_NO,
138                                      &run_continuation,
139                                      crc,
140                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
141 }
142
143
144 static void 
145 check_value (void *cls,
146              const GNUNET_HashCode * key,
147              uint32_t size,
148              const void *data,
149              uint32_t type,
150              uint32_t priority,
151              uint32_t anonymity,
152              struct GNUNET_TIME_Absolute
153              expiration, uint64_t uid)
154 {
155   struct CpsRunContext *crc = cls;
156   int i;
157
158   if (key == NULL)
159     {
160       crc->i--;
161       if (crc->found == GNUNET_YES)
162         {
163           crc->phase = RP_GET;
164           crc->found = GNUNET_NO;
165         }
166       else
167         {
168           fprintf (stderr,
169                    "First not found was %u\n", crc->i);
170           crc->phase = RP_GET_FAIL;
171         }
172       if (0 == crc->i)
173         crc->phase = RP_DONE;
174       GNUNET_SCHEDULER_add_continuation (crc->sched,
175                                          GNUNET_NO,
176                                          &run_continuation,
177                                          crc,
178                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
179       return;
180     }
181   i = crc->i;
182   crc->found = GNUNET_YES;
183   GNUNET_assert (size == get_size (i));
184   GNUNET_assert (0 == memcmp (data, get_data(i), size));
185   GNUNET_assert (type == get_type (i));
186   GNUNET_assert (priority == get_priority (i));
187   GNUNET_assert (anonymity == get_anonymity(i));
188   GNUNET_assert (expiration.value == get_expiration(i).value);
189   GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
190 }
191
192
193 static void 
194 check_nothing (void *cls,
195              const GNUNET_HashCode * key,
196              uint32_t size,
197              const void *data,
198              uint32_t type,
199              uint32_t priority,
200              uint32_t anonymity,
201              struct GNUNET_TIME_Absolute
202              expiration, uint64_t uid)
203 {
204   struct CpsRunContext *crc = cls;
205
206   GNUNET_assert (key == NULL);
207   if (0 == --crc->i)
208     crc->phase = RP_DONE;
209   GNUNET_SCHEDULER_add_continuation (crc->sched,
210                                      GNUNET_NO,
211                                      &run_continuation,
212                                      crc,
213                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
214 }
215
216
217 static void
218 run_continuation (void *cls,
219                   const struct GNUNET_SCHEDULER_TaskContext *tc)
220 {
221   struct CpsRunContext *crc = cls;
222   ok = (int) crc->phase;
223   switch (crc->phase)
224     {
225     case RP_PUT:
226 #if VERBOSE
227       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228                   "Executing `%s' number %u\n",
229                   "PUT",
230                   crc->i);
231 #endif
232       GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
233       GNUNET_DATASTORE_put (datastore,
234                             0,
235                             &crc->key,
236                             get_size (crc->i),
237                             get_data (crc->i),
238                             get_type (crc->i),
239                             get_priority (crc->i),
240                             get_anonymity (crc->i),
241                             get_expiration (crc->i),
242                             TIMEOUT,
243                             &check_success,
244                             crc);
245       crc->i++;
246       if (crc->i == ITERATIONS)
247         {
248           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
249                       "Sleeping to give datastore time to clean up\n");
250           sleep (5);
251           crc->phase = RP_GET;
252           crc->i--;
253         }
254       break;
255     case RP_GET:
256 #if VERBOSE
257       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
258                   "Executing `%s' number %u\n",
259                   "GET",
260                   crc->i);
261 #endif
262       GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
263       GNUNET_DATASTORE_get (datastore, 
264                             &crc->key,
265                             get_type (crc->i),
266                             &check_value,
267                             crc,
268                             TIMEOUT);
269       break;
270     case RP_GET_FAIL:
271 #if VERBOSE
272       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273                   "Executing `%s' number %u\n",
274                   "GET",
275                   crc->i);
276 #endif
277       GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
278       GNUNET_DATASTORE_get (datastore, 
279                             &crc->key,
280                             get_type (crc->i),
281                             &check_nothing,
282                             crc,
283                             TIMEOUT);
284       break;
285     case RP_DONE:
286       GNUNET_assert (0 == crc->i);
287 #if VERBOSE
288       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289                   "Finished, disconnecting\n");
290 #endif
291       GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
292       GNUNET_free (crc);
293       ok = 0;
294     }
295 }
296
297
298 static void
299 run (void *cls,
300      struct GNUNET_SCHEDULER_Handle *sched,
301      char *const *args,
302      const char *cfgfile,
303      const struct GNUNET_CONFIGURATION_Handle *cfg)
304 {
305   struct CpsRunContext *crc;
306
307   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
308   crc->sched = sched;
309   crc->cfg = cfg;
310   crc->phase = RP_PUT;
311   now = GNUNET_TIME_absolute_get ();
312   datastore = GNUNET_DATASTORE_connect (cfg, sched);
313   GNUNET_SCHEDULER_add_continuation (crc->sched,
314                                      GNUNET_NO,
315                                      &run_continuation,
316                                      crc,
317                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
318
319 }
320
321
322
323 static int
324 check ()
325 {
326   pid_t pid;
327   char *const argv[] = { "test-datastore-api-management",
328     "-c",
329     "test_datastore_api_data.conf",
330 #if VERBOSE
331     "-L", "DEBUG",
332 #endif
333     NULL
334   };
335   struct GNUNET_GETOPT_CommandLineOption options[] = {
336     GNUNET_GETOPT_OPTION_END
337   };
338   pid = GNUNET_OS_start_process ("gnunet-service-datastore",
339                                  "gnunet-service-datastore",
340 #if VERBOSE
341                                  "-L", "DEBUG",
342 #endif
343                                  "-c", "test_datastore_api_data.conf", NULL);
344   sleep (1);
345   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
346                       argv, "test-datastore-api", "nohelp",
347                       options, &run, NULL);
348   if (0 != PLIBC_KILL (pid, SIGTERM))
349     {
350       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
351       ok = 1;
352     }
353   GNUNET_OS_process_wait(pid);
354   if (ok != 0)
355     fprintf (stderr, "Missed some testcases: %u\n", ok);
356   return ok;
357 }
358
359 int
360 main (int argc, char *argv[])
361 {
362   int ret;
363   
364   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-datastore");
365   GNUNET_log_setup ("test-datastore-api",
366 #if VERBOSE
367                     "DEBUG",
368 #else
369                     "WARNING",
370 #endif
371                     NULL);
372   ret = check ();
373   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-datastore");
374   return ret;
375 }
376
377
378
379 /* end of test_datastore_api_management.c */