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.
16 * @file hello/test_hello.c
17 * @brief test for hello.c
18 * @author Christian Grothoff
21 #include "gnunet_hello_lib.h"
33 my_addr_gen (void *cls,
37 unsigned int *i = cls;
39 struct GNUNET_HELLO_Address address;
41 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
42 "DEBUG: my_addr_gen called with i = %d\n",
46 memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
47 address.address = "address_information";
48 address.transport_name = "test";
49 address.address_length = *i;
50 ret = GNUNET_HELLO_add_address (&address,
51 GNUNET_TIME_absolute_get (),
68 check_addr (void *cls,
69 const struct GNUNET_HELLO_Address *address,
70 struct GNUNET_TIME_Absolute expiration)
72 unsigned int *i = cls;
74 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
75 "DEBUG: check_addr called with i = %d and addrlen = %u\n",
77 (unsigned int) address->address_length);
78 GNUNET_assert (address->address_length > 0);
79 GNUNET_assert (*i & (1 << (address->address_length - 1)));
80 *i -= (1 << (address->address_length - 1));
82 strncmp ("address_information",
84 address->address_length));
85 GNUNET_assert (0 == strcmp ("test",
86 address->transport_name));
100 remove_some (void *cls,
101 const struct GNUNET_HELLO_Address *address,
102 struct GNUNET_TIME_Absolute expiration)
104 unsigned int *i = cls;
106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
107 "DEBUG: remove_some called with i = %d and addrlen = %u\n",
109 (unsigned int) address->address_length);
110 GNUNET_assert (address->address_length > 0);
111 if (*i & (1 << (address->address_length - 1)))
113 *i -= (1 << (address->address_length - 1));
124 struct GNUNET_HELLO_Message *msg1;
125 struct GNUNET_HELLO_Message *msg2;
126 struct GNUNET_HELLO_Message *msg3;
127 struct GNUNET_CRYPTO_EddsaPublicKey publicKey;
128 struct GNUNET_PeerIdentity pid;
129 struct GNUNET_TIME_Absolute startup_time;
132 GNUNET_log_setup ("test-hello",
135 startup_time = GNUNET_TIME_absolute_get ();
136 memset (&publicKey, 42, sizeof (publicKey));
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
138 "Testing HELLO creation (without addresses)...\n");
140 msg1 = GNUNET_HELLO_create (&publicKey,
144 GNUNET_assert (msg1 != NULL);
145 GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148 "Testing address iteration (empty set)...\n");
149 GNUNET_assert (NULL ==
150 GNUNET_HELLO_iterate_addresses (msg1,
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 "Testing HELLO creation (with one address)...\n");
157 msg2 = GNUNET_HELLO_create (&publicKey,
161 GNUNET_assert (msg2 != NULL);
162 GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165 "Testing address iteration (one address)...\n");
167 GNUNET_assert (NULL ==
168 GNUNET_HELLO_iterate_addresses (msg2,
172 GNUNET_assert (i == 0);
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 "Testing get_key from HELLO...\n");
176 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (msg2, &pid));
177 GNUNET_assert (0 == memcmp (&publicKey,
179 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)));
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183 "Testing HELLO creation (with two addresses)...\n");
185 msg3 = GNUNET_HELLO_create (&publicKey,
189 GNUNET_assert (msg3 != NULL);
190 GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
193 "Testing address iteration (two addresses)...\n");
195 GNUNET_assert (NULL ==
196 GNUNET_HELLO_iterate_addresses (msg3,
200 GNUNET_assert (i == 0);
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 "Testing HELLO merge...\n");
204 msg1 = GNUNET_HELLO_merge (msg2, msg3);
205 GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
208 GNUNET_assert (NULL ==
209 GNUNET_HELLO_iterate_addresses (msg1,
213 GNUNET_assert (i == 0);
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "Testing address iteration to copy HELLO...\n");
219 msg1 = GNUNET_HELLO_iterate_addresses (msg3,
223 GNUNET_assert (msg1 != NULL);
224 GNUNET_assert (i == 0);
226 GNUNET_assert (NULL ==
227 GNUNET_HELLO_iterate_addresses (msg1,
231 GNUNET_assert (i == 0);
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235 "Testing delta address iteration...\n");
237 GNUNET_HELLO_iterate_new_addresses (msg3,
242 GNUNET_assert (i == 0);
245 return 0; /* testcase passed */
248 /* end of test_hello.c */