diff options
Diffstat (limited to 'src/plugin/block/plugin_block_test.c')
-rw-r--r-- | src/plugin/block/plugin_block_test.c | 270 |
1 files changed, 270 insertions, 0 deletions
diff --git a/src/plugin/block/plugin_block_test.c b/src/plugin/block/plugin_block_test.c new file mode 100644 index 000000000..5afac9205 --- /dev/null +++ b/src/plugin/block/plugin_block_test.c | |||
@@ -0,0 +1,270 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2010, 2021, 2022 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 | /** | ||
22 | * @file block/plugin_block_test.c | ||
23 | * @brief block plugin to test the DHT as a simple key-value store; | ||
24 | * this plugin simply accepts any (new) response for any key | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | |||
28 | #include "platform.h" | ||
29 | #include "gnunet_block_plugin.h" | ||
30 | #include "gnunet_block_group_lib.h" | ||
31 | |||
32 | /** | ||
33 | * Number of bits we set per entry in the bloomfilter. | ||
34 | * Do not change! | ||
35 | */ | ||
36 | #define BLOOMFILTER_K 16 | ||
37 | |||
38 | /** | ||
39 | * How big is the BF we use for DHT blocks? | ||
40 | */ | ||
41 | #define TEST_BF_SIZE 8 | ||
42 | |||
43 | |||
44 | /** | ||
45 | * Create a new block group. | ||
46 | * | ||
47 | * @param ctx block context in which the block group is created | ||
48 | * @param type type of the block for which we are creating the group | ||
49 | * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh | ||
50 | * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh | ||
51 | * @param va variable arguments specific to @a type | ||
52 | * @return block group handle, NULL if block groups are not supported | ||
53 | * by this @a type of block (this is not an error) | ||
54 | */ | ||
55 | static struct GNUNET_BLOCK_Group * | ||
56 | block_plugin_test_create_group (void *ctx, | ||
57 | enum GNUNET_BLOCK_Type type, | ||
58 | const void *raw_data, | ||
59 | size_t raw_data_size, | ||
60 | va_list va) | ||
61 | { | ||
62 | unsigned int bf_size; | ||
63 | const char *guard; | ||
64 | |||
65 | guard = va_arg (va, const char *); | ||
66 | if (0 == strcmp (guard, | ||
67 | "seen-set-size")) | ||
68 | bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned | ||
69 | int), | ||
70 | BLOOMFILTER_K); | ||
71 | else if (0 == strcmp (guard, | ||
72 | "filter-size")) | ||
73 | bf_size = va_arg (va, unsigned int); | ||
74 | else | ||
75 | { | ||
76 | GNUNET_break (0); | ||
77 | bf_size = TEST_BF_SIZE; | ||
78 | } | ||
79 | GNUNET_break (NULL == va_arg (va, const char *)); | ||
80 | return GNUNET_BLOCK_GROUP_bf_create (ctx, | ||
81 | bf_size, | ||
82 | BLOOMFILTER_K, | ||
83 | type, | ||
84 | raw_data, | ||
85 | raw_data_size); | ||
86 | } | ||
87 | |||
88 | |||
89 | /** | ||
90 | * Function called to validate a query. | ||
91 | * | ||
92 | * @param cls closure | ||
93 | * @param type block type | ||
94 | * @param query original query (hash) | ||
95 | * @param xquery extended query data (can be NULL, depending on type) | ||
96 | * @param xquery_size number of bytes in @a xquery | ||
97 | * @return #GNUNET_OK if the query is fine, #GNUNET_NO if not, #GNUNET_SYSERR if @a type is not supported | ||
98 | */ | ||
99 | static enum GNUNET_GenericReturnValue | ||
100 | block_plugin_test_check_query (void *cls, | ||
101 | enum GNUNET_BLOCK_Type type, | ||
102 | const struct GNUNET_HashCode *query, | ||
103 | const void *xquery, | ||
104 | size_t xquery_size) | ||
105 | { | ||
106 | (void) cls; | ||
107 | (void) query; | ||
108 | (void) xquery; | ||
109 | if (GNUNET_BLOCK_TYPE_TEST != type) | ||
110 | { | ||
111 | GNUNET_break (0); | ||
112 | return GNUNET_SYSERR; | ||
113 | } | ||
114 | if (0 != xquery_size) | ||
115 | { | ||
116 | GNUNET_break_op (0); | ||
117 | return GNUNET_NO; | ||
118 | } | ||
119 | return GNUNET_OK; | ||
120 | } | ||
121 | |||
122 | |||
123 | /** | ||
124 | * Function called to validate a block for storage. | ||
125 | * | ||
126 | * @param cls closure | ||
127 | * @param type block type | ||
128 | * @param block block data to validate | ||
129 | * @param block_size number of bytes in @a block | ||
130 | * @return #GNUNET_OK if the block is fine, #GNUNET_NO if not, #GNUNET_SYSERR if @a type is not supported | ||
131 | */ | ||
132 | static enum GNUNET_GenericReturnValue | ||
133 | block_plugin_test_check_block (void *cls, | ||
134 | enum GNUNET_BLOCK_Type type, | ||
135 | const void *block, | ||
136 | size_t block_size) | ||
137 | { | ||
138 | (void) cls; | ||
139 | (void) block; | ||
140 | (void) block_size; | ||
141 | if (GNUNET_BLOCK_TYPE_TEST != type) | ||
142 | { | ||
143 | GNUNET_break (0); | ||
144 | return GNUNET_SYSERR; | ||
145 | } | ||
146 | return GNUNET_OK; | ||
147 | } | ||
148 | |||
149 | |||
150 | /** | ||
151 | * Function called to validate a reply to a request. Note that it is assumed | ||
152 | * that the reply has already been matched to the key (and signatures checked) | ||
153 | * as it would be done with the GetKeyFunction and the | ||
154 | * BlockEvaluationFunction. | ||
155 | * | ||
156 | * @param cls closure | ||
157 | * @param type block type | ||
158 | * @param group which block group to use for evaluation | ||
159 | * @param query original query (hash) | ||
160 | * @param xquery extrended query data (can be NULL, depending on type) | ||
161 | * @param xquery_size number of bytes in @a xquery | ||
162 | * @param reply_block response to validate | ||
163 | * @param reply_block_size number of bytes in @a reply_block | ||
164 | * @return characterization of result | ||
165 | */ | ||
166 | static enum GNUNET_BLOCK_ReplyEvaluationResult | ||
167 | block_plugin_test_check_reply (void *cls, | ||
168 | enum GNUNET_BLOCK_Type type, | ||
169 | struct GNUNET_BLOCK_Group *group, | ||
170 | const struct GNUNET_HashCode *query, | ||
171 | const void *xquery, | ||
172 | size_t xquery_size, | ||
173 | const void *reply_block, | ||
174 | size_t reply_block_size) | ||
175 | { | ||
176 | struct GNUNET_HashCode chash; | ||
177 | |||
178 | (void) cls; | ||
179 | (void) query; | ||
180 | (void) xquery; | ||
181 | (void) xquery_size; | ||
182 | if (GNUNET_BLOCK_TYPE_TEST != type) | ||
183 | { | ||
184 | GNUNET_break (0); | ||
185 | return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED; | ||
186 | } | ||
187 | GNUNET_CRYPTO_hash (reply_block, | ||
188 | reply_block_size, | ||
189 | &chash); | ||
190 | if (GNUNET_YES == | ||
191 | GNUNET_BLOCK_GROUP_bf_test_and_set (group, | ||
192 | &chash)) | ||
193 | return GNUNET_BLOCK_REPLY_OK_DUPLICATE; | ||
194 | return GNUNET_BLOCK_REPLY_OK_MORE; | ||
195 | } | ||
196 | |||
197 | |||
198 | /** | ||
199 | * Function called to obtain the key for a block. | ||
200 | * | ||
201 | * @param cls closure | ||
202 | * @param type block type | ||
203 | * @param block block to get the key for | ||
204 | * @param block_size number of bytes in @a block | ||
205 | * @param key set to the key (query) for the given block | ||
206 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported, #GNUNET_NO if extracting a key from a block of this type does not work | ||
207 | */ | ||
208 | static enum GNUNET_GenericReturnValue | ||
209 | block_plugin_test_get_key (void *cls, | ||
210 | enum GNUNET_BLOCK_Type type, | ||
211 | const void *block, | ||
212 | size_t block_size, | ||
213 | struct GNUNET_HashCode *key) | ||
214 | { | ||
215 | (void) cls; | ||
216 | (void) block; | ||
217 | (void) block_size; | ||
218 | (void) key; | ||
219 | if (GNUNET_BLOCK_TYPE_TEST != type) | ||
220 | { | ||
221 | GNUNET_break (0); | ||
222 | return GNUNET_SYSERR; | ||
223 | } | ||
224 | return GNUNET_NO; | ||
225 | } | ||
226 | |||
227 | |||
228 | /** | ||
229 | * Entry point for the plugin. | ||
230 | * | ||
231 | * @param cls NULL | ||
232 | * @return the exported block API | ||
233 | */ | ||
234 | void * | ||
235 | libgnunet_plugin_block_test_init (void *cls) | ||
236 | { | ||
237 | static const enum GNUNET_BLOCK_Type types[] = { | ||
238 | GNUNET_BLOCK_TYPE_TEST, | ||
239 | GNUNET_BLOCK_TYPE_ANY /* end of list */ | ||
240 | }; | ||
241 | struct GNUNET_BLOCK_PluginFunctions *api; | ||
242 | |||
243 | api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); | ||
244 | api->get_key = &block_plugin_test_get_key; | ||
245 | api->check_query = &block_plugin_test_check_query; | ||
246 | api->check_block = &block_plugin_test_check_block; | ||
247 | api->check_reply = &block_plugin_test_check_reply; | ||
248 | api->create_group = &block_plugin_test_create_group; | ||
249 | api->types = types; | ||
250 | return api; | ||
251 | } | ||
252 | |||
253 | |||
254 | /** | ||
255 | * Exit point from the plugin. | ||
256 | * | ||
257 | * @param cls the return value from #libgnunet_plugin_block_test_init | ||
258 | * @return NULL | ||
259 | */ | ||
260 | void * | ||
261 | libgnunet_plugin_block_test_done (void *cls) | ||
262 | { | ||
263 | struct GNUNET_BLOCK_PluginFunctions *api = cls; | ||
264 | |||
265 | GNUNET_free (api); | ||
266 | return NULL; | ||
267 | } | ||
268 | |||
269 | |||
270 | /* end of plugin_block_test.c */ | ||