2 This file is part of GNUnet.
3 (C) 2004, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file peerinfo/test_peerinfo_api.c
23 * @brief testcase for peerinfo_api.c
24 * @author Christian Grothoff
27 * - test merging of HELLOs (add same peer twice...)
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"
39 static const struct GNUNET_CONFIGURATION_Handle *cfg;
41 static struct GNUNET_PEERINFO_IteratorContext *ic;
43 static struct GNUNET_PEERINFO_Handle *h;
45 static unsigned int retries;
48 check_it (void *cls, const struct GNUNET_HELLO_Address *address,
49 struct GNUNET_TIME_Absolute expiration)
51 unsigned int *agc = cls;
55 GNUNET_assert (0 == strcmp ("peerinfotest", address->transport_name));
57 strncmp ("Address", address->address,
58 address->address_length));
59 (*agc) -= (1 << (address->address_length - 1));
66 address_generator (void *cls, size_t max, void *buf)
70 struct GNUNET_HELLO_Address address;
74 memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
75 address.address = "Address";
76 address.transport_name = "peerinfotest";
77 address.address_length = *agc;
79 GNUNET_HELLO_add_address (&address,
80 GNUNET_TIME_relative_to_absolute
81 (GNUNET_TIME_UNIT_HOURS), buf, max);
90 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
91 struct GNUNET_PeerIdentity pid;
92 struct GNUNET_HELLO_Message *h2;
96 memset (&pkey, 32, sizeof (pkey));
97 GNUNET_CRYPTO_hash (&pkey, sizeof (pkey), &pid.hashPubKey);
98 h2 = GNUNET_HELLO_create (&pkey, &address_generator, &agc);
99 GNUNET_PEERINFO_add_peer (h, h2, NULL, NULL);
106 process (void *cls, const struct GNUNET_PeerIdentity *peer,
107 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
115 _("Error in communication with PEERINFO service\n"));
121 if ((3 == *ok) && (retries < 50))
126 ic = GNUNET_PEERINFO_iterate (h, NULL,
127 GNUNET_TIME_relative_multiply
128 (GNUNET_TIME_UNIT_SECONDS, 15), &process,
132 GNUNET_assert (peer == NULL);
133 GNUNET_assert (2 == *ok);
134 GNUNET_PEERINFO_disconnect (h);
141 GNUNET_assert (3 == *ok);
143 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &check_it, &agc);
144 GNUNET_assert (agc == 0);
151 run (void *cls, char *const *args, const char *cfgfile,
152 const struct GNUNET_CONFIGURATION_Handle *c)
155 h = GNUNET_PEERINFO_connect (cfg);
156 GNUNET_assert (h != NULL);
158 ic = GNUNET_PEERINFO_iterate (h, NULL,
159 GNUNET_TIME_relative_multiply
160 (GNUNET_TIME_UNIT_SECONDS, 15), &process, cls);
168 struct GNUNET_OS_Process *proc;
170 char *const argv[] = { "test-peerinfo-api",
172 "test_peerinfo_api_data.conf",
175 struct GNUNET_GETOPT_CommandLineOption options[] = {
176 GNUNET_GETOPT_OPTION_END
179 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-peerinfo",
180 "gnunet-service-peerinfo",
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))
187 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
190 GNUNET_OS_process_wait (proc);
191 GNUNET_OS_process_close (proc);
198 main (int argc, char *argv[])
202 GNUNET_log_setup ("test_peerinfo_api",
206 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-peerinfo");
210 /* end of test_peerinfo_api.c */