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