2 This file is part of GNUnet.
3 (C) 2012 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.
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"
31 #define ASSERT(x) do { if (! (x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE;} } while (0)
36 * Name of plugin under test.
38 static const char *plugin_name;
42 * Function called when the service shuts down. Unloads our namestore
45 * @param api api to unload
48 unload_plugin (struct GNUNET_NAMESTORE_PluginFunctions *api)
52 GNUNET_asprintf (&libname, "libgnunet_plugin_namestore_%s", plugin_name);
53 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
54 GNUNET_free (libname);
59 * Load the namestore plugin.
61 * @param cfg configuration to pass
62 * @return NULL on error
64 static struct GNUNET_NAMESTORE_PluginFunctions *
65 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
67 struct GNUNET_NAMESTORE_PluginFunctions *ret;
70 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' namestore plugin\n"),
72 GNUNET_asprintf (&libname, "libgnunet_plugin_namestore_%s", plugin_name);
73 if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
75 FPRINTF (stderr, "Failed to load plugin `%s'!\n", plugin_name);
78 GNUNET_free (libname);
84 test_record (void *cls,
85 const struct GNUNET_CRYPTO_EccPrivateKey *private_key,
87 unsigned int rd_count,
88 const struct GNUNET_NAMESTORE_RecordData *rd)
92 struct GNUNET_CRYPTO_EccPrivateKey tzone_private_key;
94 unsigned int trd_count = 1 + (id % 1024);
97 GNUNET_snprintf (tname, sizeof (tname),
98 "a%u", (unsigned int ) id);
99 for (i=0;i<trd_count;i++)
101 GNUNET_assert (rd[i].data_size == id % 10);
102 GNUNET_assert (0 == memcmp ("Hello World", rd[i].data, id % 10));
103 GNUNET_assert (rd[i].record_type == 1 + (id % 13));
104 GNUNET_assert (rd[i].flags == (id % 7));
106 memset (&tzone_private_key, (id % 241), sizeof (tzone_private_key));
107 GNUNET_assert (0 == strcmp (label, tname));
108 GNUNET_assert (0 == memcmp (&tzone_private_key, private_key, sizeof (struct GNUNET_CRYPTO_EccPrivateKey)));
113 get_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
115 GNUNET_assert (GNUNET_OK == nsp->iterate_records (nsp->cls,
116 NULL, 0, &test_record, &id));
121 put_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
123 struct GNUNET_CRYPTO_EccPrivateKey zone_private_key;
125 unsigned int rd_count = 1 + (id % 1024);
126 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
127 struct GNUNET_CRYPTO_EccSignature signature;
130 GNUNET_snprintf (label, sizeof (label),
131 "a%u", (unsigned int ) id);
132 for (i=0;i<rd_count;i++)
134 rd[i].data = "Hello World";
135 rd[i].data_size = id % 10;
136 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES).abs_value_us;
137 rd[i].record_type = 1 + (id % 13);
138 rd[i].flags = (id % 7);
140 memset (&zone_private_key, (id % 241), sizeof (zone_private_key));
141 memset (&signature, (id % 243), sizeof (signature));
142 GNUNET_assert (GNUNET_OK == nsp->store_records (nsp->cls,
151 run (void *cls, char *const *args, const char *cfgfile,
152 const struct GNUNET_CONFIGURATION_Handle *cfg)
154 struct GNUNET_NAMESTORE_PluginFunctions *nsp;
157 nsp = load_plugin (cfg);
162 "Failed to initialize namestore. Database likely not setup, skipping test.\n");
173 main (int argc, char *argv[])
176 char *const xargv[] = {
177 "test-plugin-namestore",
182 struct GNUNET_GETOPT_CommandLineOption options[] = {
183 GNUNET_GETOPT_OPTION_END
186 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namestore-sqlite");
187 GNUNET_log_setup ("test-plugin-namestore",
190 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
191 GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_plugin_namestore_%s.conf",
193 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
194 "test-plugin-namestore", "nohelp", options, &run, NULL);
196 FPRINTF (stderr, "Missed some testcases: %d\n", ok);
197 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namestore-sqlite");
201 /* end of test_plugin_namestore.c */