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