2 * This file is part of GNUnet
3 * Copyright (C) 2013 GNUnet e.V.
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 3, or (at your
8 * option) any later version.
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.
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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * @author Gabor X Toth
23 * @author Christian Grothoff
26 * Test for the PSYCstore plugins.
32 #include "gnunet_util_lib.h"
33 #include "gnunet_testing_lib.h"
34 #include "gnunet_psycstore_plugin.h"
35 #include "gnunet_psycstore_service.h"
36 #include "gnunet_multicast_service.h"
38 #define DEBUG_PSYCSTORE GNUNET_EXTRA_LOGGING
40 # define LOG_LEVEL "DEBUG"
42 # define LOG_LEVEL "WARNING"
45 #define C2ARG(str) str, (sizeof (str) - 1)
47 #define LOG(kind,...) \
48 GNUNET_log_from (kind, "test-plugin-psycstore", __VA_ARGS__)
53 * Name of plugin under test.
55 static const char *plugin_name;
57 static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
58 static struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;
60 static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
61 static struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
64 * Function called when the service shuts down. Unloads our psycstore
67 * @param api api to unload
70 unload_plugin (struct GNUNET_PSYCSTORE_PluginFunctions *api)
74 GNUNET_asprintf (&libname, "libgnunet_plugin_psycstore_%s", plugin_name);
75 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
76 GNUNET_free (libname);
81 * Load the psycstore plugin.
83 * @param cfg configuration to pass
84 * @return NULL on error
86 static struct GNUNET_PSYCSTORE_PluginFunctions *
87 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
89 struct GNUNET_PSYCSTORE_PluginFunctions *ret;
92 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Loading `%s' psycstore plugin\n"),
94 GNUNET_asprintf (&libname, "libgnunet_plugin_psycstore_%s", plugin_name);
95 if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
97 FPRINTF (stderr, "Failed to load plugin `%s'!\n", plugin_name);
100 GNUNET_free (libname);
105 struct FragmentClosure
109 struct GNUNET_MULTICAST_MessageHeader *msg[16];
113 fragment_cb (void *cls, struct GNUNET_MULTICAST_MessageHeader *msg2,
114 enum GNUNET_PSYCSTORE_MessageFlags flags)
116 struct FragmentClosure *fcls = cls;
117 struct GNUNET_MULTICAST_MessageHeader *msg1 = fcls->msg[fcls->n];
118 uint64_t flags1 = fcls->flags[fcls->n++];
121 if (flags1 == flags && msg1->header.size == msg2->header.size
122 && 0 == memcmp (msg1, msg2, ntohs (msg1->header.size)))
124 LOG (GNUNET_ERROR_TYPE_DEBUG, "Fragment %llu matches\n",
125 GNUNET_ntohll (msg1->fragment_id));
130 LOG (GNUNET_ERROR_TYPE_ERROR, "Fragment %llu differs\n",
131 GNUNET_ntohll (msg1->fragment_id));
140 struct StateClosure {
144 size_t value_size[16];
148 state_cb (void *cls, const char *name, const void *value, uint32_t value_size)
150 struct StateClosure *scls = cls;
151 const void *val = scls->value[scls->n];
152 size_t val_size = scls->value_size[scls->n++];
154 /* FIXME: check name */
156 return value_size == val_size && 0 == memcmp (value, val, val_size)
163 run (void *cls, char *const *args, const char *cfgfile,
164 const struct GNUNET_CONFIGURATION_Handle *cfg)
166 struct GNUNET_PSYCSTORE_PluginFunctions *db;
169 db = load_plugin (cfg);
174 "Failed to initialize PSYCstore. "
175 "Database likely not setup, skipping test.\n");
180 /* Store & test membership */
182 LOG (GNUNET_ERROR_TYPE_INFO, "MEMBERSHIP\n");
184 channel_key = GNUNET_CRYPTO_eddsa_key_create ();
185 slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
187 GNUNET_CRYPTO_eddsa_key_get_public (channel_key,
189 GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
191 LOG (GNUNET_ERROR_TYPE_INFO, "membership_store()\n");
193 GNUNET_assert (GNUNET_OK == db->membership_store (db->cls, &channel_pub_key,
194 &slave_pub_key, GNUNET_YES,
197 LOG (GNUNET_ERROR_TYPE_INFO, "membership_test()\n");
199 GNUNET_assert (GNUNET_YES == db->membership_test (db->cls, &channel_pub_key,
202 GNUNET_assert (GNUNET_YES == db->membership_test (db->cls, &channel_pub_key,
205 GNUNET_assert (GNUNET_NO == db->membership_test (db->cls, &channel_pub_key,
208 /* Store & get messages */
210 LOG (GNUNET_ERROR_TYPE_INFO, "MESSAGES\n");
212 struct GNUNET_MULTICAST_MessageHeader *msg
213 = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
214 GNUNET_assert (msg != NULL);
216 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
217 msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
219 uint64_t fragment_id = INT64_MAX - 1;
220 msg->fragment_id = GNUNET_htonll (fragment_id);
222 uint64_t message_id = INT64_MAX - 10;
223 msg->message_id = GNUNET_htonll (message_id);
225 uint64_t group_generation = INT64_MAX - 3;
226 msg->group_generation = GNUNET_htonll (group_generation);
228 msg->hop_counter = htonl (9);
229 msg->fragment_offset = GNUNET_htonll (0);
230 msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
232 GNUNET_memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
234 msg->purpose.size = htonl (ntohs (msg->header.size)
235 - sizeof (msg->header)
236 - sizeof (msg->hop_counter)
237 - sizeof (msg->signature));
238 msg->purpose.purpose = htonl (234);
239 GNUNET_assert (GNUNET_OK ==
240 GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose, &msg->signature));
242 LOG (GNUNET_ERROR_TYPE_INFO, "fragment_store()\n");
244 struct FragmentClosure fcls = { 0 };
247 fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
250 GNUNET_OK == db->fragment_store (db->cls, &channel_pub_key, msg,
253 LOG (GNUNET_ERROR_TYPE_INFO, "fragment_get(%" PRIu64 ")\n", fragment_id);
255 uint64_t ret_frags = 0;
257 GNUNET_OK == db->fragment_get (db->cls, &channel_pub_key,
258 fragment_id, fragment_id,
259 &ret_frags, fragment_cb, &fcls));
260 GNUNET_assert (fcls.n == 1);
262 LOG (GNUNET_ERROR_TYPE_INFO, "fragment_get(%" PRIu64 ")\n", fragment_id+1);
266 GNUNET_OK == db->fragment_get (db->cls, &channel_pub_key,
267 fragment_id+1, fragment_id+1,
268 &ret_frags, fragment_cb, &fcls));
269 GNUNET_assert (fcls.n == 1);
271 // FIXME: test fragment_get_latest and message_get_latest
273 LOG (GNUNET_ERROR_TYPE_INFO, "message_get_fragment()\n");
278 GNUNET_OK == db->message_get_fragment (db->cls, &channel_pub_key,
279 GNUNET_ntohll (msg->message_id),
280 GNUNET_ntohll (msg->fragment_offset),
281 fragment_cb, &fcls));
282 GNUNET_assert (fcls.n == 1);
284 LOG (GNUNET_ERROR_TYPE_INFO, "message_add_flags()\n");
287 GNUNET_OK == db->message_add_flags (db->cls, &channel_pub_key,
288 GNUNET_ntohll (msg->message_id),
289 GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED));
291 LOG (GNUNET_ERROR_TYPE_INFO, "fragment_get(%" PRIu64 ")\n", fragment_id);
294 fcls.flags[0] |= GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
297 GNUNET_OK == db->fragment_get (db->cls, &channel_pub_key,
298 fragment_id, fragment_id,
299 &ret_frags, fragment_cb, &fcls));
301 GNUNET_assert (fcls.n == 1);
303 LOG (GNUNET_ERROR_TYPE_INFO, "fragment_store()\n");
305 struct GNUNET_MULTICAST_MessageHeader *msg1
306 = GNUNET_malloc (sizeof (*msg1) + sizeof (channel_pub_key));
308 GNUNET_memcpy (msg1, msg, sizeof (*msg1) + sizeof (channel_pub_key));
310 msg1->fragment_id = GNUNET_htonll (INT64_MAX);
311 msg1->fragment_offset = GNUNET_htonll (32768);
315 fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
317 GNUNET_assert (GNUNET_OK == db->fragment_store (db->cls, &channel_pub_key, msg1,
320 LOG (GNUNET_ERROR_TYPE_INFO, "message_get()\n");
323 GNUNET_OK == db->message_get (db->cls, &channel_pub_key,
324 message_id, message_id, 0,
325 &ret_frags, fragment_cb, &fcls));
326 GNUNET_assert (fcls.n == 2 && ret_frags == 2);
328 /* Message counters */
330 LOG (GNUNET_ERROR_TYPE_INFO, "counters_message_get()\n");
334 group_generation = 0;
336 GNUNET_OK == db->counters_message_get (db->cls, &channel_pub_key,
337 &fragment_id, &message_id,
339 && fragment_id == GNUNET_ntohll (msg1->fragment_id)
340 && message_id == GNUNET_ntohll (msg1->message_id)
341 && group_generation == GNUNET_ntohll (msg1->group_generation));
345 LOG (GNUNET_ERROR_TYPE_INFO, "STATE\n");
347 LOG (GNUNET_ERROR_TYPE_INFO, "state_modify_*()\n");
349 message_id = GNUNET_ntohll (fcls.msg[0]->message_id) + 1;
350 GNUNET_assert (GNUNET_OK == db->state_modify_begin (db->cls, &channel_pub_key,
353 GNUNET_assert (GNUNET_OK == db->state_modify_op (db->cls, &channel_pub_key,
354 GNUNET_PSYC_OP_ASSIGN,
356 C2ARG("one two three")));
358 GNUNET_assert (GNUNET_OK == db->state_modify_op (db->cls, &channel_pub_key,
359 GNUNET_PSYC_OP_ASSIGN,
360 "_foo_bar", slave_key,
361 sizeof (*slave_key)));
363 GNUNET_assert (GNUNET_OK == db->state_modify_end (db->cls, &channel_pub_key,
366 LOG (GNUNET_ERROR_TYPE_INFO, "state_get()\n");
368 struct StateClosure scls = { 0 };
370 scls.value[0] = "one two three";
371 scls.value_size[0] = strlen ("one two three");
373 GNUNET_assert (GNUNET_OK == db->state_get (db->cls, &channel_pub_key, "_foo",
375 GNUNET_assert (scls.n == 1);
377 LOG (GNUNET_ERROR_TYPE_INFO, "state_get_prefix()\n");
380 scls.value[1] = slave_key;
381 scls.value_size[1] = sizeof (*slave_key);
383 GNUNET_assert (GNUNET_OK == db->state_get_prefix (db->cls, &channel_pub_key,
384 "_foo", state_cb, &scls));
385 GNUNET_assert (scls.n == 2);
387 LOG (GNUNET_ERROR_TYPE_INFO, "state_get_signed()\n");
390 GNUNET_assert (GNUNET_NO == db->state_get_signed (db->cls, &channel_pub_key,
392 GNUNET_assert (scls.n == 0);
394 LOG (GNUNET_ERROR_TYPE_INFO, "state_update_signed()\n");
396 GNUNET_assert (GNUNET_OK == db->state_update_signed (db->cls,
399 LOG (GNUNET_ERROR_TYPE_INFO, "state_get_signed()\n");
402 GNUNET_assert (GNUNET_YES == db->state_get_signed (db->cls, &channel_pub_key,
404 GNUNET_assert (scls.n == 2);
408 LOG (GNUNET_ERROR_TYPE_INFO, "counters_state_get()\n");
410 uint64_t max_state_msg_id = 0;
411 GNUNET_assert (GNUNET_OK == db->counters_state_get (db->cls, &channel_pub_key,
413 && max_state_msg_id == message_id);
417 LOG (GNUNET_ERROR_TYPE_INFO, "state_sync_*()\n");
420 scls.value[0] = channel_key;
421 scls.value_size[0] = sizeof (*channel_key);
422 scls.value[1] = "three two one";
423 scls.value_size[1] = strlen ("three two one");
425 GNUNET_assert (GNUNET_OK == db->state_sync_begin (db->cls, &channel_pub_key));
427 GNUNET_assert (GNUNET_OK == db->state_sync_assign (db->cls, &channel_pub_key,
428 "_sync_bar", scls.value[0],
429 scls.value_size[0]));
431 GNUNET_assert (GNUNET_OK == db->state_sync_assign (db->cls, &channel_pub_key,
432 "_sync_foo", scls.value[1],
433 scls.value_size[1]));
435 GNUNET_assert (GNUNET_OK == db->state_sync_end (db->cls, &channel_pub_key,
439 GNUNET_assert (GNUNET_NO == db->state_get_prefix (db->cls, &channel_pub_key,
440 "_foo", state_cb, &scls));
441 GNUNET_assert (scls.n == 0);
443 GNUNET_assert (GNUNET_OK == db->state_get_prefix (db->cls, &channel_pub_key,
444 "_sync", state_cb, &scls));
445 GNUNET_assert (scls.n == 2);
448 GNUNET_assert (GNUNET_OK == db->state_get_signed (db->cls, &channel_pub_key,
450 GNUNET_assert (scls.n == 2);
452 /* Modify state after sync */
454 LOG (GNUNET_ERROR_TYPE_INFO, "state_modify_*()\n");
456 message_id = GNUNET_ntohll (fcls.msg[0]->message_id) + 6;
457 GNUNET_assert (GNUNET_OK == db->state_modify_begin (db->cls, &channel_pub_key,
459 message_id - max_state_msg_id));
461 GNUNET_assert (GNUNET_OK == db->state_modify_op (db->cls, &channel_pub_key,
462 GNUNET_PSYC_OP_ASSIGN,
464 C2ARG("five six seven")));
466 GNUNET_assert (GNUNET_OK == db->state_modify_end (db->cls, &channel_pub_key,
471 LOG (GNUNET_ERROR_TYPE_INFO, "state_reset()\n");
474 GNUNET_assert (GNUNET_OK == db->state_reset (db->cls, &channel_pub_key));
475 GNUNET_assert (scls.n == 0);
479 if (NULL != channel_key)
481 GNUNET_free (channel_key);
484 if (NULL != slave_key)
486 GNUNET_free (slave_key);
495 main (int argc, char *argv[])
498 char *const xargv[] = {
499 "test-plugin-psycstore",
504 struct GNUNET_GETOPT_CommandLineOption options[] = {
505 GNUNET_GETOPT_OPTION_END
507 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-psycstore-sqlite");
508 GNUNET_log_setup ("test-plugin-psycstore", LOG_LEVEL, NULL);
509 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
510 GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_plugin_psycstore_%s.conf",
512 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
513 "test-plugin-psycstore", "nohelp", options, &run, NULL);
517 FPRINTF (stderr, "Missed some testcases: %d\n", ok);
519 #if ! DEBUG_PSYCSTORE
520 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-psycstore-sqlite");
526 /* end of test_plugin_psycstore.c */