fix for size
[oweals/gnunet.git] / src / namestore / test_namestore_api_lookup_private.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 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_store.c
22  * @brief testcase for namestore_api.c: store a record
23  */
24 #include "platform.h"
25 #include "gnunet_namestore_service.h"
26 #include "gnunet_testing_lib.h"
27
28 #define TEST_RECORD_TYPE 1234
29
30 #define TEST_RECORD_DATALEN 123
31
32 #define TEST_RECORD_DATA 'a'
33
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
35
36 static struct GNUNET_NAMESTORE_Handle *nsh;
37
38 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
39
40 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
41
42 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
43
44 static int res;
45
46 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
47
48 //static const char * name = "dummy.dummy.gnunet";
49 static const char * name = "d";
50
51 static void
52 cleanup ()
53 {
54   if (NULL != nsh)
55   {
56     GNUNET_NAMESTORE_disconnect (nsh);
57     nsh = NULL;
58   }
59   if (NULL != privkey)
60   {
61     GNUNET_free (privkey);
62     privkey = NULL;
63   }
64   GNUNET_SCHEDULER_shutdown ();
65 }
66
67
68 /**
69  * Re-establish the connection to the service.
70  *
71  * @param cls handle to use to re-connect.
72  * @param tc scheduler context
73  */
74 static void
75 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
76 {
77   if (NULL != nsqe)
78   {
79     GNUNET_NAMESTORE_cancel (nsqe);
80     nsqe = NULL;
81   }
82   cleanup ();
83   res = 1;
84 }
85
86
87 static void
88 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
89 {
90   cleanup ();
91   res = 0;
92 }
93
94 void lookup_it (void *cls,
95                 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
96                 const char *label,
97                 unsigned int rd_count,
98                 const struct GNUNET_GNSRECORD_Data *rd)
99 {
100   nsqe = NULL;
101
102   if (0 != memcmp(privkey, zone, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
103   {
104     GNUNET_break(0);
105     GNUNET_SCHEDULER_cancel (endbadly_task);
106     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
107     return;
108   }
109
110
111   if (NULL == label)
112   {
113     GNUNET_break(0);
114     GNUNET_SCHEDULER_cancel (endbadly_task);
115     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
116     return;
117   }
118
119   if (0 != strcmp (label, name))
120   {
121     GNUNET_break(0);
122     GNUNET_SCHEDULER_cancel (endbadly_task);
123     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
124     return;
125   }
126
127   if (1 != rd_count)
128   {
129     GNUNET_break(0);
130     GNUNET_SCHEDULER_cancel (endbadly_task);
131     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL );
132     return;
133   }
134
135   /* Done */
136   GNUNET_SCHEDULER_cancel (endbadly_task);
137   endbadly_task = GNUNET_SCHEDULER_NO_TASK;
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   const char *name = cls;
146
147   nsqe = NULL;
148   GNUNET_assert (NULL != cls);
149   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150               "Name store added record for `%s': %s\n",
151               name,
152               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
153
154   if (GNUNET_OK != success)
155   {
156     GNUNET_SCHEDULER_cancel (endbadly_task);
157     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
158     return;
159   }
160   /* Lookup */
161   nsqe = GNUNET_NAMESTORE_records_lookup (nsh, privkey, name, lookup_it, NULL);
162 }
163
164
165 static void
166 run (void *cls,
167      const struct GNUNET_CONFIGURATION_Handle *cfg,
168      struct GNUNET_TESTING_Peer *peer)
169 {
170   struct GNUNET_GNSRECORD_Data rd;
171   char *hostkey_file;
172
173   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
174                                                 &endbadly, NULL);
175   GNUNET_asprintf (&hostkey_file,
176                    "zonefiles%s%s",
177                    DIR_SEPARATOR_STR,
178                    "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
179   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
180   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
181   GNUNET_free (hostkey_file);
182   GNUNET_assert (privkey != NULL);
183   GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
184
185   rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
186   rd.record_type = TEST_RECORD_TYPE;
187   rd.data_size = TEST_RECORD_DATALEN;
188   rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
189   rd.flags = 0;
190   memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
191
192   nsh = GNUNET_NAMESTORE_connect (cfg);
193   GNUNET_break (NULL != nsh);
194   nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
195                                       1, &rd, &put_cont, (void *) name);
196   if (NULL == nsqe)
197   {
198     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
199               _("Namestore cannot store no block\n"));
200   }
201
202   GNUNET_free ((void *)rd.data);
203 }
204
205
206 int
207 main (int argc, char *argv[])
208 {
209   res = 1;
210   if (0 !=
211       GNUNET_TESTING_peer_run ("test-namestore-api",
212                                "test_namestore_api.conf",
213                                &run,
214                                NULL))
215     return 1;
216   return res;
217 }
218
219
220 /* end of test_namestore_api_store.c */