aboutsummaryrefslogtreecommitdiff
path: root/src/service/namestore/test_namestore_api_remove.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/namestore/test_namestore_api_remove.c')
-rw-r--r--src/service/namestore/test_namestore_api_remove.c219
1 files changed, 219 insertions, 0 deletions
diff --git a/src/service/namestore/test_namestore_api_remove.c b/src/service/namestore/test_namestore_api_remove.c
new file mode 100644
index 000000000..f096c6310
--- /dev/null
+++ b/src/service/namestore/test_namestore_api_remove.c
@@ -0,0 +1,219 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2013 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.c
22 * @brief testcase for namestore_api.c to: remove record
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 TEST_RECORD_DATALEN 123
31
32#define TEST_RECORD_DATA 'a'
33
34#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
35
36
37static struct GNUNET_NAMESTORE_Handle *nsh;
38
39static struct GNUNET_SCHEDULER_Task *endbadly_task;
40
41static struct GNUNET_CRYPTO_PrivateKey privkey;
42
43static struct GNUNET_CRYPTO_PublicKey pubkey;
44
45static int res;
46
47static int removed;
48
49static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
50
51
52static void
53cleanup ()
54{
55 if (NULL != nsh)
56 {
57 GNUNET_NAMESTORE_disconnect (nsh);
58 nsh = NULL;
59 }
60 GNUNET_SCHEDULER_shutdown ();
61}
62
63
64/**
65 * Re-establish the connection to the service.
66 *
67 * @param cls handle to use to re-connect.
68 */
69static void
70endbadly (void *cls)
71{
72 if (NULL != nsqe)
73 {
74 GNUNET_NAMESTORE_cancel (nsqe);
75 nsqe = NULL;
76 }
77 cleanup ();
78 res = 1;
79}
80
81
82static void
83end (void *cls)
84{
85 cleanup ();
86 res = 0;
87}
88
89
90static void
91remove_cont (void *cls,
92 enum GNUNET_ErrorCode ec)
93{
94 nsqe = NULL;
95 if (GNUNET_EC_NONE != ec)
96 {
97 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
98 _ ("Records could not be removed: `%s'\n"),
99 GNUNET_ErrorCode_get_hint (ec));
100 if (NULL != endbadly_task)
101 GNUNET_SCHEDULER_cancel (endbadly_task);
102 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly,
103 NULL);
104 return;
105 }
106 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
107 "Records were removed, perform lookup\n");
108 removed = GNUNET_YES;
109 if (NULL != endbadly_task)
110 GNUNET_SCHEDULER_cancel (endbadly_task);
111 GNUNET_SCHEDULER_add_now (&end, NULL);
112}
113
114
115static void
116put_cont (void *cls,
117 enum GNUNET_ErrorCode ec)
118{
119 const char *name = cls;
120
121 GNUNET_assert (NULL != cls);
122 nsqe = NULL;
123 if (GNUNET_EC_NONE != ec)
124 {
125 GNUNET_break (0);
126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
127 "Namestore could not store record: `%s'\n",
128 GNUNET_ErrorCode_get_hint (ec));
129 if (endbadly_task != NULL)
130 GNUNET_SCHEDULER_cancel (endbadly_task);
131 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
132 return;
133 }
134
135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136 "Name store added record for `%s': %s\n",
137 name,
138 (GNUNET_EC_NONE == ec) ? "SUCCESS" : "FAIL");
139 nsqe = GNUNET_NAMESTORE_record_set_store (nsh,
140 &privkey,
141 name,
142 0, NULL,
143 &remove_cont, (void *) name);
144}
145
146
147static void
148run (void *cls,
149 const struct GNUNET_CONFIGURATION_Handle *cfg,
150 struct GNUNET_TESTING_Peer *peer)
151{
152 struct GNUNET_GNSRECORD_Data rd;
153 const char *name = "dummy";
154
155 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
156 &endbadly,
157 NULL);
158 privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY);
159 GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key);
160 GNUNET_CRYPTO_key_get_public (&privkey,
161 &pubkey);
162
163 removed = GNUNET_NO;
164
165 rd.expiration_time = GNUNET_TIME_UNIT_MINUTES.rel_value_us;
166 rd.record_type = TEST_RECORD_TYPE;
167 rd.data_size = TEST_RECORD_DATALEN;
168 rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
169 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
170 memset ((char *) rd.data,
171 'a',
172 TEST_RECORD_DATALEN);
173
174 nsh = GNUNET_NAMESTORE_connect (cfg);
175 GNUNET_break (NULL != nsh);
176 nsqe = GNUNET_NAMESTORE_record_set_store (nsh,
177 &privkey,
178 name,
179 1,
180 &rd,
181 &put_cont,
182 (void *) name);
183 if (NULL == nsqe)
184 {
185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
186 _ ("Namestore cannot store no block\n"));
187 }
188 GNUNET_free_nz ((void *) rd.data);
189}
190
191
192#include "test_common.c"
193
194
195int
196main (int argc, char *argv[])
197{
198 char *plugin_name;
199 char *cfg_name;
200
201 SETUP_CFG (plugin_name, cfg_name);
202 res = 1;
203 if (0 !=
204 GNUNET_TESTING_peer_run ("test-namestore-api-remove",
205 cfg_name,
206 &run,
207 NULL))
208 {
209 res = 1;
210 }
211 GNUNET_DISK_purge_cfg_dir (cfg_name,
212 "GNUNET_TEST_HOME");
213 GNUNET_free (plugin_name);
214 GNUNET_free (cfg_name);
215 return res;
216}
217
218
219/* end of test_namestore_api_remove.c */