gpl3
[oweals/gnunet.git] / src / hello / test_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 #define DEBUG GNUNET_NO
29
30 #define VERBOSE GNUNET_NO
31
32
33 static size_t
34 my_addr_gen (void *cls, size_t max, void *buf)
35 {
36   unsigned int *i = cls;
37   size_t ret;
38
39 #if DEBUG
40   fprintf (stderr, "DEBUG: my_addr_gen called with i = %d\n", *i);
41 #endif
42   if (0 == *i)
43     return 0;
44   ret = GNUNET_HELLO_add_address ("test",
45                                   GNUNET_TIME_absolute_get (),
46                                   "address_information", *i, buf, max);
47   (*i)--;
48   return ret;
49 }
50
51
52 static int
53 check_addr (void *cls,
54             const char *tname,
55             struct GNUNET_TIME_Absolute expiration,
56             const void *addr, uint16_t addrlen)
57 {
58   unsigned int *i = cls;
59
60 #if DEBUG
61   fprintf (stderr, "DEBUG: check_addr called with i = %d and addrlen = %u\n",
62            *i, addrlen);
63 #endif
64   GNUNET_assert (addrlen > 0);
65   GNUNET_assert (*i & (1 << (addrlen - 1)));
66   *i -= (1 << (addrlen - 1));
67   GNUNET_assert (0 == strncmp ("address_information", addr, addrlen));
68   GNUNET_assert (0 == strcmp ("test", tname));
69   return GNUNET_OK;
70 }
71
72
73 static int
74 remove_some (void *cls,
75              const char *tname,
76              struct GNUNET_TIME_Absolute expiration,
77              const void *addr, uint16_t addrlen)
78 {
79   unsigned int *i = cls;
80
81 #if DEBUG
82   fprintf (stderr, "DEBUG: remove_some called with i = %d and addrlen = %u\n",
83            *i, addrlen);
84 #endif
85   GNUNET_assert (addrlen > 0);
86   if (*i & (1 << (addrlen - 1)))
87     {
88       *i -= (1 << (addrlen - 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_RsaPublicKeyBinaryEncoded publicKey;
102   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
103   struct GNUNET_TIME_Absolute startup_time;
104   unsigned int i;
105
106   GNUNET_log_setup ("test-hello", "DEBUG", NULL);
107   startup_time = GNUNET_TIME_absolute_get ();
108   memset (&publicKey, 42, sizeof (publicKey));
109 #if VERBOSE
110   fprintf (stderr, "Testing HELLO creation (without addresses)...\n");
111 #endif
112   i = 0;
113   msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
114   GNUNET_assert (msg1 != NULL);
115   GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
116
117 #if VERBOSE
118   fprintf (stderr, "Testing address iteration (empty set)...\n");
119 #endif
120   GNUNET_assert (NULL ==
121                  GNUNET_HELLO_iterate_addresses (msg1,
122                                                  GNUNET_NO, &check_addr, &i));
123
124 #if VERBOSE
125   fprintf (stderr, "Testing HELLO creation (with one address)...\n");
126 #endif
127   i = 1;
128   msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
129   GNUNET_assert (msg2 != NULL);
130   GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
131
132 #if VERBOSE
133   fprintf (stderr, "Testing address iteration (one address)...\n");
134 #endif
135   i = 1;
136   GNUNET_assert (NULL ==
137                  GNUNET_HELLO_iterate_addresses (msg2,
138                                                  GNUNET_NO, &check_addr, &i));
139   GNUNET_assert (i == 0);
140
141 #if VERBOSE
142   fprintf (stderr, "Testing get_key from HELLO...\n");
143 #endif
144   GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (msg2, &pk));
145   GNUNET_assert (0 == memcmp (&publicKey, &pk, sizeof (pk)));
146   GNUNET_free (msg1);
147
148 #if VERBOSE
149   fprintf (stderr, "Testing HELLO creation (with two addresses)...\n");
150 #endif
151   i = 2;
152   msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
153   GNUNET_assert (msg3 != NULL);
154   GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
155
156 #if VERBOSE
157   fprintf (stderr, "Testing address iteration (two addresses)...\n");
158 #endif
159   i = 3;
160   GNUNET_assert (NULL ==
161                  GNUNET_HELLO_iterate_addresses (msg3,
162                                                  GNUNET_NO, &check_addr, &i));
163   GNUNET_assert (i == 0);
164
165 #if VERBOSE
166   fprintf (stderr, "Testing HELLO merge...\n");
167 #endif
168   msg1 = GNUNET_HELLO_merge (msg2, msg3);
169   GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
170
171   i = 3;
172   GNUNET_assert (NULL ==
173                  GNUNET_HELLO_iterate_addresses (msg1,
174                                                  GNUNET_NO, &check_addr, &i));
175   GNUNET_assert (i == 0);
176   GNUNET_free (msg1);
177
178 #if VERBOSE
179   fprintf (stderr, "Testing address iteration to copy HELLO...\n");
180 #endif
181   i = 2;
182   msg1 = GNUNET_HELLO_iterate_addresses (msg3, GNUNET_YES, &remove_some, &i);
183   GNUNET_assert (msg1 != NULL);
184   GNUNET_assert (i == 0);
185   i = 1;
186   GNUNET_assert (NULL ==
187                  GNUNET_HELLO_iterate_addresses (msg1,
188                                                  GNUNET_NO, &check_addr, &i));
189   GNUNET_assert (i == 0);
190   GNUNET_free (msg1);
191
192 #if VERBOSE
193   fprintf (stderr, "Testing delta address iteration...\n");
194 #endif
195   i = 2;
196   GNUNET_HELLO_iterate_new_addresses (msg3,
197                                       msg2, startup_time, &check_addr, &i);
198   GNUNET_assert (i == 0);
199   GNUNET_free (msg2);
200   GNUNET_free (msg3);
201   return 0;                     /* testcase passed */
202 }