2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file namestore/test_namestore_api.c
22 * @brief testcase for namestore_api.c for updating an existing record
25 #include "gnunet_common.h"
26 #include "gnunet_namestore_service.h"
27 #include "gnunet_testing_lib-new.h"
28 #include "namestore.h"
29 #include "gnunet_signatures.h"
34 #define TEST_RECORD_TYPE 1234
36 #define TEST_RECORD_DATALEN 123
38 #define TEST_RECORD_DATA 'a'
40 #define TEST_CREATE_RECORD_TYPE 4321
42 #define TEST_CREATE_RECORD_DATALEN 255
44 #define TEST_CREATE_RECORD_DATA 'b'
46 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
49 static struct GNUNET_NAMESTORE_Handle * nsh;
51 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
53 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
55 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
57 static struct GNUNET_CRYPTO_RsaSignature *s_signature;
59 static struct GNUNET_CRYPTO_ShortHashCode s_zone;
61 static struct GNUNET_NAMESTORE_RecordData *s_first_record;
63 static struct GNUNET_NAMESTORE_RecordData *s_second_record;
71 * Re-establish the connection to the service.
73 * @param cls handle to use to re-connect.
74 * @param tc scheduler context
77 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
80 GNUNET_NAMESTORE_disconnect (nsh);
83 GNUNET_CRYPTO_rsa_key_free (privkey);
85 GNUNET_free_non_null (s_name);
91 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
95 GNUNET_SCHEDULER_cancel (endbadly_task);
96 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
98 GNUNET_free ((void *) s_first_record->data);
99 GNUNET_free (s_first_record);
100 GNUNET_free_non_null (s_second_record);
102 GNUNET_CRYPTO_rsa_key_free (privkey);
105 GNUNET_NAMESTORE_disconnect (nsh);
107 GNUNET_free_non_null (s_name);
112 create_updated_cont (void *cls, int32_t success, const char *emsg)
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating expiration for record `%s': %s `%s'\n", name, ((success == GNUNET_YES) || (success == GNUNET_NO)) ? "SUCCESS" : "FAIL", emsg);
116 if (success == GNUNET_NO)
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updated record for name `%s'\n", name);
121 else if (success == GNUNET_OK)
124 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "FAIL, Create new record for name `%s'\n", name);
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create records for name `%s'\n", name);
131 GNUNET_SCHEDULER_add_now(&end, NULL);
136 create_identical_cont (void *cls, int32_t success, const char *emsg)
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating identical record for `%s': %s `%s'\n", name, ((success == GNUNET_YES) || (success == GNUNET_NO)) ? "SUCCESS" : "FAIL", emsg);
140 if (success == GNUNET_NO)
143 s_first_record->expiration_time = GNUNET_TIME_absolute_get ().abs_value;
144 GNUNET_NAMESTORE_record_create (nsh, privkey, s_name, s_first_record, &create_updated_cont, s_name);
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating identical record for `%s': %s `%s'\n", name, ((success == GNUNET_YES) || (success == GNUNET_NO)) ? "SUCCESS" : "FAIL", emsg);
150 GNUNET_SCHEDULER_add_now(&end, NULL);
157 create_first_cont (void *cls, int32_t success, const char *emsg)
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Create record for `%s': %s `%s'\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL", emsg);
161 if (success == GNUNET_OK)
164 /* check if record was created correct */
165 GNUNET_NAMESTORE_record_create (nsh, privkey, s_name, s_first_record, &create_identical_cont, s_name);
170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
171 GNUNET_SCHEDULER_add_now(&end, NULL);
177 static struct GNUNET_NAMESTORE_RecordData *
178 create_record (unsigned int count)
181 struct GNUNET_NAMESTORE_RecordData * rd;
183 rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
184 for (c = 0; c < count; c++)
186 rd[c].expiration_time = 0;
187 rd[c].record_type = TEST_RECORD_TYPE;
188 rd[c].data_size = TEST_RECORD_DATALEN;
189 rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
190 memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
198 const struct GNUNET_CONFIGURATION_Handle *cfg,
199 struct GNUNET_TESTING_Peer *peer)
203 struct GNUNET_TIME_Absolute et;
205 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
207 /* load privat key */
208 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
209 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
211 privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
212 GNUNET_free (hostkey_file);
213 GNUNET_assert (privkey != NULL);
215 GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
218 s_name = GNUNET_NAMESTORE_normalize_string ("DUMMY.dummy.gnunet");
219 s_first_record = create_record (1);
221 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, s_first_record);
222 char rd_ser[rd_ser_len];
223 GNUNET_NAMESTORE_records_serialize(1, s_first_record, rd_ser_len, rd_ser);
225 et.abs_value = s_first_record->expiration_time;
226 s_signature = GNUNET_NAMESTORE_create_signature(privkey, et, s_name, s_first_record, 1);
228 /* create random zone hash */
229 GNUNET_CRYPTO_short_hash (&pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &s_zone);
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name: `%s' Zone: `%s' \n", s_name, GNUNET_short_h2s (&s_zone));
232 nsh = GNUNET_NAMESTORE_connect (cfg);
233 GNUNET_break (NULL != nsh);
235 GNUNET_break (s_first_record != NULL);
236 GNUNET_break (s_name != NULL);
238 /* create initial record */
239 GNUNET_NAMESTORE_record_create (nsh, privkey, s_name, s_first_record, &create_first_cont, s_name);
244 main (int argc, char *argv[])
248 GNUNET_TESTING_service_run ("test-namestore-api-create-update",
250 "test_namestore_api.conf",
254 GNUNET_free_non_null (s_signature);
258 /* end of test_namestore_api_create_update.c */