paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / hello / test_hello.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2009, 2015 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file hello/test_hello.c
20  * @brief test for hello.c
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_hello_lib.h"
25
26
27 /**
28  *
29  *
30  * @param cls
31  * @param max
32  * @param buf
33  * @return
34  */
35 static ssize_t
36 my_addr_gen (void *cls,
37              size_t max,
38              void *buf)
39 {
40   unsigned int *i = cls;
41   size_t ret;
42   struct GNUNET_HELLO_Address address;
43
44   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
45               "DEBUG: my_addr_gen called with i = %d\n",
46               *i);
47   if (0 == *i)
48     return GNUNET_SYSERR;
49   memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
50   address.address = "address_information";
51   address.transport_name = "test";
52   address.address_length = *i;
53   ret = GNUNET_HELLO_add_address (&address,
54                                   GNUNET_TIME_absolute_get (),
55                                   buf,
56                                   max);
57   (*i)--;
58   return ret;
59 }
60
61
62 /**
63  *
64  *
65  * @param cls
66  * @param address
67  * @param expiration
68  * @return
69  */
70 static int
71 check_addr (void *cls,
72             const struct GNUNET_HELLO_Address *address,
73             struct GNUNET_TIME_Absolute expiration)
74 {
75   unsigned int *i = cls;
76
77   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
78               "DEBUG: check_addr called with i = %d and addrlen = %u\n",
79               *i,
80               (unsigned int) address->address_length);
81   GNUNET_assert (address->address_length > 0);
82   GNUNET_assert (*i & (1 << (address->address_length - 1)));
83   *i -= (1 << (address->address_length - 1));
84   GNUNET_assert (0 ==
85                  strncmp ("address_information",
86                           address->address,
87                           address->address_length));
88   GNUNET_assert (0 == strcmp ("test",
89                               address->transport_name));
90   return GNUNET_OK;
91 }
92
93
94 /**
95  *
96  *
97  * @param cls
98  * @param address
99  * @param expiration
100  * @return
101  */
102 static int
103 remove_some (void *cls,
104              const struct GNUNET_HELLO_Address *address,
105              struct GNUNET_TIME_Absolute expiration)
106 {
107   unsigned int *i = cls;
108
109   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
110            "DEBUG: remove_some called with i = %d and addrlen = %u\n",
111            *i,
112               (unsigned int) address->address_length);
113   GNUNET_assert (address->address_length > 0);
114   if (*i & (1 << (address->address_length - 1)))
115   {
116     *i -= (1 << (address->address_length - 1));
117     return GNUNET_NO;
118   }
119   return GNUNET_OK;
120 }
121
122
123 int
124 main (int argc,
125       char *argv[])
126 {
127   struct GNUNET_HELLO_Message *msg1;
128   struct GNUNET_HELLO_Message *msg2;
129   struct GNUNET_HELLO_Message *msg3;
130   struct GNUNET_CRYPTO_EddsaPublicKey publicKey;
131   struct GNUNET_PeerIdentity pid;
132   struct GNUNET_TIME_Absolute startup_time;
133   unsigned int i;
134
135   GNUNET_log_setup ("test-hello",
136                     "DEBUG",
137                     NULL);
138   startup_time = GNUNET_TIME_absolute_get ();
139   memset (&publicKey, 42, sizeof (publicKey));
140   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
141               "Testing HELLO creation (without addresses)...\n");
142   i = 0;
143   msg1 = GNUNET_HELLO_create (&publicKey,
144                               &my_addr_gen,
145                               &i,
146                               GNUNET_NO);
147   GNUNET_assert (msg1 != NULL);
148   GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
149
150   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151               "Testing address iteration (empty set)...\n");
152   GNUNET_assert (NULL ==
153                  GNUNET_HELLO_iterate_addresses (msg1,
154                                                  GNUNET_NO,
155                                                  &check_addr,
156                                                  &i));
157   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158            "Testing HELLO creation (with one address)...\n");
159   i = 1;
160   msg2 = GNUNET_HELLO_create (&publicKey,
161                               &my_addr_gen,
162                               &i,
163                               GNUNET_NO);
164   GNUNET_assert (msg2 != NULL);
165   GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
166
167   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168            "Testing address iteration (one address)...\n");
169   i = 1;
170   GNUNET_assert (NULL ==
171                  GNUNET_HELLO_iterate_addresses (msg2,
172                                                  GNUNET_NO,
173                                                  &check_addr,
174                                                  &i));
175   GNUNET_assert (i == 0);
176
177   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178            "Testing get_key from HELLO...\n");
179   GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (msg2, &pid));
180   GNUNET_assert (0 == memcmp (&publicKey,
181                               &pid.public_key,
182                               sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)));
183   GNUNET_free (msg1);
184
185   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186            "Testing HELLO creation (with two addresses)...\n");
187   i = 2;
188   msg3 = GNUNET_HELLO_create (&publicKey,
189                               &my_addr_gen,
190                               &i,
191                               GNUNET_NO);
192   GNUNET_assert (msg3 != NULL);
193   GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
194
195   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196            "Testing address iteration (two addresses)...\n");
197   i = 3;
198   GNUNET_assert (NULL ==
199                  GNUNET_HELLO_iterate_addresses (msg3,
200                                                  GNUNET_NO,
201                                                  &check_addr,
202                                                  &i));
203   GNUNET_assert (i == 0);
204
205   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206            "Testing HELLO merge...\n");
207   msg1 = GNUNET_HELLO_merge (msg2, msg3);
208   GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
209
210   i = 3;
211   GNUNET_assert (NULL ==
212                  GNUNET_HELLO_iterate_addresses (msg1,
213                                                  GNUNET_NO,
214                                                  &check_addr,
215                                                  &i));
216   GNUNET_assert (i == 0);
217   GNUNET_free (msg1);
218
219   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220            "Testing address iteration to copy HELLO...\n");
221   i = 2;
222   msg1 = GNUNET_HELLO_iterate_addresses (msg3,
223                                          GNUNET_YES,
224                                          &remove_some,
225                                          &i);
226   GNUNET_assert (msg1 != NULL);
227   GNUNET_assert (i == 0);
228   i = 1;
229   GNUNET_assert (NULL ==
230                  GNUNET_HELLO_iterate_addresses (msg1,
231                                                  GNUNET_NO,
232                                                  &check_addr,
233                                                  &i));
234   GNUNET_assert (i == 0);
235   GNUNET_free (msg1);
236
237   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238            "Testing delta address iteration...\n");
239   i = 2;
240   GNUNET_HELLO_iterate_new_addresses (msg3,
241                                       msg2,
242                                       startup_time,
243                                       &check_addr,
244                                       &i);
245   GNUNET_assert (i == 0);
246   GNUNET_free (msg2);
247   GNUNET_free (msg3);
248   return 0;                     /* testcase passed */
249 }
250
251 /* end of test_hello.c */