48daebb1df48b696801d9855211c92fd5723f029
[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   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 (crc->sched,
136                                      GNUNET_NO,
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              uint32_t size,
147              const void *data,
148              uint32_t 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                                          GNUNET_NO,
175                                          &run_continuation,
176                                          crc,
177                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
178       return;
179     }
180   i = crc->i;
181   crc->found = GNUNET_YES;
182   GNUNET_assert (size == get_size (i));
183   GNUNET_assert (0 == memcmp (data, get_data(i), size));
184   GNUNET_assert (type == get_type (i));
185   GNUNET_assert (priority == get_priority (i));
186   GNUNET_assert (anonymity == get_anonymity(i));
187   GNUNET_assert (expiration.value == get_expiration(i).value);
188   GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
189 }
190
191
192 static void 
193 check_nothing (void *cls,
194              const GNUNET_HashCode * key,
195              uint32_t size,
196              const void *data,
197              uint32_t type,
198              uint32_t priority,
199              uint32_t anonymity,
200              struct GNUNET_TIME_Absolute
201              expiration, uint64_t uid)
202 {
203   struct CpsRunContext *crc = cls;
204
205   GNUNET_assert (key == NULL);
206   if (0 == --crc->i)
207     crc->phase = RP_DONE;
208   GNUNET_SCHEDULER_add_continuation (crc->sched,
209                                      GNUNET_NO,
210                                      &run_continuation,
211                                      crc,
212                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
213 }
214
215
216 static void
217 run_continuation (void *cls,
218                   const struct GNUNET_SCHEDULER_TaskContext *tc)
219 {
220   struct CpsRunContext *crc = cls;
221   ok = (int) crc->phase;
222   switch (crc->phase)
223     {
224     case RP_PUT:
225 #if VERBOSE
226       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227                   "Executing `%s' number %u\n",
228                   "PUT",
229                   crc->i);
230 #endif
231       GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
232       GNUNET_DATASTORE_put (datastore,
233                             0,
234                             &crc->key,
235                             get_size (crc->i),
236                             get_data (crc->i),
237                             get_type (crc->i),
238                             get_priority (crc->i),
239                             get_anonymity (crc->i),
240                             get_expiration (crc->i),
241                             TIMEOUT,
242                             &check_success,
243                             crc);
244       crc->i++;
245       if (crc->i == ITERATIONS)
246         {
247           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
248                       "Sleeping to give datastore time to clean up\n");
249           sleep (5);
250           crc->phase = RP_GET;
251           crc->i--;
252         }
253       break;
254     case RP_GET:
255 #if VERBOSE
256       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
257                   "Executing `%s' number %u\n",
258                   "GET",
259                   crc->i);
260 #endif
261       GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
262       GNUNET_DATASTORE_get (datastore, 
263                             &crc->key,
264                             get_type (crc->i),
265                             &check_value,
266                             crc,
267                             TIMEOUT);
268       break;
269     case RP_GET_FAIL:
270 #if VERBOSE
271       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272                   "Executing `%s' number %u\n",
273                   "GET",
274                   crc->i);
275 #endif
276       GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
277       GNUNET_DATASTORE_get (datastore, 
278                             &crc->key,
279                             get_type (crc->i),
280                             &check_nothing,
281                             crc,
282                             TIMEOUT);
283       break;
284     case RP_DONE:
285       GNUNET_assert (0 == crc->i);
286 #if VERBOSE
287       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288                   "Finished, disconnecting\n");
289 #endif
290       GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
291       GNUNET_free (crc);
292       ok = 0;
293     }
294 }
295
296
297 static void
298 run (void *cls,
299      struct GNUNET_SCHEDULER_Handle *sched,
300      char *const *args,
301      const char *cfgfile,
302      const struct GNUNET_CONFIGURATION_Handle *cfg)
303 {
304   struct CpsRunContext *crc;
305
306   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
307   crc->sched = sched;
308   crc->cfg = cfg;
309   crc->phase = RP_PUT;
310   now = GNUNET_TIME_absolute_get ();
311   datastore = GNUNET_DATASTORE_connect (cfg, sched);
312   GNUNET_SCHEDULER_add_continuation (crc->sched,
313                                      GNUNET_NO,
314                                      &run_continuation,
315                                      crc,
316                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
317
318 }
319
320
321
322 static int
323 check ()
324 {
325   pid_t pid;
326   char *const argv[] = { "test-datastore-api-management",
327     "-c",
328     "test_datastore_api_data.conf",
329 #if VERBOSE
330     "-L", "DEBUG",
331 #endif
332     NULL
333   };
334   struct GNUNET_GETOPT_CommandLineOption options[] = {
335     GNUNET_GETOPT_OPTION_END
336   };
337   pid = GNUNET_OS_start_process ("gnunet-service-arm",
338                                  "gnunet-service-arm",
339 #if VERBOSE
340                                  "-L", "DEBUG",
341 #endif
342                                  "-c", "test_datastore_api_data.conf", NULL);
343   sleep (1);
344   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
345                       argv, "test-datastore-api", "nohelp",
346                       options, &run, NULL);
347   if (0 != PLIBC_KILL (pid, SIGTERM))
348     {
349       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
350       ok = 1;
351     }
352   GNUNET_OS_process_wait(pid);
353   if (ok != 0)
354     fprintf (stderr, "Missed some testcases: %u\n", ok);
355   return ok;
356 }
357
358 int
359 main (int argc, char *argv[])
360 {
361   int ret;
362   
363   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-datastore");
364   GNUNET_log_setup ("test-datastore-api",
365 #if VERBOSE
366                     "DEBUG",
367 #else
368                     "WARNING",
369 #endif
370                     NULL);
371   ret = check ();
372   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-datastore");
373   return ret;
374 }
375
376
377
378 /* end of test_datastore_api_management.c */