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