8b47ac1dc90b14a190fa88a7d4d70d0aa8402cf2
[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 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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /*
19  * @file namestore/test_plugin_namestore.c
20  * @brief Test for the namestore plugins
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_peerstore_plugin.h"
26 #include "gnunet_testing_lib.h"
27
28
29 static int ok;
30
31 /**
32  * Name of plugin under test.
33  */
34 static const char *plugin_name;
35
36
37 static struct GNUNET_PEERSTORE_PluginFunctions *psp;
38
39 static struct GNUNET_PeerIdentity p1;
40
41
42 /**
43  * Function called when the service shuts down.  Unloads our namestore
44  * plugin.
45  *
46  * @param api api to unload
47  */
48 static void
49 unload_plugin (struct GNUNET_PEERSTORE_PluginFunctions *api)
50 {
51   char *libname;
52
53   GNUNET_asprintf (&libname,
54                    "libgnunet_plugin_peer_%s",
55                    plugin_name);
56   GNUNET_break (NULL ==
57                 GNUNET_PLUGIN_unload (libname,
58                                       api));
59   GNUNET_free (libname);
60 }
61
62
63 /**
64  * Load the namestore plugin.
65  *
66  * @param cfg configuration to pass
67  * @return NULL on error
68  */
69 static struct GNUNET_PEERSTORE_PluginFunctions *
70 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
71 {
72   struct GNUNET_PEERSTORE_PluginFunctions *ret;
73   char *libname;
74
75   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
76               _("Loading `%s' peer plugin\n"),
77               plugin_name);
78   GNUNET_asprintf (&libname,
79                    "libgnunet_plugin_peerstore_%s",
80                    plugin_name);
81   if (NULL == (ret = GNUNET_PLUGIN_load (libname,
82                                          (void*) cfg)))
83   {
84     FPRINTF (stderr,
85              "Failed to load plugin `%s'!\n",
86              plugin_name);
87     GNUNET_free (libname);
88     return NULL;
89   }
90   GNUNET_free (libname);
91   return ret;
92 }
93
94
95 static void
96 test_record (void *cls,
97              const struct GNUNET_PEERSTORE_Record *record,
98              const char *error)
99 {
100   const struct GNUNET_PeerIdentity *id = cls;
101   const char* testval = "test_val";
102
103   if (NULL == record)
104   {
105     unload_plugin (psp);
106     return;
107   }
108   GNUNET_assert (0 == memcmp (&record->peer,
109                               id,
110                               sizeof (struct GNUNET_PeerIdentity)));
111   GNUNET_assert (0 == strcmp ("subsys",
112                               record->sub_system));
113   GNUNET_assert (0 == strcmp ("key",
114                               record->key));
115   GNUNET_assert (0 == memcmp (testval,
116                               record->value,
117                               strlen (testval)));
118   ok = 0;
119 }
120
121
122 static void
123 get_record (struct GNUNET_PEERSTORE_PluginFunctions *psp,
124             const struct GNUNET_PeerIdentity *identity)
125 {
126   GNUNET_assert (GNUNET_OK ==
127                  psp->iterate_records (psp->cls,
128                                        "subsys",
129                                        identity,
130                                        "key",
131                                        &test_record,
132                                        (void*)identity));
133 }
134
135
136 static void
137 store_cont (void *cls,
138             int status)
139 {
140   GNUNET_assert (GNUNET_OK == status);
141   get_record (psp,
142               &p1);
143 }
144
145
146 static void
147 put_record (struct GNUNET_PEERSTORE_PluginFunctions *psp,
148             const struct GNUNET_PeerIdentity *identity)
149 {
150   GNUNET_assert (GNUNET_OK ==
151                  psp->store_record (psp->cls,
152                                     "subsys",
153                                     identity,
154                                     "key",
155                                     "test_value",
156                                     strlen ("test_value"),
157                                     GNUNET_TIME_absolute_get (),
158                                     GNUNET_PEERSTORE_STOREOPTION_REPLACE,
159                                     &store_cont,
160                                     NULL));
161 }
162
163
164 static void
165 run (void *cls,
166      char *const *args,
167      const char *cfgfile,
168      const struct GNUNET_CONFIGURATION_Handle *cfg)
169 {
170
171   ok = 1;
172   psp = load_plugin (cfg);
173   if (NULL == psp)
174   {
175     FPRINTF (stderr,
176              "%s",
177              "Failed to initialize peerstore.  Database likely not setup, skipping test.\n");
178     return;
179   }
180   memset (&p1, 1, sizeof (p1));
181   put_record (psp,
182               &p1);
183 }
184
185
186 int
187 main (int argc, char *argv[])
188 {
189   char cfg_name[128];
190   char *const xargv[] = {
191     "test-plugin-peerstore",
192     "-c",
193     cfg_name,
194     NULL
195   };
196   struct GNUNET_GETOPT_CommandLineOption options[] = {
197     GNUNET_GETOPT_OPTION_END
198   };
199
200   GNUNET_log_setup ("test-plugin-peerstore",
201                     "WARNING",
202                     NULL);
203   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
204   GNUNET_snprintf (cfg_name,
205                    sizeof (cfg_name),
206                    "test_plugin_peerstore_%s.conf",
207                    plugin_name);
208   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
209                       xargv,
210                       "test-plugin-peerstore",
211                       "nohelp",
212                       options,
213                       &run,
214                       NULL);
215   if (ok != 0)
216     FPRINTF (stderr,
217              "Missed some testcases: %d\n",
218              ok);
219   return ok;
220 }
221
222 /* end of test_plugin_peerstore.c */