diff options
Diffstat (limited to 'src/plugin/namestore/test_namestore_api_zone_to_name.c')
-rw-r--r-- | src/plugin/namestore/test_namestore_api_zone_to_name.c | 266 |
1 files changed, 266 insertions, 0 deletions
diff --git a/src/plugin/namestore/test_namestore_api_zone_to_name.c b/src/plugin/namestore/test_namestore_api_zone_to_name.c new file mode 100644 index 000000000..c70eef53a --- /dev/null +++ b/src/plugin/namestore/test_namestore_api_zone_to_name.c | |||
@@ -0,0 +1,266 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2009 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_zone_to_name.c | ||
22 | * @brief testcase for zone to name translation | ||
23 | */ | ||
24 | #include "platform.h" | ||
25 | #include "gnunet_namestore_service.h" | ||
26 | #include "gnunet_testing_lib.h" | ||
27 | #include "namestore.h" | ||
28 | |||
29 | #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT | ||
30 | |||
31 | #define RECORDS 5 | ||
32 | |||
33 | #define TEST_RECORD_DATALEN 123 | ||
34 | |||
35 | #define TEST_RECORD_DATA 'a' | ||
36 | |||
37 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100) | ||
38 | |||
39 | |||
40 | static struct GNUNET_NAMESTORE_Handle *nsh; | ||
41 | |||
42 | static struct GNUNET_SCHEDULER_Task *endbadly_task; | ||
43 | |||
44 | static struct GNUNET_CRYPTO_PrivateKey privkey; | ||
45 | |||
46 | static struct GNUNET_CRYPTO_PublicKey pubkey; | ||
47 | |||
48 | static struct GNUNET_CRYPTO_PublicKey s_zone_value; | ||
49 | |||
50 | static char *s_name; | ||
51 | |||
52 | static int res; | ||
53 | |||
54 | static struct GNUNET_NAMESTORE_QueueEntry *qe; | ||
55 | |||
56 | |||
57 | /** | ||
58 | * Re-establish the connection to the service. | ||
59 | * | ||
60 | * @param cls handle to use to re-connect. | ||
61 | */ | ||
62 | static void | ||
63 | endbadly (void *cls) | ||
64 | { | ||
65 | (void) cls; | ||
66 | GNUNET_SCHEDULER_shutdown (); | ||
67 | res = 1; | ||
68 | } | ||
69 | |||
70 | |||
71 | static void | ||
72 | end (void *cls) | ||
73 | { | ||
74 | if (NULL != qe) | ||
75 | { | ||
76 | GNUNET_NAMESTORE_cancel (qe); | ||
77 | qe = NULL; | ||
78 | } | ||
79 | if (NULL != endbadly_task) | ||
80 | { | ||
81 | GNUNET_SCHEDULER_cancel (endbadly_task); | ||
82 | endbadly_task = NULL; | ||
83 | } | ||
84 | if (NULL != nsh) | ||
85 | { | ||
86 | GNUNET_NAMESTORE_disconnect (nsh); | ||
87 | nsh = NULL; | ||
88 | } | ||
89 | } | ||
90 | |||
91 | |||
92 | static void | ||
93 | zone_to_name_proc (void *cls, | ||
94 | const struct GNUNET_CRYPTO_PrivateKey *zone_key, | ||
95 | const char *n, | ||
96 | unsigned int rd_count, | ||
97 | const struct GNUNET_GNSRECORD_Data *rd) | ||
98 | { | ||
99 | int fail = GNUNET_NO; | ||
100 | |||
101 | qe = NULL; | ||
102 | if ((NULL == zone_key) && | ||
103 | (NULL == n) && | ||
104 | (0 == rd_count) && | ||
105 | (NULL == rd)) | ||
106 | { | ||
107 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
108 | "No result found\n"); | ||
109 | res = 1; | ||
110 | } | ||
111 | else | ||
112 | { | ||
113 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
114 | "Result found: `%s'\n", | ||
115 | n); | ||
116 | if ((NULL == n) || | ||
117 | (0 != strcmp (n, | ||
118 | s_name))) | ||
119 | { | ||
120 | fail = GNUNET_YES; | ||
121 | GNUNET_break (0); | ||
122 | } | ||
123 | if (1 != rd_count) | ||
124 | { | ||
125 | fail = GNUNET_YES; | ||
126 | GNUNET_break (0); | ||
127 | } | ||
128 | if ((NULL == zone_key) || | ||
129 | (0 != GNUNET_memcmp (zone_key, | ||
130 | &privkey))) | ||
131 | { | ||
132 | fail = GNUNET_YES; | ||
133 | GNUNET_break (0); | ||
134 | } | ||
135 | if (fail == GNUNET_NO) | ||
136 | res = 0; | ||
137 | else | ||
138 | res = 1; | ||
139 | } | ||
140 | GNUNET_SCHEDULER_add_now (&end, | ||
141 | NULL); | ||
142 | } | ||
143 | |||
144 | |||
145 | static void | ||
146 | error_cb (void *cls) | ||
147 | { | ||
148 | (void) cls; | ||
149 | qe = NULL; | ||
150 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
151 | "Not found!\n"); | ||
152 | GNUNET_SCHEDULER_shutdown (); | ||
153 | res = 2; | ||
154 | } | ||
155 | |||
156 | |||
157 | static void | ||
158 | put_cont (void *cls, | ||
159 | enum GNUNET_ErrorCode ec) | ||
160 | { | ||
161 | char *name = cls; | ||
162 | |||
163 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
164 | "Name store added record for `%s': %s\n", | ||
165 | name, | ||
166 | (GNUNET_EC_NONE == ec) ? | ||
167 | "SUCCESS" : GNUNET_ErrorCode_get_hint (ec)); | ||
168 | if (GNUNET_EC_NONE == ec) | ||
169 | { | ||
170 | res = 0; | ||
171 | |||
172 | qe = GNUNET_NAMESTORE_zone_to_name (nsh, | ||
173 | &privkey, | ||
174 | &s_zone_value, | ||
175 | &error_cb, | ||
176 | NULL, | ||
177 | &zone_to_name_proc, | ||
178 | NULL); | ||
179 | } | ||
180 | else | ||
181 | { | ||
182 | res = 1; | ||
183 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
184 | "Failed to put records for name `%s'\n", | ||
185 | name); | ||
186 | GNUNET_SCHEDULER_add_now (&end, | ||
187 | NULL); | ||
188 | } | ||
189 | } | ||
190 | |||
191 | |||
192 | static void | ||
193 | run (void *cls, | ||
194 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
195 | struct GNUNET_TESTING_Peer *peer) | ||
196 | { | ||
197 | (void) cls; | ||
198 | (void) peer; | ||
199 | endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, | ||
200 | &endbadly, | ||
201 | NULL); | ||
202 | GNUNET_SCHEDULER_add_shutdown (&end, | ||
203 | NULL); | ||
204 | GNUNET_asprintf (&s_name, "dummy"); | ||
205 | privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY); | ||
206 | GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key); | ||
207 | /* get public key */ | ||
208 | GNUNET_CRYPTO_key_get_public (&privkey, | ||
209 | &pubkey); | ||
210 | |||
211 | GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, | ||
212 | &s_zone_value, | ||
213 | sizeof(s_zone_value)); | ||
214 | s_zone_value.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY); | ||
215 | { | ||
216 | struct GNUNET_GNSRECORD_Data rd; | ||
217 | |||
218 | rd.expiration_time = GNUNET_TIME_UNIT_HOURS.rel_value_us; | ||
219 | rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY; | ||
220 | rd.data_size = sizeof (s_zone_value.ecdsa_key); | ||
221 | rd.data = &s_zone_value.ecdsa_key; | ||
222 | rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION; | ||
223 | |||
224 | nsh = GNUNET_NAMESTORE_connect (cfg); | ||
225 | GNUNET_break (NULL != nsh); | ||
226 | GNUNET_NAMESTORE_records_store (nsh, | ||
227 | &privkey, | ||
228 | s_name, | ||
229 | 1, | ||
230 | &rd, | ||
231 | &put_cont, | ||
232 | s_name); | ||
233 | } | ||
234 | } | ||
235 | |||
236 | |||
237 | #include "test_common.c" | ||
238 | |||
239 | |||
240 | int | ||
241 | main (int argc, | ||
242 | char *argv[]) | ||
243 | { | ||
244 | const char *plugin_name; | ||
245 | char *cfg_name; | ||
246 | |||
247 | (void) argc; | ||
248 | SETUP_CFG (plugin_name, cfg_name); | ||
249 | res = 1; | ||
250 | if (0 != | ||
251 | GNUNET_TESTING_peer_run ("test-namestore-api-zone-to-name", | ||
252 | cfg_name, | ||
253 | &run, | ||
254 | NULL)) | ||
255 | { | ||
256 | res = 1; | ||
257 | } | ||
258 | GNUNET_DISK_purge_cfg_dir (cfg_name, | ||
259 | "GNUNET_TEST_HOME"); | ||
260 | GNUNET_free (plugin_name); | ||
261 | GNUNET_free (cfg_name); | ||
262 | return res; | ||
263 | } | ||
264 | |||
265 | |||
266 | /* end of test_namestore_api_zone_to_name.c */ | ||