-dce
[oweals/gnunet.git] / src / hello / test_hello.c
1 /*
2      This file is part of GNUnet
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file hello/test_hello.c
22  * @brief test for hello.c
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_hello_lib.h"
27
28 #define DEBUG GNUNET_EXTRA_LOGGING
29
30 #define VERBOSE GNUNET_EXTRA_LOGGING
31
32
33 static size_t
34 my_addr_gen (void *cls, size_t max, void *buf)
35 {
36   unsigned int *i = cls;
37   size_t ret;
38   struct GNUNET_HELLO_Address address;
39
40 #if DEBUG
41   FPRINTF (stderr, "DEBUG: my_addr_gen called with i = %d\n", *i);
42 #endif
43   if (0 == *i)
44     return 0;
45   memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
46   address.address = "address_information";
47   address.transport_name = "test";
48   address.address_length = *i;
49   ret =
50       GNUNET_HELLO_add_address (&address, GNUNET_TIME_absolute_get (), buf,
51                                 max);
52   (*i)--;
53   return ret;
54 }
55
56
57 static int
58 check_addr (void *cls, const struct GNUNET_HELLO_Address *address,
59             struct GNUNET_TIME_Absolute expiration)
60 {
61   unsigned int *i = cls;
62
63 #if DEBUG
64   FPRINTF (stderr, "DEBUG: check_addr called with i = %d and addrlen = %u\n",
65            *i, address->address_length);
66 #endif
67   GNUNET_assert (address->address_length > 0);
68   GNUNET_assert (*i & (1 << (address->address_length - 1)));
69   *i -= (1 << (address->address_length - 1));
70   GNUNET_assert (0 ==
71                  strncmp ("address_information", address->address,
72                           address->address_length));
73   GNUNET_assert (0 == strcmp ("test", address->transport_name));
74   return GNUNET_OK;
75 }
76
77
78 static int
79 remove_some (void *cls, const struct GNUNET_HELLO_Address *address,
80              struct GNUNET_TIME_Absolute expiration)
81 {
82   unsigned int *i = cls;
83
84 #if DEBUG
85   FPRINTF (stderr, "DEBUG: remove_some called with i = %d and addrlen = %u\n",
86            *i, address->address_length);
87 #endif
88   GNUNET_assert (address->address_length > 0);
89   if (*i & (1 << (address->address_length - 1)))
90   {
91     *i -= (1 << (address->address_length - 1));
92     return GNUNET_NO;
93   }
94   return GNUNET_OK;
95 }
96
97
98 int
99 main (int argc, char *argv[])
100 {
101   struct GNUNET_HELLO_Message *msg1;
102   struct GNUNET_HELLO_Message *msg2;
103   struct GNUNET_HELLO_Message *msg3;
104   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
105   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
106   struct GNUNET_TIME_Absolute startup_time;
107   unsigned int i;
108
109   GNUNET_log_setup ("test-hello", "DEBUG", NULL);
110   startup_time = GNUNET_TIME_absolute_get ();
111   memset (&publicKey, 42, sizeof (publicKey));
112 #if VERBOSE
113   FPRINTF (stderr, "%s",  "Testing HELLO creation (without addresses)...\n");
114 #endif
115   i = 0;
116   msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
117   GNUNET_assert (msg1 != NULL);
118   GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
119
120 #if VERBOSE
121   FPRINTF (stderr, "%s",  "Testing address iteration (empty set)...\n");
122 #endif
123   GNUNET_assert (NULL ==
124                  GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
125                                                  &i));
126
127 #if VERBOSE
128   FPRINTF (stderr, "%s",  "Testing HELLO creation (with one address)...\n");
129 #endif
130   i = 1;
131   msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
132   GNUNET_assert (msg2 != NULL);
133   GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
134
135 #if VERBOSE
136   FPRINTF (stderr, "%s",  "Testing address iteration (one address)...\n");
137 #endif
138   i = 1;
139   GNUNET_assert (NULL ==
140                  GNUNET_HELLO_iterate_addresses (msg2, GNUNET_NO, &check_addr,
141                                                  &i));
142   GNUNET_assert (i == 0);
143
144 #if VERBOSE
145   FPRINTF (stderr, "%s",  "Testing get_key from HELLO...\n");
146 #endif
147   GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (msg2, &pk));
148   GNUNET_assert (0 == memcmp (&publicKey, &pk, sizeof (pk)));
149   GNUNET_free (msg1);
150
151 #if VERBOSE
152   FPRINTF (stderr, "%s",  "Testing HELLO creation (with two addresses)...\n");
153 #endif
154   i = 2;
155   msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
156   GNUNET_assert (msg3 != NULL);
157   GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
158
159 #if VERBOSE
160   FPRINTF (stderr, "%s",  "Testing address iteration (two addresses)...\n");
161 #endif
162   i = 3;
163   GNUNET_assert (NULL ==
164                  GNUNET_HELLO_iterate_addresses (msg3, GNUNET_NO, &check_addr,
165                                                  &i));
166   GNUNET_assert (i == 0);
167
168 #if VERBOSE
169   FPRINTF (stderr, "%s",  "Testing HELLO merge...\n");
170 #endif
171   msg1 = GNUNET_HELLO_merge (msg2, msg3);
172   GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
173
174   i = 3;
175   GNUNET_assert (NULL ==
176                  GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
177                                                  &i));
178   GNUNET_assert (i == 0);
179   GNUNET_free (msg1);
180
181 #if VERBOSE
182   FPRINTF (stderr, "%s",  "Testing address iteration to copy HELLO...\n");
183 #endif
184   i = 2;
185   msg1 = GNUNET_HELLO_iterate_addresses (msg3, GNUNET_YES, &remove_some, &i);
186   GNUNET_assert (msg1 != NULL);
187   GNUNET_assert (i == 0);
188   i = 1;
189   GNUNET_assert (NULL ==
190                  GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
191                                                  &i));
192   GNUNET_assert (i == 0);
193   GNUNET_free (msg1);
194
195 #if VERBOSE
196   FPRINTF (stderr, "%s",  "Testing delta address iteration...\n");
197 #endif
198   i = 2;
199   GNUNET_HELLO_iterate_new_addresses (msg3, msg2, startup_time, &check_addr,
200                                       &i);
201   GNUNET_assert (i == 0);
202   GNUNET_free (msg2);
203   GNUNET_free (msg3);
204   return 0;                     /* testcase passed */
205 }