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);
44 ret = GNUNET_HELLO_add_address ("test",
45 GNUNET_TIME_absolute_get (),
46 "address_information", *i, buf, max);
53 check_addr (void *cls,
55 struct GNUNET_TIME_Absolute expiration,
56 const void *addr, uint16_t addrlen)
58 unsigned int *i = cls;
61 fprintf (stderr, "DEBUG: check_addr called with i = %d and addrlen = %u\n",
64 GNUNET_assert (addrlen > 0);
65 GNUNET_assert (*i & (1 << (addrlen - 1)));
66 *i -= (1 << (addrlen - 1));
67 GNUNET_assert (0 == strncmp ("address_information", addr, addrlen));
68 GNUNET_assert (0 == strcmp ("test", tname));
74 remove_some (void *cls,
76 struct GNUNET_TIME_Absolute expiration,
77 const void *addr, uint16_t addrlen)
79 unsigned int *i = cls;
82 fprintf (stderr, "DEBUG: remove_some called with i = %d and addrlen = %u\n",
85 GNUNET_assert (addrlen > 0);
86 if (*i & (1 << (addrlen - 1)))
88 *i -= (1 << (addrlen - 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_RsaPublicKeyBinaryEncoded publicKey;
102 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
103 struct GNUNET_TIME_Absolute startup_time;
106 GNUNET_log_setup ("test-hello", "DEBUG", NULL);
107 startup_time = GNUNET_TIME_absolute_get ();
108 memset (&publicKey, 42, sizeof (publicKey));
110 fprintf (stderr, "Testing HELLO creation (without addresses)...\n");
113 msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
114 GNUNET_assert (msg1 != NULL);
115 GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
118 fprintf (stderr, "Testing address iteration (empty set)...\n");
120 GNUNET_assert (NULL ==
121 GNUNET_HELLO_iterate_addresses (msg1,
122 GNUNET_NO, &check_addr, &i));
125 fprintf (stderr, "Testing HELLO creation (with one address)...\n");
128 msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
129 GNUNET_assert (msg2 != NULL);
130 GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
133 fprintf (stderr, "Testing address iteration (one address)...\n");
136 GNUNET_assert (NULL ==
137 GNUNET_HELLO_iterate_addresses (msg2,
138 GNUNET_NO, &check_addr, &i));
139 GNUNET_assert (i == 0);
142 fprintf (stderr, "Testing get_key from HELLO...\n");
144 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (msg2, &pk));
145 GNUNET_assert (0 == memcmp (&publicKey, &pk, sizeof (pk)));
149 fprintf (stderr, "Testing HELLO creation (with two addresses)...\n");
152 msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
153 GNUNET_assert (msg3 != NULL);
154 GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
157 fprintf (stderr, "Testing address iteration (two addresses)...\n");
160 GNUNET_assert (NULL ==
161 GNUNET_HELLO_iterate_addresses (msg3,
162 GNUNET_NO, &check_addr, &i));
163 GNUNET_assert (i == 0);
166 fprintf (stderr, "Testing HELLO merge...\n");
168 msg1 = GNUNET_HELLO_merge (msg2, msg3);
169 GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
172 GNUNET_assert (NULL ==
173 GNUNET_HELLO_iterate_addresses (msg1,
174 GNUNET_NO, &check_addr, &i));
175 GNUNET_assert (i == 0);
179 fprintf (stderr, "Testing address iteration to copy HELLO...\n");
182 msg1 = GNUNET_HELLO_iterate_addresses (msg3, GNUNET_YES, &remove_some, &i);
183 GNUNET_assert (msg1 != NULL);
184 GNUNET_assert (i == 0);
186 GNUNET_assert (NULL ==
187 GNUNET_HELLO_iterate_addresses (msg1,
188 GNUNET_NO, &check_addr, &i));
189 GNUNET_assert (i == 0);
193 fprintf (stderr, "Testing delta address iteration...\n");
196 GNUNET_HELLO_iterate_new_addresses (msg3,
197 msg2, startup_time, &check_addr, &i);
198 GNUNET_assert (i == 0);
201 return 0; /* testcase passed */