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