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__)
50 LOG (GNUNET_ERROR_TYPE_ERROR, "Error at %s:%d\n", __FILE__, __LINE__); \
58 * Name of plugin under test.
60 static const char *plugin_name;
62 static struct GNUNET_CRYPTO_EccPrivateKey *channel_key;
63 static struct GNUNET_CRYPTO_EccPrivateKey *slave_key;
65 static struct GNUNET_CRYPTO_EccPublicSignKey channel_pub_key;
66 static struct GNUNET_CRYPTO_EccPublicSignKey slave_pub_key;
69 * Function called when the service shuts down. Unloads our psycstore
72 * @param api api to unload
75 unload_plugin (struct GNUNET_PSYCSTORE_PluginFunctions *api)
79 GNUNET_asprintf (&libname, "libgnunet_plugin_psycstore_%s", plugin_name);
80 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
81 GNUNET_free (libname);
86 * Load the psycstore plugin.
88 * @param cfg configuration to pass
89 * @return NULL on error
91 static struct GNUNET_PSYCSTORE_PluginFunctions *
92 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
94 struct GNUNET_PSYCSTORE_PluginFunctions *ret;
97 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' psycstore plugin\n"),
99 GNUNET_asprintf (&libname, "libgnunet_plugin_psycstore_%s", plugin_name);
100 if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
102 FPRINTF (stderr, "Failed to load plugin `%s'!\n", plugin_name);
105 GNUNET_free (libname);
110 struct FragmentClosure
114 struct GNUNET_MULTICAST_MessageHeader *msg[16];
118 fragment_cb (void *cls, struct GNUNET_MULTICAST_MessageHeader *msg2,
119 enum GNUNET_PSYCSTORE_MessageFlags flags)
121 struct FragmentClosure *fcls = cls;
122 struct GNUNET_MULTICAST_MessageHeader *msg1 = fcls->msg[fcls->n];
123 uint64_t flags1 = fcls->flags[fcls->n++];
126 if (flags1 == flags && msg1->header.size == msg2->header.size
127 && 0 == memcmp (msg1, msg2, ntohs (msg1->header.size)))
129 LOG (GNUNET_ERROR_TYPE_DEBUG, "Fragment %llu matches\n",
130 GNUNET_ntohll (msg1->fragment_id));
135 LOG (GNUNET_ERROR_TYPE_ERROR, "Fragment %llu differs\n",
136 GNUNET_ntohll (msg1->fragment_id));
145 struct StateClosure {
149 size_t value_size[16];
153 state_cb (void *cls, const char *name, const void *value, size_t value_size)
155 struct StateClosure *scls = cls;
156 const void *val = scls->value[scls->n];
157 size_t val_size = scls->value_size[scls->n++];
159 /* FIXME: check name */
161 return value_size == val_size && 0 == memcmp (value, val, val_size)
168 run (void *cls, char *const *args, const char *cfgfile,
169 const struct GNUNET_CONFIGURATION_Handle *cfg)
171 struct GNUNET_PSYCSTORE_PluginFunctions *db;
174 db = load_plugin (cfg);
179 "Failed to initialize PSYCstore. "
180 "Database likely not setup, skipping test.\n");
184 /* Store & test membership */
186 channel_key = GNUNET_CRYPTO_ecc_key_create ();
187 slave_key = GNUNET_CRYPTO_ecc_key_create ();
189 GNUNET_CRYPTO_ecc_key_get_public_for_signature (channel_key, &channel_pub_key);
190 GNUNET_CRYPTO_ecc_key_get_public_for_signature (slave_key, &slave_pub_key);
192 ASSERT (GNUNET_OK == db->membership_store (db->cls, &channel_pub_key,
193 &slave_pub_key, GNUNET_YES,
196 ASSERT (GNUNET_YES == db->membership_test (db->cls, &channel_pub_key,
199 ASSERT (GNUNET_YES == db->membership_test (db->cls, &channel_pub_key,
202 ASSERT (GNUNET_NO == db->membership_test (db->cls, &channel_pub_key,
206 /* Store & get messages */
208 struct GNUNET_MULTICAST_MessageHeader *msg
209 = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
210 ASSERT (msg != NULL);
212 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
213 msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
215 msg->hop_counter = htonl (9);
216 msg->fragment_id = GNUNET_htonll (INT64_MAX - 1);
217 msg->fragment_offset = GNUNET_htonll (0);
218 msg->message_id = GNUNET_htonll (INT64_MAX - 10);
219 msg->group_generation = GNUNET_htonll (INT64_MAX - 3);
220 msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
222 memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
224 msg->purpose.size = htonl (ntohs (msg->header.size)
225 - sizeof (msg->header)
226 - sizeof (msg->hop_counter)
227 - sizeof (msg->signature));
228 msg->purpose.purpose = htonl (234);
229 GNUNET_CRYPTO_ecc_sign (slave_key, &msg->purpose, &msg->signature);
231 struct FragmentClosure fcls = { 0 };
234 fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
236 ASSERT (GNUNET_OK == db->fragment_store (db->cls, &channel_pub_key, msg,
239 ASSERT (GNUNET_OK == db->fragment_get (db->cls, &channel_pub_key,
240 GNUNET_ntohll (msg->fragment_id),
241 fragment_cb, &fcls));
242 ASSERT (fcls.n == 1);
246 ASSERT (GNUNET_OK == db->message_get_fragment (db->cls, &channel_pub_key,
247 GNUNET_ntohll (msg->message_id),
248 GNUNET_ntohll (msg->fragment_offset),
249 fragment_cb, &fcls));
250 ASSERT (fcls.n == 1);
252 ASSERT (GNUNET_OK == db->message_add_flags (
253 db->cls, &channel_pub_key, GNUNET_ntohll (msg->message_id),
254 GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED));
257 fcls.flags[0] |= GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
259 ASSERT (GNUNET_OK == db->fragment_get (db->cls, &channel_pub_key,
260 GNUNET_ntohll (msg->fragment_id),
261 fragment_cb, &fcls));
262 ASSERT (fcls.n == 1);
264 struct GNUNET_MULTICAST_MessageHeader *msg1
265 = GNUNET_malloc (sizeof (*msg1) + sizeof (channel_pub_key));
267 memcpy (msg1, msg, sizeof (*msg1) + sizeof (channel_pub_key));
269 msg1->fragment_id = GNUNET_htonll (INT64_MAX);
270 msg1->fragment_offset = GNUNET_htonll (32768);
274 fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
276 ASSERT (GNUNET_OK == db->fragment_store (db->cls, &channel_pub_key, msg1,
279 uint64_t retfrags = 0;
280 ASSERT (GNUNET_OK == db->message_get (db->cls, &channel_pub_key,
281 GNUNET_ntohll (msg->message_id),
282 &retfrags, fragment_cb, &fcls));
283 ASSERT (fcls.n == 2 && retfrags == 2);
285 /* Master counters */
287 uint64_t fragment_id = 0, message_id = 0, group_generation = 0;
288 ASSERT (GNUNET_OK == db->counters_get_master (db->cls, &channel_pub_key,
289 &fragment_id, &message_id,
291 && fragment_id == GNUNET_ntohll (msg1->fragment_id)
292 && message_id == GNUNET_ntohll (msg1->message_id)
293 && group_generation == GNUNET_ntohll (msg1->group_generation));
298 message_id = GNUNET_ntohll (fcls.msg[0]->message_id) + 1;
299 ASSERT (GNUNET_OK == db->state_modify_begin (db->cls, &channel_pub_key,
302 ASSERT (GNUNET_OK == db->state_modify_set (db->cls, &channel_pub_key, "_foo",
303 C2ARG("one two three")));
305 ASSERT (GNUNET_OK == db->state_modify_set (db->cls, &channel_pub_key,
306 "_foo_bar", slave_key,
307 sizeof (*slave_key)));
309 ASSERT (GNUNET_OK == db->state_modify_end (db->cls, &channel_pub_key,
312 struct StateClosure scls = { 0 };
314 scls.value[0] = "one two three";
315 scls.value_size[0] = strlen ("one two three");
317 ASSERT (GNUNET_OK == db->state_get (db->cls, &channel_pub_key, "_foo",
319 ASSERT (scls.n == 1);
322 scls.value[1] = slave_key;
323 scls.value_size[1] = sizeof (*slave_key);
325 ASSERT (GNUNET_OK == db->state_get_prefix (db->cls, &channel_pub_key, "_foo",
327 ASSERT (scls.n == 2);
330 ASSERT (GNUNET_NO == db->state_get_signed (db->cls, &channel_pub_key,
332 ASSERT (scls.n == 0);
334 ASSERT (GNUNET_OK == db->state_update_signed (db->cls, &channel_pub_key));
337 ASSERT (GNUNET_YES == db->state_get_signed (db->cls, &channel_pub_key,
339 ASSERT (scls.n == 2);
343 uint64_t max_state_msg_id = 0;
344 ASSERT (GNUNET_OK == db->counters_get_slave (db->cls, &channel_pub_key,
346 && max_state_msg_id == message_id);
351 scls.value[0] = channel_key;
352 scls.value_size[0] = sizeof (*channel_key);
353 scls.value[1] = "three two one";
354 scls.value_size[1] = strlen ("three two one");
356 ASSERT (GNUNET_OK == db->state_sync_begin (db->cls, &channel_pub_key));
358 ASSERT (GNUNET_OK == db->state_sync_set (db->cls, &channel_pub_key,
360 scls.value[0], scls.value_size[0]));
362 ASSERT (GNUNET_OK == db->state_sync_set (db->cls, &channel_pub_key,
364 scls.value[1], scls.value_size[1]));
366 ASSERT (GNUNET_OK == db->state_sync_end (db->cls, &channel_pub_key, INT64_MAX - 5));
368 ASSERT (GNUNET_NO == db->state_get_prefix (db->cls, &channel_pub_key, "_foo",
370 ASSERT (scls.n == 0);
372 ASSERT (GNUNET_OK == db->state_get_prefix (db->cls, &channel_pub_key, "_sync",
374 ASSERT (scls.n == 2);
377 ASSERT (GNUNET_OK == db->state_get_signed (db->cls, &channel_pub_key,
379 ASSERT (scls.n == 2);
381 /* Modify state after sync */
383 message_id = GNUNET_ntohll (fcls.msg[0]->message_id) + 6;
384 ASSERT (GNUNET_OK == db->state_modify_begin (db->cls, &channel_pub_key,
387 ASSERT (GNUNET_OK == db->state_modify_set (db->cls, &channel_pub_key, "_sync_foo",
388 C2ARG("five six seven")));
390 ASSERT (GNUNET_OK == db->state_modify_end (db->cls, &channel_pub_key,
396 ASSERT (GNUNET_OK == db->state_reset (db->cls, &channel_pub_key));
397 ASSERT (scls.n == 0);
403 if (NULL != channel_key)
405 GNUNET_free (channel_key);
408 if (NULL != slave_key)
410 GNUNET_free (slave_key);
419 main (int argc, char *argv[])
422 char *const xargv[] = {
423 "test-plugin-psycstore",
428 struct GNUNET_GETOPT_CommandLineOption options[] = {
429 GNUNET_GETOPT_OPTION_END
431 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-psycstore-sqlite");
432 GNUNET_log_setup ("test-plugin-psycstore", LOG_LEVEL, NULL);
433 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
434 GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_plugin_psycstore_%s.conf",
436 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
437 "test-plugin-psycstore", "nohelp", options, &run, NULL);
440 FPRINTF (stderr, "Missed some testcases: %d\n", ok);
442 #if ! DEBUG_PSYCSTORE
443 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-psycstore-sqlite");
449 /* end of test_plugin_psycstore.c */