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/>.
19 * @file hello/test_hello.c
20 * @brief test for hello.c
21 * @author Christian Grothoff
24 #include "gnunet_hello_lib.h"
36 my_addr_gen (void *cls,
40 unsigned int *i = cls;
42 struct GNUNET_HELLO_Address address;
44 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
45 "DEBUG: my_addr_gen called with i = %d\n",
49 memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
50 address.address = "address_information";
51 address.transport_name = "test";
52 address.address_length = *i;
53 ret = GNUNET_HELLO_add_address (&address,
54 GNUNET_TIME_absolute_get (),
71 check_addr (void *cls,
72 const struct GNUNET_HELLO_Address *address,
73 struct GNUNET_TIME_Absolute expiration)
75 unsigned int *i = cls;
77 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
78 "DEBUG: check_addr called with i = %d and addrlen = %u\n",
80 (unsigned int) address->address_length);
81 GNUNET_assert (address->address_length > 0);
82 GNUNET_assert (*i & (1 << (address->address_length - 1)));
83 *i -= (1 << (address->address_length - 1));
85 strncmp ("address_information",
87 address->address_length));
88 GNUNET_assert (0 == strcmp ("test",
89 address->transport_name));
103 remove_some (void *cls,
104 const struct GNUNET_HELLO_Address *address,
105 struct GNUNET_TIME_Absolute expiration)
107 unsigned int *i = cls;
109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
110 "DEBUG: remove_some called with i = %d and addrlen = %u\n",
112 (unsigned int) address->address_length);
113 GNUNET_assert (address->address_length > 0);
114 if (*i & (1 << (address->address_length - 1)))
116 *i -= (1 << (address->address_length - 1));
127 struct GNUNET_HELLO_Message *msg1;
128 struct GNUNET_HELLO_Message *msg2;
129 struct GNUNET_HELLO_Message *msg3;
130 struct GNUNET_CRYPTO_EddsaPublicKey publicKey;
131 struct GNUNET_PeerIdentity pid;
132 struct GNUNET_TIME_Absolute startup_time;
135 GNUNET_log_setup ("test-hello",
138 startup_time = GNUNET_TIME_absolute_get ();
139 memset (&publicKey, 42, sizeof (publicKey));
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
141 "Testing HELLO creation (without addresses)...\n");
143 msg1 = GNUNET_HELLO_create (&publicKey,
147 GNUNET_assert (msg1 != NULL);
148 GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151 "Testing address iteration (empty set)...\n");
152 GNUNET_assert (NULL ==
153 GNUNET_HELLO_iterate_addresses (msg1,
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "Testing HELLO creation (with one address)...\n");
160 msg2 = GNUNET_HELLO_create (&publicKey,
164 GNUNET_assert (msg2 != NULL);
165 GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "Testing address iteration (one address)...\n");
170 GNUNET_assert (NULL ==
171 GNUNET_HELLO_iterate_addresses (msg2,
175 GNUNET_assert (i == 0);
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178 "Testing get_key from HELLO...\n");
179 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (msg2, &pid));
180 GNUNET_assert (0 == memcmp (&publicKey,
182 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)));
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 "Testing HELLO creation (with two addresses)...\n");
188 msg3 = GNUNET_HELLO_create (&publicKey,
192 GNUNET_assert (msg3 != NULL);
193 GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196 "Testing address iteration (two addresses)...\n");
198 GNUNET_assert (NULL ==
199 GNUNET_HELLO_iterate_addresses (msg3,
203 GNUNET_assert (i == 0);
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "Testing HELLO merge...\n");
207 msg1 = GNUNET_HELLO_merge (msg2, msg3);
208 GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
211 GNUNET_assert (NULL ==
212 GNUNET_HELLO_iterate_addresses (msg1,
216 GNUNET_assert (i == 0);
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220 "Testing address iteration to copy HELLO...\n");
222 msg1 = GNUNET_HELLO_iterate_addresses (msg3,
226 GNUNET_assert (msg1 != NULL);
227 GNUNET_assert (i == 0);
229 GNUNET_assert (NULL ==
230 GNUNET_HELLO_iterate_addresses (msg1,
234 GNUNET_assert (i == 0);
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 "Testing delta address iteration...\n");
240 GNUNET_HELLO_iterate_new_addresses (msg3,
245 GNUNET_assert (i == 0);
248 return 0; /* testcase passed */
251 /* end of test_hello.c */