51da7fc75645eb49670c4b015b14aa4d484356f0
[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   int ok;
102   unsigned int i;
103
104   GNUNET_log_setup ("test-hello", "DEBUG", NULL);
105   startup_time = GNUNET_TIME_absolute_get ();
106   ok = 0;
107   memset (&publicKey, 42, sizeof (publicKey));
108   fprintf (stderr, "Testing HELLO creation (without addresses)...\n");
109   i = 0;
110   msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
111   GNUNET_assert (msg1 != NULL);
112   GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
113
114   fprintf (stderr, "Testing address iteration (empty set)...\n");
115   GNUNET_assert (NULL ==
116                  GNUNET_HELLO_iterate_addresses (msg1,
117                                                  GNUNET_NO, &check_addr, &i));
118
119   fprintf (stderr, "Testing HELLO creation (with one address)...\n");
120   i = 1;
121   msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
122   GNUNET_assert (msg2 != NULL);
123   GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
124
125   fprintf (stderr, "Testing address iteration (one address)...\n");
126   i = 1;
127   GNUNET_assert (NULL ==
128                  GNUNET_HELLO_iterate_addresses (msg2,
129                                                  GNUNET_NO, &check_addr, &i));
130   GNUNET_assert (i == 0);
131
132   fprintf (stderr, "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   fprintf (stderr, "Testing HELLO creation (with two addresses)...\n");
138   i = 2;
139   msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
140   GNUNET_assert (msg3 != NULL);
141   GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
142
143   fprintf (stderr, "Testing address iteration (two addresses)...\n");
144   i = 3;
145   GNUNET_assert (NULL ==
146                  GNUNET_HELLO_iterate_addresses (msg3,
147                                                  GNUNET_NO, &check_addr, &i));
148   GNUNET_assert (i == 0);
149
150   fprintf (stderr, "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,
157                                                  GNUNET_NO, &check_addr, &i));
158   GNUNET_assert (i == 0);
159   GNUNET_free (msg1);
160
161   fprintf (stderr, "Testing address iteration to copy HELLO...\n");
162   i = 2;
163   msg1 = GNUNET_HELLO_iterate_addresses (msg3, GNUNET_YES, &remove_some, &i);
164   GNUNET_assert (msg1 != NULL);
165   GNUNET_assert (i == 0);
166   i = 1;
167   GNUNET_assert (NULL ==
168                  GNUNET_HELLO_iterate_addresses (msg1,
169                                                  GNUNET_NO, &check_addr, &i));
170   GNUNET_assert (i == 0);
171   GNUNET_free (msg1);
172
173   fprintf (stderr, "Testing delta address iteration...\n");
174   i = 2;
175   GNUNET_HELLO_iterate_new_addresses (msg3,
176                                       msg2, startup_time, &check_addr, &i);
177   GNUNET_assert (i == 0);
178   GNUNET_free (msg2);
179   GNUNET_free (msg3);
180   return 0;                     /* testcase passed */
181 }