2 This file is part of GNUnet.
3 Copyright (C) 2012 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_namestore_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_NAMESTORE_PluginFunctions *api)
50 GNUNET_asprintf (&libname, "libgnunet_plugin_namestore_%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_NAMESTORE_PluginFunctions *
63 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
65 struct GNUNET_NAMESTORE_PluginFunctions *ret;
68 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' namestore plugin\n"),
70 GNUNET_asprintf (&libname, "libgnunet_plugin_namestore_%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_CRYPTO_EcdsaPrivateKey *private_key,
86 unsigned int rd_count,
87 const struct GNUNET_GNSRECORD_Data *rd)
91 struct GNUNET_CRYPTO_EcdsaPrivateKey tzone_private_key;
93 unsigned int trd_count = 1 + (id % 1024);
96 GNUNET_snprintf (tname, sizeof (tname),
97 "a%u", (unsigned int ) id);
98 for (i=0;i<trd_count;i++)
100 GNUNET_assert (rd[i].data_size == id % 10);
101 GNUNET_assert (0 == memcmp ("Hello World", rd[i].data, id % 10));
102 GNUNET_assert (rd[i].record_type == 1 + (id % 13));
103 GNUNET_assert (rd[i].flags == 0);
105 memset (&tzone_private_key, (id % 241), sizeof (tzone_private_key));
106 GNUNET_assert (0 == strcmp (label, tname));
107 GNUNET_assert (0 == memcmp (&tzone_private_key, private_key, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)));
112 get_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
114 GNUNET_assert (GNUNET_OK == nsp->iterate_records (nsp->cls,
115 NULL, 0, &test_record, &id));
120 put_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
122 struct GNUNET_CRYPTO_EcdsaPrivateKey zone_private_key;
124 unsigned int rd_count = 1 + (id % 1024);
125 struct GNUNET_GNSRECORD_Data rd[rd_count];
126 struct GNUNET_CRYPTO_EcdsaSignature signature;
129 GNUNET_snprintf (label, sizeof (label),
130 "a%u", (unsigned int ) id);
131 for (i=0;i<rd_count;i++)
133 rd[i].data = "Hello World";
134 rd[i].data_size = id % 10;
135 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES).abs_value_us;
136 rd[i].record_type = 1 + (id % 13);
139 memset (&zone_private_key, (id % 241), sizeof (zone_private_key));
140 memset (&signature, (id % 243), sizeof (signature));
141 GNUNET_assert (GNUNET_OK == nsp->store_records (nsp->cls,
150 run (void *cls, char *const *args, const char *cfgfile,
151 const struct GNUNET_CONFIGURATION_Handle *cfg)
153 struct GNUNET_NAMESTORE_PluginFunctions *nsp;
156 nsp = load_plugin (cfg);
161 "Failed to initialize namestore. Database likely not setup, skipping test.\n");
172 main (int argc, char *argv[])
175 char *const xargv[] = {
176 "test-plugin-namestore",
181 struct GNUNET_GETOPT_CommandLineOption options[] = {
182 GNUNET_GETOPT_OPTION_END
185 //GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namestore-sqlite");
186 GNUNET_log_setup ("test-plugin-namestore",
189 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
190 GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_plugin_namestore_%s.conf",
192 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
193 "test-plugin-namestore", "nohelp", options, &run, NULL);
195 FPRINTF (stderr, "Missed some testcases: %d\n", ok);
196 //GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namestore-sqlite");
200 /* end of test_plugin_namestore.c */