- change signing func
[oweals/gnunet.git] / src / namestore / test_namestore_api_put.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 #include "namestore.h"
28
29 #define VERBOSE GNUNET_NO
30
31 #define RECORDS 5
32 #define TEST_RECORD_TYPE 1234
33 #define TEST_RECORD_DATALEN 123
34 #define TEST_RECORD_DATA 'a'
35
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
37
38 static struct GNUNET_NAMESTORE_Handle * nsh;
39
40 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
41 static struct GNUNET_OS_Process *arm;
42
43 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
44 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
45
46 struct GNUNET_NAMESTORE_RecordData *s_rd;
47
48 static int res;
49
50 static void
51 start_arm (const char *cfgname)
52 {
53   arm = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
54                                "gnunet-service-arm", "-c", cfgname,
55 #if VERBOSE_PEERS
56                                "-L", "DEBUG",
57 #else
58                                "-L", "ERROR",
59 #endif
60                                NULL);
61 }
62
63 static void
64 stop_arm ()
65 {
66   if (NULL != arm)
67   {
68     if (0 != GNUNET_OS_process_kill (arm, SIGTERM))
69       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
70     GNUNET_OS_process_wait (arm);
71     GNUNET_OS_process_close (arm);
72     arm = NULL;
73   }
74 }
75
76 /**
77  * Re-establish the connection to the service.
78  *
79  * @param cls handle to use to re-connect.
80  * @param tc scheduler context
81  */
82 static void
83 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
84 {
85   if (nsh != NULL)
86     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
87   nsh = NULL;
88
89   if (privkey != NULL)
90     GNUNET_CRYPTO_rsa_key_free (privkey);
91   privkey = NULL;
92
93   if (NULL != arm)
94     stop_arm();
95
96   res = 1;
97 }
98
99
100 static void
101 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102 {
103   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
104   {
105     GNUNET_SCHEDULER_cancel (endbadly_task);
106     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
107   }
108
109   if (privkey != NULL)
110     GNUNET_CRYPTO_rsa_key_free (privkey);
111   privkey = NULL;
112
113   if (nsh != NULL)
114     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
115   nsh = NULL;
116
117   if (NULL != arm)
118     stop_arm();
119 }
120
121 void
122 put_cont (void *cls, int32_t success, const char *emsg)
123 {
124   char * name = cls;
125
126   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name store added record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
127   if (success == GNUNET_OK)
128     res = 0;
129   else
130     res = 1;
131
132   GNUNET_SCHEDULER_add_now(&end, NULL);
133 }
134
135 static struct GNUNET_NAMESTORE_RecordData *
136 create_record (int count)
137 {
138   int c;
139   struct GNUNET_NAMESTORE_RecordData * rd;
140   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
141
142   for (c = 0; c < RECORDS; c++)
143   {
144   rd[c].expiration = GNUNET_TIME_absolute_get();
145   rd[c].record_type = TEST_RECORD_TYPE;
146   rd[c].data_size = TEST_RECORD_DATALEN;
147   rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
148   memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
149   }
150
151   return rd;
152 }
153
154 static void
155 run (void *cls, char *const *args, const char *cfgfile,
156      const struct GNUNET_CONFIGURATION_Handle *cfg)
157 {
158   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
159
160   /* load privat key */
161   privkey = GNUNET_CRYPTO_rsa_key_create_from_file("hostkey");
162   GNUNET_assert (privkey != NULL);
163   /* get public key */
164   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
165
166   struct GNUNET_CRYPTO_RsaSignature *signature;
167
168   start_arm (cfgfile);
169   GNUNET_assert (arm != NULL);
170
171   nsh = GNUNET_NAMESTORE_connect (cfg);
172   GNUNET_break (NULL != nsh);
173
174   /* create record */
175   char * s_name = "dummy.dummy.gnunet";
176   s_rd = create_record (RECORDS);
177
178   signature = GNUNET_NAMESTORE_create_signature(privkey, s_name, s_rd, RECORDS);
179
180   GNUNET_break (s_rd != NULL);
181   GNUNET_break (s_name != NULL);
182
183   GNUNET_NAMESTORE_record_put (nsh, &pubkey, s_name,
184                               GNUNET_TIME_absolute_get_forever(),
185                               RECORDS, s_rd, signature, put_cont, s_name);
186
187   GNUNET_free (signature);
188
189   int c;
190   for (c = 0; c < RECORDS; c++)
191     GNUNET_free_non_null((void *) s_rd[c].data);
192   GNUNET_free (s_rd);
193
194 }
195
196 static int
197 check ()
198 {
199   static char *const argv[] = { "test-namestore-api",
200     "-c",
201     "test_namestore_api.conf",
202 #if VERBOSE
203     "-L", "DEBUG",
204 #endif
205     NULL
206   };
207   static struct GNUNET_GETOPT_CommandLineOption options[] = {
208     GNUNET_GETOPT_OPTION_END
209   };
210
211   res = 1;
212   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-namestore-api",
213                       "nohelp", options, &run, &res);
214   return res;
215 }
216
217 int
218 main (int argc, char *argv[])
219 {
220   int ret;
221
222   ret = check ();
223
224   return ret;
225 }
226
227 /* end of test_namestore_api.c */