15efeccade0352f016b61ba2654357af2059247d
[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 struct GNUNET_PeerIdentity pid;
48
49 static int
50 check_it (void *cls,
51           const char *tname,
52           struct GNUNET_TIME_Absolute expiration,
53           const void *addr, uint16_t addrlen)
54 {
55   unsigned int *agc = cls;
56
57   if (addrlen > 0)
58     {
59       GNUNET_assert (0 == strcmp ("peerinfotest", tname));
60       GNUNET_assert (0 == strncmp ("Address", addr, addrlen));
61       (*agc) -= (1 << (addrlen - 1));
62     }
63   return GNUNET_OK;
64 }
65
66
67 static size_t
68 address_generator (void *cls, size_t max, void *buf)
69 {
70   size_t *agc = cls;
71   size_t ret;
72
73   if (0 == *agc)
74     return 0;
75   ret = GNUNET_HELLO_add_address ("peerinfotest",
76                                   GNUNET_TIME_relative_to_absolute
77                                   (GNUNET_TIME_UNIT_HOURS), "Address", *agc,
78                                   buf, max);
79   (*agc)--;
80   return ret;
81 }
82
83
84 static void
85 add_peer ()
86 {
87   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
88   struct GNUNET_HELLO_Message *h2;
89   size_t agc;
90
91   agc = 2;
92   memset (&pkey, 32, sizeof (pkey));
93   GNUNET_CRYPTO_hash (&pkey, sizeof (pkey), &pid.hashPubKey);
94   h2 = GNUNET_HELLO_create (&pkey, &address_generator, &agc);
95   GNUNET_PEERINFO_standalone_add_peer (h, h2);
96   GNUNET_free (h2);
97
98 }
99
100
101 static void
102 process (void *cls,
103          const struct GNUNET_PeerIdentity *peer,
104          const struct GNUNET_HELLO_Message *hello,
105          const char * err_msg)
106 {
107   int *ok = cls;
108   unsigned int agc;
109
110   if (err_msg != NULL)
111   {
112           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
113                       _("Error in communication with PEERINFO service\n"));
114   }
115
116   if (peer == NULL)
117     {
118       ic = NULL;
119       if ( (3 == *ok) &&
120            (retries < 50) )
121         {
122           /* try again */
123           retries++;      
124           add_peer ();
125           ic = GNUNET_PEERINFO_standalone_iterate (h,
126                                         &pid,
127                                         GNUNET_TIME_relative_multiply
128                                         (GNUNET_TIME_UNIT_SECONDS, 15), 
129                                         &process, cls);
130           return;
131         }
132       GNUNET_assert (peer == NULL);
133       GNUNET_assert (2 == *ok);
134       GNUNET_PEERINFO_standalone_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,
152      char *const *args,
153      const char *cfgfile, 
154      const struct GNUNET_CONFIGURATION_Handle *c)
155 {
156   cfg = c;
157   h = GNUNET_PEERINFO_standalone_connect (cfg);
158   GNUNET_assert (h != NULL);
159   add_peer ();
160   ic = GNUNET_PEERINFO_standalone_iterate (h,
161                                 &pid,
162                                 GNUNET_TIME_relative_multiply
163                                 (GNUNET_TIME_UNIT_SECONDS, 15),
164                                 &process, cls);
165 }
166
167
168 static int
169 check ()
170 {
171   int ok = 3;
172   struct GNUNET_OS_Process *proc;
173   char *const argv[] = { "test-peerinfo-api",
174     "-c",
175     "test_peerinfo_api_data.conf",
176 #if DEBUG_PEERINFO
177     "-L", "DEBUG",
178 #endif
179     NULL
180   };
181   struct GNUNET_GETOPT_CommandLineOption options[] = {
182     GNUNET_GETOPT_OPTION_END
183   };
184   proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-peerinfo",
185                                  "gnunet-service-peerinfo",
186 #if DEBUG_PEERINFO
187                                  "-L", "DEBUG",
188 #endif
189                                  "-c", "test_peerinfo_api_data.conf", NULL);
190   GNUNET_assert (NULL != proc);
191   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
192                       argv, "test-peerinfo-api", "nohelp",
193                       options, &run, &ok);
194   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
195     {
196       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
197       ok = 1;
198     }
199   GNUNET_OS_process_wait (proc);
200   GNUNET_OS_process_close (proc);
201   proc = NULL;
202   return ok;
203 }
204
205
206 int
207 main (int argc, char *argv[])
208 {
209   int ret = 0;
210
211   GNUNET_log_setup ("test_peerinfo_api",
212 #if DEBUG_PEERINFO
213                     "DEBUG",
214 #else
215                     "WARNING",
216 #endif
217                     NULL);
218   ret = check ();
219   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-peerinfo");
220   return ret;
221 }
222
223 /* end of test_peerinfo_api.c */