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
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_friend_hello.c
22 * @brief test for hello.c
23 * @author Christian Grothoff
26 #include "gnunet_hello_lib.h"
30 my_addr_gen (void *cls,
34 unsigned int *i = cls;
36 struct GNUNET_HELLO_Address address;
38 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
39 "DEBUG: my_addr_gen called with i = %d\n", *i);
41 return GNUNET_SYSERR; /* Stop iteration */
42 memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
43 address.address = "address_information";
44 address.transport_name = "test";
45 address.address_length = *i;
47 GNUNET_HELLO_add_address (&address, GNUNET_TIME_absolute_get (), buf,
55 check_addr (void *cls,
56 const struct GNUNET_HELLO_Address *address,
57 struct GNUNET_TIME_Absolute expiration)
59 unsigned int *i = cls;
61 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
62 "DEBUG: check_addr called with i = %d and addrlen = %u\n",
63 *i, (unsigned int) address->address_length);
64 GNUNET_assert (address->address_length > 0);
65 GNUNET_assert (*i & (1 << (address->address_length - 1)));
66 *i -= (1 << (address->address_length - 1));
68 strncmp ("address_information", address->address,
69 address->address_length));
70 GNUNET_assert (0 == strcmp ("test", address->transport_name));
76 remove_some (void *cls,
77 const struct GNUNET_HELLO_Address *address,
78 struct GNUNET_TIME_Absolute expiration)
80 unsigned int *i = cls;
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
83 "DEBUG: remove_some called with i = %d and addrlen = %u\n",
84 *i, (unsigned int) address->address_length);
85 GNUNET_assert (address->address_length > 0);
86 if (*i & (1 << (address->address_length - 1)))
88 *i -= (1 << (address->address_length - 1));
96 main (int argc, char *argv[])
98 struct GNUNET_HELLO_Message *msg1;
99 struct GNUNET_HELLO_Message *msg2;
100 struct GNUNET_HELLO_Message *msg3;
101 struct GNUNET_CRYPTO_EddsaPublicKey publicKey;
102 struct GNUNET_TIME_Absolute startup_time;
105 GNUNET_log_setup ("test-hello", "DEBUG", NULL);
106 startup_time = GNUNET_TIME_absolute_get ();
107 memset (&publicKey, 42, sizeof (publicKey));
108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109 "Testing HELLO creation (without addresses)...\n");
111 msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
112 GNUNET_assert (msg1 != NULL);
113 GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116 "Testing address iteration (empty set)...\n");
117 GNUNET_assert (NULL ==
118 GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
121 "Testing HELLO creation (with one address)...\n");
123 msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
124 GNUNET_assert (msg2 != NULL);
125 GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128 "Testing address iteration (one address)...\n");
130 GNUNET_assert (NULL ==
131 GNUNET_HELLO_iterate_addresses (msg2, GNUNET_NO, &check_addr,
133 GNUNET_assert (i == 0);
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137 "Testing HELLO creation (with two addresses)...\n");
139 msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
140 GNUNET_assert (msg3 != NULL);
141 GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
144 "Testing address iteration (two addresses)...\n");
146 GNUNET_assert (NULL ==
147 GNUNET_HELLO_iterate_addresses (msg3, GNUNET_NO, &check_addr,
149 GNUNET_assert (i == 0);
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152 "Testing HELLO merge...\n");
153 msg1 = GNUNET_HELLO_merge (msg2, msg3);
154 GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
157 GNUNET_assert (NULL ==
158 GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
160 GNUNET_assert (i == 0);
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164 "Testing address iteration to copy HELLO...\n");
166 msg1 = GNUNET_HELLO_iterate_addresses (msg3, GNUNET_YES, &remove_some, &i);
167 GNUNET_assert (msg1 != NULL);
168 GNUNET_assert (i == 0);
170 GNUNET_assert (NULL ==
171 GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
173 GNUNET_assert (i == 0);
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177 "Testing delta address iteration...\n");
179 GNUNET_HELLO_iterate_new_addresses (msg3, msg2, startup_time, &check_addr,
181 GNUNET_assert (i == 0);
184 return 0; /* testcase passed */