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;
103 GNUNET_log_setup ("test-hello", "DEBUG", NULL);
104 startup_time = GNUNET_TIME_absolute_get ();
105 memset (&publicKey, 42, sizeof (publicKey));
106 fprintf (stderr, "Testing HELLO creation (without addresses)...\n");
108 msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
109 GNUNET_assert (msg1 != NULL);
110 GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
112 fprintf (stderr, "Testing address iteration (empty set)...\n");
113 GNUNET_assert (NULL ==
114 GNUNET_HELLO_iterate_addresses (msg1,
115 GNUNET_NO, &check_addr, &i));
117 fprintf (stderr, "Testing HELLO creation (with one address)...\n");
119 msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
120 GNUNET_assert (msg2 != NULL);
121 GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
123 fprintf (stderr, "Testing address iteration (one address)...\n");
125 GNUNET_assert (NULL ==
126 GNUNET_HELLO_iterate_addresses (msg2,
127 GNUNET_NO, &check_addr, &i));
128 GNUNET_assert (i == 0);
130 fprintf (stderr, "Testing get_key from HELLO...\n");
131 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (msg2, &pk));
132 GNUNET_assert (0 == memcmp (&publicKey, &pk, sizeof (pk)));
135 fprintf (stderr, "Testing HELLO creation (with two addresses)...\n");
137 msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
138 GNUNET_assert (msg3 != NULL);
139 GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
141 fprintf (stderr, "Testing address iteration (two addresses)...\n");
143 GNUNET_assert (NULL ==
144 GNUNET_HELLO_iterate_addresses (msg3,
145 GNUNET_NO, &check_addr, &i));
146 GNUNET_assert (i == 0);
148 fprintf (stderr, "Testing HELLO merge...\n");
149 msg1 = GNUNET_HELLO_merge (msg2, msg3);
150 GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
153 GNUNET_assert (NULL ==
154 GNUNET_HELLO_iterate_addresses (msg1,
155 GNUNET_NO, &check_addr, &i));
156 GNUNET_assert (i == 0);
159 fprintf (stderr, "Testing address iteration to copy HELLO...\n");
161 msg1 = GNUNET_HELLO_iterate_addresses (msg3, GNUNET_YES, &remove_some, &i);
162 GNUNET_assert (msg1 != NULL);
163 GNUNET_assert (i == 0);
165 GNUNET_assert (NULL ==
166 GNUNET_HELLO_iterate_addresses (msg1,
167 GNUNET_NO, &check_addr, &i));
168 GNUNET_assert (i == 0);
171 fprintf (stderr, "Testing delta address iteration...\n");
173 GNUNET_HELLO_iterate_new_addresses (msg3,
174 msg2, startup_time, &check_addr, &i);
175 GNUNET_assert (i == 0);
178 return 0; /* testcase passed */