aboutsummaryrefslogtreecommitdiff
path: root/src/datastore/test_datastore_api_management.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2009-07-25 14:30:10 +0000
committerChristian Grothoff <christian@grothoff.org>2009-07-25 14:30:10 +0000
commit4f3705cbfef52322292ce8b60f1d5326825f8606 (patch)
treed46c3294cb791d674ab63829d3559c9a162404f1 /src/datastore/test_datastore_api_management.c
parenta8871b8ff983bdb8adc224a9a88ba5d2960df859 (diff)
downloadgnunet-4f3705cbfef52322292ce8b60f1d5326825f8606.tar.gz
gnunet-4f3705cbfef52322292ce8b60f1d5326825f8606.zip
more tests
Diffstat (limited to 'src/datastore/test_datastore_api_management.c')
-rw-r--r--src/datastore/test_datastore_api_management.c377
1 files changed, 377 insertions, 0 deletions
diff --git a/src/datastore/test_datastore_api_management.c b/src/datastore/test_datastore_api_management.c
new file mode 100644
index 000000000..f5dfa8a07
--- /dev/null
+++ b/src/datastore/test_datastore_api_management.c
@@ -0,0 +1,377 @@
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
44static struct GNUNET_DATASTORE_Handle *datastore;
45
46static struct GNUNET_TIME_Absolute now;
47
48static int ok;
49
50
51static size_t
52get_size (int i)
53{
54 return 8 + 8 * (i % 256);
55}
56
57
58static const void *
59get_data (int i)
60{
61 static char buf[60000];
62 memset (buf, i, 8 + 8 * (i % 256));
63 return buf;
64}
65
66
67static int
68get_type(int i)
69{
70 return 1;
71}
72
73
74static int
75get_priority (int i)
76{
77 return i+1;
78}
79
80
81static int
82get_anonymity(int i)
83{
84 return i;
85}
86
87
88static struct GNUNET_TIME_Absolute
89get_expiration (int i)
90{
91 struct GNUNET_TIME_Absolute av;
92
93 av.value = now.value + i * 1000;
94 return av;
95}
96
97enum RunPhase
98 {
99 RP_DONE = 0,
100 RP_PUT,
101 RP_GET,
102 RP_GET_FAIL
103 };
104
105
106struct CpsRunContext
107{
108 GNUNET_HashCode key;
109 int i;
110 int found;
111 struct GNUNET_SCHEDULER_Handle *sched;
112 struct GNUNET_CONFIGURATION_Handle *cfg;
113 void *data;
114 size_t size;
115 enum RunPhase phase;
116};
117
118
119static void
120run_continuation (void *cls,
121 const struct GNUNET_SCHEDULER_TaskContext *tc);
122
123
124static void
125check_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
144static void
145check_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}
190
191
192static void
193check_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 GNUNET_assert (key == NULL);
205 if (crc->i == 0)
206 {
207 crc->phase = RP_DONE;
208 }
209 GNUNET_SCHEDULER_add_continuation (crc->sched,
210 GNUNET_NO,
211 &run_continuation,
212 crc,
213 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
214}
215
216
217static void
218run_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#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
297static void
298run (void *cls,
299 struct GNUNET_SCHEDULER_Handle *sched,
300 char *const *args,
301 const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
302{
303 struct CpsRunContext *crc;
304
305 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
306 crc->sched = sched;
307 crc->cfg = cfg;
308 crc->phase = RP_PUT;
309 now = GNUNET_TIME_absolute_get ();
310 datastore = GNUNET_DATASTORE_connect (cfg, sched);
311 GNUNET_SCHEDULER_add_continuation (crc->sched,
312 GNUNET_NO,
313 &run_continuation,
314 crc,
315 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
316
317}
318
319
320
321static int
322check ()
323{
324 pid_t pid;
325 char *const argv[] = { "test-datastore-api-management",
326 "-c",
327 "test_datastore_api_data.conf",
328#if VERBOSE
329 "-L", "DEBUG",
330#endif
331 NULL
332 };
333 struct GNUNET_GETOPT_CommandLineOption options[] = {
334 GNUNET_GETOPT_OPTION_END
335 };
336 pid = GNUNET_OS_start_process ("gnunet-service-datastore",
337 "gnunet-service-datastore",
338#if VERBOSE
339 "-L", "DEBUG",
340#endif
341 "-c", "test_datastore_api_data.conf", NULL);
342 sleep (1);
343 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
344 argv, "test-datastore-api", "nohelp",
345 options, &run, NULL);
346 if (0 != PLIBC_KILL (pid, SIGTERM))
347 {
348 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
349 ok = 1;
350 }
351 GNUNET_OS_process_wait(pid);
352 if (ok != 0)
353 fprintf (stderr, "Missed some testcases: %u\n", ok);
354 return ok;
355}
356
357int
358main (int argc, char *argv[])
359{
360 int ret;
361
362 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-datastore");
363 GNUNET_log_setup ("test-datastore-api",
364#if VERBOSE
365 "DEBUG",
366#else
367 "WARNING",
368#endif
369 NULL);
370 ret = check ();
371
372 return ret;
373}
374
375
376
377/* end of test_datastore_api_management.c */