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
27 * Testcase for HELLO code.
30 #include "gnunet_hello_lib.h"
35 my_addr_gen (void *cls, size_t max, void *buf)
37 unsigned int *i = cls;
41 fprintf (stderr, "DEBUG: my_addr_gen called with i = %d\n", *i);
45 ret = GNUNET_HELLO_add_address ("test",
46 GNUNET_TIME_absolute_get (),
47 "address_information", *i, buf, max);
54 check_addr (void *cls,
56 struct GNUNET_TIME_Absolute expiration,
57 const void *addr, size_t addrlen)
59 unsigned int *i = cls;
62 fprintf (stderr, "DEBUG: check_addr called with i = %d and addrlen = %u\n",
65 GNUNET_assert (addrlen > 0);
66 GNUNET_assert (*i & (1 << (addrlen - 1)));
67 *i -= (1 << (addrlen - 1));
68 GNUNET_assert (0 == strncmp ("address_information", addr, addrlen));
69 GNUNET_assert (0 == strcmp ("test", tname));
75 remove_some (void *cls,
77 struct GNUNET_TIME_Absolute expiration,
78 const void *addr, size_t addrlen)
80 unsigned int *i = cls;
83 fprintf (stderr, "DEBUG: remove_some called with i = %d and addrlen = %u\n",
86 GNUNET_assert (addrlen > 0);
87 if (*i & (1 << (addrlen - 1)))
89 *i -= (1 << (addrlen - 1));
97 main (int argc, char *argv[])
99 struct GNUNET_HELLO_Message *msg1;
100 struct GNUNET_HELLO_Message *msg2;
101 struct GNUNET_HELLO_Message *msg3;
102 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
103 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
104 struct GNUNET_TIME_Absolute startup_time;
108 GNUNET_log_setup ("test-hello", "DEBUG", NULL);
109 startup_time = GNUNET_TIME_absolute_get ();
111 memset (&publicKey, 42, sizeof (publicKey));
112 fprintf (stderr, "Testing HELLO creation (without addresses)...\n");
114 msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
115 GNUNET_assert (msg1 != NULL);
116 GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
118 fprintf (stderr, "Testing address iteration (empty set)...\n");
119 GNUNET_assert (NULL ==
120 GNUNET_HELLO_iterate_addresses (msg1,
121 GNUNET_NO, &check_addr, &i));
123 fprintf (stderr, "Testing HELLO creation (with one address)...\n");
125 msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
126 GNUNET_assert (msg2 != NULL);
127 GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
129 fprintf (stderr, "Testing address iteration (one address)...\n");
131 GNUNET_assert (NULL ==
132 GNUNET_HELLO_iterate_addresses (msg2,
133 GNUNET_NO, &check_addr, &i));
134 GNUNET_assert (i == 0);
136 fprintf (stderr, "Testing get_key from HELLO...\n");
137 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (msg2, &pk));
138 GNUNET_assert (0 == memcmp (&publicKey, &pk, sizeof (pk)));
141 fprintf (stderr, "Testing HELLO creation (with two addresses)...\n");
143 msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
144 GNUNET_assert (msg3 != NULL);
145 GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
147 fprintf (stderr, "Testing address iteration (two addresses)...\n");
149 GNUNET_assert (NULL ==
150 GNUNET_HELLO_iterate_addresses (msg3,
151 GNUNET_NO, &check_addr, &i));
152 GNUNET_assert (i == 0);
154 fprintf (stderr, "Testing HELLO merge...\n");
155 msg1 = GNUNET_HELLO_merge (msg2, msg3);
156 GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
159 GNUNET_assert (NULL ==
160 GNUNET_HELLO_iterate_addresses (msg1,
161 GNUNET_NO, &check_addr, &i));
162 GNUNET_assert (i == 0);
165 fprintf (stderr, "Testing address iteration to copy HELLO...\n");
167 msg1 = GNUNET_HELLO_iterate_addresses (msg3, GNUNET_YES, &remove_some, &i);
168 GNUNET_assert (msg1 != NULL);
169 GNUNET_assert (i == 0);
171 GNUNET_assert (NULL ==
172 GNUNET_HELLO_iterate_addresses (msg1,
173 GNUNET_NO, &check_addr, &i));
174 GNUNET_assert (i == 0);
177 fprintf (stderr, "Testing delta address iteration...\n");
179 GNUNET_HELLO_iterate_new_addresses (msg3,
180 msg2, startup_time, &check_addr, &i);
181 GNUNET_assert (i == 0);
184 return 0; /* testcase passed */