summaryrefslogtreecommitdiff
path: root/src/datastore/test_datastore_api_management.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/datastore/test_datastore_api_management.c')
-rw-r--r--src/datastore/test_datastore_api_management.c377
1 files changed, 189 insertions, 188 deletions
diff --git a/src/datastore/test_datastore_api_management.c b/src/datastore/test_datastore_api_management.c
index fc4c03885..e7599999f 100644
--- a/src/datastore/test_datastore_api_management.c
+++ b/src/datastore/test_datastore_api_management.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/* 20/*
21 * @file datastore/test_datastore_api_management.c 21 * @file datastore/test_datastore_api_management.c
22 * @brief Test for the space management functions of the datastore implementation. 22 * @brief Test for the space management functions of the datastore implementation.
@@ -33,7 +33,7 @@
33/** 33/**
34 * How long until we give up on transmitting the message? 34 * How long until we give up on transmitting the message?
35 */ 35 */
36#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60) 36#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60)
37 37
38/** 38/**
39 * Number of iterations to run; must be large enough 39 * Number of iterations to run; must be large enough
@@ -41,8 +41,7 @@
41 */ 41 */
42#define ITERATIONS 5000 42#define ITERATIONS 5000
43 43
44enum RunPhase 44enum RunPhase {
45{
46 RP_PUT, 45 RP_PUT,
47 RP_GET, 46 RP_GET,
48 RP_DONE, 47 RP_DONE,
@@ -50,8 +49,7 @@ enum RunPhase
50}; 49};
51 50
52 51
53struct CpsRunContext 52struct CpsRunContext {
54{
55 struct GNUNET_HashCode key; 53 struct GNUNET_HashCode key;
56 int i; 54 int i;
57 int found; 55 int found;
@@ -71,45 +69,45 @@ static const char *plugin_name;
71 69
72 70
73static size_t 71static size_t
74get_size (int i) 72get_size(int i)
75{ 73{
76 return 8 + 8 * (i % 256); 74 return 8 + 8 * (i % 256);
77} 75}
78 76
79 77
80static const void * 78static const void *
81get_data (int i) 79get_data(int i)
82{ 80{
83 static char buf[60000]; 81 static char buf[60000];
84 82
85 memset (buf, i, 8 + 8 * (i % 256)); 83 memset(buf, i, 8 + 8 * (i % 256));
86 return buf; 84 return buf;
87} 85}
88 86
89 87
90static int 88static int
91get_type (int i) 89get_type(int i)
92{ 90{
93 return 1; 91 return 1;
94} 92}
95 93
96 94
97static int 95static int
98get_priority (int i) 96get_priority(int i)
99{ 97{
100 return i + 1; 98 return i + 1;
101} 99}
102 100
103 101
104static int 102static int
105get_anonymity (int i) 103get_anonymity(int i)
106{ 104{
107 return i; 105 return i;
108} 106}
109 107
110 108
111static struct GNUNET_TIME_Absolute 109static struct GNUNET_TIME_Absolute
112get_expiration (int i) 110get_expiration(int i)
113{ 111{
114 struct GNUNET_TIME_Absolute av; 112 struct GNUNET_TIME_Absolute av;
115 113
@@ -119,201 +117,204 @@ get_expiration (int i)
119 117
120 118
121static void 119static void
122run_continuation (void *cls); 120run_continuation(void *cls);
123 121
124 122
125static void 123static void
126check_success (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg) 124check_success(void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
127{ 125{
128 struct CpsRunContext *crc = cls; 126 struct CpsRunContext *crc = cls;
129 127
130 if (GNUNET_OK != success) 128 if (GNUNET_OK != success)
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", msg); 129 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", msg);
132 GNUNET_assert (GNUNET_OK == success); 130 GNUNET_assert(GNUNET_OK == success);
133 GNUNET_free_non_null (crc->data); 131 GNUNET_free_non_null(crc->data);
134 crc->data = NULL; 132 crc->data = NULL;
135 GNUNET_SCHEDULER_add_now (&run_continuation, crc); 133 GNUNET_SCHEDULER_add_now(&run_continuation, crc);
136} 134}
137 135
138 136
139static void 137static void
140check_value (void *cls, 138check_value(void *cls,
141 const struct GNUNET_HashCode *key, 139 const struct GNUNET_HashCode *key,
142 size_t size, 140 size_t size,
143 const void *data, 141 const void *data,
144 enum GNUNET_BLOCK_Type type, 142 enum GNUNET_BLOCK_Type type,
145 uint32_t priority, 143 uint32_t priority,
146 uint32_t anonymity, 144 uint32_t anonymity,
147 uint32_t replication, 145 uint32_t replication,
148 struct GNUNET_TIME_Absolute expiration, 146 struct GNUNET_TIME_Absolute expiration,
149 uint64_t uid) 147 uint64_t uid)
150{ 148{
151 struct CpsRunContext *crc = cls; 149 struct CpsRunContext *crc = cls;
152 int i; 150 int i;
153 151
154 if (NULL == key) 152 if (NULL == key)
155 { 153 {
156 crc->phase = RP_GET_FAIL; 154 crc->phase = RP_GET_FAIL;
157 GNUNET_SCHEDULER_add_now (&run_continuation, crc); 155 GNUNET_SCHEDULER_add_now(&run_continuation, crc);
158 return; 156 return;
159 } 157 }
160 i = crc->i; 158 i = crc->i;
161 GNUNET_assert (size == get_size (i)); 159 GNUNET_assert(size == get_size(i));
162 GNUNET_assert (0 == memcmp (data, get_data (i), size)); 160 GNUNET_assert(0 == memcmp(data, get_data(i), size));
163 GNUNET_assert (type == get_type (i)); 161 GNUNET_assert(type == get_type(i));
164 GNUNET_assert (priority == get_priority (i)); 162 GNUNET_assert(priority == get_priority(i));
165 GNUNET_assert (anonymity == get_anonymity (i)); 163 GNUNET_assert(anonymity == get_anonymity(i));
166 GNUNET_assert (expiration.abs_value_us == get_expiration (i).abs_value_us); 164 GNUNET_assert(expiration.abs_value_us == get_expiration(i).abs_value_us);
167 crc->i--; 165 crc->i--;
168 if (crc->i == 0) 166 if (crc->i == 0)
169 crc->phase = RP_DONE; 167 crc->phase = RP_DONE;
170 GNUNET_SCHEDULER_add_now (&run_continuation, crc); 168 GNUNET_SCHEDULER_add_now(&run_continuation, crc);
171} 169}
172 170
173 171
174static void 172static void
175check_nothing (void *cls, 173check_nothing(void *cls,
176 const struct GNUNET_HashCode *key, 174 const struct GNUNET_HashCode *key,
177 size_t size, 175 size_t size,
178 const void *data, 176 const void *data,
179 enum GNUNET_BLOCK_Type type, 177 enum GNUNET_BLOCK_Type type,
180 uint32_t priority, 178 uint32_t priority,
181 uint32_t anonymity, 179 uint32_t anonymity,
182 uint32_t replication, 180 uint32_t replication,
183 struct GNUNET_TIME_Absolute expiration, 181 struct GNUNET_TIME_Absolute expiration,
184 uint64_t uid) 182 uint64_t uid)
185{ 183{
186 struct CpsRunContext *crc = cls; 184 struct CpsRunContext *crc = cls;
187 185
188 GNUNET_assert (key == NULL); 186 GNUNET_assert(key == NULL);
189 if (0 == --crc->i) 187 if (0 == --crc->i)
190 crc->phase = RP_DONE; 188 crc->phase = RP_DONE;
191 GNUNET_SCHEDULER_add_now (&run_continuation, crc); 189 GNUNET_SCHEDULER_add_now(&run_continuation, crc);
192} 190}
193 191
194 192
195static void 193static void
196run_continuation (void *cls) 194run_continuation(void *cls)
197{ 195{
198 struct CpsRunContext *crc = cls; 196 struct CpsRunContext *crc = cls;
199 197
200 ok = (int) crc->phase; 198 ok = (int)crc->phase;
201 switch (crc->phase) 199 switch (crc->phase)
202 {
203 case RP_PUT:
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "PUT",
205 crc->i);
206 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
207 GNUNET_DATASTORE_put (datastore,
208 0,
209 &crc->key,
210 get_size (crc->i),
211 get_data (crc->i),
212 get_type (crc->i),
213 get_priority (crc->i),
214 get_anonymity (crc->i),
215 0,
216 get_expiration (crc->i),
217 1,
218 1,
219 &check_success, crc);
220 crc->i++;
221 if (crc->i == ITERATIONS)
222 { 200 {
223 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 201 case RP_PUT:
224 "Sleeping to give datastore time to clean up\n"); 202 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "PUT",
225 sleep (1); 203 crc->i);
226 crc->phase = RP_GET; 204 GNUNET_CRYPTO_hash(&crc->i, sizeof(int), &crc->key);
227 crc->i--; 205 GNUNET_DATASTORE_put(datastore,
206 0,
207 &crc->key,
208 get_size(crc->i),
209 get_data(crc->i),
210 get_type(crc->i),
211 get_priority(crc->i),
212 get_anonymity(crc->i),
213 0,
214 get_expiration(crc->i),
215 1,
216 1,
217 &check_success, crc);
218 crc->i++;
219 if (crc->i == ITERATIONS)
220 {
221 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
222 "Sleeping to give datastore time to clean up\n");
223 sleep(1);
224 crc->phase = RP_GET;
225 crc->i--;
226 }
227 break;
228
229 case RP_GET:
230 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET",
231 crc->i);
232 GNUNET_CRYPTO_hash(&crc->i, sizeof(int), &crc->key);
233 GNUNET_DATASTORE_get_key(datastore,
234 0,
235 false,
236 &crc->key,
237 get_type(crc->i),
238 1,
239 1,
240 &check_value,
241 crc);
242 break;
243
244 case RP_GET_FAIL:
245 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET(f)",
246 crc->i);
247 GNUNET_CRYPTO_hash(&crc->i, sizeof(int), &crc->key);
248 GNUNET_DATASTORE_get_key(datastore,
249 0,
250 false,
251 &crc->key,
252 get_type(crc->i),
253 1,
254 1,
255 &check_nothing,
256 crc);
257 break;
258
259 case RP_DONE:
260 GNUNET_assert(0 == crc->i);
261 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n");
262 GNUNET_DATASTORE_disconnect(datastore, GNUNET_YES);
263 GNUNET_free(crc);
264 ok = 0;
228 } 265 }
229 break;
230 case RP_GET:
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET",
232 crc->i);
233 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
234 GNUNET_DATASTORE_get_key (datastore,
235 0,
236 false,
237 &crc->key,
238 get_type (crc->i),
239 1,
240 1,
241 &check_value,
242 crc);
243 break;
244 case RP_GET_FAIL:
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET(f)",
246 crc->i);
247 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
248 GNUNET_DATASTORE_get_key (datastore,
249 0,
250 false,
251 &crc->key,
252 get_type (crc->i),
253 1,
254 1,
255 &check_nothing,
256 crc);
257 break;
258 case RP_DONE:
259 GNUNET_assert (0 == crc->i);
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n");
261 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
262 GNUNET_free (crc);
263 ok = 0;
264 }
265} 266}
266 267
267 268
268static void 269static void
269run_tests (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg) 270run_tests(void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
270{ 271{
271 struct CpsRunContext *crc = cls; 272 struct CpsRunContext *crc = cls;
272 273
273 if (success != GNUNET_YES) 274 if (success != GNUNET_YES)
274 { 275 {
275 fprintf (stderr, 276 fprintf(stderr,
276 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n", 277 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
277 msg); 278 msg);
278 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES); 279 GNUNET_DATASTORE_disconnect(datastore, GNUNET_YES);
279 GNUNET_free (crc); 280 GNUNET_free(crc);
280 return; 281 return;
281 } 282 }
282 GNUNET_SCHEDULER_add_now (&run_continuation, crc); 283 GNUNET_SCHEDULER_add_now(&run_continuation, crc);
283} 284}
284 285
285 286
286static void 287static void
287run (void *cls, 288run(void *cls,
288 const struct GNUNET_CONFIGURATION_Handle *cfg, 289 const struct GNUNET_CONFIGURATION_Handle *cfg,
289 struct GNUNET_TESTING_Peer *peer) 290 struct GNUNET_TESTING_Peer *peer)
290{ 291{
291 struct CpsRunContext *crc; 292 struct CpsRunContext *crc;
292 static struct GNUNET_HashCode zkey; 293 static struct GNUNET_HashCode zkey;
293 294
294 crc = GNUNET_new (struct CpsRunContext); 295 crc = GNUNET_new(struct CpsRunContext);
295 crc->cfg = cfg; 296 crc->cfg = cfg;
296 crc->phase = RP_PUT; 297 crc->phase = RP_PUT;
297 now = GNUNET_TIME_absolute_get (); 298 now = GNUNET_TIME_absolute_get();
298 datastore = GNUNET_DATASTORE_connect (cfg); 299 datastore = GNUNET_DATASTORE_connect(cfg);
299 if (NULL == 300 if (NULL ==
300 GNUNET_DATASTORE_put (datastore, 301 GNUNET_DATASTORE_put(datastore,
301 0, 302 0,
302 &zkey, 303 &zkey,
303 4, 304 4,
304 "TEST", 305 "TEST",
305 GNUNET_BLOCK_TYPE_TEST, 306 GNUNET_BLOCK_TYPE_TEST,
306 0, 0, 0, 307 0, 0, 0,
307 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS), 308 GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_SECONDS),
308 0, 309 0,
309 1, 310 1,
310 &run_tests, 311 &run_tests,
311 crc)) 312 crc))
312 { 313 {
313 fprintf (stderr, "%s", "Test 'put' operation failed.\n"); 314 fprintf(stderr, "%s", "Test 'put' operation failed.\n");
314 GNUNET_free (crc); 315 GNUNET_free(crc);
315 ok = 1; 316 ok = 1;
316 } 317 }
317} 318}
318 319
319 320
@@ -324,8 +325,8 @@ run (void *cls,
324 * @param delta change in utilization 325 * @param delta change in utilization
325 */ 326 */
326static void 327static void
327ignore_payload_cb (void *cls, 328ignore_payload_cb(void *cls,
328 int delta) 329 int delta)
329{ 330{
330 /* do nothing */ 331 /* do nothing */
331} 332}
@@ -335,65 +336,65 @@ ignore_payload_cb (void *cls,
335 * check if plugin is actually working 336 * check if plugin is actually working
336 */ 337 */
337static int 338static int
338test_plugin (const char *cfg_name) 339test_plugin(const char *cfg_name)
339{ 340{
340 char libname[PATH_MAX]; 341 char libname[PATH_MAX];
341 struct GNUNET_CONFIGURATION_Handle *cfg; 342 struct GNUNET_CONFIGURATION_Handle *cfg;
342 struct GNUNET_DATASTORE_PluginFunctions *api; 343 struct GNUNET_DATASTORE_PluginFunctions *api;
343 struct GNUNET_DATASTORE_PluginEnvironment env; 344 struct GNUNET_DATASTORE_PluginEnvironment env;
344 345
345 cfg = GNUNET_CONFIGURATION_create (); 346 cfg = GNUNET_CONFIGURATION_create();
346 if (GNUNET_OK != 347 if (GNUNET_OK !=
347 GNUNET_CONFIGURATION_load (cfg, 348 GNUNET_CONFIGURATION_load(cfg,
348 cfg_name)) 349 cfg_name))
349 { 350 {
350 GNUNET_CONFIGURATION_destroy (cfg); 351 GNUNET_CONFIGURATION_destroy(cfg);
351 fprintf (stderr, 352 fprintf(stderr,
352 "Failed to load configuration %s\n", 353 "Failed to load configuration %s\n",
353 cfg_name); 354 cfg_name);
354 return 1; 355 return 1;
355 } 356 }
356 memset (&env, 0, sizeof (env)); 357 memset(&env, 0, sizeof(env));
357 env.cfg = cfg; 358 env.cfg = cfg;
358 env.duc = &ignore_payload_cb; 359 env.duc = &ignore_payload_cb;
359 GNUNET_snprintf (libname, 360 GNUNET_snprintf(libname,
360 sizeof (libname), 361 sizeof(libname),
361 "libgnunet_plugin_datastore_%s", 362 "libgnunet_plugin_datastore_%s",
362 plugin_name); 363 plugin_name);
363 api = GNUNET_PLUGIN_load (libname, &env); 364 api = GNUNET_PLUGIN_load(libname, &env);
364 if (NULL == api) 365 if (NULL == api)
365 { 366 {
366 GNUNET_CONFIGURATION_destroy (cfg); 367 GNUNET_CONFIGURATION_destroy(cfg);
367 fprintf (stderr, 368 fprintf(stderr,
368 "Failed to load plugin `%s'\n", 369 "Failed to load plugin `%s'\n",
369 libname); 370 libname);
370 return 77; 371 return 77;
371 } 372 }
372 GNUNET_PLUGIN_unload (libname, api); 373 GNUNET_PLUGIN_unload(libname, api);
373 GNUNET_CONFIGURATION_destroy (cfg); 374 GNUNET_CONFIGURATION_destroy(cfg);
374 return 0; 375 return 0;
375} 376}
376 377
377 378
378int 379int
379main (int argc, char *argv[]) 380main(int argc, char *argv[])
380{ 381{
381 char cfg_name[PATH_MAX]; 382 char cfg_name[PATH_MAX];
382 int ret; 383 int ret;
383 384
384 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]); 385 plugin_name = GNUNET_TESTING_get_testname_from_underscore(argv[0]);
385 GNUNET_snprintf (cfg_name, 386 GNUNET_snprintf(cfg_name,
386 sizeof (cfg_name), 387 sizeof(cfg_name),
387 "test_datastore_api_data_%s.conf", 388 "test_datastore_api_data_%s.conf",
388 plugin_name); 389 plugin_name);
389 ret = test_plugin (cfg_name); 390 ret = test_plugin(cfg_name);
390 if (0 != ret) 391 if (0 != ret)
391 return ret; 392 return ret;
392 if (0 != 393 if (0 !=
393 GNUNET_TESTING_peer_run ("test-gnunet-datastore-management", 394 GNUNET_TESTING_peer_run("test-gnunet-datastore-management",
394 cfg_name, 395 cfg_name,
395 &run, 396 &run,
396 NULL)) 397 NULL))
397 return 1; 398 return 1;
398 return ok; 399 return ok;
399} 400}