10ea1fa8179ef8527d401a6ef03930f38d6223b0
[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 2, 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_NewIteratorContext *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, size_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_new (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, uint32_t trust)
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_new (h,
120                                             NULL,
121                                             0,
122                                             GNUNET_TIME_relative_multiply
123                                             (GNUNET_TIME_UNIT_SECONDS, 15), 
124                                             &process, cls);
125           return;
126         }
127       GNUNET_assert (peer == NULL);
128       GNUNET_assert (2 == *ok);
129       GNUNET_assert (trust == 0);
130       GNUNET_PEERINFO_disconnect (h);
131       h = NULL;
132       *ok = 0;
133       return;
134     }
135   if (hello != NULL)
136     {
137       GNUNET_assert (3 == *ok);
138       agc = 3;
139       GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &check_it, &agc);
140       GNUNET_assert (agc == 0);
141       *ok = 2;
142     }
143 }
144
145
146 static void
147 run (void *cls,
148      struct GNUNET_SCHEDULER_Handle *s,
149      char *const *args,
150      const char *cfgfile, 
151      const struct GNUNET_CONFIGURATION_Handle *c)
152 {
153   sched = s;
154   cfg = c;
155   h = GNUNET_PEERINFO_connect (cfg, sched);
156   add_peer ();
157   ic = GNUNET_PEERINFO_iterate_new (h,
158                                     NULL,
159                                     0,
160                                     GNUNET_TIME_relative_multiply
161                                     (GNUNET_TIME_UNIT_SECONDS, 15),
162                                     &process, cls);
163 }
164
165
166 static int
167 check ()
168 {
169   int ok = 3;
170   pid_t pid;
171   char *const argv[] = { "test-peerinfo-api",
172     "-c",
173     "test_peerinfo_api_data.conf",
174 #if DEBUG_PEERINFO
175     "-L", "DEBUG",
176 #endif
177     NULL
178   };
179   struct GNUNET_GETOPT_CommandLineOption options[] = {
180     GNUNET_GETOPT_OPTION_END
181   };
182   pid = GNUNET_OS_start_process (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_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
189                       argv, "test-peerinfo-api", "nohelp",
190                       options, &run, &ok);
191   if (0 != PLIBC_KILL (pid, SIGTERM))
192     {
193       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
194       ok = 1;
195     }
196   GNUNET_OS_process_wait(pid);
197   return ok;
198 }
199
200
201 int
202 main (int argc, char *argv[])
203 {
204   int ret = 0;
205
206   GNUNET_log_setup ("test_peerinfo_api",
207 #if DEBUG_PEERINFO
208                     "DEBUG",
209 #else
210                     "WARNING",
211 #endif
212                     NULL);
213   ret = check ();
214   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-peerinfo");
215   return ret;
216 }
217
218 /* end of test_peerinfo_api.c */