aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/test_namestore_api_lookup_nick.c
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2013-11-13 15:04:21 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2013-11-13 15:04:21 +0000
commit97f8a1ba8d1d43025ae43ab33bca12efbd49f355 (patch)
tree406396c6cd263e9cac02f60a9b80663f66bb374c /src/namestore/test_namestore_api_lookup_nick.c
parentbe479981b97e8eeeb4ac251bb413a08b425a0fdd (diff)
downloadgnunet-97f8a1ba8d1d43025ae43ab33bca12efbd49f355.tar.gz
gnunet-97f8a1ba8d1d43025ae43ab33bca12efbd49f355.zip
adding pseudonym NICK automatically to records sets
Diffstat (limited to 'src/namestore/test_namestore_api_lookup_nick.c')
-rw-r--r--src/namestore/test_namestore_api_lookup_nick.c308
1 files changed, 308 insertions, 0 deletions
diff --git a/src/namestore/test_namestore_api_lookup_nick.c b/src/namestore/test_namestore_api_lookup_nick.c
new file mode 100644
index 000000000..c9a1c5f19
--- /dev/null
+++ b/src/namestore/test_namestore_api_lookup_nick.c
@@ -0,0 +1,308 @@
1/*
2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20/**
21 * @file namestore/test_namestore_api_store.c
22 * @brief testcase for namestore_api.c: store a record
23 */
24#include "platform.h"
25#include "gnunet_namestore_service.h"
26#include "gnunet_testing_lib.h"
27
28#define TEST_RECORD_TYPE 1234
29
30#define TEST_RECORD_DATALEN 123
31
32#define TEST_NICK "gnunettestnick"
33
34#define TEST_RECORD_DATA 'a'
35
36#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
37
38static struct GNUNET_NAMESTORE_Handle *nsh;
39
40static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
41
42static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
43
44static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
45
46static int res;
47
48static struct GNUNET_GNSRECORD_Data rd_orig;
49
50static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
51
52//static const char * name = "dummy.dummy.gnunet";
53static const char * name = "d";
54
55static void
56cleanup ()
57{
58 GNUNET_free_non_null ((void *)rd_orig.data);
59 if (NULL != nsh)
60 {
61 GNUNET_NAMESTORE_disconnect (nsh);
62 nsh = NULL;
63 }
64 if (NULL != privkey)
65 {
66 GNUNET_free (privkey);
67 privkey = NULL;
68 }
69 GNUNET_SCHEDULER_shutdown ();
70}
71
72
73/**
74 * Re-establish the connection to the service.
75 *
76 * @param cls handle to use to re-connect.
77 * @param tc scheduler context
78 */
79static void
80endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
81{
82 if (NULL != nsqe)
83 {
84 GNUNET_NAMESTORE_cancel (nsqe);
85 nsqe = NULL;
86 }
87 cleanup ();
88 res = 1;
89}
90
91
92static void
93end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
94{
95 cleanup ();
96 res = 0;
97}
98
99void lookup_it (void *cls,
100 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
101 const char *label,
102 unsigned int rd_count,
103 const struct GNUNET_GNSRECORD_Data *rd)
104{
105 nsqe = NULL;
106 int c;
107 int found_record = GNUNET_NO;
108 int found_nick = GNUNET_NO;
109
110 if (0 != memcmp(privkey, zone, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
111 {
112 GNUNET_break(0);
113 GNUNET_SCHEDULER_cancel (endbadly_task);
114 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
115 return;
116 }
117
118 if (NULL == label)
119 {
120 GNUNET_break(0);
121 GNUNET_SCHEDULER_cancel (endbadly_task);
122 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
123 return;
124 }
125
126 if (0 != strcmp (label, name))
127 {
128 GNUNET_break(0);
129 GNUNET_SCHEDULER_cancel (endbadly_task);
130 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
131 return;
132 }
133
134 if (2 != rd_count)
135 {
136 GNUNET_break(0);
137 GNUNET_SCHEDULER_cancel (endbadly_task);
138 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
139 return;
140 }
141
142 for (c = 0; c < rd_count; c++)
143 {
144 if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
145 {
146 if (rd[c].data_size != strlen(TEST_NICK)+1)
147 {
148 GNUNET_break(0);
149 GNUNET_SCHEDULER_cancel (endbadly_task);
150 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
151 return;
152 }
153 if (rd[c].flags != GNUNET_GNSRECORD_RF_PRIVATE)
154 {
155 GNUNET_break(0);
156 GNUNET_SCHEDULER_cancel (endbadly_task);
157 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
158 return;
159 }
160 if (0 != strcmp(rd[c].data, TEST_NICK))
161 {
162 GNUNET_SCHEDULER_cancel (endbadly_task);
163 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
164 return;
165 }
166 found_nick = GNUNET_YES;
167 }
168 else
169 {
170 if (rd[c].record_type != TEST_RECORD_TYPE)
171 {
172 GNUNET_break(0);
173 GNUNET_SCHEDULER_cancel (endbadly_task);
174 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
175 return;
176 }
177 if (rd[c].data_size != TEST_RECORD_DATALEN)
178 {
179 GNUNET_break(0);
180 GNUNET_SCHEDULER_cancel (endbadly_task);
181 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
182 return;
183 }
184 if (0 != memcmp (rd[c].data, rd_orig.data, TEST_RECORD_DATALEN))
185 {
186 GNUNET_break(0);
187 GNUNET_SCHEDULER_cancel (endbadly_task);
188 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
189 return;
190 }
191 if (rd[c].flags != rd->flags)
192 {
193 GNUNET_break(0);
194 GNUNET_SCHEDULER_cancel (endbadly_task);
195 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
196 return;
197 }
198 found_record = GNUNET_YES;
199 }
200
201 }
202
203 /* Done */
204 if ((GNUNET_YES == found_nick) && (GNUNET_YES == found_record))
205 {
206 GNUNET_SCHEDULER_cancel (endbadly_task);
207 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
208 GNUNET_SCHEDULER_add_now (&end, NULL );
209 }
210 else
211 {
212 GNUNET_break (0);
213 GNUNET_SCHEDULER_cancel (endbadly_task);
214 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
215 GNUNET_SCHEDULER_add_now (&endbadly, NULL );
216 }
217}
218
219
220static void
221put_cont (void *cls, int32_t success, const char *emsg)
222{
223 const char *name = cls;
224
225 nsqe = NULL;
226 GNUNET_assert (NULL != cls);
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "Name store added record for `%s': %s\n",
229 name,
230 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
231
232 if (GNUNET_OK != success)
233 {
234 GNUNET_SCHEDULER_cancel (endbadly_task);
235 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
236 return;
237 }
238 /* Lookup */
239 nsqe = GNUNET_NAMESTORE_records_lookup (nsh, privkey, name, lookup_it, NULL);
240}
241
242static void
243nick_cont (void *cls, int32_t success, const char *emsg)
244{
245 const char *name = cls;
246
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "Nick added : %s\n",
249 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
250
251 rd_orig.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
252 rd_orig.record_type = TEST_RECORD_TYPE;
253 rd_orig.data_size = TEST_RECORD_DATALEN;
254 rd_orig.data = GNUNET_malloc (TEST_RECORD_DATALEN);
255 rd_orig.flags = 0;
256 memset ((char *) rd_orig.data, 'a', TEST_RECORD_DATALEN);
257
258 nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
259 1, &rd_orig, &put_cont, (void *) name);
260}
261
262
263static void
264run (void *cls,
265 const struct GNUNET_CONFIGURATION_Handle *cfg,
266 struct GNUNET_TESTING_Peer *peer)
267{
268 char *hostkey_file;
269
270 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
271 &endbadly, NULL);
272 GNUNET_asprintf (&hostkey_file,
273 "zonefiles%s%s",
274 DIR_SEPARATOR_STR,
275 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
277 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
278 GNUNET_free (hostkey_file);
279 GNUNET_assert (privkey != NULL);
280 GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
281
282 nsh = GNUNET_NAMESTORE_connect (cfg);
283 GNUNET_break (NULL != nsh);
284
285 nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey, TEST_NICK, &nick_cont, (void *) name);
286 if (NULL == nsqe)
287 {
288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
289 _("Namestore cannot store no block\n"));
290 }
291}
292
293
294int
295main (int argc, char *argv[])
296{
297 res = 1;
298 if (0 !=
299 GNUNET_TESTING_peer_run ("test-namestore-api",
300 "test_namestore_api.conf",
301 &run,
302 NULL))
303 return 1;
304 return res;
305}
306
307
308/* end of test_namestore_api_store.c */