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