diff options
Diffstat (limited to 'src/service/datastore/test_datastore_api_management.c')
-rw-r--r-- | src/service/datastore/test_datastore_api_management.c | 413 |
1 files changed, 413 insertions, 0 deletions
diff --git a/src/service/datastore/test_datastore_api_management.c b/src/service/datastore/test_datastore_api_management.c new file mode 100644 index 000000000..de99d757d --- /dev/null +++ b/src/service/datastore/test_datastore_api_management.c | |||
@@ -0,0 +1,413 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2004, 2005, 2006, 2007, 2009, 2011 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
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 | #include "platform.h" | ||
26 | #include "gnunet_util_lib.h" | ||
27 | #include "gnunet_protocols.h" | ||
28 | #include "gnunet_datastore_service.h" | ||
29 | #include "gnunet_datastore_plugin.h" | ||
30 | #include "gnunet_testing_lib.h" | ||
31 | |||
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 | enum RunPhase | ||
45 | { | ||
46 | RP_PUT, | ||
47 | RP_GET, | ||
48 | RP_DONE, | ||
49 | RP_GET_FAIL | ||
50 | }; | ||
51 | |||
52 | |||
53 | struct CpsRunContext | ||
54 | { | ||
55 | struct GNUNET_HashCode key; | ||
56 | int i; | ||
57 | int found; | ||
58 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
59 | void *data; | ||
60 | enum RunPhase phase; | ||
61 | }; | ||
62 | |||
63 | |||
64 | static struct GNUNET_DATASTORE_Handle *datastore; | ||
65 | |||
66 | static struct GNUNET_TIME_Absolute now; | ||
67 | |||
68 | static int ok; | ||
69 | |||
70 | static const char *plugin_name; | ||
71 | |||
72 | |||
73 | static size_t | ||
74 | get_size (int i) | ||
75 | { | ||
76 | return 8 + 8 * (i % 256); | ||
77 | } | ||
78 | |||
79 | |||
80 | static const void * | ||
81 | get_data (int i) | ||
82 | { | ||
83 | static char buf[60000]; | ||
84 | |||
85 | memset (buf, i, 8 + 8 * (i % 256)); | ||
86 | return buf; | ||
87 | } | ||
88 | |||
89 | |||
90 | static int | ||
91 | get_type (int i) | ||
92 | { | ||
93 | return 1; | ||
94 | } | ||
95 | |||
96 | |||
97 | static int | ||
98 | get_priority (int i) | ||
99 | { | ||
100 | return i + 1; | ||
101 | } | ||
102 | |||
103 | |||
104 | static int | ||
105 | get_anonymity (int i) | ||
106 | { | ||
107 | return i; | ||
108 | } | ||
109 | |||
110 | |||
111 | static struct GNUNET_TIME_Absolute | ||
112 | get_expiration (int i) | ||
113 | { | ||
114 | struct GNUNET_TIME_Absolute av; | ||
115 | |||
116 | av.abs_value_us = now.abs_value_us + i * 1000 * 1000LL; | ||
117 | return av; | ||
118 | } | ||
119 | |||
120 | |||
121 | static void | ||
122 | run_continuation (void *cls); | ||
123 | |||
124 | |||
125 | static void | ||
126 | check_success (void *cls, int success, struct GNUNET_TIME_Absolute | ||
127 | min_expiration, const char *msg) | ||
128 | { | ||
129 | struct CpsRunContext *crc = cls; | ||
130 | |||
131 | if (GNUNET_OK != success) | ||
132 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", msg); | ||
133 | GNUNET_assert (GNUNET_OK == success); | ||
134 | GNUNET_free (crc->data); | ||
135 | crc->data = NULL; | ||
136 | GNUNET_SCHEDULER_add_now (&run_continuation, crc); | ||
137 | } | ||
138 | |||
139 | |||
140 | static void | ||
141 | check_value (void *cls, | ||
142 | const struct GNUNET_HashCode *key, | ||
143 | size_t size, | ||
144 | const void *data, | ||
145 | enum GNUNET_BLOCK_Type type, | ||
146 | uint32_t priority, | ||
147 | uint32_t anonymity, | ||
148 | uint32_t replication, | ||
149 | struct GNUNET_TIME_Absolute expiration, | ||
150 | uint64_t uid) | ||
151 | { | ||
152 | struct CpsRunContext *crc = cls; | ||
153 | int i; | ||
154 | |||
155 | if (NULL == key) | ||
156 | { | ||
157 | crc->phase = RP_GET_FAIL; | ||
158 | GNUNET_SCHEDULER_add_now (&run_continuation, crc); | ||
159 | return; | ||
160 | } | ||
161 | i = crc->i; | ||
162 | GNUNET_assert (size == get_size (i)); | ||
163 | GNUNET_assert (0 == memcmp (data, get_data (i), size)); | ||
164 | GNUNET_assert (type == get_type (i)); | ||
165 | GNUNET_assert (priority == get_priority (i)); | ||
166 | GNUNET_assert (anonymity == get_anonymity (i)); | ||
167 | GNUNET_assert (expiration.abs_value_us == get_expiration (i).abs_value_us); | ||
168 | crc->i--; | ||
169 | if (crc->i == 0) | ||
170 | crc->phase = RP_DONE; | ||
171 | GNUNET_SCHEDULER_add_now (&run_continuation, crc); | ||
172 | } | ||
173 | |||
174 | |||
175 | static void | ||
176 | check_nothing (void *cls, | ||
177 | const struct GNUNET_HashCode *key, | ||
178 | size_t size, | ||
179 | const void *data, | ||
180 | enum GNUNET_BLOCK_Type type, | ||
181 | uint32_t priority, | ||
182 | uint32_t anonymity, | ||
183 | uint32_t replication, | ||
184 | struct GNUNET_TIME_Absolute expiration, | ||
185 | uint64_t uid) | ||
186 | { | ||
187 | struct CpsRunContext *crc = cls; | ||
188 | |||
189 | GNUNET_assert (key == NULL); | ||
190 | if (0 == --crc->i) | ||
191 | crc->phase = RP_DONE; | ||
192 | GNUNET_SCHEDULER_add_now (&run_continuation, crc); | ||
193 | } | ||
194 | |||
195 | |||
196 | static void | ||
197 | run_continuation (void *cls) | ||
198 | { | ||
199 | struct CpsRunContext *crc = cls; | ||
200 | |||
201 | ok = (int) crc->phase; | ||
202 | switch (crc->phase) | ||
203 | { | ||
204 | case RP_PUT: | ||
205 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "PUT", | ||
206 | crc->i); | ||
207 | GNUNET_CRYPTO_hash (&crc->i, sizeof(int), &crc->key); | ||
208 | GNUNET_DATASTORE_put (datastore, | ||
209 | 0, | ||
210 | &crc->key, | ||
211 | get_size (crc->i), | ||
212 | get_data (crc->i), | ||
213 | get_type (crc->i), | ||
214 | get_priority (crc->i), | ||
215 | get_anonymity (crc->i), | ||
216 | 0, | ||
217 | get_expiration (crc->i), | ||
218 | 1, | ||
219 | 1, | ||
220 | &check_success, crc); | ||
221 | crc->i++; | ||
222 | if (crc->i == ITERATIONS) | ||
223 | { | ||
224 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
225 | "Sleeping to give datastore time to clean up\n"); | ||
226 | sleep (1); | ||
227 | crc->phase = RP_GET; | ||
228 | crc->i--; | ||
229 | } | ||
230 | break; | ||
231 | |||
232 | case RP_GET: | ||
233 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET", | ||
234 | crc->i); | ||
235 | GNUNET_CRYPTO_hash (&crc->i, sizeof(int), &crc->key); | ||
236 | GNUNET_DATASTORE_get_key (datastore, | ||
237 | 0, | ||
238 | false, | ||
239 | &crc->key, | ||
240 | get_type (crc->i), | ||
241 | 1, | ||
242 | 1, | ||
243 | &check_value, | ||
244 | crc); | ||
245 | break; | ||
246 | |||
247 | case RP_GET_FAIL: | ||
248 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET(f)", | ||
249 | crc->i); | ||
250 | GNUNET_CRYPTO_hash (&crc->i, sizeof(int), &crc->key); | ||
251 | GNUNET_DATASTORE_get_key (datastore, | ||
252 | 0, | ||
253 | false, | ||
254 | &crc->key, | ||
255 | get_type (crc->i), | ||
256 | 1, | ||
257 | 1, | ||
258 | &check_nothing, | ||
259 | crc); | ||
260 | break; | ||
261 | |||
262 | case RP_DONE: | ||
263 | GNUNET_assert (0 == crc->i); | ||
264 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n"); | ||
265 | GNUNET_DATASTORE_disconnect (datastore, | ||
266 | GNUNET_YES); | ||
267 | GNUNET_free (crc); | ||
268 | ok = 0; | ||
269 | } | ||
270 | } | ||
271 | |||
272 | |||
273 | static void | ||
274 | run_tests (void *cls, | ||
275 | int success, | ||
276 | struct GNUNET_TIME_Absolute min_expiration, | ||
277 | const char *msg) | ||
278 | { | ||
279 | struct CpsRunContext *crc = cls; | ||
280 | |||
281 | if (success != GNUNET_YES) | ||
282 | { | ||
283 | fprintf (stderr, | ||
284 | "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n", | ||
285 | msg); | ||
286 | GNUNET_DATASTORE_disconnect (datastore, | ||
287 | GNUNET_YES); | ||
288 | GNUNET_free (crc); | ||
289 | return; | ||
290 | } | ||
291 | GNUNET_SCHEDULER_add_now (&run_continuation, | ||
292 | crc); | ||
293 | } | ||
294 | |||
295 | |||
296 | static void | ||
297 | run (void *cls, | ||
298 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
299 | struct GNUNET_TESTING_Peer *peer) | ||
300 | { | ||
301 | struct CpsRunContext *crc; | ||
302 | static struct GNUNET_HashCode zkey; | ||
303 | |||
304 | crc = GNUNET_new (struct CpsRunContext); | ||
305 | crc->cfg = cfg; | ||
306 | crc->phase = RP_PUT; | ||
307 | now = GNUNET_TIME_absolute_get (); | ||
308 | datastore = GNUNET_DATASTORE_connect (cfg); | ||
309 | if (NULL == | ||
310 | GNUNET_DATASTORE_put (datastore, | ||
311 | 0, | ||
312 | &zkey, | ||
313 | 4, | ||
314 | "TEST", | ||
315 | GNUNET_BLOCK_TYPE_TEST, | ||
316 | 0, 0, 0, | ||
317 | GNUNET_TIME_relative_to_absolute ( | ||
318 | GNUNET_TIME_UNIT_SECONDS), | ||
319 | 0, | ||
320 | 1, | ||
321 | &run_tests, | ||
322 | crc)) | ||
323 | { | ||
324 | fprintf (stderr, "%s", "Test 'put' operation failed.\n"); | ||
325 | GNUNET_free (crc); | ||
326 | ok = 1; | ||
327 | } | ||
328 | } | ||
329 | |||
330 | |||
331 | /** | ||
332 | * Function called when disk utilization changes, does nothing. | ||
333 | * | ||
334 | * @param cls closure | ||
335 | * @param delta change in utilization | ||
336 | */ | ||
337 | static void | ||
338 | ignore_payload_cb (void *cls, | ||
339 | int delta) | ||
340 | { | ||
341 | /* do nothing */ | ||
342 | } | ||
343 | |||
344 | |||
345 | /** | ||
346 | * check if plugin is actually working | ||
347 | */ | ||
348 | static int | ||
349 | test_plugin (const char *cfg_name) | ||
350 | { | ||
351 | char libname[PATH_MAX]; | ||
352 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
353 | struct GNUNET_DATASTORE_PluginFunctions *api; | ||
354 | struct GNUNET_DATASTORE_PluginEnvironment env; | ||
355 | |||
356 | cfg = GNUNET_CONFIGURATION_create (); | ||
357 | if (GNUNET_OK != | ||
358 | GNUNET_CONFIGURATION_load (cfg, | ||
359 | cfg_name)) | ||
360 | { | ||
361 | GNUNET_CONFIGURATION_destroy (cfg); | ||
362 | fprintf (stderr, | ||
363 | "Failed to load configuration %s\n", | ||
364 | cfg_name); | ||
365 | return 1; | ||
366 | } | ||
367 | memset (&env, 0, sizeof(env)); | ||
368 | env.cfg = cfg; | ||
369 | env.duc = &ignore_payload_cb; | ||
370 | GNUNET_snprintf (libname, | ||
371 | sizeof(libname), | ||
372 | "libgnunet_plugin_datastore_%s", | ||
373 | plugin_name); | ||
374 | api = GNUNET_PLUGIN_load (libname, &env); | ||
375 | if (NULL == api) | ||
376 | { | ||
377 | GNUNET_CONFIGURATION_destroy (cfg); | ||
378 | fprintf (stderr, | ||
379 | "Failed to load plugin `%s'\n", | ||
380 | libname); | ||
381 | return 77; | ||
382 | } | ||
383 | GNUNET_PLUGIN_unload (libname, api); | ||
384 | GNUNET_CONFIGURATION_destroy (cfg); | ||
385 | return 0; | ||
386 | } | ||
387 | |||
388 | |||
389 | int | ||
390 | main (int argc, char *argv[]) | ||
391 | { | ||
392 | char cfg_name[PATH_MAX]; | ||
393 | int ret; | ||
394 | |||
395 | plugin_name = GNUNET_STRINGS_get_suffix_from_binary_name (argv[0]); | ||
396 | GNUNET_snprintf (cfg_name, | ||
397 | sizeof(cfg_name), | ||
398 | "test_datastore_api_data_%s.conf", | ||
399 | plugin_name); | ||
400 | ret = test_plugin (cfg_name); | ||
401 | if (0 != ret) | ||
402 | return ret; | ||
403 | if (0 != | ||
404 | GNUNET_TESTING_peer_run ("test-gnunet-datastore-management", | ||
405 | cfg_name, | ||
406 | &run, | ||
407 | NULL)) | ||
408 | return 1; | ||
409 | return ok; | ||
410 | } | ||
411 | |||
412 | |||
413 | /* end of test_datastore_api_management.c */ | ||