aboutsummaryrefslogtreecommitdiff
path: root/src/service/namestore/test_namestore_api_edit_records.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/namestore/test_namestore_api_edit_records.c')
-rw-r--r--src/service/namestore/test_namestore_api_edit_records.c250
1 files changed, 250 insertions, 0 deletions
diff --git a/src/service/namestore/test_namestore_api_edit_records.c b/src/service/namestore/test_namestore_api_edit_records.c
new file mode 100644
index 000000000..398a2ee8a
--- /dev/null
+++ b/src/service/namestore/test_namestore_api_edit_records.c
@@ -0,0 +1,250 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 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 * @file namestore/test_namestore_api_edit_records.c
22 * @brief testcase for namestore_api.c: Multiple clients work with record set.
23 */
24#include "platform.h"
25#include "gnunet_error_codes.h"
26#include "gnunet_namestore_service.h"
27#include "gnunet_scheduler_lib.h"
28#include "gnunet_testing_lib.h"
29
30#define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
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_NAMESTORE_Handle *nsh2;
42
43static struct GNUNET_SCHEDULER_Task *endbadly_task;
44
45static struct GNUNET_CRYPTO_PrivateKey privkey;
46
47static struct GNUNET_CRYPTO_PublicKey pubkey;
48
49static int res;
50
51static int removed;
52
53static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
54
55static void
56cleanup ()
57{
58 if (NULL != nsh)
59 {
60 GNUNET_NAMESTORE_disconnect (nsh);
61 nsh = NULL;
62 }
63 if (NULL != nsh2)
64 {
65 GNUNET_NAMESTORE_disconnect (nsh2);
66 nsh2 = NULL;
67 }
68 GNUNET_SCHEDULER_shutdown ();
69}
70
71
72/**
73 * Re-establish the connection to the service.
74 *
75 * @param cls handle to use to re-connect.
76 */
77static void
78endbadly (void *cls)
79{
80 if (NULL != nsqe)
81 {
82 GNUNET_NAMESTORE_cancel (nsqe);
83 nsqe = NULL;
84 }
85 cleanup ();
86 res = 1;
87}
88
89
90static void
91end (void *cls)
92{
93 if (endbadly_task != NULL)
94 GNUNET_SCHEDULER_cancel (endbadly_task);
95 cleanup ();
96 res = 0;
97 GNUNET_SCHEDULER_shutdown ();
98}
99
100
101static void
102cancel_done (void *cls, enum GNUNET_ErrorCode ec)
103{
104 GNUNET_assert (GNUNET_EC_NONE == ec);
105 GNUNET_SCHEDULER_add_now (&end, NULL);
106}
107
108
109static void
110begin_cont_b (void *cls,
111 enum GNUNET_ErrorCode ec,
112 unsigned int rd_count,
113 const struct
114 GNUNET_GNSRECORD_Data *rd,
115 const char *editor_hint)
116{
117 char *name = cls;
118
119 GNUNET_assert (GNUNET_EC_NONE == ec);
120 GNUNET_assert (0 != strcmp (editor_hint, "B"));
121 nsqe = GNUNET_NAMESTORE_record_set_edit_cancel (nsh2, &privkey, name, "A",
122 "B", &cancel_done, name);
123}
124
125
126static void
127begin_cont (void *cls,
128 enum GNUNET_ErrorCode ec,
129 unsigned int rd_count,
130 const struct
131 GNUNET_GNSRECORD_Data *rd,
132 const char *editor_hint)
133{
134 char *name = cls;
135
136 GNUNET_assert (GNUNET_EC_NONE == ec);
137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
138 "records: `%u'\n",
139 rd_count);
140 GNUNET_assert (1 == rd_count);
141 nsqe = GNUNET_NAMESTORE_record_set_edit_begin (nsh2, &privkey, name, "B",
142 &begin_cont_b, name);
143 GNUNET_assert (NULL != nsqe);
144}
145
146
147static void
148preload_cont (void *cls,
149 enum GNUNET_ErrorCode ec)
150{
151 char *name = cls;
152
153 GNUNET_assert (NULL != cls);
154 nsqe = NULL;
155 if (GNUNET_EC_NONE != ec)
156 {
157 GNUNET_break (0);
158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
159 "Namestore could not store record: `%s'\n",
160 GNUNET_ErrorCode_get_hint (ec));
161 if (endbadly_task != NULL)
162 GNUNET_SCHEDULER_cancel (endbadly_task);
163 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
164 return;
165 }
166
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "Name store added record for `%s': %s\n",
169 name,
170 (GNUNET_EC_NONE == ec) ? "SUCCESS" : "FAIL");
171 /* We start transaction for A */
172 nsqe = GNUNET_NAMESTORE_record_set_edit_begin (nsh, &privkey, name, "A",
173 &begin_cont, name);
174
175}
176
177
178static void
179run (void *cls,
180 const struct GNUNET_CONFIGURATION_Handle *cfg,
181 struct GNUNET_TESTING_Peer *peer)
182{
183 struct GNUNET_GNSRECORD_Data rd;
184 char *name = "dummy";
185
186 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
187 &endbadly,
188 NULL);
189 nsh = GNUNET_NAMESTORE_connect (cfg);
190 nsh2 = GNUNET_NAMESTORE_connect (cfg);
191 GNUNET_break (NULL != nsh);
192 GNUNET_break (NULL != nsh2);
193
194 privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY);
195 GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key);
196 GNUNET_CRYPTO_key_get_public (&privkey,
197 &pubkey);
198
199 removed = GNUNET_NO;
200
201 rd.expiration_time = GNUNET_TIME_absolute_get ().abs_value_us
202 + GNUNET_TIME_UNIT_DAYS.rel_value_us;
203 rd.record_type = TEST_RECORD_TYPE;
204 rd.data_size = TEST_RECORD_DATALEN;
205 rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
206 rd.flags = 0;
207 memset ((char *) rd.data,
208 'a',
209 TEST_RECORD_DATALEN);
210 nsqe = GNUNET_NAMESTORE_record_set_store (nsh,
211 &privkey,
212 name,
213 1,
214 &rd,
215 &preload_cont,
216 (void *) name);
217 GNUNET_assert (NULL != nsqe);
218 GNUNET_free_nz ((void *) rd.data);
219 GNUNET_assert (NULL != nsqe);
220}
221
222
223#include "test_common.c"
224
225
226int
227main (int argc, char *argv[])
228{
229 char *plugin_name;
230 char *cfg_name;
231
232 SETUP_CFG (plugin_name, cfg_name);
233 res = 1;
234 if (0 !=
235 GNUNET_TESTING_peer_run ("test-namestore-api-remove",
236 cfg_name,
237 &run,
238 NULL))
239 {
240 res = 1;
241 }
242 GNUNET_DISK_purge_cfg_dir (cfg_name,
243 "GNUNET_TEST_HOME");
244 GNUNET_free (plugin_name);
245 GNUNET_free (cfg_name);
246 return res;
247}
248
249
250/* end of test_namestore_api_remove.c */