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.
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_psycstore_plugin.h"
32 #include "gnunet_psycstore_service.h"
33 #include "gnunet_multicast_service.h"
35 #define DEBUG_PSYCSTORE GNUNET_EXTRA_LOGGING
37 # define LOG_LEVEL "DEBUG"
39 # define LOG_LEVEL "WARNING"
42 #define C2ARG(str) str, (sizeof (str) - 1)
44 #define LOG(kind,...) \
45 GNUNET_log_from (kind, "test-plugin-psycstore", __VA_ARGS__)
50 * Name of plugin under test.
52 static const char *plugin_name;
54 static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
55 static struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;
57 static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
58 static struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
61 * Function called when the service shuts down. Unloads our psycstore
64 * @param api api to unload
67 unload_plugin (struct GNUNET_PSYCSTORE_PluginFunctions *api)
71 GNUNET_asprintf (&libname, "libgnunet_plugin_psycstore_%s", plugin_name);
72 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
73 GNUNET_free (libname);
78 * Load the psycstore plugin.
80 * @param cfg configuration to pass
81 * @return NULL on error
83 static struct GNUNET_PSYCSTORE_PluginFunctions *
84 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
86 struct GNUNET_PSYCSTORE_PluginFunctions *ret;
89 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Loading `%s' psycstore plugin\n"),
91 GNUNET_asprintf (&libname, "libgnunet_plugin_psycstore_%s", plugin_name);
92 if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
94 FPRINTF (stderr, "Failed to load plugin `%s'!\n", plugin_name);
97 GNUNET_free (libname);
102 struct FragmentClosure
106 struct GNUNET_MULTICAST_MessageHeader *msg[16];
110 fragment_cb (void *cls, struct GNUNET_MULTICAST_MessageHeader *msg2,
111 enum GNUNET_PSYCSTORE_MessageFlags flags)
113 struct FragmentClosure *fcls = cls;
114 struct GNUNET_MULTICAST_MessageHeader *msg1 = fcls->msg[fcls->n];
115 uint64_t flags1 = fcls->flags[fcls->n++];
118 if (flags1 == flags && msg1->header.size == msg2->header.size
119 && 0 == memcmp (msg1, msg2, ntohs (msg1->header.size)))
121 LOG (GNUNET_ERROR_TYPE_DEBUG, "Fragment %llu matches\n",
122 GNUNET_ntohll (msg1->fragment_id));
127 LOG (GNUNET_ERROR_TYPE_ERROR, "Fragment %llu differs\n",
128 GNUNET_ntohll (msg1->fragment_id));
137 struct StateClosure {
141 size_t value_size[16];
145 state_cb (void *cls, const char *name, const void *value, uint32_t value_size)
147 struct StateClosure *scls = cls;
148 const void *val = scls->value[scls->n];
149 size_t val_size = scls->value_size[scls->n++];
151 /* FIXME: check name */
153 return value_size == val_size && 0 == memcmp (value, val, val_size)
160 run (void *cls, char *const *args, const char *cfgfile,
161 const struct GNUNET_CONFIGURATION_Handle *cfg)
163 struct GNUNET_PSYCSTORE_PluginFunctions *db;
166 db = load_plugin (cfg);
171 "Failed to initialize PSYCstore. "
172 "Database likely not setup, skipping test.\n");
176 /* Store & test membership */
178 channel_key = GNUNET_CRYPTO_eddsa_key_create ();
179 slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
181 GNUNET_CRYPTO_eddsa_key_get_public (channel_key,
183 GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
185 GNUNET_assert (GNUNET_OK == db->membership_store (db->cls, &channel_pub_key,
186 &slave_pub_key, GNUNET_YES,
189 GNUNET_assert (GNUNET_YES == db->membership_test (db->cls, &channel_pub_key,
192 GNUNET_assert (GNUNET_YES == db->membership_test (db->cls, &channel_pub_key,
195 GNUNET_assert (GNUNET_NO == db->membership_test (db->cls, &channel_pub_key,
199 /* Store & get messages */
201 struct GNUNET_MULTICAST_MessageHeader *msg
202 = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
203 GNUNET_assert (msg != NULL);
205 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
206 msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
208 uint64_t fragment_id = INT64_MAX - 1;
209 msg->fragment_id = GNUNET_htonll (fragment_id);
211 uint64_t message_id = INT64_MAX - 10;
212 msg->message_id = GNUNET_htonll (message_id);
214 uint64_t group_generation = INT64_MAX - 3;
215 msg->group_generation = GNUNET_htonll (group_generation);
217 msg->hop_counter = htonl (9);
218 msg->fragment_offset = GNUNET_htonll (0);
219 msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
221 memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
223 msg->purpose.size = htonl (ntohs (msg->header.size)
224 - sizeof (msg->header)
225 - sizeof (msg->hop_counter)
226 - sizeof (msg->signature));
227 msg->purpose.purpose = htonl (234);
228 GNUNET_assert (GNUNET_OK ==
229 GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose, &msg->signature));
231 struct FragmentClosure fcls = { 0 };
234 fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
237 GNUNET_OK == db->fragment_store (db->cls, &channel_pub_key, msg,
240 uint64_t ret_frags = 0;
242 GNUNET_OK == db->fragment_get (db->cls, &channel_pub_key,
243 fragment_id, fragment_id,
244 &ret_frags, fragment_cb, &fcls));
245 GNUNET_assert (fcls.n == 1);
247 // FIXME: test fragment_get_latest and message_get_latest
252 GNUNET_OK == db->message_get_fragment (db->cls, &channel_pub_key,
253 GNUNET_ntohll (msg->message_id),
254 GNUNET_ntohll (msg->fragment_offset),
255 fragment_cb, &fcls));
256 GNUNET_assert (fcls.n == 1);
259 GNUNET_OK == db->message_add_flags (db->cls, &channel_pub_key,
260 GNUNET_ntohll (msg->message_id),
261 GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED));
264 fcls.flags[0] |= GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
267 GNUNET_OK == db->fragment_get (db->cls, &channel_pub_key,
268 fragment_id, fragment_id,
269 &ret_frags, fragment_cb, &fcls));
270 GNUNET_assert (fcls.n == 1);
272 struct GNUNET_MULTICAST_MessageHeader *msg1
273 = GNUNET_malloc (sizeof (*msg1) + sizeof (channel_pub_key));
275 memcpy (msg1, msg, sizeof (*msg1) + sizeof (channel_pub_key));
277 msg1->fragment_id = GNUNET_htonll (INT64_MAX);
278 msg1->fragment_offset = GNUNET_htonll (32768);
282 fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
284 GNUNET_assert (GNUNET_OK == db->fragment_store (db->cls, &channel_pub_key, msg1,
288 GNUNET_OK == db->message_get (db->cls, &channel_pub_key,
289 message_id, message_id, 0,
290 &ret_frags, fragment_cb, &fcls));
291 GNUNET_assert (fcls.n == 2 && ret_frags == 2);
293 /* Message counters */
297 group_generation = 0;
299 GNUNET_OK == db->counters_message_get (db->cls, &channel_pub_key,
300 &fragment_id, &message_id,
302 && fragment_id == GNUNET_ntohll (msg1->fragment_id)
303 && message_id == GNUNET_ntohll (msg1->message_id)
304 && group_generation == GNUNET_ntohll (msg1->group_generation));
308 message_id = GNUNET_ntohll (fcls.msg[0]->message_id) + 1;
309 GNUNET_assert (GNUNET_OK == db->state_modify_begin (db->cls, &channel_pub_key,
312 GNUNET_assert (GNUNET_OK == db->state_modify_op (db->cls, &channel_pub_key,
313 GNUNET_PSYC_OP_ASSIGN,
315 C2ARG("one two three")));
317 GNUNET_assert (GNUNET_OK == db->state_modify_op (db->cls, &channel_pub_key,
318 GNUNET_PSYC_OP_ASSIGN,
319 "_foo_bar", slave_key,
320 sizeof (*slave_key)));
322 GNUNET_assert (GNUNET_OK == db->state_modify_end (db->cls, &channel_pub_key,
325 struct StateClosure scls = { 0 };
327 scls.value[0] = "one two three";
328 scls.value_size[0] = strlen ("one two three");
330 GNUNET_assert (GNUNET_OK == db->state_get (db->cls, &channel_pub_key, "_foo",
332 GNUNET_assert (scls.n == 1);
335 scls.value[1] = slave_key;
336 scls.value_size[1] = sizeof (*slave_key);
338 GNUNET_assert (GNUNET_OK == db->state_get_prefix (db->cls, &channel_pub_key,
339 "_foo", state_cb, &scls));
340 GNUNET_assert (scls.n == 2);
343 GNUNET_assert (GNUNET_NO == db->state_get_signed (db->cls, &channel_pub_key,
345 GNUNET_assert (scls.n == 0);
347 GNUNET_assert (GNUNET_OK == db->state_update_signed (db->cls,
351 GNUNET_assert (GNUNET_YES == db->state_get_signed (db->cls, &channel_pub_key,
353 GNUNET_assert (scls.n == 2);
357 uint64_t max_state_msg_id = 0;
358 GNUNET_assert (GNUNET_OK == db->counters_state_get (db->cls, &channel_pub_key,
360 && max_state_msg_id == message_id);
365 scls.value[0] = channel_key;
366 scls.value_size[0] = sizeof (*channel_key);
367 scls.value[1] = "three two one";
368 scls.value_size[1] = strlen ("three two one");
370 GNUNET_assert (GNUNET_OK == db->state_sync_begin (db->cls, &channel_pub_key));
372 GNUNET_assert (GNUNET_OK == db->state_sync_assign (db->cls, &channel_pub_key,
373 "_sync_bar", scls.value[0],
374 scls.value_size[0]));
376 GNUNET_assert (GNUNET_OK == db->state_sync_assign (db->cls, &channel_pub_key,
377 "_sync_foo", scls.value[1],
378 scls.value_size[1]));
380 GNUNET_assert (GNUNET_OK == db->state_sync_end (db->cls, &channel_pub_key,
384 GNUNET_assert (GNUNET_NO == db->state_get_prefix (db->cls, &channel_pub_key,
385 "_foo", state_cb, &scls));
386 GNUNET_assert (scls.n == 0);
388 GNUNET_assert (GNUNET_OK == db->state_get_prefix (db->cls, &channel_pub_key,
389 "_sync", state_cb, &scls));
390 GNUNET_assert (scls.n == 2);
393 GNUNET_assert (GNUNET_OK == db->state_get_signed (db->cls, &channel_pub_key,
395 GNUNET_assert (scls.n == 2);
397 /* Modify state after sync */
399 message_id = GNUNET_ntohll (fcls.msg[0]->message_id) + 6;
400 GNUNET_assert (GNUNET_OK == db->state_modify_begin (db->cls, &channel_pub_key,
402 message_id - max_state_msg_id));
404 GNUNET_assert (GNUNET_OK == db->state_modify_op (db->cls, &channel_pub_key,
405 GNUNET_PSYC_OP_ASSIGN,
407 C2ARG("five six seven")));
409 GNUNET_assert (GNUNET_OK == db->state_modify_end (db->cls, &channel_pub_key,
415 GNUNET_assert (GNUNET_OK == db->state_reset (db->cls, &channel_pub_key));
416 GNUNET_assert (scls.n == 0);
420 if (NULL != channel_key)
422 GNUNET_free (channel_key);
425 if (NULL != slave_key)
427 GNUNET_free (slave_key);
436 main (int argc, char *argv[])
439 char *const xargv[] = {
440 "test-plugin-psycstore",
445 struct GNUNET_GETOPT_CommandLineOption options[] = {
446 GNUNET_GETOPT_OPTION_END
448 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-psycstore-sqlite");
449 GNUNET_log_setup ("test-plugin-psycstore", LOG_LEVEL, NULL);
450 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
451 GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_plugin_psycstore_%s.conf",
453 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
454 "test-plugin-psycstore", "nohelp", options, &run, NULL);
457 FPRINTF (stderr, "Missed some testcases: %d\n", ok);
459 #if ! DEBUG_PSYCSTORE
460 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-psycstore-sqlite");
466 /* end of test_plugin_psycstore.c */