cd722ab8a3886164f2ee724e5415d02ee80889fb
[oweals/gnunet.git] / src / namestore / test_namestore_api_zone_to_name.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_to_name.c
22  * @brief testcase for zone to name translation
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_namestore_service.h"
27 #include "gnunet_testing_lib-new.h"
28 #include "namestore.h"
29 #include "gnunet_signatures.h"
30
31 #define RECORDS 5
32
33 #define TEST_RECORD_TYPE 1234
34
35 #define TEST_RECORD_DATALEN 123
36
37 #define TEST_RECORD_DATA 'a'
38
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
40
41
42 static struct GNUNET_NAMESTORE_Handle * nsh;
43
44 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
45
46 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
47
48 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
49
50 static struct GNUNET_TIME_Absolute expire;
51
52 static struct GNUNET_CRYPTO_ShortHashCode s_zone;
53
54 static struct GNUNET_CRYPTO_ShortHashCode s_zone_value;
55
56 static char * s_name;
57
58 static struct GNUNET_CRYPTO_RsaSignature *s_signature;
59
60 static int res;
61
62
63 /**
64  * Re-establish the connection to the service.
65  *
66  * @param cls handle to use to re-connect.
67  * @param tc scheduler context
68  */
69 static void
70 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
71 {
72   if (nsh != NULL)
73     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
74   nsh = NULL;
75   if (privkey != NULL)
76     GNUNET_CRYPTO_rsa_key_free (privkey);
77   privkey = NULL;
78   res = 1;
79 }
80
81
82 static void
83 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
84 {
85   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
86   {
87     GNUNET_SCHEDULER_cancel (endbadly_task);
88     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
89   }
90   if (privkey != NULL)
91     GNUNET_CRYPTO_rsa_key_free (privkey);
92   privkey = NULL;
93   if (nsh != NULL)
94     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
95   nsh = NULL;
96 }
97
98
99 static void
100 zone_to_name_proc (void *cls,
101                    const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
102                    struct GNUNET_TIME_Absolute expire,
103                    const char *n,
104                    unsigned int rd_count,
105                    const struct GNUNET_NAMESTORE_RecordData *rd,
106                    const struct GNUNET_CRYPTO_RsaSignature *signature)
107 {
108   int fail = GNUNET_NO;
109
110   if ((zone_key == NULL) && (n == NULL) && (rd_count == 0) && (rd == NULL) && (signature == NULL))
111   {
112     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No result found\n");
113     res = 1;
114   }
115   else
116   {
117     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result found: `%s'\n", n);
118     if ((n == NULL) || (0 != strcmp(n, s_name)))
119     {
120       fail = GNUNET_YES;
121       GNUNET_break (0);
122     }
123     if (rd_count != 1)
124     {
125       fail = GNUNET_YES;
126       GNUNET_break (0);
127     }
128     if ((zone_key == NULL) || (0 != memcmp (zone_key, &pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))))
129     {
130       fail = GNUNET_YES;
131       GNUNET_break (0);
132     }
133     if (fail == GNUNET_NO)
134       res = 0;
135     else
136       res = 1;
137   }
138   GNUNET_SCHEDULER_add_now(&end, NULL);
139 }
140
141
142 static void
143 put_cont (void *cls, int32_t success, const char *emsg)
144 {
145   char *name = cls;
146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name store added record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
147   if (success == GNUNET_OK)
148   {
149     res = 0;
150
151     /* create initial record */
152     GNUNET_NAMESTORE_zone_to_name (nsh, &s_zone, &s_zone_value, zone_to_name_proc, NULL);
153
154   }
155   else
156   {
157     res = 1;
158     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
159     GNUNET_SCHEDULER_add_now(&end, NULL);
160   }
161 }
162
163
164 static void
165 run (void *cls,
166      const struct GNUNET_CONFIGURATION_Handle *cfg)
167 {
168   struct GNUNET_TIME_Absolute et;
169
170   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
171   GNUNET_asprintf(&s_name, "dummy");
172   /* load privat key */
173   char *hostkey_file;
174   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
175       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
176   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
177   privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
178   GNUNET_free (hostkey_file);
179   GNUNET_assert (privkey != NULL);
180   /* get public key */
181   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
182
183   /* zone hash */
184   GNUNET_CRYPTO_short_hash (&pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &s_zone);
185   GNUNET_CRYPTO_short_hash (s_name, strlen (s_name) + 1, &s_zone_value);
186   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using PKEY `%s' \n", GNUNET_short_h2s (&s_zone_value));
187
188   struct GNUNET_NAMESTORE_RecordData rd;
189   rd.expiration_time = GNUNET_TIME_absolute_get().abs_value;
190   rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
191   rd.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
192   rd.data = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_ShortHashCode));
193   memcpy ((char *) rd.data, &s_zone_value, sizeof (struct GNUNET_CRYPTO_ShortHashCode));
194   nsh = GNUNET_NAMESTORE_connect (cfg);
195   GNUNET_break (NULL != nsh);
196
197   expire = GNUNET_TIME_absolute_get ();
198   et.abs_value = rd.expiration_time;
199   s_signature = GNUNET_NAMESTORE_create_signature(privkey, et, s_name, &rd, 1);
200   GNUNET_NAMESTORE_record_put(nsh, &pubkey, s_name, expire, 1, &rd, s_signature, put_cont, NULL);
201
202   GNUNET_free ((void *) rd.data);
203 }
204
205
206
207 int
208 main (int argc, char *argv[])
209 {
210   res = 1;
211   if (0 != 
212       GNUNET_TESTING_service_run ("test-namestore-api-zone-to-name",
213                                   "namestore",
214                                   "test_namestore_api.conf",
215                                   &run,
216                                   NULL))
217     return 1;
218   return res;
219 }
220
221 /* end of test_namestore_api_zone_to_name.c */