glitch in the license text detected by hyazinthe, thank you!
[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 /*
16  * @file namestore/test_plugin_namestore.c
17  * @brief Test for the namestore plugins
18  * @author Christian Grothoff
19  */
20 #include "platform.h"
21 #include "gnunet_util_lib.h"
22 #include "gnunet_namestore_plugin.h"
23 #include "gnunet_testing_lib.h"
24 #include "gnunet_dnsparser_lib.h"
25
26 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
27
28 static int ok;
29
30 /**
31  * Name of plugin under test.
32  */
33 static const char *plugin_name;
34
35
36 /**
37  * Function called when the service shuts down.  Unloads our namestore
38  * plugin.
39  *
40  * @param api api to unload
41  */
42 static void
43 unload_plugin (struct GNUNET_NAMESTORE_PluginFunctions *api)
44 {
45   char *libname;
46
47   GNUNET_asprintf (&libname,
48                    "libgnunet_plugin_namestore_%s",
49                    plugin_name);
50   GNUNET_break (NULL ==
51                 GNUNET_PLUGIN_unload (libname,
52                                       api));
53   GNUNET_free (libname);
54 }
55
56
57 /**
58  * Load the namestore plugin.
59  *
60  * @param cfg configuration to pass
61  * @return NULL on error
62  */
63 static struct GNUNET_NAMESTORE_PluginFunctions *
64 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
65 {
66   struct GNUNET_NAMESTORE_PluginFunctions *ret;
67   char *libname;
68
69   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
70               _("Loading `%s' namestore plugin\n"),
71               plugin_name);
72   GNUNET_asprintf (&libname,
73                    "libgnunet_plugin_namestore_%s",
74                    plugin_name);
75   if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
76   {
77     FPRINTF (stderr,
78              "Failed to load plugin `%s'!\n",
79              plugin_name);
80     GNUNET_free (libname);
81     return NULL;
82   }
83   GNUNET_free (libname);
84   return ret;
85 }
86
87
88 static void
89 test_record (void *cls,
90              uint64_t seq,
91              const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
92              const char *label,
93              unsigned int rd_count,
94              const struct GNUNET_GNSRECORD_Data *rd)
95 {
96   int *idp = cls;
97   int id = *idp;
98   struct GNUNET_CRYPTO_EcdsaPrivateKey tzone_private_key;
99   char tname[64];
100   unsigned int trd_count = 1 + (id % 1024);
101
102   GNUNET_snprintf (tname,
103                    sizeof (tname),
104                    "a%u",
105                    (unsigned int ) id);
106   for (unsigned int i=0;i<trd_count;i++)
107   {
108     GNUNET_assert (rd[i].data_size == id % 10);
109     GNUNET_assert (0 == memcmp ("Hello World", rd[i].data, id % 10));
110     GNUNET_assert (rd[i].record_type == TEST_RECORD_TYPE);
111     GNUNET_assert (rd[i].flags == 0);
112   }
113   memset (&tzone_private_key,
114           (id % 241),
115           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 = TEST_RECORD_TYPE;
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 ==
160                  nsp->store_records (nsp->cls,
161                                      &zone_private_key,
162                                      label,
163                                      rd_count,
164                                      rd));
165 }
166
167
168 static void
169 run (void *cls,
170      char *const *args,
171      const char *cfgfile,
172      const struct GNUNET_CONFIGURATION_Handle *cfg)
173 {
174   struct GNUNET_NAMESTORE_PluginFunctions *nsp;
175
176   ok = 0;
177   nsp = load_plugin (cfg);
178   if (NULL == nsp)
179   {
180     FPRINTF (stderr,
181              "%s",
182              "Failed to initialize namestore.  Database likely not setup, skipping test.\n");
183     return;
184   }
185   put_record (nsp, 1);
186   get_record (nsp, 1);
187
188   unload_plugin (nsp);
189 }
190
191
192 int
193 main (int argc,
194       char *argv[])
195 {
196   char cfg_name[128];
197   char *const xargv[] = {
198     "test-plugin-namestore",
199     "-c",
200     cfg_name,
201     NULL
202   };
203   struct GNUNET_GETOPT_CommandLineOption options[] = {
204     GNUNET_GETOPT_OPTION_END
205   };
206
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_DISK_purge_cfg_dir (cfg_name,
216                              "GNUNET_TMP");
217   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
218                       xargv,
219                       "test-plugin-namestore",
220                       "nohelp",
221                       options,
222                       &run,
223                       NULL);
224   GNUNET_DISK_purge_cfg_dir (cfg_name,
225                              "GNUNET_TMP");
226   if (ok != 0)
227     FPRINTF (stderr,
228              "Missed some testcases: %d\n",
229              ok);
230   return ok;
231 }
232
233 /* end of test_plugin_namestore.c */