- coverity
[oweals/gnunet.git] / src / namestore / test_namestore_api.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
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 struct GNUNET_OS_Process *arm;
36
37 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
38 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
39 static struct GNUNET_CRYPTO_ShortHashCode zone;
40
41 static int res;
42
43 #define TEST_RECORD_TYPE 1234
44 #define TEST_RECORD_DATALEN 123
45 #define TEST_RECORD_DATA 'a'
46
47
48 static void
49 start_arm (const char *cfgname)
50 {
51   arm = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
52                                "gnunet-service-arm", "-c", cfgname,
53 #if VERBOSE_PEERS
54                                "-L", "DEBUG",
55 #else
56                                "-L", "ERROR",
57 #endif
58                                NULL);
59 }
60
61 static void
62 stop_arm ()
63 {
64   if (NULL != arm)
65   {
66     if (0 != GNUNET_OS_process_kill (arm, SIGTERM))
67       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
68     GNUNET_OS_process_wait (arm);
69     GNUNET_OS_process_close (arm);
70     arm = NULL;
71   }
72 }
73
74 /**
75  * Re-establish the connection to the service.
76  *
77  * @param cls handle to use to re-connect.
78  * @param tc scheduler context
79  */
80 static void
81 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 {
83   if (nsh != NULL)
84     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
85   nsh = NULL;
86
87   if (privkey != NULL)
88     GNUNET_CRYPTO_rsa_key_free (privkey);
89   privkey = NULL;
90
91   if (NULL != arm)
92     stop_arm();
93
94   res = 1;
95 }
96
97
98 static void
99 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
100 {
101   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
102   {
103     GNUNET_SCHEDULER_cancel (endbadly_task);
104     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
105   }
106
107   if (privkey != NULL)
108     GNUNET_CRYPTO_rsa_key_free (privkey);
109   privkey = NULL;
110
111   if (nsh != NULL)
112     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
113   nsh = NULL;
114
115
116   if (NULL != arm)
117     stop_arm();
118
119   res = 0;
120 }
121
122
123 void name_lookup_proc (void *cls,
124                             const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
125                             struct GNUNET_TIME_Absolute expire,
126                             const char *name,
127                             unsigned int rd_count,
128                             const struct GNUNET_NAMESTORE_RecordData *rd,
129                             const struct GNUNET_CRYPTO_RsaSignature *signature)
130 {
131   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore lookup result %p `%s' %i %p %p\n", zone_key, name, rd_count, rd, signature);
132   res = 0;
133   GNUNET_SCHEDULER_add_now(&end, NULL);
134 }
135
136 void put_cont (void *cls, int32_t success, const char *emsg)
137 {
138   char * name = cls;
139
140   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name store added record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
141
142   GNUNET_NAMESTORE_lookup_record (nsh, &zone, name, 0, &name_lookup_proc, NULL);
143 }
144
145 void
146 delete_existing_db (const struct GNUNET_CONFIGURATION_Handle *cfg)
147 {
148   char *afsdir;
149
150   if (GNUNET_OK ==
151       GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore-sqlite",
152                                                "FILENAME", &afsdir))
153   {
154     if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
155       if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
156         if (GNUNET_OK == GNUNET_DISK_directory_remove(afsdir))
157           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleted existing database `%s' \n", afsdir);
158    GNUNET_free (afsdir);
159   }
160
161 }
162
163 static void
164 run (void *cls, char *const *args, const char *cfgfile,
165      const struct GNUNET_CONFIGURATION_Handle *cfg)
166 {
167   delete_existing_db(cfg);
168   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
169
170   char *hostkey_file;
171   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
172       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
173   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
174   privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
175   GNUNET_free (hostkey_file);
176   GNUNET_assert (privkey != NULL);
177   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
178
179   GNUNET_CRYPTO_short_hash (&pubkey, sizeof (pubkey), &zone);
180
181
182   struct GNUNET_CRYPTO_RsaSignature signature;
183   memset (&signature, '\0', sizeof (signature));
184   struct GNUNET_NAMESTORE_RecordData rd;
185
186   rd.expiration = GNUNET_TIME_absolute_get();
187   rd.record_type = TEST_RECORD_TYPE;
188   rd.data_size = TEST_RECORD_DATALEN;
189   rd.data = GNUNET_malloc(TEST_RECORD_DATALEN);
190   memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
191   char * name = "dummy.dummy.gnunet";
192
193   start_arm (cfgfile);
194   GNUNET_assert (arm != NULL);
195
196   nsh = GNUNET_NAMESTORE_connect (cfg);
197   GNUNET_break (NULL != nsh);
198
199   GNUNET_NAMESTORE_record_put (nsh, &pubkey, name,
200                               GNUNET_TIME_absolute_get_forever(),
201                               1, &rd, &signature, put_cont, name);
202
203   GNUNET_free ((void *)rd.data);
204
205 }
206
207 static int
208 check ()
209 {
210   static char *const argv[] = { "test-namestore-api",
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",
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.c */