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