2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V.
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file namestore/test_namestore_api_store.c
22 * @brief testcase for namestore_api.c: store a record
25 #include "gnunet_namestore_service.h"
26 #include "gnunet_testing_lib.h"
28 #define TEST_RECORD_TYPE 1234
30 #define TEST_RECORD_DATALEN 123
32 #define TEST_NICK "gnunettestnick"
34 #define TEST_RECORD_DATA 'a'
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
38 static struct GNUNET_NAMESTORE_Handle *nsh;
40 static struct GNUNET_SCHEDULER_Task * endbadly_task;
42 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
44 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
48 static struct GNUNET_GNSRECORD_Data rd_orig;
50 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
52 //static const char * name = "dummy.dummy.gnunet";
53 static const char * name = "d";
55 static char *directory;
60 GNUNET_free_non_null ((void *)rd_orig.data);
63 GNUNET_NAMESTORE_disconnect (nsh);
68 GNUNET_free (privkey);
71 GNUNET_SCHEDULER_shutdown ();
76 * Re-establish the connection to the service.
78 * @param cls handle to use to re-connect.
79 * @param tc scheduler context
82 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
86 GNUNET_NAMESTORE_cancel (nsqe);
95 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
101 void lookup_it (void *cls,
102 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
104 unsigned int rd_count,
105 const struct GNUNET_GNSRECORD_Data *rd)
109 int found_record = GNUNET_NO;
110 int found_nick = GNUNET_NO;
112 if (0 != memcmp(privkey, zone, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
115 GNUNET_SCHEDULER_cancel (endbadly_task);
116 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
123 GNUNET_SCHEDULER_cancel (endbadly_task);
124 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
128 if (0 != strcmp (label, name))
131 GNUNET_SCHEDULER_cancel (endbadly_task);
132 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
139 GNUNET_SCHEDULER_cancel (endbadly_task);
140 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
144 for (c = 0; c < rd_count; c++)
146 if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
148 if (rd[c].data_size != strlen(TEST_NICK)+1)
151 GNUNET_SCHEDULER_cancel (endbadly_task);
152 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
155 if (0 != (rd[c].flags & GNUNET_GNSRECORD_RF_PRIVATE))
158 GNUNET_SCHEDULER_cancel (endbadly_task);
159 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
162 if (0 != strcmp(rd[c].data, TEST_NICK))
164 GNUNET_SCHEDULER_cancel (endbadly_task);
165 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
168 found_nick = GNUNET_YES;
172 if (rd[c].record_type != TEST_RECORD_TYPE)
175 GNUNET_SCHEDULER_cancel (endbadly_task);
176 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
179 if (rd[c].data_size != TEST_RECORD_DATALEN)
182 GNUNET_SCHEDULER_cancel (endbadly_task);
183 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
186 if (0 != memcmp (rd[c].data, rd_orig.data, TEST_RECORD_DATALEN))
189 GNUNET_SCHEDULER_cancel (endbadly_task);
190 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
193 if (rd[c].flags != rd->flags)
196 GNUNET_SCHEDULER_cancel (endbadly_task);
197 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
200 found_record = GNUNET_YES;
206 if ((GNUNET_YES == found_nick) && (GNUNET_YES == found_record))
208 GNUNET_SCHEDULER_cancel (endbadly_task);
209 endbadly_task = NULL;
210 GNUNET_SCHEDULER_add_now (&end, NULL );
215 GNUNET_SCHEDULER_cancel (endbadly_task);
216 endbadly_task = NULL;
217 GNUNET_SCHEDULER_add_now (&endbadly, NULL );
223 put_cont (void *cls, int32_t success, const char *emsg)
225 const char *name = cls;
228 GNUNET_assert (NULL != cls);
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230 "Name store added record for `%s': %s\n",
232 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
234 if (GNUNET_OK != success)
236 GNUNET_SCHEDULER_cancel (endbadly_task);
237 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
241 nsqe = GNUNET_NAMESTORE_records_lookup (nsh, privkey, name, lookup_it, NULL);
245 nick_cont (void *cls, int32_t success, const char *emsg)
247 const char *name = cls;
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
253 rd_orig.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
254 rd_orig.record_type = TEST_RECORD_TYPE;
255 rd_orig.data_size = TEST_RECORD_DATALEN;
256 rd_orig.data = GNUNET_malloc (TEST_RECORD_DATALEN);
258 memset ((char *) rd_orig.data, 'a', TEST_RECORD_DATALEN);
260 nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
261 1, &rd_orig, &put_cont, (void *) name);
267 const struct GNUNET_CONFIGURATION_Handle *cfg,
268 struct GNUNET_TESTING_Peer *peer)
273 GNUNET_assert (GNUNET_OK ==
274 GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", "GNUNET_TEST_HOME", &directory));
275 GNUNET_DISK_directory_remove (directory);
277 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
279 GNUNET_asprintf (&hostkey_file,
282 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
284 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
285 GNUNET_free (hostkey_file);
286 GNUNET_assert (privkey != NULL);
287 GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
289 nsh = GNUNET_NAMESTORE_connect (cfg);
290 GNUNET_break (NULL != nsh);
292 nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey, TEST_NICK, &nick_cont, (void *) name);
295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
296 _("Namestore cannot store no block\n"));
302 main (int argc, char *argv[])
306 GNUNET_TESTING_peer_run ("test-namestore-api",
307 "test_namestore_api.conf",
313 if (NULL != directory)
315 GNUNET_DISK_directory_remove (directory);
316 GNUNET_free (directory);
322 /* end of test_namestore_api_store.c */