src: for every AGPL3.0 file, add SPDX identifier.
[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,
53                    "libgnunet_plugin_namestore_%s",
54                    plugin_name);
55   GNUNET_break (NULL ==
56                 GNUNET_PLUGIN_unload (libname,
57                                       api));
58   GNUNET_free (libname);
59 }
60
61
62 /**
63  * Load the namestore plugin.
64  *
65  * @param cfg configuration to pass
66  * @return NULL on error
67  */
68 static struct GNUNET_NAMESTORE_PluginFunctions *
69 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
70 {
71   struct GNUNET_NAMESTORE_PluginFunctions *ret;
72   char *libname;
73
74   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
75               _("Loading `%s' namestore plugin\n"),
76               plugin_name);
77   GNUNET_asprintf (&libname,
78                    "libgnunet_plugin_namestore_%s",
79                    plugin_name);
80   if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
81   {
82     FPRINTF (stderr,
83              "Failed to load plugin `%s'!\n",
84              plugin_name);
85     GNUNET_free (libname);
86     return NULL;
87   }
88   GNUNET_free (libname);
89   return ret;
90 }
91
92
93 static void
94 test_record (void *cls,
95              uint64_t seq,
96              const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
97              const char *label,
98              unsigned int rd_count,
99              const struct GNUNET_GNSRECORD_Data *rd)
100 {
101   int *idp = cls;
102   int id = *idp;
103   struct GNUNET_CRYPTO_EcdsaPrivateKey tzone_private_key;
104   char tname[64];
105   unsigned int trd_count = 1 + (id % 1024);
106
107   GNUNET_snprintf (tname,
108                    sizeof (tname),
109                    "a%u",
110                    (unsigned int ) id);
111   for (unsigned int i=0;i<trd_count;i++)
112   {
113     GNUNET_assert (rd[i].data_size == id % 10);
114     GNUNET_assert (0 == memcmp ("Hello World", rd[i].data, id % 10));
115     GNUNET_assert (rd[i].record_type == TEST_RECORD_TYPE);
116     GNUNET_assert (rd[i].flags == 0);
117   }
118   memset (&tzone_private_key,
119           (id % 241),
120           sizeof (tzone_private_key));
121   GNUNET_assert (0 == strcmp (label, tname));
122   GNUNET_assert (0 == memcmp (&tzone_private_key,
123                               private_key,
124                               sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)));
125 }
126
127
128 static void
129 get_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp,
130             int id)
131 {
132   GNUNET_assert (GNUNET_OK ==
133                  nsp->iterate_records (nsp->cls,
134                                        NULL,
135                                        0,
136                                        1,
137                                        &test_record,
138                                        &id));
139 }
140
141
142 static void
143 put_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp,
144             int id)
145 {
146   struct GNUNET_CRYPTO_EcdsaPrivateKey zone_private_key;
147   char label[64];
148   unsigned int rd_count = 1 + (id % 1024);
149   struct GNUNET_GNSRECORD_Data rd[rd_count];
150   struct GNUNET_CRYPTO_EcdsaSignature signature;
151
152   GNUNET_snprintf (label, sizeof (label),
153                    "a%u", (unsigned int ) id);
154   for (unsigned int i=0;i<rd_count;i++)
155   {
156     rd[i].data = "Hello World";
157     rd[i].data_size = id % 10;
158     rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES).abs_value_us;
159     rd[i].record_type = TEST_RECORD_TYPE;
160     rd[i].flags = 0;
161   }
162   memset (&zone_private_key, (id % 241), sizeof (zone_private_key));
163   memset (&signature, (id % 243), sizeof (signature));
164   GNUNET_assert (GNUNET_OK ==
165                  nsp->store_records (nsp->cls,
166                                      &zone_private_key,
167                                      label,
168                                      rd_count,
169                                      rd));
170 }
171
172
173 static void
174 run (void *cls,
175      char *const *args,
176      const char *cfgfile,
177      const struct GNUNET_CONFIGURATION_Handle *cfg)
178 {
179   struct GNUNET_NAMESTORE_PluginFunctions *nsp;
180
181   ok = 0;
182   nsp = load_plugin (cfg);
183   if (NULL == nsp)
184   {
185     FPRINTF (stderr,
186              "%s",
187              "Failed to initialize namestore.  Database likely not setup, skipping test.\n");
188     return;
189   }
190   put_record (nsp, 1);
191   get_record (nsp, 1);
192
193   unload_plugin (nsp);
194 }
195
196
197 int
198 main (int argc,
199       char *argv[])
200 {
201   char cfg_name[128];
202   char *const xargv[] = {
203     "test-plugin-namestore",
204     "-c",
205     cfg_name,
206     NULL
207   };
208   struct GNUNET_GETOPT_CommandLineOption options[] = {
209     GNUNET_GETOPT_OPTION_END
210   };
211
212   GNUNET_log_setup ("test-plugin-namestore",
213                     "WARNING",
214                     NULL);
215   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
216   GNUNET_snprintf (cfg_name,
217                    sizeof (cfg_name),
218                    "test_plugin_namestore_%s.conf",
219                    plugin_name);
220   GNUNET_DISK_purge_cfg_dir (cfg_name,
221                              "GNUNET_TMP");
222   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
223                       xargv,
224                       "test-plugin-namestore",
225                       "nohelp",
226                       options,
227                       &run,
228                       NULL);
229   GNUNET_DISK_purge_cfg_dir (cfg_name,
230                              "GNUNET_TMP");
231   if (ok != 0)
232     FPRINTF (stderr,
233              "Missed some testcases: %d\n",
234              ok);
235   return ok;
236 }
237
238 /* end of test_plugin_namestore.c */