2 * This file is part of GNUnet
3 * (C) 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * @file psycstore/test_plugin_psycstore.c
23 * @brief Test for the PSYCstore plugins.
24 * @author Gabor X Toth
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testing_lib.h"
30 #include "gnunet_psycstore_plugin.h"
31 #include "gnunet_psycstore_service.h"
32 #include "gnunet_multicast_service.h"
34 #define DEBUG_PSYCSTORE GNUNET_EXTRA_LOGGING
36 # define LOG_LEVEL "DEBUG"
38 # define LOG_LEVEL "WARNING"
41 #define C2ARG(str) str, (sizeof (str) - 1)
43 #define LOG(kind,...) \
44 GNUNET_log_from (kind, "test-plugin-psycstore", __VA_ARGS__)
49 * Name of plugin under test.
51 static const char *plugin_name;
53 static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
54 static struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;
56 static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
57 static struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
60 * Function called when the service shuts down. Unloads our psycstore
63 * @param api api to unload
66 unload_plugin (struct GNUNET_PSYCSTORE_PluginFunctions *api)
70 GNUNET_asprintf (&libname, "libgnunet_plugin_psycstore_%s", plugin_name);
71 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
72 GNUNET_free (libname);
77 * Load the psycstore plugin.
79 * @param cfg configuration to pass
80 * @return NULL on error
82 static struct GNUNET_PSYCSTORE_PluginFunctions *
83 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
85 struct GNUNET_PSYCSTORE_PluginFunctions *ret;
88 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' psycstore plugin\n"),
90 GNUNET_asprintf (&libname, "libgnunet_plugin_psycstore_%s", plugin_name);
91 if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
93 FPRINTF (stderr, "Failed to load plugin `%s'!\n", plugin_name);
96 GNUNET_free (libname);
101 struct FragmentClosure
105 struct GNUNET_MULTICAST_MessageHeader *msg[16];
109 fragment_cb (void *cls, struct GNUNET_MULTICAST_MessageHeader *msg2,
110 enum GNUNET_PSYCSTORE_MessageFlags flags)
112 struct FragmentClosure *fcls = cls;
113 struct GNUNET_MULTICAST_MessageHeader *msg1 = fcls->msg[fcls->n];
114 uint64_t flags1 = fcls->flags[fcls->n++];
117 if (flags1 == flags && msg1->header.size == msg2->header.size
118 && 0 == memcmp (msg1, msg2, ntohs (msg1->header.size)))
120 LOG (GNUNET_ERROR_TYPE_DEBUG, "Fragment %llu matches\n",
121 GNUNET_ntohll (msg1->fragment_id));
126 LOG (GNUNET_ERROR_TYPE_ERROR, "Fragment %llu differs\n",
127 GNUNET_ntohll (msg1->fragment_id));
136 struct StateClosure {
140 size_t value_size[16];
144 state_cb (void *cls, const char *name, const void *value, size_t value_size)
146 struct StateClosure *scls = cls;
147 const void *val = scls->value[scls->n];
148 size_t val_size = scls->value_size[scls->n++];
150 /* FIXME: check name */
152 return value_size == val_size && 0 == memcmp (value, val, val_size)
159 run (void *cls, char *const *args, const char *cfgfile,
160 const struct GNUNET_CONFIGURATION_Handle *cfg)
162 struct GNUNET_PSYCSTORE_PluginFunctions *db;
165 db = load_plugin (cfg);
170 "Failed to initialize PSYCstore. "
171 "Database likely not setup, skipping test.\n");
175 /* Store & test membership */
177 channel_key = GNUNET_CRYPTO_eddsa_key_create ();
178 slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
180 GNUNET_CRYPTO_eddsa_key_get_public (channel_key,
182 GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
184 GNUNET_assert (GNUNET_OK == db->membership_store (db->cls, &channel_pub_key,
185 &slave_pub_key, GNUNET_YES,
188 GNUNET_assert (GNUNET_YES == db->membership_test (db->cls, &channel_pub_key,
191 GNUNET_assert (GNUNET_YES == db->membership_test (db->cls, &channel_pub_key,
194 GNUNET_assert (GNUNET_NO == db->membership_test (db->cls, &channel_pub_key,
198 /* Store & get messages */
200 struct GNUNET_MULTICAST_MessageHeader *msg
201 = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
202 GNUNET_assert (msg != NULL);
204 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
205 msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
207 uint64_t fragment_id = INT64_MAX - 1;
208 msg->fragment_id = GNUNET_htonll (fragment_id);
210 uint64_t message_id = INT64_MAX - 10;
211 msg->message_id = GNUNET_htonll (message_id);
213 uint64_t group_generation = INT64_MAX - 3;
214 msg->group_generation = GNUNET_htonll (group_generation);
216 msg->hop_counter = htonl (9);
217 msg->fragment_offset = GNUNET_htonll (0);
218 msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
220 memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
222 msg->purpose.size = htonl (ntohs (msg->header.size)
223 - sizeof (msg->header)
224 - sizeof (msg->hop_counter)
225 - sizeof (msg->signature));
226 msg->purpose.purpose = htonl (234);
227 GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose, &msg->signature);
229 struct FragmentClosure fcls = { 0 };
232 fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
235 GNUNET_OK == db->fragment_store (db->cls, &channel_pub_key, msg,
238 uint64_t ret_frags = 0;
240 GNUNET_OK == db->fragment_get (db->cls, &channel_pub_key,
241 fragment_id, fragment_id,
242 &ret_frags, fragment_cb, &fcls));
243 GNUNET_assert (fcls.n == 1);
245 // FIXME: test fragment_get_latest and message_get_latest
250 GNUNET_OK == db->message_get_fragment (db->cls, &channel_pub_key,
251 GNUNET_ntohll (msg->message_id),
252 GNUNET_ntohll (msg->fragment_offset),
253 fragment_cb, &fcls));
254 GNUNET_assert (fcls.n == 1);
257 GNUNET_OK == db->message_add_flags (db->cls, &channel_pub_key,
258 GNUNET_ntohll (msg->message_id),
259 GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED));
262 fcls.flags[0] |= GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
265 GNUNET_OK == db->fragment_get (db->cls, &channel_pub_key,
266 fragment_id, fragment_id,
267 &ret_frags, fragment_cb, &fcls));
268 GNUNET_assert (fcls.n == 1);
270 struct GNUNET_MULTICAST_MessageHeader *msg1
271 = GNUNET_malloc (sizeof (*msg1) + sizeof (channel_pub_key));
273 memcpy (msg1, msg, sizeof (*msg1) + sizeof (channel_pub_key));
275 msg1->fragment_id = GNUNET_htonll (INT64_MAX);
276 msg1->fragment_offset = GNUNET_htonll (32768);
280 fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
282 GNUNET_assert (GNUNET_OK == db->fragment_store (db->cls, &channel_pub_key, msg1,
286 GNUNET_OK == db->message_get (db->cls, &channel_pub_key,
287 message_id, message_id,
288 &ret_frags, fragment_cb, &fcls));
289 GNUNET_assert (fcls.n == 2 && ret_frags == 2);
291 /* Message counters */
295 group_generation = 0;
297 GNUNET_OK == db->counters_message_get (db->cls, &channel_pub_key,
298 &fragment_id, &message_id,
300 && fragment_id == GNUNET_ntohll (msg1->fragment_id)
301 && message_id == GNUNET_ntohll (msg1->message_id)
302 && group_generation == GNUNET_ntohll (msg1->group_generation));
306 message_id = GNUNET_ntohll (fcls.msg[0]->message_id) + 1;
307 GNUNET_assert (GNUNET_OK == db->state_modify_begin (db->cls, &channel_pub_key,
310 GNUNET_assert (GNUNET_OK == db->state_modify_set (db->cls, &channel_pub_key,
312 C2ARG("one two three")));
314 GNUNET_assert (GNUNET_OK == db->state_modify_set (db->cls, &channel_pub_key,
315 "_foo_bar", slave_key,
316 sizeof (*slave_key)));
318 GNUNET_assert (GNUNET_OK == db->state_modify_end (db->cls, &channel_pub_key,
321 struct StateClosure scls = { 0 };
323 scls.value[0] = "one two three";
324 scls.value_size[0] = strlen ("one two three");
326 GNUNET_assert (GNUNET_OK == db->state_get (db->cls, &channel_pub_key, "_foo",
328 GNUNET_assert (scls.n == 1);
331 scls.value[1] = slave_key;
332 scls.value_size[1] = sizeof (*slave_key);
334 GNUNET_assert (GNUNET_OK == db->state_get_prefix (db->cls, &channel_pub_key,
335 "_foo", state_cb, &scls));
336 GNUNET_assert (scls.n == 2);
339 GNUNET_assert (GNUNET_NO == db->state_get_signed (db->cls, &channel_pub_key,
341 GNUNET_assert (scls.n == 0);
343 GNUNET_assert (GNUNET_OK == db->state_update_signed (db->cls,
347 GNUNET_assert (GNUNET_YES == db->state_get_signed (db->cls, &channel_pub_key,
349 GNUNET_assert (scls.n == 2);
353 uint64_t max_state_msg_id = 0;
354 GNUNET_assert (GNUNET_OK == db->counters_state_get (db->cls, &channel_pub_key,
356 && max_state_msg_id == message_id);
361 scls.value[0] = channel_key;
362 scls.value_size[0] = sizeof (*channel_key);
363 scls.value[1] = "three two one";
364 scls.value_size[1] = strlen ("three two one");
366 GNUNET_assert (GNUNET_OK == db->state_sync_begin (db->cls, &channel_pub_key));
368 GNUNET_assert (GNUNET_OK == db->state_sync_set (db->cls, &channel_pub_key,
369 "_sync_bar", scls.value[0],
370 scls.value_size[0]));
372 GNUNET_assert (GNUNET_OK == db->state_sync_set (db->cls, &channel_pub_key,
373 "_sync_foo", scls.value[1],
374 scls.value_size[1]));
376 GNUNET_assert (GNUNET_OK == db->state_sync_end (db->cls, &channel_pub_key,
379 GNUNET_assert (GNUNET_NO == db->state_get_prefix (db->cls, &channel_pub_key,
380 "_foo", state_cb, &scls));
381 GNUNET_assert (scls.n == 0);
383 GNUNET_assert (GNUNET_OK == db->state_get_prefix (db->cls, &channel_pub_key,
384 "_sync", state_cb, &scls));
385 GNUNET_assert (scls.n == 2);
388 GNUNET_assert (GNUNET_OK == db->state_get_signed (db->cls, &channel_pub_key,
390 GNUNET_assert (scls.n == 2);
392 /* Modify state after sync */
394 message_id = GNUNET_ntohll (fcls.msg[0]->message_id) + 6;
395 GNUNET_assert (GNUNET_OK == db->state_modify_begin (db->cls, &channel_pub_key,
398 GNUNET_assert (GNUNET_OK == db->state_modify_set (db->cls, &channel_pub_key,
400 C2ARG("five six seven")));
402 GNUNET_assert (GNUNET_OK == db->state_modify_end (db->cls, &channel_pub_key,
408 GNUNET_assert (GNUNET_OK == db->state_reset (db->cls, &channel_pub_key));
409 GNUNET_assert (scls.n == 0);
413 if (NULL != channel_key)
415 GNUNET_free (channel_key);
418 if (NULL != slave_key)
420 GNUNET_free (slave_key);
429 main (int argc, char *argv[])
432 char *const xargv[] = {
433 "test-plugin-psycstore",
438 struct GNUNET_GETOPT_CommandLineOption options[] = {
439 GNUNET_GETOPT_OPTION_END
441 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-psycstore-sqlite");
442 GNUNET_log_setup ("test-plugin-psycstore", LOG_LEVEL, NULL);
443 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
444 GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_plugin_psycstore_%s.conf",
446 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
447 "test-plugin-psycstore", "nohelp", options, &run, NULL);
450 FPRINTF (stderr, "Missed some testcases: %d\n", ok);
452 #if ! DEBUG_PSYCSTORE
453 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-psycstore-sqlite");
459 /* end of test_plugin_psycstore.c */