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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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 == memcmp (&publicKey,
184 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)));
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188 "Testing HELLO creation (with two addresses)...\n");
190 msg3 = GNUNET_HELLO_create (&publicKey,
194 GNUNET_assert (msg3 != NULL);
195 GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "Testing address iteration (two addresses)...\n");
200 GNUNET_assert (NULL ==
201 GNUNET_HELLO_iterate_addresses (msg3,
205 GNUNET_assert (i == 0);
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208 "Testing HELLO merge...\n");
209 msg1 = GNUNET_HELLO_merge (msg2, msg3);
210 GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
213 GNUNET_assert (NULL ==
214 GNUNET_HELLO_iterate_addresses (msg1,
218 GNUNET_assert (i == 0);
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "Testing address iteration to copy HELLO...\n");
224 msg1 = GNUNET_HELLO_iterate_addresses (msg3,
228 GNUNET_assert (msg1 != NULL);
229 GNUNET_assert (i == 0);
231 GNUNET_assert (NULL ==
232 GNUNET_HELLO_iterate_addresses (msg1,
236 GNUNET_assert (i == 0);
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240 "Testing delta address iteration...\n");
242 GNUNET_HELLO_iterate_new_addresses (msg3,
247 GNUNET_assert (i == 0);
250 return 0; /* testcase passed */
253 /* end of test_hello.c */