diff options
author | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 20:19:34 +0200 |
---|---|---|
committer | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 20:19:34 +0200 |
commit | 498cb28b16dbebbacb674fa166aeac3fe4ea9922 (patch) | |
tree | cd1f59025a55f263f297ca706d90b1a395f6e163 /src/service/datastore/test_datastore_api_management.c | |
parent | 11949e419fd7230431646703ac0cf8e34f615673 (diff) | |
download | gnunet-498cb28b16dbebbacb674fa166aeac3fe4ea9922.tar.gz gnunet-498cb28b16dbebbacb674fa166aeac3fe4ea9922.zip |
BUILD: Move dht/datastore to service/cli
Diffstat (limited to 'src/service/datastore/test_datastore_api_management.c')
-rw-r--r-- | src/service/datastore/test_datastore_api_management.c | 408 |
1 files changed, 408 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..b9b134fbc --- /dev/null +++ b/src/service/datastore/test_datastore_api_management.c | |||
@@ -0,0 +1,408 @@ | |||
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, GNUNET_YES); | ||
266 | GNUNET_free (crc); | ||
267 | ok = 0; | ||
268 | } | ||
269 | } | ||
270 | |||
271 | |||
272 | static void | ||
273 | run_tests (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, | ||
274 | const char *msg) | ||
275 | { | ||
276 | struct CpsRunContext *crc = cls; | ||
277 | |||
278 | if (success != GNUNET_YES) | ||
279 | { | ||
280 | fprintf (stderr, | ||
281 | "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n", | ||
282 | msg); | ||
283 | GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES); | ||
284 | GNUNET_free (crc); | ||
285 | return; | ||
286 | } | ||
287 | GNUNET_SCHEDULER_add_now (&run_continuation, crc); | ||
288 | } | ||
289 | |||
290 | |||
291 | static void | ||
292 | run (void *cls, | ||
293 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
294 | struct GNUNET_TESTING_Peer *peer) | ||
295 | { | ||
296 | struct CpsRunContext *crc; | ||
297 | static struct GNUNET_HashCode zkey; | ||
298 | |||
299 | crc = GNUNET_new (struct CpsRunContext); | ||
300 | crc->cfg = cfg; | ||
301 | crc->phase = RP_PUT; | ||
302 | now = GNUNET_TIME_absolute_get (); | ||
303 | datastore = GNUNET_DATASTORE_connect (cfg); | ||
304 | if (NULL == | ||
305 | GNUNET_DATASTORE_put (datastore, | ||
306 | 0, | ||
307 | &zkey, | ||
308 | 4, | ||
309 | "TEST", | ||
310 | GNUNET_BLOCK_TYPE_TEST, | ||
311 | 0, 0, 0, | ||
312 | GNUNET_TIME_relative_to_absolute ( | ||
313 | GNUNET_TIME_UNIT_SECONDS), | ||
314 | 0, | ||
315 | 1, | ||
316 | &run_tests, | ||
317 | crc)) | ||
318 | { | ||
319 | fprintf (stderr, "%s", "Test 'put' operation failed.\n"); | ||
320 | GNUNET_free (crc); | ||
321 | ok = 1; | ||
322 | } | ||
323 | } | ||
324 | |||
325 | |||
326 | /** | ||
327 | * Function called when disk utilization changes, does nothing. | ||
328 | * | ||
329 | * @param cls closure | ||
330 | * @param delta change in utilization | ||
331 | */ | ||
332 | static void | ||
333 | ignore_payload_cb (void *cls, | ||
334 | int delta) | ||
335 | { | ||
336 | /* do nothing */ | ||
337 | } | ||
338 | |||
339 | |||
340 | /** | ||
341 | * check if plugin is actually working | ||
342 | */ | ||
343 | static int | ||
344 | test_plugin (const char *cfg_name) | ||
345 | { | ||
346 | char libname[PATH_MAX]; | ||
347 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
348 | struct GNUNET_DATASTORE_PluginFunctions *api; | ||
349 | struct GNUNET_DATASTORE_PluginEnvironment env; | ||
350 | |||
351 | cfg = GNUNET_CONFIGURATION_create (); | ||
352 | if (GNUNET_OK != | ||
353 | GNUNET_CONFIGURATION_load (cfg, | ||
354 | cfg_name)) | ||
355 | { | ||
356 | GNUNET_CONFIGURATION_destroy (cfg); | ||
357 | fprintf (stderr, | ||
358 | "Failed to load configuration %s\n", | ||
359 | cfg_name); | ||
360 | return 1; | ||
361 | } | ||
362 | memset (&env, 0, sizeof(env)); | ||
363 | env.cfg = cfg; | ||
364 | env.duc = &ignore_payload_cb; | ||
365 | GNUNET_snprintf (libname, | ||
366 | sizeof(libname), | ||
367 | "libgnunet_plugin_datastore_%s", | ||
368 | plugin_name); | ||
369 | api = GNUNET_PLUGIN_load (libname, &env); | ||
370 | if (NULL == api) | ||
371 | { | ||
372 | GNUNET_CONFIGURATION_destroy (cfg); | ||
373 | fprintf (stderr, | ||
374 | "Failed to load plugin `%s'\n", | ||
375 | libname); | ||
376 | return 77; | ||
377 | } | ||
378 | GNUNET_PLUGIN_unload (libname, api); | ||
379 | GNUNET_CONFIGURATION_destroy (cfg); | ||
380 | return 0; | ||
381 | } | ||
382 | |||
383 | |||
384 | int | ||
385 | main (int argc, char *argv[]) | ||
386 | { | ||
387 | char cfg_name[PATH_MAX]; | ||
388 | int ret; | ||
389 | |||
390 | plugin_name = GNUNET_STRINGS_get_suffix_from_binary_name (argv[0]); | ||
391 | GNUNET_snprintf (cfg_name, | ||
392 | sizeof(cfg_name), | ||
393 | "test_datastore_api_data_%s.conf", | ||
394 | plugin_name); | ||
395 | ret = test_plugin (cfg_name); | ||
396 | if (0 != ret) | ||
397 | return ret; | ||
398 | if (0 != | ||
399 | GNUNET_TESTING_peer_run ("test-gnunet-datastore-management", | ||
400 | cfg_name, | ||
401 | &run, | ||
402 | NULL)) | ||
403 | return 1; | ||
404 | return ok; | ||
405 | } | ||
406 | |||
407 | |||
408 | /* end of test_datastore_api_management.c */ | ||