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