-rasp pi redefines slow
[oweals/gnunet.git] / src / namestore / test_namestore_api_create_update.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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.c
22  * @brief testcase for namestore_api.c for updating an existing record
23  */
24 #include "platform.h"
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"
30
31
32 #define RECORDS 1
33
34 #define TEST_RECORD_TYPE 1234
35
36 #define TEST_RECORD_DATALEN 123
37
38 #define TEST_RECORD_DATA 'a'
39
40 #define TEST_CREATE_RECORD_TYPE 4321
41
42 #define TEST_CREATE_RECORD_DATALEN 255
43
44 #define TEST_CREATE_RECORD_DATA 'b'
45
46 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
47
48
49 static struct GNUNET_NAMESTORE_Handle * nsh;
50
51 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
52
53 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
54
55 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
56
57 static struct GNUNET_CRYPTO_RsaSignature *s_signature;
58
59 static struct GNUNET_CRYPTO_ShortHashCode s_zone;
60
61 static struct GNUNET_NAMESTORE_RecordData *s_first_record;
62
63 static struct GNUNET_NAMESTORE_RecordData *s_second_record;
64
65 static char *s_name;
66
67 static int res;
68
69
70 /**
71  * Re-establish the connection to the service.
72  *
73  * @param cls handle to use to re-connect.
74  * @param tc scheduler context
75  */
76 static void
77 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
78 {
79   if (nsh != NULL)
80     GNUNET_NAMESTORE_disconnect (nsh);
81   nsh = NULL;
82   if (privkey != NULL)
83     GNUNET_CRYPTO_rsa_key_free (privkey);
84   privkey = NULL;
85   res = 1;
86 }
87
88
89 static void
90 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
91 {
92   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
93   {
94     GNUNET_SCHEDULER_cancel (endbadly_task);
95     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
96   }
97   GNUNET_free ((void *) s_first_record->data);
98   GNUNET_free (s_first_record);
99   GNUNET_free_non_null (s_second_record);
100   if (privkey != NULL)
101     GNUNET_CRYPTO_rsa_key_free (privkey);
102   privkey = NULL;
103   if (nsh != NULL)
104     GNUNET_NAMESTORE_disconnect (nsh);
105   nsh = NULL;
106 }
107
108
109 static void
110 create_updated_cont (void *cls, int32_t success, const char *emsg)
111 {
112   char *name = cls;
113   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating expiration for record `%s': %s `%s'\n", name, ((success == GNUNET_YES) || (success == GNUNET_NO)) ? "SUCCESS" : "FAIL", emsg);
114   if (success == GNUNET_NO)
115   {
116     res = 0;
117     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updated record for name `%s'\n", name);
118   }
119   else if (success == GNUNET_OK)
120   {
121     res = 1;
122     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "FAIL, Create new record for name `%s'\n", name);
123   }
124   else
125   {
126     res = 1;
127     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create records for name `%s'\n", name);
128   }
129   GNUNET_SCHEDULER_add_now(&end, NULL);
130 }
131
132
133 static void
134 create_identical_cont (void *cls, int32_t success, const char *emsg)
135 {
136   char *name = cls;
137   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating identical record for `%s': %s `%s'\n", name, ((success == GNUNET_YES) || (success == GNUNET_NO)) ? "SUCCESS" : "FAIL", emsg);
138   if (success == GNUNET_NO)
139   {
140     res = 0;
141     s_first_record->expiration_time = GNUNET_TIME_absolute_get ().abs_value;
142     GNUNET_NAMESTORE_record_create (nsh, privkey, s_name, s_first_record, &create_updated_cont, s_name);
143   }
144   else
145   {
146     res = 1;
147     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating identical record for `%s': %s `%s'\n", name, ((success == GNUNET_YES) || (success == GNUNET_NO)) ? "SUCCESS" : "FAIL", emsg);
148     GNUNET_SCHEDULER_add_now(&end, NULL);
149   }
150
151 }
152
153
154 static void
155 create_first_cont (void *cls, int32_t success, const char *emsg)
156 {
157   char *name = cls;
158   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Create record for `%s': %s `%s'\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL", emsg);
159   if (success == GNUNET_OK)
160   {
161     res = 0;
162     /* check if record was created correct */
163     GNUNET_NAMESTORE_record_create (nsh, privkey, s_name, s_first_record, &create_identical_cont, s_name);
164   }
165   else
166   {
167     res = 1;
168     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
169     GNUNET_SCHEDULER_add_now(&end, NULL);
170   }
171
172 }
173
174
175 static struct GNUNET_NAMESTORE_RecordData *
176 create_record (unsigned int count)
177 {
178   unsigned int c;
179   struct GNUNET_NAMESTORE_RecordData * rd;
180
181   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
182   for (c = 0; c < count; c++)
183   {
184     rd[c].expiration_time = 0;
185     rd[c].record_type = TEST_RECORD_TYPE;
186     rd[c].data_size = TEST_RECORD_DATALEN;
187     rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
188     memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
189   }
190   return rd;
191 }
192
193
194 static void
195 run (void *cls, 
196      const struct GNUNET_CONFIGURATION_Handle *cfg,
197      struct GNUNET_TESTING_Peer *peer)
198 {
199   size_t rd_ser_len;
200   char *hostkey_file;
201   struct GNUNET_TIME_Absolute et;
202
203   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
204
205   /* load privat key */
206   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
207       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
209   privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
210   GNUNET_free (hostkey_file);
211   GNUNET_assert (privkey != NULL);
212   /* get public key */
213   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
214
215   /* create record */
216   s_name = "dummy.dummy.gnunet";
217   s_first_record = create_record (1);
218
219   rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, s_first_record);
220   char rd_ser[rd_ser_len];
221   GNUNET_NAMESTORE_records_serialize(1, s_first_record, rd_ser_len, rd_ser);
222
223   et.abs_value = s_first_record->expiration_time;
224   s_signature = GNUNET_NAMESTORE_create_signature(privkey, et, s_name, s_first_record, 1);
225
226   /* create random zone hash */
227   GNUNET_CRYPTO_short_hash (&pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &s_zone);
228
229   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name: `%s' Zone: `%s' \n", s_name, GNUNET_short_h2s (&s_zone));
230   nsh = GNUNET_NAMESTORE_connect (cfg);
231   GNUNET_break (NULL != nsh);
232
233   GNUNET_break (s_first_record != NULL);
234   GNUNET_break (s_name != NULL);
235
236   /* create initial record */
237   GNUNET_NAMESTORE_record_create (nsh, privkey, s_name, s_first_record, &create_first_cont, s_name);
238 }
239
240
241 int
242 main (int argc, char *argv[])
243 {
244   res = 1;
245   if (0 != 
246       GNUNET_TESTING_service_run ("test-namestore-api-create-update",
247                                   "namestore",
248                                   "test_namestore_api.conf",
249                                   &run,
250                                   NULL))
251     return 1;
252   GNUNET_free_non_null (s_signature);
253   return res;
254 }
255
256 /* end of test_namestore_api_create_update.c */