-fixing #2405
[oweals/gnunet.git] / src / namestore / test_namestore_api_zone_to_name.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_zone_to_name.c
22  * @brief testcase for zone to name translation
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_namestore_service.h"
27 #include "namestore.h"
28 #include "gnunet_signatures.h"
29
30 #define VERBOSE GNUNET_NO
31
32 #define RECORDS 5
33 #define TEST_RECORD_TYPE 1234
34 #define TEST_RECORD_DATALEN 123
35 #define TEST_RECORD_DATA 'a'
36
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
38
39 static struct GNUNET_NAMESTORE_Handle * nsh;
40
41 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
42 static struct GNUNET_OS_Process *arm;
43
44 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
45 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
46
47 struct GNUNET_TIME_Absolute expire;
48
49 static struct GNUNET_CRYPTO_ShortHashCode s_zone;
50 static struct GNUNET_CRYPTO_ShortHashCode s_zone_value;
51
52 char * s_name;
53
54 struct GNUNET_NAMESTORE_RecordData *s_rd;
55 struct GNUNET_CRYPTO_RsaSignature *s_signature;
56
57 static int res;
58
59 static void
60 start_arm (const char *cfgname)
61 {
62   arm = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
63                                "gnunet-service-arm", "-c", cfgname,
64 #if VERBOSE_PEERS
65                                "-L", "DEBUG",
66 #else
67                                "-L", "ERROR",
68 #endif
69                                NULL);
70 }
71
72 static void
73 stop_arm ()
74 {
75   if (NULL != arm)
76   {
77     if (0 != GNUNET_OS_process_kill (arm, SIGTERM))
78       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
79     GNUNET_OS_process_wait (arm);
80     GNUNET_OS_process_destroy (arm);
81     arm = NULL;
82   }
83 }
84
85 /**
86  * Re-establish the connection to the service.
87  *
88  * @param cls handle to use to re-connect.
89  * @param tc scheduler context
90  */
91 static void
92 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 {
94   if (nsh != NULL)
95     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
96   nsh = NULL;
97
98   if (privkey != NULL)
99     GNUNET_CRYPTO_rsa_key_free (privkey);
100   privkey = NULL;
101
102   if (NULL != arm)
103     stop_arm();
104
105   res = 1;
106 }
107
108
109 static void
110 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
111 {
112   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
113   {
114     GNUNET_SCHEDULER_cancel (endbadly_task);
115     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
116   }
117
118   if (privkey != NULL)
119     GNUNET_CRYPTO_rsa_key_free (privkey);
120   privkey = NULL;
121
122   if (nsh != NULL)
123     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
124   nsh = NULL;
125
126   if (NULL != arm)
127     stop_arm();
128 }
129
130 void zone_to_name_proc (void *cls,
131                             const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
132                             struct GNUNET_TIME_Absolute expire,
133                             const char *n,
134                             unsigned int rd_count,
135                             const struct GNUNET_NAMESTORE_RecordData *rd,
136                             const struct GNUNET_CRYPTO_RsaSignature *signature)
137 {
138   int fail = GNUNET_NO;
139
140   if ((zone_key == NULL) && (n == NULL) && (rd_count == 0) && (rd == NULL) && (signature == NULL))
141   {
142     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No result found\n");
143     res = 1;
144   }
145   else
146   {
147     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result found: `%s'\n", n);
148     if ((n == NULL) || (0 != strcmp(n, s_name)))
149     {
150       fail = GNUNET_YES;
151       GNUNET_break (0);
152     }
153     if (rd_count != 1)
154     {
155       fail = GNUNET_YES;
156       GNUNET_break (0);
157     }
158     if ((zone_key == NULL) || (0 != memcmp (zone_key, &pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))))
159     {
160       fail = GNUNET_YES;
161       GNUNET_break (0);
162     }
163     if (fail == GNUNET_NO)
164       res = 0;
165     else
166       res = 1;
167   }
168   GNUNET_SCHEDULER_add_now(&end, NULL);
169 }
170
171
172 void
173 delete_existing_db (const struct GNUNET_CONFIGURATION_Handle *cfg)
174 {
175   char *afsdir;
176
177   if (GNUNET_OK ==
178       GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore-sqlite",
179                                                "FILENAME", &afsdir))
180   {
181     if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
182       if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
183         if (GNUNET_OK == GNUNET_DISK_directory_remove(afsdir))
184           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleted existing database `%s' \n", afsdir);
185    GNUNET_free (afsdir);
186   }
187
188 }
189
190
191 void
192 put_cont (void *cls, int32_t success, const char *emsg)
193 {
194   char *name = cls;
195   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name store added record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
196   if (success == GNUNET_OK)
197   {
198     res = 0;
199
200     /* create initial record */
201     GNUNET_NAMESTORE_zone_to_name (nsh, &s_zone, &s_zone_value, zone_to_name_proc, NULL);
202
203   }
204   else
205   {
206     res = 1;
207     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
208     GNUNET_SCHEDULER_add_now(&end, NULL);
209   }
210 }
211
212 static void
213 run (void *cls, char *const *args, const char *cfgfile,
214      const struct GNUNET_CONFIGURATION_Handle *cfg)
215 {
216   delete_existing_db(cfg);
217
218   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
219   GNUNET_asprintf(&s_name, "dummy");
220
221
222   /* load privat key */
223   char *hostkey_file;
224   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
225       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
226   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
227   privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
228   GNUNET_free (hostkey_file);
229   GNUNET_assert (privkey != NULL);
230   /* get public key */
231   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
232
233   /* zone hash */
234   GNUNET_CRYPTO_short_hash (&pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &s_zone);
235   GNUNET_CRYPTO_short_hash (s_name, strlen (s_name) + 1, &s_zone_value);
236   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using PKEY `%s' \n", GNUNET_short_h2s (&s_zone_value));
237
238   struct GNUNET_NAMESTORE_RecordData rd;
239   rd.expiration = GNUNET_TIME_absolute_get();
240   rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
241   rd.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
242   rd.data = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_ShortHashCode));
243   memcpy ((char *) rd.data, &s_zone_value, sizeof (struct GNUNET_CRYPTO_ShortHashCode));
244
245   start_arm (cfgfile);
246   GNUNET_assert (arm != NULL);
247
248   nsh = GNUNET_NAMESTORE_connect (cfg);
249   GNUNET_break (NULL != nsh);
250
251   expire = GNUNET_TIME_absolute_get ();
252   s_signature = GNUNET_NAMESTORE_create_signature(privkey, rd.expiration, s_name, &rd, 1);
253   GNUNET_NAMESTORE_record_put(nsh, &pubkey, s_name, expire, 1, &rd, s_signature, put_cont, NULL);
254
255   GNUNET_free ((void *) rd.data);
256 }
257
258 static int
259 check ()
260 {
261   static char *const argv[] = { "test-namestore-api",
262     "-c",
263     "test_namestore_api.conf",
264 #if VERBOSE
265     "-L", "DEBUG",
266 #endif
267     NULL
268   };
269   static struct GNUNET_GETOPT_CommandLineOption options[] = {
270     GNUNET_GETOPT_OPTION_END
271   };
272
273   res = 1;
274   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-namestore-api",
275                       "nohelp", options, &run, &res);
276   return res;
277 }
278
279 int
280 main (int argc, char *argv[])
281 {
282   int ret;
283
284   ret = check ();
285   return ret;
286 }
287
288 /* end of test_namestore_api.c */