tighten formatting rules
[oweals/gnunet.git] / src / namestore / test_plugin_namestore.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /*
21  * @file namestore/test_plugin_namestore.c
22  * @brief Test for the namestore plugins
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_namestore_plugin.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_dnsparser_lib.h"
30
31 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
32
33 static int ok;
34
35 /**
36  * Name of plugin under test.
37  */
38 static const char *plugin_name;
39
40
41 /**
42  * Function called when the service shuts down.  Unloads our namestore
43  * plugin.
44  *
45  * @param api api to unload
46  */
47 static void
48 unload_plugin (struct GNUNET_NAMESTORE_PluginFunctions *api)
49 {
50   char *libname;
51
52   GNUNET_asprintf (&libname, "libgnunet_plugin_namestore_%s", plugin_name);
53   GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
54   GNUNET_free (libname);
55 }
56
57
58 /**
59  * Load the namestore plugin.
60  *
61  * @param cfg configuration to pass
62  * @return NULL on error
63  */
64 static struct GNUNET_NAMESTORE_PluginFunctions *
65 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
66 {
67   struct GNUNET_NAMESTORE_PluginFunctions *ret;
68   char *libname;
69
70   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
71               _ ("Loading `%s' namestore plugin\n"),
72               plugin_name);
73   GNUNET_asprintf (&libname, "libgnunet_plugin_namestore_%s", plugin_name);
74   if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void *) cfg)))
75   {
76     fprintf (stderr, "Failed to load plugin `%s'!\n", plugin_name);
77     GNUNET_free (libname);
78     return NULL;
79   }
80   GNUNET_free (libname);
81   return ret;
82 }
83
84
85 static void
86 test_record (void *cls,
87              uint64_t seq,
88              const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
89              const char *label,
90              unsigned int rd_count,
91              const struct GNUNET_GNSRECORD_Data *rd)
92 {
93   int *idp = cls;
94   int id = *idp;
95   struct GNUNET_CRYPTO_EcdsaPrivateKey tzone_private_key;
96   char tname[64];
97   unsigned int trd_count = 1 + (id % 1024);
98
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++)
102   {
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);
107   }
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));
111 }
112
113
114 static void
115 get_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
116 {
117   GNUNET_assert (
118     GNUNET_OK ==
119     nsp->iterate_records (nsp->cls, NULL, 0, 1, &test_record, &id));
120 }
121
122
123 static void
124 put_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
125 {
126   struct GNUNET_CRYPTO_EcdsaPrivateKey zone_private_key;
127   char label[64];
128   unsigned int rd_count = 1 + (id % 1024);
129   struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL (rd_count)];
130   struct GNUNET_CRYPTO_EcdsaSignature signature;
131
132   GNUNET_snprintf (label, sizeof(label), "a%u", (unsigned int) id);
133   for (unsigned int i = 0; i < rd_count; i++)
134   {
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;
140     rd[i].flags = 0;
141   }
142   memset (&zone_private_key, (id % 241), sizeof(zone_private_key));
143   memset (&signature, (id % 243), sizeof(signature));
144   GNUNET_assert (
145     GNUNET_OK ==
146     nsp->store_records (nsp->cls, &zone_private_key, label, rd_count, rd));
147 }
148
149
150 static void
151 run (void *cls,
152      char *const *args,
153      const char *cfgfile,
154      const struct GNUNET_CONFIGURATION_Handle *cfg)
155 {
156   struct GNUNET_NAMESTORE_PluginFunctions *nsp;
157
158   ok = 0;
159   nsp = load_plugin (cfg);
160   if (NULL == nsp)
161   {
162     fprintf (
163       stderr,
164       "%s",
165       "Failed to initialize namestore.  Database likely not setup, skipping test.\n");
166     return;
167   }
168   put_record (nsp, 1);
169   get_record (nsp, 1);
170 #ifndef DARWIN // #5582
171   unload_plugin (nsp);
172 #endif
173 }
174
175
176 int
177 main (int argc, char *argv[])
178 {
179   char cfg_name[PATH_MAX];
180   char *const xargv[] = { "test-plugin-namestore", "-c", cfg_name, NULL };
181   struct GNUNET_GETOPT_CommandLineOption options[] =
182   { GNUNET_GETOPT_OPTION_END };
183
184   GNUNET_log_setup ("test-plugin-namestore", "WARNING", NULL);
185   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
186   GNUNET_snprintf (cfg_name,
187                    sizeof(cfg_name),
188                    "test_plugin_namestore_%s.conf",
189                    plugin_name);
190   GNUNET_DISK_purge_cfg_dir (cfg_name, "GNUNET_TMP");
191   GNUNET_PROGRAM_run ((sizeof(xargv) / sizeof(char *)) - 1,
192                       xargv,
193                       "test-plugin-namestore",
194                       "nohelp",
195                       options,
196                       &run,
197                       NULL);
198   GNUNET_DISK_purge_cfg_dir (cfg_name, "GNUNET_TMP");
199   if (ok != 0)
200     fprintf (stderr, "Missed some testcases: %d\n", ok);
201   return ok;
202 }
203
204
205 /* end of test_plugin_namestore.c */