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"
29 my_addr_gen (void *cls, size_t max, void *buf)
31 unsigned int *i = cls;
33 struct GNUNET_HELLO_Address address;
35 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
36 "DEBUG: my_addr_gen called with i = %d\n", *i);
39 memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
40 address.address = "address_information";
41 address.transport_name = "test";
42 address.address_length = *i;
44 GNUNET_HELLO_add_address (&address, GNUNET_TIME_absolute_get (), buf,
52 check_addr (void *cls, const struct GNUNET_HELLO_Address *address,
53 struct GNUNET_TIME_Absolute expiration)
55 unsigned int *i = cls;
57 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
58 "DEBUG: check_addr called with i = %d and addrlen = %u\n",
59 *i, (unsigned int) address->address_length);
60 GNUNET_assert (address->address_length > 0);
61 GNUNET_assert (*i & (1 << (address->address_length - 1)));
62 *i -= (1 << (address->address_length - 1));
64 strncmp ("address_information", address->address,
65 address->address_length));
66 GNUNET_assert (0 == strcmp ("test", address->transport_name));
72 remove_some (void *cls, const struct GNUNET_HELLO_Address *address,
73 struct GNUNET_TIME_Absolute expiration)
75 unsigned int *i = cls;
77 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
78 "DEBUG: remove_some called with i = %d and addrlen = %u\n",
79 *i, (unsigned int) address->address_length);
80 GNUNET_assert (address->address_length > 0);
81 if (*i & (1 << (address->address_length - 1)))
83 *i -= (1 << (address->address_length - 1));
91 main (int argc, char *argv[])
93 struct GNUNET_HELLO_Message *msg1;
94 struct GNUNET_HELLO_Message *msg2;
95 struct GNUNET_HELLO_Message *msg3;
96 struct GNUNET_CRYPTO_EddsaPublicKey publicKey;
97 struct GNUNET_CRYPTO_EddsaPublicKey pk;
98 struct GNUNET_TIME_Absolute startup_time;
101 GNUNET_log_setup ("test-hello", "DEBUG", NULL);
102 startup_time = GNUNET_TIME_absolute_get ();
103 memset (&publicKey, 42, sizeof (publicKey));
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
105 "Testing HELLO creation (without addresses)...\n");
107 msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
108 GNUNET_assert (msg1 != NULL);
109 GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
112 "Testing address iteration (empty set)...\n");
113 GNUNET_assert (NULL ==
114 GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
117 "Testing HELLO creation (with one address)...\n");
119 msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
120 GNUNET_assert (msg2 != NULL);
121 GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
124 "Testing address iteration (one address)...\n");
126 GNUNET_assert (NULL ==
127 GNUNET_HELLO_iterate_addresses (msg2, GNUNET_NO, &check_addr,
129 GNUNET_assert (i == 0);
131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132 "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 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
138 "Testing HELLO creation (with two addresses)...\n");
140 msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
141 GNUNET_assert (msg3 != NULL);
142 GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145 "Testing address iteration (two addresses)...\n");
147 GNUNET_assert (NULL ==
148 GNUNET_HELLO_iterate_addresses (msg3, GNUNET_NO, &check_addr,
150 GNUNET_assert (i == 0);
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153 "Testing HELLO merge...\n");
154 msg1 = GNUNET_HELLO_merge (msg2, msg3);
155 GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
158 GNUNET_assert (NULL ==
159 GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
161 GNUNET_assert (i == 0);
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165 "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, GNUNET_NO, &check_addr,
174 GNUNET_assert (i == 0);
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178 "Testing delta address iteration...\n");
180 GNUNET_HELLO_iterate_new_addresses (msg3, msg2, startup_time, &check_addr,
182 GNUNET_assert (i == 0);
185 return 0; /* testcase passed */