2 This file is part of GNUnet
3 (C) 2009 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file hello/test_hello.c
22 * @brief test for hello.c
23 * @author Christian Grothoff
26 #include "gnunet_hello_lib.h"
28 #define DEBUG GNUNET_NO
30 #define VERBOSE GNUNET_NO
34 my_addr_gen (void *cls, size_t max, void *buf)
36 unsigned int *i = cls;
38 struct GNUNET_HELLO_Address address;
41 FPRINTF (stderr, "DEBUG: my_addr_gen called with i = %d\n", *i);
45 memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
46 address.address = "address_information";
47 address.transport_name = "test";
48 address.address_length = *i;
50 GNUNET_HELLO_add_address (&address, GNUNET_TIME_absolute_get (), buf,
58 check_addr (void *cls, const struct GNUNET_HELLO_Address *address,
59 struct GNUNET_TIME_Absolute expiration)
61 unsigned int *i = cls;
64 FPRINTF (stderr, "DEBUG: check_addr called with i = %d and addrlen = %u\n",
65 *i, (unsigned int) address->address_length);
67 GNUNET_assert (address->address_length > 0);
68 GNUNET_assert (*i & (1 << (address->address_length - 1)));
69 *i -= (1 << (address->address_length - 1));
71 strncmp ("address_information", address->address,
72 address->address_length));
73 GNUNET_assert (0 == strcmp ("test", address->transport_name));
79 remove_some (void *cls, const struct GNUNET_HELLO_Address *address,
80 struct GNUNET_TIME_Absolute expiration)
82 unsigned int *i = cls;
85 FPRINTF (stderr, "DEBUG: remove_some called with i = %d and addrlen = %u\n",
86 *i, (unsigned int) address->address_length);
88 GNUNET_assert (address->address_length > 0);
89 if (*i & (1 << (address->address_length - 1)))
91 *i -= (1 << (address->address_length - 1));
99 main (int argc, char *argv[])
101 struct GNUNET_HELLO_Message *msg1;
102 struct GNUNET_HELLO_Message *msg2;
103 struct GNUNET_HELLO_Message *msg3;
104 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
105 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
106 struct GNUNET_TIME_Absolute startup_time;
109 GNUNET_log_setup ("test-hello", "DEBUG", NULL);
110 startup_time = GNUNET_TIME_absolute_get ();
111 memset (&publicKey, 42, sizeof (publicKey));
113 FPRINTF (stderr, "%s", "Testing HELLO creation (without addresses)...\n");
116 msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
117 GNUNET_assert (msg1 != NULL);
118 GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
121 FPRINTF (stderr, "%s", "Testing address iteration (empty set)...\n");
123 GNUNET_assert (NULL ==
124 GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
128 FPRINTF (stderr, "%s", "Testing HELLO creation (with one address)...\n");
131 msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
132 GNUNET_assert (msg2 != NULL);
133 GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
136 FPRINTF (stderr, "%s", "Testing address iteration (one address)...\n");
139 GNUNET_assert (NULL ==
140 GNUNET_HELLO_iterate_addresses (msg2, GNUNET_NO, &check_addr,
142 GNUNET_assert (i == 0);
145 FPRINTF (stderr, "%s", "Testing get_key from HELLO...\n");
147 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (msg2, &pk));
148 GNUNET_assert (0 == memcmp (&publicKey, &pk, sizeof (pk)));
152 FPRINTF (stderr, "%s", "Testing HELLO creation (with two addresses)...\n");
155 msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
156 GNUNET_assert (msg3 != NULL);
157 GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
160 FPRINTF (stderr, "%s", "Testing address iteration (two addresses)...\n");
163 GNUNET_assert (NULL ==
164 GNUNET_HELLO_iterate_addresses (msg3, GNUNET_NO, &check_addr,
166 GNUNET_assert (i == 0);
169 FPRINTF (stderr, "%s", "Testing HELLO merge...\n");
171 msg1 = GNUNET_HELLO_merge (msg2, msg3);
172 GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
175 GNUNET_assert (NULL ==
176 GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
178 GNUNET_assert (i == 0);
182 FPRINTF (stderr, "%s", "Testing address iteration to copy HELLO...\n");
185 msg1 = GNUNET_HELLO_iterate_addresses (msg3, GNUNET_YES, &remove_some, &i);
186 GNUNET_assert (msg1 != NULL);
187 GNUNET_assert (i == 0);
189 GNUNET_assert (NULL ==
190 GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
192 GNUNET_assert (i == 0);
196 FPRINTF (stderr, "%s", "Testing delta address iteration...\n");
199 GNUNET_HELLO_iterate_new_addresses (msg3, msg2, startup_time, &check_addr,
201 GNUNET_assert (i == 0);
204 return 0; /* testcase passed */