fix
[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 /*
19  * @file namestore/test_plugin_namestore.c
20  * @brief Test for the namestore plugins
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_namestore_plugin.h"
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_dnsparser_lib.h"
28
29 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
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              uint64_t seq,
94              const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
95              const char *label,
96              unsigned int rd_count,
97              const struct GNUNET_GNSRECORD_Data *rd)
98 {
99   int *idp = cls;
100   int id = *idp;
101   struct GNUNET_CRYPTO_EcdsaPrivateKey tzone_private_key;
102   char tname[64];
103   unsigned int trd_count = 1 + (id % 1024);
104
105   GNUNET_snprintf (tname,
106                    sizeof (tname),
107                    "a%u",
108                    (unsigned int ) id);
109   for (unsigned int i=0;i<trd_count;i++)
110   {
111     GNUNET_assert (rd[i].data_size == id % 10);
112     GNUNET_assert (0 == memcmp ("Hello World", rd[i].data, id % 10));
113     GNUNET_assert (rd[i].record_type == TEST_RECORD_TYPE);
114     GNUNET_assert (rd[i].flags == 0);
115   }
116   memset (&tzone_private_key,
117           (id % 241),
118           sizeof (tzone_private_key));
119   GNUNET_assert (0 == strcmp (label, tname));
120   GNUNET_assert (0 == memcmp (&tzone_private_key,
121                               private_key,
122                               sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)));
123 }
124
125
126 static void
127 get_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp,
128             int id)
129 {
130   GNUNET_assert (GNUNET_OK ==
131                  nsp->iterate_records (nsp->cls,
132                                        NULL,
133                                        0,
134                                        1,
135                                        &test_record,
136                                        &id));
137 }
138
139
140 static void
141 put_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp,
142             int id)
143 {
144   struct GNUNET_CRYPTO_EcdsaPrivateKey zone_private_key;
145   char label[64];
146   unsigned int rd_count = 1 + (id % 1024);
147   struct GNUNET_GNSRECORD_Data rd[rd_count];
148   struct GNUNET_CRYPTO_EcdsaSignature signature;
149
150   GNUNET_snprintf (label, sizeof (label),
151                    "a%u", (unsigned int ) id);
152   for (unsigned int i=0;i<rd_count;i++)
153   {
154     rd[i].data = "Hello World";
155     rd[i].data_size = id % 10;
156     rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES).abs_value_us;
157     rd[i].record_type = TEST_RECORD_TYPE;
158     rd[i].flags = 0;
159   }
160   memset (&zone_private_key, (id % 241), sizeof (zone_private_key));
161   memset (&signature, (id % 243), sizeof (signature));
162   GNUNET_assert (GNUNET_OK ==
163                  nsp->store_records (nsp->cls,
164                                      &zone_private_key,
165                                      label,
166                                      rd_count,
167                                      rd));
168 }
169
170
171 static void
172 run (void *cls,
173      char *const *args,
174      const char *cfgfile,
175      const struct GNUNET_CONFIGURATION_Handle *cfg)
176 {
177   struct GNUNET_NAMESTORE_PluginFunctions *nsp;
178
179   ok = 0;
180   nsp = load_plugin (cfg);
181   if (NULL == nsp)
182   {
183     FPRINTF (stderr,
184              "%s",
185              "Failed to initialize namestore.  Database likely not setup, skipping test.\n");
186     return;
187   }
188   put_record (nsp, 1);
189   get_record (nsp, 1);
190
191   unload_plugin (nsp);
192 }
193
194
195 int
196 main (int argc,
197       char *argv[])
198 {
199   char cfg_name[128];
200   char *const xargv[] = {
201     "test-plugin-namestore",
202     "-c",
203     cfg_name,
204     NULL
205   };
206   struct GNUNET_GETOPT_CommandLineOption options[] = {
207     GNUNET_GETOPT_OPTION_END
208   };
209
210   GNUNET_log_setup ("test-plugin-namestore",
211                     "WARNING",
212                     NULL);
213   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
214   GNUNET_snprintf (cfg_name,
215                    sizeof (cfg_name),
216                    "test_plugin_namestore_%s.conf",
217                    plugin_name);
218   GNUNET_DISK_purge_cfg_dir (cfg_name,
219                              "GNUNET_TMP");
220   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
221                       xargv,
222                       "test-plugin-namestore",
223                       "nohelp",
224                       options,
225                       &run,
226                       NULL);
227   GNUNET_DISK_purge_cfg_dir (cfg_name,
228                              "GNUNET_TMP");
229   if (ok != 0)
230     FPRINTF (stderr,
231              "Missed some testcases: %d\n",
232              ok);
233   return ok;
234 }
235
236 /* end of test_plugin_namestore.c */