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