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