aboutsummaryrefslogtreecommitdiff
path: root/src/namecache/test_namecache_api_cache_block.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/namecache/test_namecache_api_cache_block.c')
-rw-r--r--src/namecache/test_namecache_api_cache_block.c243
1 files changed, 0 insertions, 243 deletions
diff --git a/src/namecache/test_namecache_api_cache_block.c b/src/namecache/test_namecache_api_cache_block.c
deleted file mode 100644
index 6188fb014..000000000
--- a/src/namecache/test_namecache_api_cache_block.c
+++ /dev/null
@@ -1,243 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012 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 namecache/test_namecache_api.c
22 * @brief testcase for namecache_api.c: store a record and perform a lookup
23 */
24#include "platform.h"
25#include "gnunet_namecache_service.h"
26#include "gnunet_testing_lib.h"
27#include "gnunet_dnsparser_lib.h"
28
29#define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
30
31#define TEST_RECORD_DATALEN 123
32
33#define TEST_RECORD_DATA 'a'
34
35#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
36
37
38static struct GNUNET_NAMECACHE_Handle *nsh;
39
40static struct GNUNET_SCHEDULER_Task *endbadly_task;
41
42static struct GNUNET_IDENTITY_PrivateKey privkey;
43
44static struct GNUNET_IDENTITY_PublicKey pubkey;
45
46static int res;
47
48static struct GNUNET_NAMECACHE_QueueEntry *nsqe;
49
50
51static void
52cleanup ()
53{
54 if (NULL != nsh)
55 {
56 GNUNET_NAMECACHE_disconnect (nsh);
57 nsh = NULL;
58 }
59 GNUNET_SCHEDULER_shutdown ();
60}
61
62
63/**
64 * Re-establish the connection to the service.
65 *
66 * @param cls handle to use to re-connect.
67 */
68static void
69endbadly (void *cls)
70{
71 if (NULL != nsqe)
72 {
73 GNUNET_NAMECACHE_cancel (nsqe);
74 nsqe = NULL;
75 }
76 cleanup ();
77 res = 1;
78}
79
80
81static void
82end (void *cls)
83{
84 cleanup ();
85 res = 0;
86}
87
88
89static void
90rd_decrypt_cb (void *cls,
91 unsigned int rd_count,
92 const struct GNUNET_GNSRECORD_Data *rd)
93{
94 char rd_cmp_data[TEST_RECORD_DATALEN];
95
96 GNUNET_assert (1 == rd_count);
97 GNUNET_assert (NULL != rd);
98
99 memset (rd_cmp_data, 'a', TEST_RECORD_DATALEN);
100
101 GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
102 GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
103 GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN));
104
105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
106 "Block was decrypted successfully \n");
107
108 GNUNET_SCHEDULER_add_now (&end, NULL);
109}
110
111
112static void
113name_lookup_proc (void *cls,
114 const struct GNUNET_GNSRECORD_Block *block)
115{
116 const char *name = cls;
117
118 nsqe = NULL;
119
120 GNUNET_assert (NULL != cls);
121
122 if (endbadly_task != NULL)
123 {
124 GNUNET_SCHEDULER_cancel (endbadly_task);
125 endbadly_task = NULL;
126 }
127
128 if (NULL == block)
129 {
130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
131 _ ("Namecache returned no block\n"));
132 if (NULL != endbadly_task)
133 GNUNET_SCHEDULER_cancel (endbadly_task);
134 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
135 return;
136 }
137
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
139 "Namecache returned block, decrypting \n");
140 GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt (block,
141 &pubkey, name,
142 &rd_decrypt_cb,
143 (void *) name));
144}
145
146
147static void
148cache_cont (void *cls, int32_t success, const char *emsg)
149{
150 const char *name = cls;
151 struct GNUNET_HashCode derived_hash;
152
153 GNUNET_assert (NULL != cls);
154
155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
156 "Name store cached record for `%s': %s\n",
157 name,
158 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
159
160 /* Create derived hash */
161 GNUNET_GNSRECORD_query_from_public_key (&pubkey, name, &derived_hash);
162
163 nsqe = GNUNET_NAMECACHE_lookup_block (nsh, &derived_hash,
164 &name_lookup_proc, (void *) name);
165}
166
167
168static void
169run (void *cls,
170 const struct GNUNET_CONFIGURATION_Handle *cfg,
171 struct GNUNET_TESTING_Peer *peer)
172{
173 struct GNUNET_GNSRECORD_Data rd;
174 struct GNUNET_GNSRECORD_Block *block;
175 const char *name = "dummy.dummy.gnunet";
176
177 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
178 &endbadly, NULL);
179 privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY);
180 GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key);
181 GNUNET_IDENTITY_key_get_public (&privkey, &pubkey);
182
183
184 rd.expiration_time = GNUNET_TIME_absolute_get ().abs_value_us + 10000000000;
185 rd.record_type = TEST_RECORD_TYPE;
186 rd.data_size = TEST_RECORD_DATALEN;
187 rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
188 rd.flags = 0;
189 memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
190 GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_create (&privkey,
191 GNUNET_TIME_UNIT_FOREVER_ABS,
192 name, &rd, 1,
193 &block));
194 if (NULL == block)
195 {
196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
197 "Namecache cannot cache no block!\n");
198 GNUNET_SCHEDULER_shutdown ();
199 GNUNET_free (block);
200 return;
201 }
202
203 nsh = GNUNET_NAMECACHE_connect (cfg);
204 if (NULL == nsh)
205 {
206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
207 _ ("Namecache cannot connect to namecache\n"));
208 GNUNET_SCHEDULER_shutdown ();
209 GNUNET_free (block);
210 return;
211 }
212 GNUNET_break (NULL != nsh);
213
214 nsqe = GNUNET_NAMECACHE_block_cache (nsh,
215 block,
216 &cache_cont, (void *) name);
217 if (NULL == nsqe)
218 {
219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
220 _ ("Namecache cannot cache no block\n"));
221 }
222 GNUNET_free (block);
223 GNUNET_free_nz ((void *) rd.data);
224}
225
226
227int
228main (int argc, char *argv[])
229{
230 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-namecache/");
231 res = 1;
232 if (0 !=
233 GNUNET_TESTING_service_run ("test-namecache-api",
234 "namecache",
235 "test_namecache_api.conf",
236 &run,
237 NULL))
238 return 1;
239 return res;
240}
241
242
243/* end of test_namecache_api_cache_block.c */