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