diff options
Diffstat (limited to 'src/plugin/peerstore/test_plugin_peerstore.c')
-rw-r--r-- | src/plugin/peerstore/test_plugin_peerstore.c | 224 |
1 files changed, 224 insertions, 0 deletions
diff --git a/src/plugin/peerstore/test_plugin_peerstore.c b/src/plugin/peerstore/test_plugin_peerstore.c new file mode 100644 index 000000000..bce62dda9 --- /dev/null +++ b/src/plugin/peerstore/test_plugin_peerstore.c | |||
@@ -0,0 +1,224 @@ | |||
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 | SPDX-License-Identifier: AGPL3.0-or-later | ||
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 | ok = 1; | ||
173 | psp = load_plugin (cfg); | ||
174 | if (NULL == psp) | ||
175 | { | ||
176 | fprintf (stderr, | ||
177 | "%s", | ||
178 | "Failed to initialize peerstore. Database likely not setup, skipping test.\n"); | ||
179 | return; | ||
180 | } | ||
181 | memset (&p1, 1, sizeof(p1)); | ||
182 | put_record (psp, | ||
183 | &p1); | ||
184 | } | ||
185 | |||
186 | |||
187 | int | ||
188 | main (int argc, char *argv[]) | ||
189 | { | ||
190 | char cfg_name[PATH_MAX]; | ||
191 | char *const xargv[] = { | ||
192 | "test-plugin-peerstore", | ||
193 | "-c", | ||
194 | cfg_name, | ||
195 | NULL | ||
196 | }; | ||
197 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
198 | GNUNET_GETOPT_OPTION_END | ||
199 | }; | ||
200 | |||
201 | GNUNET_log_setup ("test-plugin-peerstore", | ||
202 | "WARNING", | ||
203 | NULL); | ||
204 | plugin_name = GNUNET_STRINGS_get_suffix_from_binary_name (argv[0]); | ||
205 | GNUNET_snprintf (cfg_name, | ||
206 | sizeof(cfg_name), | ||
207 | "test_plugin_peerstore_%s.conf", | ||
208 | plugin_name); | ||
209 | GNUNET_PROGRAM_run ((sizeof(xargv) / sizeof(char *)) - 1, | ||
210 | xargv, | ||
211 | "test-plugin-peerstore", | ||
212 | "nohelp", | ||
213 | options, | ||
214 | &run, | ||
215 | NULL); | ||
216 | if (ok != 0) | ||
217 | fprintf (stderr, | ||
218 | "Missed some testcases: %d\n", | ||
219 | ok); | ||
220 | return ok; | ||
221 | } | ||
222 | |||
223 | |||
224 | /* end of test_plugin_peerstore.c */ | ||