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;
40 fprintf (stderr, "DEBUG: my_addr_gen called with i = %d\n", *i);
45 GNUNET_HELLO_add_address ("test", GNUNET_TIME_absolute_get (),
46 "address_information", *i, buf, max);
53 check_addr (void *cls, const char *tname,
54 struct GNUNET_TIME_Absolute expiration, const void *addr,
57 unsigned int *i = cls;
60 fprintf (stderr, "DEBUG: check_addr called with i = %d and addrlen = %u\n",
63 GNUNET_assert (addrlen > 0);
64 GNUNET_assert (*i & (1 << (addrlen - 1)));
65 *i -= (1 << (addrlen - 1));
66 GNUNET_assert (0 == strncmp ("address_information", addr, addrlen));
67 GNUNET_assert (0 == strcmp ("test", tname));
73 remove_some (void *cls, const char *tname,
74 struct GNUNET_TIME_Absolute expiration, const void *addr,
77 unsigned int *i = cls;
80 fprintf (stderr, "DEBUG: remove_some called with i = %d and addrlen = %u\n",
83 GNUNET_assert (addrlen > 0);
84 if (*i & (1 << (addrlen - 1)))
86 *i -= (1 << (addrlen - 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_RsaPublicKeyBinaryEncoded publicKey;
100 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
101 struct GNUNET_TIME_Absolute startup_time;
104 GNUNET_log_setup ("test-hello", "DEBUG", NULL);
105 startup_time = GNUNET_TIME_absolute_get ();
106 memset (&publicKey, 42, sizeof (publicKey));
108 fprintf (stderr, "Testing HELLO creation (without addresses)...\n");
111 msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
112 GNUNET_assert (msg1 != NULL);
113 GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
116 fprintf (stderr, "Testing address iteration (empty set)...\n");
118 GNUNET_assert (NULL ==
119 GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
123 fprintf (stderr, "Testing HELLO creation (with one address)...\n");
126 msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
127 GNUNET_assert (msg2 != NULL);
128 GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
131 fprintf (stderr, "Testing address iteration (one address)...\n");
134 GNUNET_assert (NULL ==
135 GNUNET_HELLO_iterate_addresses (msg2, GNUNET_NO, &check_addr,
137 GNUNET_assert (i == 0);
140 fprintf (stderr, "Testing get_key from HELLO...\n");
142 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (msg2, &pk));
143 GNUNET_assert (0 == memcmp (&publicKey, &pk, sizeof (pk)));
147 fprintf (stderr, "Testing HELLO creation (with two addresses)...\n");
150 msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
151 GNUNET_assert (msg3 != NULL);
152 GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
155 fprintf (stderr, "Testing address iteration (two addresses)...\n");
158 GNUNET_assert (NULL ==
159 GNUNET_HELLO_iterate_addresses (msg3, GNUNET_NO, &check_addr,
161 GNUNET_assert (i == 0);
164 fprintf (stderr, "Testing HELLO merge...\n");
166 msg1 = GNUNET_HELLO_merge (msg2, msg3);
167 GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
170 GNUNET_assert (NULL ==
171 GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
173 GNUNET_assert (i == 0);
177 fprintf (stderr, "Testing address iteration to copy HELLO...\n");
180 msg1 = GNUNET_HELLO_iterate_addresses (msg3, GNUNET_YES, &remove_some, &i);
181 GNUNET_assert (msg1 != NULL);
182 GNUNET_assert (i == 0);
184 GNUNET_assert (NULL ==
185 GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
187 GNUNET_assert (i == 0);
191 fprintf (stderr, "Testing delta address iteration...\n");
194 GNUNET_HELLO_iterate_new_addresses (msg3, msg2, startup_time, &check_addr,
196 GNUNET_assert (i == 0);
199 return 0; /* testcase passed */