2 This file is part of GNUnet.
3 Copyright (C) 2015 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.
21 * @file namestore/test_plugin_namestore.c
22 * @brief Test for the namestore plugins
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_peerstore_plugin.h"
28 #include "gnunet_testing_lib.h"
34 * Name of plugin under test.
36 static const char *plugin_name;
40 * Function called when the service shuts down. Unloads our namestore
43 * @param api api to unload
46 unload_plugin (struct GNUNET_PEERSTORE_PluginFunctions *api)
50 GNUNET_asprintf (&libname, "libgnunet_plugin_peer_%s", plugin_name);
51 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
52 GNUNET_free (libname);
57 * Load the namestore plugin.
59 * @param cfg configuration to pass
60 * @return NULL on error
62 static struct GNUNET_PEERSTORE_PluginFunctions *
63 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
65 struct GNUNET_PEERSTORE_PluginFunctions *ret;
68 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' peer plugin\n"),
70 GNUNET_asprintf (&libname, "libgnunet_plugin_peerstore_%s", plugin_name);
71 if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
73 FPRINTF (stderr, "Failed to load plugin `%s'!\n", plugin_name);
74 GNUNET_free (libname);
77 GNUNET_free (libname);
83 test_record (void *cls,
84 const struct GNUNET_PEERSTORE_Record *record,
87 struct GNUNET_PeerIdentity *id = cls;
88 char* testval = "test_val";
93 GNUNET_assert (0 == memcmp (record->peer, id, sizeof (struct GNUNET_PeerIdentity)));
94 GNUNET_assert (0 == strcmp ("subsys", record->sub_system));
95 GNUNET_assert (0 == strcmp ("key", record->key));
96 GNUNET_assert (0 == memcmp (testval, record->value, strlen (testval)));
101 get_record (struct GNUNET_PEERSTORE_PluginFunctions *psp,
102 const struct GNUNET_PeerIdentity *identity)
104 GNUNET_assert (GNUNET_OK == psp->iterate_records (psp->cls,
105 "subsys", identity, "key", &test_record, (void*)identity));
109 store_cont (void *cls, int status)
111 GNUNET_assert (GNUNET_OK == status);
115 put_record (struct GNUNET_PEERSTORE_PluginFunctions *psp, struct GNUNET_PeerIdentity *identity)
117 GNUNET_assert (GNUNET_OK == psp->store_record (psp->cls,
120 "key", "test_value", strlen ("test_value"),
121 GNUNET_TIME_absolute_get (),
122 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
129 run (void *cls, char *const *args, const char *cfgfile,
130 const struct GNUNET_CONFIGURATION_Handle *cfg)
132 struct GNUNET_PEERSTORE_PluginFunctions *psp;
133 struct GNUNET_PeerIdentity p1;
136 psp = load_plugin (cfg);
141 "Failed to initialize peerstore. Database likely not setup, skipping test.\n");
144 memset (&p1, 1, sizeof (p1));
145 put_record (psp, &p1);
146 get_record (psp, &p1);
153 main (int argc, char *argv[])
156 char *const xargv[] = {
157 "test-plugin-peerstore",
162 struct GNUNET_GETOPT_CommandLineOption options[] = {
163 GNUNET_GETOPT_OPTION_END
166 //GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namestore-sqlite");
167 GNUNET_log_setup ("test-plugin-peerstore",
170 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
171 GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_plugin_peerstore_%s.conf",
173 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
174 "test-plugin-peerstore", "nohelp", options, &run, NULL);
176 FPRINTF (stderr, "Missed some testcases: %d\n", ok);
177 //GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namestore-sqlite");
181 /* end of test_plugin_namestore.c */