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