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