2 This file is part of GNUnet
3 Copyright (C) 2009, 2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file hello/test_hello.c
22 * @brief test for hello.c
23 * @author Christian Grothoff
26 #include "gnunet_hello_lib.h"
38 my_addr_gen (void *cls,
42 unsigned int *i = cls;
44 struct GNUNET_HELLO_Address address;
46 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
47 "DEBUG: my_addr_gen called with i = %d\n",
51 memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
52 address.address = "address_information";
53 address.transport_name = "test";
54 address.address_length = *i;
55 ret = GNUNET_HELLO_add_address (&address,
56 GNUNET_TIME_absolute_get (),
73 check_addr (void *cls,
74 const struct GNUNET_HELLO_Address *address,
75 struct GNUNET_TIME_Absolute expiration)
77 unsigned int *i = cls;
79 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
80 "DEBUG: check_addr called with i = %d and addrlen = %u\n",
82 (unsigned int) address->address_length);
83 GNUNET_assert (address->address_length > 0);
84 GNUNET_assert (*i & (1 << (address->address_length - 1)));
85 *i -= (1 << (address->address_length - 1));
87 strncmp ("address_information",
89 address->address_length));
90 GNUNET_assert (0 == strcmp ("test",
91 address->transport_name));
105 remove_some (void *cls,
106 const struct GNUNET_HELLO_Address *address,
107 struct GNUNET_TIME_Absolute expiration)
109 unsigned int *i = cls;
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
112 "DEBUG: remove_some called with i = %d and addrlen = %u\n",
114 (unsigned int) address->address_length);
115 GNUNET_assert (address->address_length > 0);
116 if (*i & (1 << (address->address_length - 1)))
118 *i -= (1 << (address->address_length - 1));
129 struct GNUNET_HELLO_Message *msg1;
130 struct GNUNET_HELLO_Message *msg2;
131 struct GNUNET_HELLO_Message *msg3;
132 struct GNUNET_CRYPTO_EddsaPublicKey publicKey;
133 struct GNUNET_PeerIdentity pid;
134 struct GNUNET_TIME_Absolute startup_time;
137 GNUNET_log_setup ("test-hello",
140 startup_time = GNUNET_TIME_absolute_get ();
141 memset (&publicKey, 42, sizeof (publicKey));
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
143 "Testing HELLO creation (without addresses)...\n");
145 msg1 = GNUNET_HELLO_create (&publicKey,
149 GNUNET_assert (msg1 != NULL);
150 GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153 "Testing address iteration (empty set)...\n");
154 GNUNET_assert (NULL ==
155 GNUNET_HELLO_iterate_addresses (msg1,
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160 "Testing HELLO creation (with one address)...\n");
162 msg2 = GNUNET_HELLO_create (&publicKey,
166 GNUNET_assert (msg2 != NULL);
167 GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170 "Testing address iteration (one address)...\n");
172 GNUNET_assert (NULL ==
173 GNUNET_HELLO_iterate_addresses (msg2,
177 GNUNET_assert (i == 0);
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 "Testing get_key from HELLO...\n");
181 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (msg2, &pid));
182 GNUNET_assert (0 == GNUNET_memcmp (&publicKey,
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187 "Testing HELLO creation (with two addresses)...\n");
189 msg3 = GNUNET_HELLO_create (&publicKey,
193 GNUNET_assert (msg3 != NULL);
194 GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 "Testing address iteration (two addresses)...\n");
199 GNUNET_assert (NULL ==
200 GNUNET_HELLO_iterate_addresses (msg3,
204 GNUNET_assert (i == 0);
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 "Testing HELLO merge...\n");
208 msg1 = GNUNET_HELLO_merge (msg2, msg3);
209 GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
212 GNUNET_assert (NULL ==
213 GNUNET_HELLO_iterate_addresses (msg1,
217 GNUNET_assert (i == 0);
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221 "Testing address iteration to copy HELLO...\n");
223 msg1 = GNUNET_HELLO_iterate_addresses (msg3,
227 GNUNET_assert (msg1 != NULL);
228 GNUNET_assert (i == 0);
230 GNUNET_assert (NULL ==
231 GNUNET_HELLO_iterate_addresses (msg1,
235 GNUNET_assert (i == 0);
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239 "Testing delta address iteration...\n");
241 GNUNET_HELLO_iterate_new_addresses (msg3,
246 GNUNET_assert (i == 0);
249 return 0; /* testcase passed */
252 /* end of test_hello.c */