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