Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / peerstore / test_plugin_peerstore.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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_peerstore_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 static struct GNUNET_PEERSTORE_PluginFunctions *psp;
40
41 static struct GNUNET_PeerIdentity p1;
42
43
44 /**
45  * Function called when the service shuts down.  Unloads our namestore
46  * plugin.
47  *
48  * @param api api to unload
49  */
50 static void
51 unload_plugin (struct GNUNET_PEERSTORE_PluginFunctions *api)
52 {
53   char *libname;
54
55   GNUNET_asprintf (&libname,
56                    "libgnunet_plugin_peer_%s",
57                    plugin_name);
58   GNUNET_break (NULL ==
59                 GNUNET_PLUGIN_unload (libname,
60                                       api));
61   GNUNET_free (libname);
62 }
63
64
65 /**
66  * Load the namestore plugin.
67  *
68  * @param cfg configuration to pass
69  * @return NULL on error
70  */
71 static struct GNUNET_PEERSTORE_PluginFunctions *
72 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
73 {
74   struct GNUNET_PEERSTORE_PluginFunctions *ret;
75   char *libname;
76
77   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
78               _("Loading `%s' peer plugin\n"),
79               plugin_name);
80   GNUNET_asprintf (&libname,
81                    "libgnunet_plugin_peerstore_%s",
82                    plugin_name);
83   if (NULL == (ret = GNUNET_PLUGIN_load (libname,
84                                          (void*) cfg)))
85   {
86     FPRINTF (stderr,
87              "Failed to load plugin `%s'!\n",
88              plugin_name);
89     GNUNET_free (libname);
90     return NULL;
91   }
92   GNUNET_free (libname);
93   return ret;
94 }
95
96
97 static void
98 test_record (void *cls,
99              const struct GNUNET_PEERSTORE_Record *record,
100              const char *error)
101 {
102   const struct GNUNET_PeerIdentity *id = cls;
103   const char* testval = "test_val";
104
105   if (NULL == record)
106   {
107     unload_plugin (psp);
108     return;
109   }
110   GNUNET_assert (0 == memcmp (&record->peer,
111                               id,
112                               sizeof (struct GNUNET_PeerIdentity)));
113   GNUNET_assert (0 == strcmp ("subsys",
114                               record->sub_system));
115   GNUNET_assert (0 == strcmp ("key",
116                               record->key));
117   GNUNET_assert (0 == memcmp (testval,
118                               record->value,
119                               strlen (testval)));
120   ok = 0;
121 }
122
123
124 static void
125 get_record (struct GNUNET_PEERSTORE_PluginFunctions *psp,
126             const struct GNUNET_PeerIdentity *identity)
127 {
128   GNUNET_assert (GNUNET_OK ==
129                  psp->iterate_records (psp->cls,
130                                        "subsys",
131                                        identity,
132                                        "key",
133                                        &test_record,
134                                        (void*)identity));
135 }
136
137
138 static void
139 store_cont (void *cls,
140             int status)
141 {
142   GNUNET_assert (GNUNET_OK == status);
143   get_record (psp,
144               &p1);
145 }
146
147
148 static void
149 put_record (struct GNUNET_PEERSTORE_PluginFunctions *psp,
150             const struct GNUNET_PeerIdentity *identity)
151 {
152   GNUNET_assert (GNUNET_OK ==
153                  psp->store_record (psp->cls,
154                                     "subsys",
155                                     identity,
156                                     "key",
157                                     "test_value",
158                                     strlen ("test_value"),
159                                     GNUNET_TIME_absolute_get (),
160                                     GNUNET_PEERSTORE_STOREOPTION_REPLACE,
161                                     &store_cont,
162                                     NULL));
163 }
164
165
166 static void
167 run (void *cls,
168      char *const *args,
169      const char *cfgfile,
170      const struct GNUNET_CONFIGURATION_Handle *cfg)
171 {
172
173   ok = 1;
174   psp = load_plugin (cfg);
175   if (NULL == psp)
176   {
177     FPRINTF (stderr,
178              "%s",
179              "Failed to initialize peerstore.  Database likely not setup, skipping test.\n");
180     return;
181   }
182   memset (&p1, 1, sizeof (p1));
183   put_record (psp,
184               &p1);
185 }
186
187
188 int
189 main (int argc, char *argv[])
190 {
191   char cfg_name[128];
192   char *const xargv[] = {
193     "test-plugin-peerstore",
194     "-c",
195     cfg_name,
196     NULL
197   };
198   struct GNUNET_GETOPT_CommandLineOption options[] = {
199     GNUNET_GETOPT_OPTION_END
200   };
201
202   GNUNET_log_setup ("test-plugin-peerstore",
203                     "WARNING",
204                     NULL);
205   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
206   GNUNET_snprintf (cfg_name,
207                    sizeof (cfg_name),
208                    "test_plugin_peerstore_%s.conf",
209                    plugin_name);
210   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
211                       xargv,
212                       "test-plugin-peerstore",
213                       "nohelp",
214                       options,
215                       &run,
216                       NULL);
217   if (ok != 0)
218     FPRINTF (stderr,
219              "Missed some testcases: %d\n",
220              ok);
221   return ok;
222 }
223
224 /* end of test_plugin_peerstore.c */