diff options
Diffstat (limited to 'src/namestore/test_namestore_api_store_update.c')
-rw-r--r-- | src/namestore/test_namestore_api_store_update.c | 309 |
1 files changed, 0 insertions, 309 deletions
diff --git a/src/namestore/test_namestore_api_store_update.c b/src/namestore/test_namestore_api_store_update.c deleted file mode 100644 index 5c169734a..000000000 --- a/src/namestore/test_namestore_api_store_update.c +++ /dev/null | |||
@@ -1,309 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012, 2013, 2018 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_namestore_api_store_update.c | ||
22 | * @brief testcase for namestore_api.c: store a record, update it and perform a lookup | ||
23 | * @author Matthias Wachs | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_namecache_service.h" | ||
28 | #include "gnunet_namestore_service.h" | ||
29 | #include "gnunet_testing_lib.h" | ||
30 | #include "gnunet_dnsparser_lib.h" | ||
31 | |||
32 | #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT | ||
33 | |||
34 | #define TEST_RECORD_DATALEN 123 | ||
35 | |||
36 | #define TEST_RECORD_DATA 'a' | ||
37 | |||
38 | #define TEST_RECORD_DATALEN2 234 | ||
39 | |||
40 | #define TEST_RECORD_DATA2 'b' | ||
41 | |||
42 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100) | ||
43 | |||
44 | |||
45 | static struct GNUNET_NAMESTORE_Handle *nsh; | ||
46 | |||
47 | static struct GNUNET_NAMECACHE_Handle *nch; | ||
48 | |||
49 | static struct GNUNET_SCHEDULER_Task *endbadly_task; | ||
50 | |||
51 | static struct GNUNET_IDENTITY_PrivateKey privkey; | ||
52 | |||
53 | static struct GNUNET_IDENTITY_PublicKey pubkey; | ||
54 | |||
55 | static int res; | ||
56 | |||
57 | static int update_performed; | ||
58 | |||
59 | static struct GNUNET_NAMESTORE_QueueEntry *nsqe; | ||
60 | |||
61 | static struct GNUNET_NAMECACHE_QueueEntry *ncqe; | ||
62 | |||
63 | static const char *name = "dummy"; | ||
64 | |||
65 | |||
66 | /** | ||
67 | * Terminate test with error. | ||
68 | * | ||
69 | * @param cls handle to use to re-connect. | ||
70 | */ | ||
71 | static void | ||
72 | endbadly (void *cls) | ||
73 | { | ||
74 | GNUNET_break (0); | ||
75 | endbadly_task = NULL; | ||
76 | GNUNET_SCHEDULER_shutdown (); | ||
77 | res = 1; | ||
78 | } | ||
79 | |||
80 | |||
81 | static void | ||
82 | end (void *cls) | ||
83 | { | ||
84 | if (NULL != endbadly_task) | ||
85 | { | ||
86 | GNUNET_SCHEDULER_cancel (endbadly_task); | ||
87 | endbadly_task = NULL; | ||
88 | } | ||
89 | if (NULL != nsqe) | ||
90 | { | ||
91 | GNUNET_NAMESTORE_cancel (nsqe); | ||
92 | nsqe = NULL; | ||
93 | } | ||
94 | if (NULL != ncqe) | ||
95 | { | ||
96 | GNUNET_NAMECACHE_cancel (ncqe); | ||
97 | ncqe = NULL; | ||
98 | } | ||
99 | if (NULL != nsh) | ||
100 | { | ||
101 | GNUNET_NAMESTORE_disconnect (nsh); | ||
102 | nsh = NULL; | ||
103 | } | ||
104 | if (NULL != nch) | ||
105 | { | ||
106 | GNUNET_NAMECACHE_disconnect (nch); | ||
107 | nch = NULL; | ||
108 | } | ||
109 | } | ||
110 | |||
111 | |||
112 | static void | ||
113 | put_cont (void *cls, | ||
114 | int32_t success, | ||
115 | const char *emsg); | ||
116 | |||
117 | |||
118 | static void | ||
119 | rd_decrypt_cb (void *cls, | ||
120 | unsigned int rd_count, | ||
121 | const struct GNUNET_GNSRECORD_Data *rd) | ||
122 | { | ||
123 | struct GNUNET_GNSRECORD_Data rd_new; | ||
124 | |||
125 | GNUNET_assert (1 == rd_count); | ||
126 | GNUNET_assert (NULL != rd); | ||
127 | |||
128 | if (GNUNET_NO == update_performed) | ||
129 | { | ||
130 | char rd_cmp_data[TEST_RECORD_DATALEN]; | ||
131 | |||
132 | memset (rd_cmp_data, | ||
133 | TEST_RECORD_DATA, | ||
134 | TEST_RECORD_DATALEN); | ||
135 | GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type); | ||
136 | GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size); | ||
137 | GNUNET_assert (0 == memcmp (&rd_cmp_data, | ||
138 | rd[0].data, | ||
139 | TEST_RECORD_DATALEN)); | ||
140 | |||
141 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
142 | "Block was decrypted successfully, updating record \n"); | ||
143 | |||
144 | rd_new.flags = GNUNET_GNSRECORD_RF_NONE; | ||
145 | rd_new.expiration_time = GNUNET_TIME_absolute_get ().abs_value_us | ||
146 | + 1000000000; | ||
147 | rd_new.record_type = TEST_RECORD_TYPE; | ||
148 | rd_new.data_size = TEST_RECORD_DATALEN2; | ||
149 | rd_new.data = GNUNET_malloc (TEST_RECORD_DATALEN2); | ||
150 | memset ((char *) rd_new.data, | ||
151 | TEST_RECORD_DATA2, | ||
152 | TEST_RECORD_DATALEN2); | ||
153 | |||
154 | nsqe = GNUNET_NAMESTORE_records_store (nsh, | ||
155 | &privkey, | ||
156 | name, | ||
157 | 1, | ||
158 | &rd_new, | ||
159 | &put_cont, | ||
160 | (void *) name); | ||
161 | update_performed = GNUNET_YES; | ||
162 | } | ||
163 | else | ||
164 | { | ||
165 | char rd_cmp_data[TEST_RECORD_DATALEN2]; | ||
166 | |||
167 | memset (rd_cmp_data, | ||
168 | TEST_RECORD_DATA2, | ||
169 | TEST_RECORD_DATALEN2); | ||
170 | GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type); | ||
171 | GNUNET_assert (TEST_RECORD_DATALEN2 == rd[0].data_size); | ||
172 | GNUNET_assert (0 == memcmp (&rd_cmp_data, | ||
173 | rd[0].data, | ||
174 | TEST_RECORD_DATALEN2)); | ||
175 | GNUNET_SCHEDULER_shutdown (); | ||
176 | res = 0; | ||
177 | } | ||
178 | } | ||
179 | |||
180 | |||
181 | static void | ||
182 | name_lookup_proc (void *cls, | ||
183 | const struct GNUNET_GNSRECORD_Block *block) | ||
184 | { | ||
185 | const char *name = cls; | ||
186 | |||
187 | ncqe = NULL; | ||
188 | GNUNET_assert (NULL != cls); | ||
189 | if (NULL == block) | ||
190 | { | ||
191 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
192 | _ ("Namecache returned no block for `%s'\n"), | ||
193 | name); | ||
194 | GNUNET_SCHEDULER_shutdown (); | ||
195 | return; | ||
196 | } | ||
197 | |||
198 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
199 | "Namecache returned block, decrypting \n"); | ||
200 | GNUNET_assert (GNUNET_OK == | ||
201 | GNUNET_GNSRECORD_block_decrypt (block, | ||
202 | &pubkey, | ||
203 | name, | ||
204 | &rd_decrypt_cb, | ||
205 | (void *) name)); | ||
206 | } | ||
207 | |||
208 | |||
209 | static void | ||
210 | put_cont (void *cls, | ||
211 | int32_t success, | ||
212 | const char *emsg) | ||
213 | { | ||
214 | const char *name = cls; | ||
215 | struct GNUNET_HashCode derived_hash; | ||
216 | |||
217 | nsqe = NULL; | ||
218 | GNUNET_assert (NULL != cls); | ||
219 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
220 | "Name store added record for `%s': %s\n", | ||
221 | name, | ||
222 | (success == GNUNET_OK) ? "SUCCESS" : "FAIL"); | ||
223 | /* Create derived hash */ | ||
224 | GNUNET_GNSRECORD_query_from_private_key (&privkey, | ||
225 | name, | ||
226 | &derived_hash); | ||
227 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
228 | "Looking in namecache for `%s'\n", | ||
229 | GNUNET_h2s (&derived_hash)); | ||
230 | ncqe = GNUNET_NAMECACHE_lookup_block (nch, | ||
231 | &derived_hash, | ||
232 | &name_lookup_proc, (void *) name); | ||
233 | } | ||
234 | |||
235 | |||
236 | static void | ||
237 | run (void *cls, | ||
238 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
239 | struct GNUNET_TESTING_Peer *peer) | ||
240 | { | ||
241 | struct GNUNET_GNSRECORD_Data rd; | ||
242 | |||
243 | update_performed = GNUNET_NO; | ||
244 | GNUNET_SCHEDULER_add_shutdown (&end, | ||
245 | NULL); | ||
246 | endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, | ||
247 | &endbadly, | ||
248 | NULL); | ||
249 | memset (&privkey, 0, sizeof (privkey)); | ||
250 | privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY); | ||
251 | GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key); | ||
252 | GNUNET_IDENTITY_key_get_public (&privkey, &pubkey); | ||
253 | rd.flags = GNUNET_GNSRECORD_RF_NONE; | ||
254 | rd.expiration_time = GNUNET_TIME_absolute_get ().abs_value_us + 1000000000; | ||
255 | rd.record_type = TEST_RECORD_TYPE; | ||
256 | rd.data_size = TEST_RECORD_DATALEN; | ||
257 | rd.data = GNUNET_malloc (TEST_RECORD_DATALEN); | ||
258 | memset ((char *) rd.data, | ||
259 | TEST_RECORD_DATA, | ||
260 | TEST_RECORD_DATALEN); | ||
261 | |||
262 | nsh = GNUNET_NAMESTORE_connect (cfg); | ||
263 | GNUNET_break (NULL != nsh); | ||
264 | nch = GNUNET_NAMECACHE_connect (cfg); | ||
265 | GNUNET_break (NULL != nch); | ||
266 | nsqe = GNUNET_NAMESTORE_records_store (nsh, | ||
267 | &privkey, | ||
268 | name, | ||
269 | 1, | ||
270 | &rd, | ||
271 | &put_cont, | ||
272 | (void *) name); | ||
273 | if (NULL == nsqe) | ||
274 | { | ||
275 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
276 | _ ("Namestore cannot store no block\n")); | ||
277 | } | ||
278 | GNUNET_free_nz ((void *) rd.data); | ||
279 | } | ||
280 | |||
281 | |||
282 | #include "test_common.c" | ||
283 | |||
284 | |||
285 | int | ||
286 | main (int argc, | ||
287 | char *argv[]) | ||
288 | { | ||
289 | const char *plugin_name; | ||
290 | char *cfg_name; | ||
291 | |||
292 | SETUP_CFG (plugin_name, cfg_name); | ||
293 | res = 1; | ||
294 | if (0 != | ||
295 | GNUNET_TESTING_peer_run ("test--store-update", | ||
296 | cfg_name, | ||
297 | &run, | ||
298 | NULL)) | ||
299 | { | ||
300 | res = 1; | ||
301 | } | ||
302 | GNUNET_DISK_purge_cfg_dir (cfg_name, | ||
303 | "GNUNET_TEST_HOME"); | ||
304 | GNUNET_free (cfg_name); | ||
305 | return res; | ||
306 | } | ||
307 | |||
308 | |||
309 | /* end of test_namestore_api_store_update.c */ | ||