indentation, comment and style fixes, no semantic changes
[oweals/gnunet.git] / src / hello / test_friend_hello.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2009 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_friend_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 static ssize_t
30 my_addr_gen (void *cls,
31              size_t max,
32              void *buf)
33 {
34   unsigned int *i = cls;
35   size_t ret;
36   struct GNUNET_HELLO_Address address;
37
38   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
39               "DEBUG: my_addr_gen called with i = %d\n", *i);
40   if (0 == *i)
41     return GNUNET_SYSERR; /* Stop iteration */
42   memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
43   address.address = "address_information";
44   address.transport_name = "test";
45   address.address_length = *i;
46   ret =
47       GNUNET_HELLO_add_address (&address, GNUNET_TIME_absolute_get (), buf,
48                                 max);
49   (*i)--;
50   return ret;
51 }
52
53
54 static int
55 check_addr (void *cls,
56             const struct GNUNET_HELLO_Address *address,
57             struct GNUNET_TIME_Absolute expiration)
58 {
59   unsigned int *i = cls;
60
61   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
62            "DEBUG: check_addr called with i = %d and addrlen = %u\n",
63            *i, (unsigned int) address->address_length);
64   GNUNET_assert (address->address_length > 0);
65   GNUNET_assert (*i & (1 << (address->address_length - 1)));
66   *i -= (1 << (address->address_length - 1));
67   GNUNET_assert (0 ==
68                  strncmp ("address_information", address->address,
69                           address->address_length));
70   GNUNET_assert (0 == strcmp ("test", address->transport_name));
71   return GNUNET_OK;
72 }
73
74
75 static int
76 remove_some (void *cls,
77              const struct GNUNET_HELLO_Address *address,
78              struct GNUNET_TIME_Absolute expiration)
79 {
80   unsigned int *i = cls;
81
82   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
83            "DEBUG: remove_some called with i = %d and addrlen = %u\n",
84            *i, (unsigned int) address->address_length);
85   GNUNET_assert (address->address_length > 0);
86   if (*i & (1 << (address->address_length - 1)))
87   {
88     *i -= (1 << (address->address_length - 1));
89     return GNUNET_NO;
90   }
91   return GNUNET_OK;
92 }
93
94
95 int
96 main (int argc, char *argv[])
97 {
98   struct GNUNET_HELLO_Message *msg1;
99   struct GNUNET_HELLO_Message *msg2;
100   struct GNUNET_HELLO_Message *msg3;
101   struct GNUNET_CRYPTO_EddsaPublicKey publicKey;
102   struct GNUNET_TIME_Absolute startup_time;
103   unsigned int i;
104
105   GNUNET_log_setup ("test-hello", "DEBUG", NULL);
106   startup_time = GNUNET_TIME_absolute_get ();
107   memset (&publicKey, 42, sizeof (publicKey));
108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109               "Testing HELLO creation (without addresses)...\n");
110   i = 0;
111   msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
112   GNUNET_assert (msg1 != NULL);
113   GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
114
115   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116               "Testing address iteration (empty set)...\n");
117   GNUNET_assert (NULL ==
118                  GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
119                                                  &i));
120   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
121            "Testing HELLO creation (with one address)...\n");
122   i = 1;
123   msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
124   GNUNET_assert (msg2 != NULL);
125   GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
126
127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128            "Testing address iteration (one address)...\n");
129   i = 1;
130   GNUNET_assert (NULL ==
131                  GNUNET_HELLO_iterate_addresses (msg2, GNUNET_NO, &check_addr,
132                                                  &i));
133   GNUNET_assert (i == 0);
134   GNUNET_free (msg1);
135
136   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137            "Testing HELLO creation (with two addresses)...\n");
138   i = 2;
139   msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
140   GNUNET_assert (msg3 != NULL);
141   GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
142
143   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
144            "Testing address iteration (two addresses)...\n");
145   i = 3;
146   GNUNET_assert (NULL ==
147                  GNUNET_HELLO_iterate_addresses (msg3, GNUNET_NO, &check_addr,
148                                                  &i));
149   GNUNET_assert (i == 0);
150
151   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152            "Testing HELLO merge...\n");
153   msg1 = GNUNET_HELLO_merge (msg2, msg3);
154   GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
155
156   i = 3;
157   GNUNET_assert (NULL ==
158                  GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
159                                                  &i));
160   GNUNET_assert (i == 0);
161   GNUNET_free (msg1);
162
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164            "Testing address iteration to copy HELLO...\n");
165   i = 2;
166   msg1 = GNUNET_HELLO_iterate_addresses (msg3, GNUNET_YES, &remove_some, &i);
167   GNUNET_assert (msg1 != NULL);
168   GNUNET_assert (i == 0);
169   i = 1;
170   GNUNET_assert (NULL ==
171                  GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
172                                                  &i));
173   GNUNET_assert (i == 0);
174   GNUNET_free (msg1);
175
176   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177            "Testing delta address iteration...\n");
178   i = 2;
179   GNUNET_HELLO_iterate_new_addresses (msg3, msg2, startup_time, &check_addr,
180                                       &i);
181   GNUNET_assert (i == 0);
182   GNUNET_free (msg2);
183   GNUNET_free (msg3);
184   return 0;                     /* testcase passed */
185 }