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