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