2 This file is part of GNUnet
3 Copyright (C) 2009 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_friend_hello.c
20 * @brief test for hello.c
21 * @author Christian Grothoff
24 #include "gnunet_hello_lib.h"
28 my_addr_gen (void *cls,
32 unsigned int *i = cls;
34 struct GNUNET_HELLO_Address address;
36 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
37 "DEBUG: my_addr_gen called with i = %d\n", *i);
39 return GNUNET_SYSERR; /* Stop iteration */
40 memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
41 address.address = "address_information";
42 address.transport_name = "test";
43 address.address_length = *i;
45 GNUNET_HELLO_add_address (&address, GNUNET_TIME_absolute_get (), buf,
53 check_addr (void *cls,
54 const struct GNUNET_HELLO_Address *address,
55 struct GNUNET_TIME_Absolute expiration)
57 unsigned int *i = cls;
59 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
60 "DEBUG: check_addr called with i = %d and addrlen = %u\n",
61 *i, (unsigned int) address->address_length);
62 GNUNET_assert (address->address_length > 0);
63 GNUNET_assert (*i & (1 << (address->address_length - 1)));
64 *i -= (1 << (address->address_length - 1));
66 strncmp ("address_information", address->address,
67 address->address_length));
68 GNUNET_assert (0 == strcmp ("test", address->transport_name));
74 remove_some (void *cls,
75 const struct GNUNET_HELLO_Address *address,
76 struct GNUNET_TIME_Absolute expiration)
78 unsigned int *i = cls;
80 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81 "DEBUG: remove_some called with i = %d and addrlen = %u\n",
82 *i, (unsigned int) address->address_length);
83 GNUNET_assert (address->address_length > 0);
84 if (*i & (1 << (address->address_length - 1)))
86 *i -= (1 << (address->address_length - 1));
94 main (int argc, char *argv[])
96 struct GNUNET_HELLO_Message *msg1;
97 struct GNUNET_HELLO_Message *msg2;
98 struct GNUNET_HELLO_Message *msg3;
99 struct GNUNET_CRYPTO_EddsaPublicKey publicKey;
100 struct GNUNET_TIME_Absolute startup_time;
103 GNUNET_log_setup ("test-hello", "DEBUG", NULL);
104 startup_time = GNUNET_TIME_absolute_get ();
105 memset (&publicKey, 42, sizeof (publicKey));
106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
107 "Testing HELLO creation (without addresses)...\n");
109 msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
110 GNUNET_assert (msg1 != NULL);
111 GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
114 "Testing address iteration (empty set)...\n");
115 GNUNET_assert (NULL ==
116 GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
119 "Testing HELLO creation (with one address)...\n");
121 msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
122 GNUNET_assert (msg2 != NULL);
123 GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
126 "Testing address iteration (one address)...\n");
128 GNUNET_assert (NULL ==
129 GNUNET_HELLO_iterate_addresses (msg2, GNUNET_NO, &check_addr,
131 GNUNET_assert (i == 0);
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Testing HELLO creation (with two addresses)...\n");
137 msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
138 GNUNET_assert (msg3 != NULL);
139 GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
142 "Testing address iteration (two addresses)...\n");
144 GNUNET_assert (NULL ==
145 GNUNET_HELLO_iterate_addresses (msg3, GNUNET_NO, &check_addr,
147 GNUNET_assert (i == 0);
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150 "Testing HELLO merge...\n");
151 msg1 = GNUNET_HELLO_merge (msg2, msg3);
152 GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
155 GNUNET_assert (NULL ==
156 GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
158 GNUNET_assert (i == 0);
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
162 "Testing address iteration to copy HELLO...\n");
164 msg1 = GNUNET_HELLO_iterate_addresses (msg3, GNUNET_YES, &remove_some, &i);
165 GNUNET_assert (msg1 != NULL);
166 GNUNET_assert (i == 0);
168 GNUNET_assert (NULL ==
169 GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
171 GNUNET_assert (i == 0);
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 "Testing delta address iteration...\n");
177 GNUNET_HELLO_iterate_new_addresses (msg3, msg2, startup_time, &check_addr,
179 GNUNET_assert (i == 0);
182 return 0; /* testcase passed */