-more rename issues
[oweals/gnunet.git] / src / namestore / test_plugin_namestore.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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 #define ASSERT(x) do { if (! (x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE;} } while (0)
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, _("Loading `%s' namestore plugin\n"),
71               plugin_name);
72   GNUNET_asprintf (&libname, "libgnunet_plugin_namestore_%s", plugin_name);
73   if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
74   {
75     FPRINTF (stderr, "Failed to load plugin `%s'!\n", plugin_name);
76     GNUNET_free (libname);
77     return NULL;
78   }
79   GNUNET_free (libname);
80   return ret;
81 }
82
83
84 static void 
85 test_record (void *cls,
86                                                  const struct GNUNET_CRYPTO_EccPrivateKey *private_key,
87                                                  const char *label,
88                                                  unsigned int rd_count,
89                                                  const struct GNUNET_NAMESTORE_RecordData *rd)
90 {
91   int *idp = cls;
92   int id = *idp;
93   struct GNUNET_CRYPTO_EccPrivateKey tzone_private_key;
94   char tname[64];
95   unsigned int trd_count = 1 + (id % 1024);
96   unsigned int i;
97
98   GNUNET_snprintf (tname, sizeof (tname),
99                    "a%u", (unsigned int ) id);
100   for (i=0;i<trd_count;i++)
101   {
102     GNUNET_assert (rd[i].data_size == id % 10);
103     GNUNET_assert (0 == memcmp ("Hello World", rd[i].data, id % 10));
104     GNUNET_assert (rd[i].record_type == 1 + (id % 13));
105     GNUNET_assert (rd[i].flags == (id  % 7));
106   }
107   memset (&tzone_private_key, (id % 241), sizeof (tzone_private_key));
108   GNUNET_assert (0 == strcmp (label, tname));
109   GNUNET_assert (0 == memcmp (&tzone_private_key, private_key, sizeof (struct GNUNET_CRYPTO_EccPrivateKey)));
110 }
111
112
113 static void
114 get_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
115 {
116   GNUNET_assert (GNUNET_OK == nsp->iterate_records (nsp->cls,
117                                             NULL, 0, &test_record, &id));
118 }
119
120
121 static void
122 put_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
123 {
124   struct GNUNET_CRYPTO_EccPrivateKey zone_private_key;
125   char label[64];
126   unsigned int rd_count = 1 + (id % 1024);
127   struct GNUNET_NAMESTORE_RecordData rd[rd_count];
128   struct GNUNET_CRYPTO_EccSignature signature;
129   unsigned int i;
130
131   GNUNET_snprintf (label, sizeof (label),
132                    "a%u", (unsigned int ) id);
133   for (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 = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES).abs_value_us;
138     rd[i].record_type = 1 + (id % 13);
139     rd[i].flags = (id  % 7);    
140   }
141   memset (&zone_private_key, (id % 241), sizeof (zone_private_key));
142   memset (&signature, (id % 243), sizeof (signature));
143   GNUNET_assert (GNUNET_OK == nsp->store_records (nsp->cls,
144                                                 &zone_private_key,
145                                                 label,
146                                                 rd_count,
147                                                 rd));
148 }
149
150
151 static void
152 run (void *cls, char *const *args, const char *cfgfile,
153      const struct GNUNET_CONFIGURATION_Handle *cfg)
154 {
155   struct GNUNET_NAMESTORE_PluginFunctions *nsp;  
156   
157   ok = 0;
158   nsp = load_plugin (cfg);
159   if (NULL == nsp)
160   {
161     FPRINTF (stderr,
162              "%s", 
163              "Failed to initialize namestore.  Database likely not setup, skipping test.\n");
164     return;
165   }
166   put_record (nsp, 1);
167   get_record (nsp, 1);
168
169   unload_plugin (nsp);
170 }
171
172
173 int
174 main (int argc, char *argv[])
175 {
176   char cfg_name[128];
177   char *const xargv[] = {
178     "test-plugin-namestore",
179     "-c",
180     cfg_name,
181     NULL
182   };
183   struct GNUNET_GETOPT_CommandLineOption options[] = {
184     GNUNET_GETOPT_OPTION_END
185   };
186
187   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namestore-sqlite");
188   GNUNET_log_setup ("test-plugin-namestore",
189                     "WARNING",
190                     NULL);
191   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
192   GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_plugin_namestore_%s.conf",
193                    plugin_name);
194   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
195                       "test-plugin-namestore", "nohelp", options, &run, NULL);
196   if (ok != 0)
197     FPRINTF (stderr, "Missed some testcases: %d\n", ok);
198   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namestore-sqlite");
199   return ok;
200 }
201
202 /* end of test_plugin_namestore.c */