97383800605c94ec1a3e5a13664db32c39c5d83e
[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_IteratorContext *ic;
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, size_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 (cfg, sched, &pid, 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, uint32_t trust)
104 {
105   int *ok = cls;
106   unsigned int agc;
107
108   if (peer == NULL)
109     {
110       ic = NULL;
111       if ( (3 == *ok) &&
112            (retries < 50) )
113         {
114           /* try again */
115           retries++;      
116           add_peer ();
117           ic = GNUNET_PEERINFO_iterate (cfg,
118                                         sched,
119                                         NULL,
120                                         0,
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_assert (trust == 0);
129       *ok = 0;
130       return;
131     }
132   if (hello != NULL)
133     {
134       GNUNET_assert (3 == *ok);
135       agc = 3;
136       GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &check_it, &agc);
137       GNUNET_assert (agc == 0);
138       *ok = 2;
139     }
140 }
141
142
143 static void
144 run (void *cls,
145      struct GNUNET_SCHEDULER_Handle *s,
146      char *const *args,
147      const char *cfgfile, 
148      const struct GNUNET_CONFIGURATION_Handle *c)
149 {
150   sched = s;
151   cfg = c;
152   add_peer ();
153   ic = GNUNET_PEERINFO_iterate (cfg,
154                                 sched,
155                                 NULL,
156                                 0,
157                                 GNUNET_TIME_relative_multiply
158                                 (GNUNET_TIME_UNIT_SECONDS, 15), &process, cls);
159 }
160
161
162 static int
163 check ()
164 {
165   int ok = 3;
166   pid_t pid;
167   char *const argv[] = { "test-peerinfo-api",
168     "-c",
169     "test_peerinfo_api_data.conf",
170 #if DEBUG_PEERINFO
171     "-L", "DEBUG",
172 #endif
173     NULL
174   };
175   struct GNUNET_GETOPT_CommandLineOption options[] = {
176     GNUNET_GETOPT_OPTION_END
177   };
178   pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-peerinfo",
179                                  "gnunet-service-peerinfo",
180 #if DEBUG_PEERINFO
181                                  "-L", "DEBUG",
182 #endif
183                                  "-c", "test_peerinfo_api_data.conf", NULL);
184   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
185                       argv, "test-peerinfo-api", "nohelp",
186                       options, &run, &ok);
187   if (0 != PLIBC_KILL (pid, SIGTERM))
188     {
189       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
190       ok = 1;
191     }
192   GNUNET_OS_process_wait(pid);
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-gnunetd-peerinfo");
211   return ret;
212 }
213
214 /* end of test_peerinfo_api.c */