47304d549c0b8d8ebebb1da715feaceb382dfde1
[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 struct GNUNET_SCHEDULER_Handle *sched;
40
41 static const struct GNUNET_CONFIGURATION_Handle *cfg;
42
43 static struct GNUNET_PEERINFO_IteratorContext *ic;
44
45 static struct GNUNET_PEERINFO_Handle *h;
46
47 static unsigned int retries;
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_PeerIdentity pid;
89   struct GNUNET_HELLO_Message *h2;
90   size_t agc;
91
92   agc = 2;
93   memset (&pkey, 32, sizeof (pkey));
94   GNUNET_CRYPTO_hash (&pkey, sizeof (pkey), &pid.hashPubKey);
95   h2 = GNUNET_HELLO_create (&pkey, &address_generator, &agc);
96   GNUNET_PEERINFO_add_peer (h, h2);
97   GNUNET_free (h2);
98
99 }
100
101
102 static void
103 process (void *cls,
104          const struct GNUNET_PeerIdentity *peer,
105          const struct GNUNET_HELLO_Message *hello)
106 {
107   int *ok = cls;
108   unsigned int agc;
109
110   if (peer == NULL)
111     {
112       ic = NULL;
113       if ( (3 == *ok) &&
114            (retries < 50) )
115         {
116           /* try again */
117           retries++;      
118           add_peer ();
119           ic = GNUNET_PEERINFO_iterate (h,
120                                         NULL,
121                                         GNUNET_TIME_relative_multiply
122                                         (GNUNET_TIME_UNIT_SECONDS, 15), 
123                                         &process, cls);
124           return;
125         }
126       GNUNET_assert (peer == NULL);
127       GNUNET_assert (2 == *ok);
128       GNUNET_PEERINFO_disconnect (h);
129       h = NULL;
130       *ok = 0;
131       return;
132     }
133   if (hello != NULL)
134     {
135       GNUNET_assert (3 == *ok);
136       agc = 3;
137       GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &check_it, &agc);
138       GNUNET_assert (agc == 0);
139       *ok = 2;
140     }
141 }
142
143
144 static void
145 run (void *cls,
146      struct GNUNET_SCHEDULER_Handle *s,
147      char *const *args,
148      const char *cfgfile, 
149      const struct GNUNET_CONFIGURATION_Handle *c)
150 {
151   sched = s;
152   cfg = c;
153   h = GNUNET_PEERINFO_connect (sched, cfg);
154   add_peer ();
155   ic = GNUNET_PEERINFO_iterate (h,
156                                 NULL,
157                                 GNUNET_TIME_relative_multiply
158                                 (GNUNET_TIME_UNIT_SECONDS, 15),
159                                 &process, cls);
160 }
161
162
163 static int
164 check ()
165 {
166   int ok = 3;
167   pid_t pid;
168   char *const argv[] = { "test-peerinfo-api",
169     "-c",
170     "test_peerinfo_api_data.conf",
171 #if DEBUG_PEERINFO
172     "-L", "DEBUG",
173 #endif
174     NULL
175   };
176   struct GNUNET_GETOPT_CommandLineOption options[] = {
177     GNUNET_GETOPT_OPTION_END
178   };
179   pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-peerinfo",
180                                  "gnunet-service-peerinfo",
181 #if DEBUG_PEERINFO
182                                  "-L", "DEBUG",
183 #endif
184                                  "-c", "test_peerinfo_api_data.conf", NULL);
185   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
186                       argv, "test-peerinfo-api", "nohelp",
187                       options, &run, &ok);
188   if (0 != PLIBC_KILL (pid, SIGTERM))
189     {
190       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
191       ok = 1;
192     }
193   GNUNET_OS_process_wait(pid);
194   return ok;
195 }
196
197
198 int
199 main (int argc, char *argv[])
200 {
201   int ret = 0;
202
203   GNUNET_log_setup ("test_peerinfo_api",
204 #if DEBUG_PEERINFO
205                     "DEBUG",
206 #else
207                     "WARNING",
208 #endif
209                     NULL);
210   ret = check ();
211   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-peerinfo");
212   return ret;
213 }
214
215 /* end of test_peerinfo_api.c */