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