- fixes
[oweals/gnunet.git] / src / namestore / test_namestore_api_zone_iteration.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_iteration.c
22  * @brief testcase for namestore_api.c zone iteration functionality
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_namestore_service.h"
27
28 #define VERBOSE GNUNET_NO
29
30 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
31
32 static struct GNUNET_NAMESTORE_Handle * nsh;
33
34 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
35 static GNUNET_SCHEDULER_TaskIdentifier stopiteration_task;
36 static struct GNUNET_OS_Process *arm;
37
38 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
39 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
40 static GNUNET_HashCode zone;
41
42 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
43 static int res;
44
45 static void
46 start_arm (const char *cfgname)
47 {
48   arm = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
49                                "gnunet-service-arm", "-c", cfgname,
50 #if VERBOSE_PEERS
51                                "-L", "DEBUG",
52 #else
53                                "-L", "ERROR",
54 #endif
55                                NULL);
56 }
57
58 static void
59 stop_arm ()
60 {
61   if (NULL != arm)
62   {
63     if (0 != GNUNET_OS_process_kill (arm, SIGTERM))
64       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
65     GNUNET_OS_process_wait (arm);
66     GNUNET_OS_process_close (arm);
67     arm = NULL;
68   }
69 }
70
71 /**
72  * Re-establish the connection to the service.
73  *
74  * @param cls handle to use to re-connect.
75  * @param tc scheduler context
76  */
77 static void
78 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
79 {
80   if (stopiteration_task != GNUNET_SCHEDULER_NO_TASK)
81   {
82     GNUNET_SCHEDULER_cancel (stopiteration_task);
83     stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
84   }
85
86   if (nsh != NULL)
87     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
88   nsh = NULL;
89
90   if (privkey != NULL)
91     GNUNET_CRYPTO_rsa_key_free (privkey);
92   privkey = NULL;
93
94   if (NULL != arm)
95     stop_arm();
96
97   res = 1;
98 }
99
100
101 static void
102 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 {
104   if (stopiteration_task != GNUNET_SCHEDULER_NO_TASK)
105   {
106     GNUNET_SCHEDULER_cancel (stopiteration_task);
107     stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
108   }
109
110   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
111   {
112     GNUNET_SCHEDULER_cancel (endbadly_task);
113     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
114   }
115
116
117   if (privkey != NULL)
118     GNUNET_CRYPTO_rsa_key_free (privkey);
119   privkey = NULL;
120
121   if (nsh != NULL)
122     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
123   nsh = NULL;
124
125
126   if (NULL != arm)
127     stop_arm();
128
129   res = 0;
130 }
131
132 static void
133 stop_iteration (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
134 {
135   stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
136
137   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping iteration for zone `%s'\n", GNUNET_h2s (&zone));
138   GNUNET_NAMESTORE_zone_iteration_stop (zi);
139
140   GNUNET_SCHEDULER_add_now (&end, NULL);
141 }
142
143 void zone_proc (void *cls,
144                 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
145                 struct GNUNET_TIME_Absolute expire,
146                 const char *name,
147                 unsigned int rd_count,
148                 const struct GNUNET_NAMESTORE_RecordData *rd,
149                 const struct GNUNET_CRYPTO_RsaSignature *signature)
150 {
151   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for zone `%s'\n", GNUNET_h2s (&zone));
152
153   stopiteration_task = GNUNET_SCHEDULER_add_now (&stop_iteration, NULL);
154 }
155
156 void
157 delete_existing_db (const struct GNUNET_CONFIGURATION_Handle *cfg)
158 {
159   char *afsdir;
160
161   if (GNUNET_OK ==
162       GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore-sqlite",
163                                                "FILENAME", &afsdir))
164   {
165     if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
166       if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
167         if (GNUNET_OK == GNUNET_DISK_directory_remove(afsdir))
168           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleted existing database `%s' \n", afsdir);
169    GNUNET_free (afsdir);
170   }
171
172 }
173
174 static void
175 run (void *cls, char *const *args, const char *cfgfile,
176      const struct GNUNET_CONFIGURATION_Handle *cfg)
177 {
178   delete_existing_db(cfg);
179   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,&endbadly, NULL);
180
181   privkey = GNUNET_CRYPTO_rsa_key_create_from_file("hostkey");
182   GNUNET_assert (privkey != NULL);
183   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
184
185   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &zone);
186
187   start_arm (cfgfile);
188   GNUNET_assert (arm != NULL);
189
190   nsh = GNUNET_NAMESTORE_connect (cfg);
191   GNUNET_break (NULL != nsh);
192
193   zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
194                                         &zone,
195                                         GNUNET_NAMESTORE_RF_NONE,
196                                         GNUNET_NAMESTORE_RF_NONE,
197                                         zone_proc,
198                                         &zone);
199   if (zi == NULL)
200   {
201     GNUNET_break (0);
202     GNUNET_SCHEDULER_cancel (endbadly_task);
203     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
204   }
205 }
206
207 static int
208 check ()
209 {
210   static char *const argv[] = { "test_namestore_api_zone_iteration",
211     "-c",
212     "test_namestore_api.conf",
213 #if VERBOSE
214     "-L", "DEBUG",
215 #endif
216     NULL
217   };
218   static struct GNUNET_GETOPT_CommandLineOption options[] = {
219     GNUNET_GETOPT_OPTION_END
220   };
221
222   res = 1;
223   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test_namestore_api_zone_iteration",
224                       "nohelp", options, &run, &res);
225   return res;
226 }
227
228 int
229 main (int argc, char *argv[])
230 {
231   int ret;
232
233   ret = check ();
234
235   return ret;
236 }
237
238 /* end of test_namestore_api_zone_iteration.c */