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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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"
29 #include "gnunet_dnsparser_lib.h"
31 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
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,
71 _ ("Loading `%s' namestore plugin\n"),
73 GNUNET_asprintf (&libname, "libgnunet_plugin_namestore_%s", plugin_name);
74 if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void *) cfg)))
76 fprintf (stderr, "Failed to load plugin `%s'!\n", plugin_name);
77 GNUNET_free (libname);
80 GNUNET_free (libname);
86 test_record (void *cls,
88 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
90 unsigned int rd_count,
91 const struct GNUNET_GNSRECORD_Data *rd)
95 struct GNUNET_CRYPTO_EcdsaPrivateKey tzone_private_key;
97 unsigned int trd_count = 1 + (id % 1024);
99 GNUNET_snprintf (tname, sizeof (tname), "a%u", (unsigned int) id);
100 GNUNET_assert (trd_count == rd_count);
101 for (unsigned int i = 0; i < trd_count; i++)
103 GNUNET_assert (rd[i].data_size == id % 10);
104 GNUNET_assert (0 == memcmp ("Hello World", rd[i].data, id % 10));
105 GNUNET_assert (rd[i].record_type == TEST_RECORD_TYPE);
106 GNUNET_assert (rd[i].flags == 0);
108 memset (&tzone_private_key, (id % 241), sizeof (tzone_private_key));
109 GNUNET_assert (0 == strcmp (label, tname));
110 GNUNET_assert (0 == GNUNET_memcmp (&tzone_private_key, private_key));
115 get_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
119 nsp->iterate_records (nsp->cls, NULL, 0, 1, &test_record, &id));
124 put_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
126 struct GNUNET_CRYPTO_EcdsaPrivateKey zone_private_key;
128 unsigned int rd_count = 1 + (id % 1024);
129 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL (rd_count)];
130 struct GNUNET_CRYPTO_EcdsaSignature signature;
132 GNUNET_snprintf (label, sizeof (label), "a%u", (unsigned int) id);
133 for (unsigned int i = 0; i < rd_count; i++)
135 rd[i].data = "Hello World";
136 rd[i].data_size = id % 10;
137 rd[i].expiration_time =
138 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES).abs_value_us;
139 rd[i].record_type = TEST_RECORD_TYPE;
142 memset (&zone_private_key, (id % 241), sizeof (zone_private_key));
143 memset (&signature, (id % 243), sizeof (signature));
146 nsp->store_records (nsp->cls, &zone_private_key, label, rd_count, rd));
154 const struct GNUNET_CONFIGURATION_Handle *cfg)
156 struct GNUNET_NAMESTORE_PluginFunctions *nsp;
159 nsp = load_plugin (cfg);
165 "Failed to initialize namestore. Database likely not setup, skipping test.\n");
176 main (int argc, char *argv[])
178 char cfg_name[PATH_MAX];
179 char *const xargv[] = {"test-plugin-namestore", "-c", cfg_name, NULL};
180 struct GNUNET_GETOPT_CommandLineOption options[] = {GNUNET_GETOPT_OPTION_END};
182 GNUNET_log_setup ("test-plugin-namestore", "WARNING", NULL);
183 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
184 GNUNET_snprintf (cfg_name,
186 "test_plugin_namestore_%s.conf",
188 GNUNET_DISK_purge_cfg_dir (cfg_name, "GNUNET_TMP");
189 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
191 "test-plugin-namestore",
196 GNUNET_DISK_purge_cfg_dir (cfg_name, "GNUNET_TMP");
198 fprintf (stderr, "Missed some testcases: %d\n", ok);
202 /* end of test_plugin_namestore.c */