-fixfix
[oweals/gnunet.git] / src / peerinfo / test_peerinfo_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 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 /**
22  * @file peerinfo/test_peerinfo_api.c
23  * @brief testcase for peerinfo_api.c
24  * @author Christian Grothoff
25  *
26  * TODO:
27  * - test merging of HELLOs (add same peer twice...)
28  */
29
30 #include "platform.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_peerinfo_service.h"
35 #include "gnunet_program_lib.h"
36 #include "gnunet_time_lib.h"
37 #include "peerinfo.h"
38
39 static const struct GNUNET_CONFIGURATION_Handle *cfg;
40
41 static struct GNUNET_PEERINFO_IteratorContext *ic;
42
43 static struct GNUNET_PEERINFO_Handle *h;
44
45 static unsigned int retries;
46
47 static int
48 check_it (void *cls, const struct GNUNET_HELLO_Address *address,
49           struct GNUNET_TIME_Absolute expiration)
50 {
51   unsigned int *agc = cls;
52
53   if (address != NULL)
54   {
55     GNUNET_assert (0 == strcmp ("peerinfotest", address->transport_name));
56     GNUNET_assert (0 ==
57                    strncmp ("Address", address->address,
58                             address->address_length));
59     (*agc) -= (1 << (address->address_length - 1));
60   }
61   return GNUNET_OK;
62 }
63
64
65 static size_t
66 address_generator (void *cls, size_t max, void *buf)
67 {
68   size_t *agc = cls;
69   size_t ret;
70   struct GNUNET_HELLO_Address address;
71
72   if (0 == *agc)
73     return 0;
74   memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
75   address.address = "Address";
76   address.transport_name = "peerinfotest";
77   address.address_length = *agc;
78   ret =
79       GNUNET_HELLO_add_address (&address,
80                                 GNUNET_TIME_relative_to_absolute
81                                 (GNUNET_TIME_UNIT_HOURS), buf, max);
82   (*agc)--;
83   return ret;
84 }
85
86
87 static void
88 add_peer ()
89 {
90   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
91   struct GNUNET_PeerIdentity pid;
92   struct GNUNET_HELLO_Message *h2;
93   size_t agc;
94
95   agc = 2;
96   memset (&pkey, 32, sizeof (pkey));
97   GNUNET_CRYPTO_hash (&pkey, sizeof (pkey), &pid.hashPubKey);
98   h2 = GNUNET_HELLO_create (&pkey, &address_generator, &agc);
99   GNUNET_PEERINFO_add_peer (h, h2, NULL, NULL);
100   GNUNET_free (h2);
101
102 }
103
104
105 static void
106 process (void *cls, const struct GNUNET_PeerIdentity *peer,
107          const struct GNUNET_HELLO_Message *hello, const char *err_msg)
108 {
109   int *ok = cls;
110   unsigned int agc;
111
112   if (err_msg != NULL)
113   {
114     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
115                 _("Error in communication with PEERINFO service\n"));
116   }
117
118   if (peer == NULL)
119   {
120     ic = NULL;
121     if ((3 == *ok) && (retries < 50))
122     {
123       /* try again */
124       retries++;
125       add_peer ();
126       ic = GNUNET_PEERINFO_iterate (h, NULL,
127                                     GNUNET_TIME_relative_multiply
128                                     (GNUNET_TIME_UNIT_SECONDS, 15), &process,
129                                     cls);
130       return;
131     }
132     GNUNET_assert (peer == NULL);
133     GNUNET_assert (2 == *ok);
134     GNUNET_PEERINFO_disconnect (h);
135     h = NULL;
136     *ok = 0;
137     return;
138   }
139   if (hello != NULL)
140   {
141     GNUNET_assert (3 == *ok);
142     agc = 3;
143     GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &check_it, &agc);
144     GNUNET_assert (agc == 0);
145     *ok = 2;
146   }
147 }
148
149
150 static void
151 run (void *cls, char *const *args, const char *cfgfile,
152      const struct GNUNET_CONFIGURATION_Handle *c)
153 {
154   cfg = c;
155   h = GNUNET_PEERINFO_connect (cfg);
156   GNUNET_assert (h != NULL);
157   add_peer ();
158   ic = GNUNET_PEERINFO_iterate (h, NULL,
159                                 GNUNET_TIME_relative_multiply
160                                 (GNUNET_TIME_UNIT_SECONDS, 15), &process, cls);
161 }
162
163
164 static int
165 check ()
166 {
167   int ok = 3;
168   struct GNUNET_OS_Process *proc;
169
170   char *const argv[] = { "test-peerinfo-api",
171     "-c",
172     "test_peerinfo_api_data.conf",
173 #if DEBUG_PEERINFO
174     "-L", "DEBUG",
175 #endif
176     NULL
177   };
178   struct GNUNET_GETOPT_CommandLineOption options[] = {
179     GNUNET_GETOPT_OPTION_END
180   };
181   proc =
182     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-peerinfo",
183                                "gnunet-service-peerinfo",
184 #if DEBUG_PEERINFO
185                                "-L", "DEBUG",
186 #endif
187                                "-c", "test_peerinfo_api_data.conf", NULL);
188   GNUNET_assert (NULL != proc);
189   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
190                       "test-peerinfo-api", "nohelp", options, &run, &ok);
191   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
192   {
193     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
194     ok = 1;
195   }
196   GNUNET_OS_process_wait (proc);
197   GNUNET_OS_process_close (proc);
198   proc = NULL;
199   return ok;
200 }
201
202
203 int
204 main (int argc, char *argv[])
205 {
206   int ret = 0;
207
208   GNUNET_log_setup ("test_peerinfo_api",
209 #if DEBUG_PEERINFO
210                     "DEBUG",
211 #else
212                     "WARNING",
213 #endif
214                     NULL);
215   ret = check ();
216   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-peerinfo");
217   return ret;
218 }
219
220 /* end of test_peerinfo_api.c */