387d582720f87e7f8bb17a5648c335570d16a306
[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 TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
33
34 static struct GNUNET_NAMESTORE_Handle * nsh;
35
36 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
37 static struct GNUNET_OS_Process *arm;
38
39 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
40 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
41
42 static GNUNET_HashCode s_zone;
43 static GNUNET_HashCode s_zone_value;
44
45 static int res;
46
47 static void
48 start_arm (const char *cfgname)
49 {
50   arm = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
51                                "gnunet-service-arm", "-c", cfgname,
52 #if VERBOSE_PEERS
53                                "-L", "DEBUG",
54 #else
55                                "-L", "ERROR",
56 #endif
57                                NULL);
58 }
59
60 static void
61 stop_arm ()
62 {
63   if (NULL != arm)
64   {
65     if (0 != GNUNET_OS_process_kill (arm, SIGTERM))
66       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
67     GNUNET_OS_process_wait (arm);
68     GNUNET_OS_process_close (arm);
69     arm = NULL;
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  */
79 static void
80 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
81 {
82   if (nsh != NULL)
83     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
84   nsh = NULL;
85
86   if (privkey != NULL)
87     GNUNET_CRYPTO_rsa_key_free (privkey);
88   privkey = NULL;
89
90   if (NULL != arm)
91     stop_arm();
92
93   res = 1;
94 }
95
96
97 static void
98 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
99 {
100   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
101   {
102     GNUNET_SCHEDULER_cancel (endbadly_task);
103     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
104   }
105
106   if (privkey != NULL)
107     GNUNET_CRYPTO_rsa_key_free (privkey);
108   privkey = NULL;
109
110   if (nsh != NULL)
111     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
112   nsh = NULL;
113
114   if (NULL != arm)
115     stop_arm();
116 }
117
118 void zone_to_name_proc (void *cls,
119                             const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
120                             struct GNUNET_TIME_Absolute expire,
121                             const char *n,
122                             unsigned int rd_count,
123                             const struct GNUNET_NAMESTORE_RecordData *rd,
124                             const struct GNUNET_CRYPTO_RsaSignature *signature)
125 {
126     if ((zone_key == NULL) && (n == NULL) && (rd_count == 0) && (rd == NULL) && (signature == NULL))
127     {
128       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No result found\n");
129       res = 0;
130     }
131     else
132     {
133       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result found\n");
134       res = 0;
135     }
136     GNUNET_SCHEDULER_add_now(&end, NULL);
137 }
138
139
140 void
141 delete_existing_db (const struct GNUNET_CONFIGURATION_Handle *cfg)
142 {
143   char *afsdir;
144
145   if (GNUNET_OK ==
146       GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore-sqlite",
147                                                "FILENAME", &afsdir))
148   {
149     if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
150       if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
151         if (GNUNET_OK == GNUNET_DISK_directory_remove(afsdir))
152           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleted existing database `%s' \n", afsdir);
153    GNUNET_free (afsdir);
154   }
155
156 }
157
158 static void
159 run (void *cls, char *const *args, const char *cfgfile,
160      const struct GNUNET_CONFIGURATION_Handle *cfg)
161 {
162   delete_existing_db(cfg);
163
164   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
165
166   /* load privat key */
167   privkey = GNUNET_CRYPTO_rsa_key_create_from_file("hostkey");
168   GNUNET_assert (privkey != NULL);
169   /* get public key */
170   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
171
172   /* zone hash */
173   GNUNET_CRYPTO_hash (&pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &s_zone);
174   GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &s_zone_value);
175
176   start_arm (cfgfile);
177   GNUNET_assert (arm != NULL);
178
179   nsh = GNUNET_NAMESTORE_connect (cfg);
180   GNUNET_break (NULL != nsh);
181
182   /* create initial record */
183   GNUNET_NAMESTORE_zone_to_name (nsh, &s_zone, &s_zone_value, zone_to_name_proc, NULL);
184 }
185
186 static int
187 check ()
188 {
189   static char *const argv[] = { "test-namestore-api",
190     "-c",
191     "test_namestore_api.conf",
192 #if VERBOSE
193     "-L", "DEBUG",
194 #endif
195     NULL
196   };
197   static struct GNUNET_GETOPT_CommandLineOption options[] = {
198     GNUNET_GETOPT_OPTION_END
199   };
200
201   res = 1;
202   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-namestore-api",
203                       "nohelp", options, &run, &res);
204   return res;
205 }
206
207 int
208 main (int argc, char *argv[])
209 {
210   int ret;
211
212   ret = check ();
213   return ret;
214 }
215
216 /* end of test_namestore_api.c */