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 2, 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"
31 my_addr_gen (void *cls, size_t max, void *buf)
33 unsigned int *i = cls;
37 fprintf (stderr, "DEBUG: my_addr_gen called with i = %d\n", *i);
41 ret = GNUNET_HELLO_add_address ("test",
42 GNUNET_TIME_absolute_get (),
43 "address_information", *i, buf, max);
50 check_addr (void *cls,
52 struct GNUNET_TIME_Absolute expiration,
53 const void *addr, size_t addrlen)
55 unsigned int *i = cls;
58 fprintf (stderr, "DEBUG: check_addr called with i = %d and addrlen = %u\n",
61 GNUNET_assert (addrlen > 0);
62 GNUNET_assert (*i & (1 << (addrlen - 1)));
63 *i -= (1 << (addrlen - 1));
64 GNUNET_assert (0 == strncmp ("address_information", addr, addrlen));
65 GNUNET_assert (0 == strcmp ("test", tname));
71 remove_some (void *cls,
73 struct GNUNET_TIME_Absolute expiration,
74 const void *addr, size_t addrlen)
76 unsigned int *i = cls;
79 fprintf (stderr, "DEBUG: remove_some called with i = %d and addrlen = %u\n",
82 GNUNET_assert (addrlen > 0);
83 if (*i & (1 << (addrlen - 1)))
85 *i -= (1 << (addrlen - 1));
93 main (int argc, char *argv[])
95 struct GNUNET_HELLO_Message *msg1;
96 struct GNUNET_HELLO_Message *msg2;
97 struct GNUNET_HELLO_Message *msg3;
98 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
99 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
100 struct GNUNET_TIME_Absolute startup_time;
104 GNUNET_log_setup ("test-hello", "DEBUG", NULL);
105 startup_time = GNUNET_TIME_absolute_get ();
107 memset (&publicKey, 42, sizeof (publicKey));
108 fprintf (stderr, "Testing HELLO creation (without addresses)...\n");
110 msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
111 GNUNET_assert (msg1 != NULL);
112 GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
114 fprintf (stderr, "Testing address iteration (empty set)...\n");
115 GNUNET_assert (NULL ==
116 GNUNET_HELLO_iterate_addresses (msg1,
117 GNUNET_NO, &check_addr, &i));
119 fprintf (stderr, "Testing HELLO creation (with one address)...\n");
121 msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
122 GNUNET_assert (msg2 != NULL);
123 GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
125 fprintf (stderr, "Testing address iteration (one address)...\n");
127 GNUNET_assert (NULL ==
128 GNUNET_HELLO_iterate_addresses (msg2,
129 GNUNET_NO, &check_addr, &i));
130 GNUNET_assert (i == 0);
132 fprintf (stderr, "Testing get_key from HELLO...\n");
133 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (msg2, &pk));
134 GNUNET_assert (0 == memcmp (&publicKey, &pk, sizeof (pk)));
137 fprintf (stderr, "Testing HELLO creation (with two addresses)...\n");
139 msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
140 GNUNET_assert (msg3 != NULL);
141 GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
143 fprintf (stderr, "Testing address iteration (two addresses)...\n");
145 GNUNET_assert (NULL ==
146 GNUNET_HELLO_iterate_addresses (msg3,
147 GNUNET_NO, &check_addr, &i));
148 GNUNET_assert (i == 0);
150 fprintf (stderr, "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,
157 GNUNET_NO, &check_addr, &i));
158 GNUNET_assert (i == 0);
161 fprintf (stderr, "Testing address iteration to copy HELLO...\n");
163 msg1 = GNUNET_HELLO_iterate_addresses (msg3, GNUNET_YES, &remove_some, &i);
164 GNUNET_assert (msg1 != NULL);
165 GNUNET_assert (i == 0);
167 GNUNET_assert (NULL ==
168 GNUNET_HELLO_iterate_addresses (msg1,
169 GNUNET_NO, &check_addr, &i));
170 GNUNET_assert (i == 0);
173 fprintf (stderr, "Testing delta address iteration...\n");
175 GNUNET_HELLO_iterate_new_addresses (msg3,
176 msg2, startup_time, &check_addr, &i);
177 GNUNET_assert (i == 0);
180 return 0; /* testcase passed */