- test for namestore
[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-new.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     return NULL;
77   }
78   GNUNET_free (libname);
79   return ret;
80 }
81
82
83 /**
84  * Function called by for each matching record.
85  *
86  * @param cls closure
87  * @param zone_key public key of the zone
88  * @param expire when does the corresponding block in the DHT expire (until
89  *               when should we never do a DHT lookup for the same name again)?
90  * @param name name that is being mapped (at most 255 characters long)
91  * @param rd_count number of entries in 'rd' array
92  * @param rd array of records with data to store
93  * @param signature signature of the record block, NULL if signature is unavailable (i.e. 
94  *        because the user queried for a particular record type only)
95  */
96 static void 
97 test_record (void *cls,
98              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
99              struct GNUNET_TIME_Absolute expire,
100              const char *name,
101              unsigned int rd_count,
102              const struct GNUNET_NAMESTORE_RecordData *rd,
103              const struct GNUNET_CRYPTO_RsaSignature *signature)
104 {
105   int *idp = cls;
106   int id = *idp;
107   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded tzone_key;
108   char tname[64];
109   unsigned int trd_count = 1 + (id % 1024);
110   struct GNUNET_CRYPTO_RsaSignature tsignature;
111   unsigned int i;
112
113   GNUNET_snprintf (tname, sizeof (tname),
114                    "a%u", (unsigned int ) id);
115   for (i=0;i<trd_count;i++)
116   {
117     GNUNET_assert (rd[i].data_size == id % 10);
118     GNUNET_assert (0 == memcmp ("Hello World", rd[i].data, id % 10));
119     GNUNET_assert (rd[i].record_type == 1 + (id % 13));
120     GNUNET_assert (rd[i].flags == (id  % 7));
121   }
122   memset (&tzone_key, (id % 241), sizeof (tzone_key));
123   memset (&tsignature, (id % 243), sizeof (tsignature));
124   GNUNET_assert (0 == strcmp (name, tname));
125   GNUNET_assert (0 == memcmp (&tzone_key, zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)));
126   GNUNET_assert (0 == memcmp (&tsignature, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature)));
127 }
128
129
130 static void
131 get_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
132 {
133   GNUNET_assert (1 == nsp->iterate_records (nsp->cls,
134                                             NULL, NULL, 0,
135                                             &test_record, &id));
136 }
137
138
139 static void
140 put_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
141 {
142   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
143   struct GNUNET_TIME_Absolute expire;
144   char name[64];
145   unsigned int rd_count = 1 + (id % 1024);
146   struct GNUNET_NAMESTORE_RecordData rd[rd_count];
147   struct GNUNET_CRYPTO_RsaSignature signature;
148   unsigned int i;
149
150   GNUNET_snprintf (name, sizeof (name),
151                    "a%u", (unsigned int ) id);
152   expire = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
153   for (i=0;i<rd_count;i++)
154   {
155     rd[i].data = "Hello World";
156     rd[i].data_size = id % 10;
157     rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES).abs_value;
158     rd[i].record_type = 1 + (id % 13);
159     rd[i].flags = (id  % 7);    
160   }
161   memset (&zone_key, (id % 241), sizeof (zone_key));
162   memset (&signature, (id % 243), sizeof (signature));
163   GNUNET_assert (GNUNET_OK == nsp->put_records (nsp->cls,
164                                                 &zone_key,
165                                                 expire,
166                                                 name,
167                                                 rd_count,
168                                                 rd,
169                                                 &signature));
170 }
171
172
173 static void
174 run (void *cls, char *const *args, const char *cfgfile,
175      const struct GNUNET_CONFIGURATION_Handle *cfg)
176 {
177   struct GNUNET_NAMESTORE_PluginFunctions *nsp;  
178   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
179   struct GNUNET_CRYPTO_ShortHashCode zone;
180   
181   ok = 0;
182   nsp = load_plugin (cfg);
183   if (NULL == nsp)
184   {
185     FPRINTF (stderr,
186              "%s", 
187              "Failed to initialize namestore.  Database likely not setup, skipping test.\n");
188     return;
189   }
190   put_record (nsp, 1);
191   get_record (nsp, 1);
192
193   memset (&zone_key, 1, sizeof (zone_key));
194   GNUNET_CRYPTO_short_hash (&zone_key, sizeof (zone_key), &zone);
195   nsp->delete_zone (nsp->cls, &zone);
196   unload_plugin (nsp);
197 }
198
199
200 int
201 main (int argc, char *argv[])
202 {
203   char cfg_name[128];
204   char *const xargv[] = {
205     "test-plugin-namestore",
206     "-c",
207     cfg_name,
208     NULL
209   };
210   struct GNUNET_GETOPT_CommandLineOption options[] = {
211     GNUNET_GETOPT_OPTION_END
212   };
213
214   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namestore-sqlite");
215   GNUNET_log_setup ("test-plugin-namestore",
216                     "WARNING",
217                     NULL);
218   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
219   GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_plugin_namestore_%s.conf",
220                    plugin_name);
221   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
222                       "test-plugin-namestore", "nohelp", options, &run, NULL);
223   if (ok != 0)
224     FPRINTF (stderr, "Missed some testcases: %d\n", ok);
225   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namestore-sqlite");
226   return ok;
227 }
228
229 /* end of test_plugin_namestore.c */