fix
[oweals/gnunet.git] / src / rps / test_service_rps_sampler_elem.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C)
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file rps/test_service_rps_sampler_elem.c
20  * @brief testcase for gnunet-service-rps_sampler_elem.c
21  */
22 #include <platform.h>
23 #include "gnunet_util_lib.h"
24 #include "gnunet-service-rps_sampler_elem.h"
25
26 #define ABORT() { fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); return 1; }
27 #define CHECK(c) { if (! (c)) ABORT(); }
28
29
30 static int
31 check ()
32 {
33   struct GNUNET_PeerIdentity pid0;
34   struct GNUNET_PeerIdentity pid1;
35   struct RPS_SamplerElement *s_elem;
36   struct GNUNET_CRYPTO_AuthKey auth_key;
37   struct GNUNET_CRYPTO_AuthKey auth_key2;
38   struct GNUNET_HashCode hash_code;
39   struct GNUNET_HashCode hash_code2;
40
41   memset (&pid0, 1, sizeof (pid0));
42   memset (&pid1, 0, sizeof (pid1));
43
44   /* Check if creation and destruction of an
45    * (empty) sampler element works */
46   s_elem = RPS_sampler_elem_create ();
47   CHECK (NULL != s_elem);
48   CHECK (EMPTY == s_elem->is_empty);
49   CHECK (NULL != &s_elem->auth_key);
50   auth_key = s_elem->auth_key;
51   RPS_sampler_elem_destroy (s_elem);
52
53
54   /* Check creation of another sampler element
55    * yields another (random) key */
56   s_elem = RPS_sampler_elem_create ();
57   CHECK (NULL != s_elem);
58   CHECK (EMPTY == s_elem->is_empty);
59   CHECK (NULL != &s_elem->auth_key);
60   CHECK (auth_key.key != s_elem->auth_key.key);
61   CHECK (0 != memcmp (auth_key.key, s_elem->auth_key.key, GNUNET_CRYPTO_HASH_LENGTH));
62   auth_key = s_elem->auth_key;
63
64   /* Check that reinitialisation
65    * yields another (random) key */
66   RPS_sampler_elem_reinit (s_elem);
67   CHECK (NULL != s_elem);
68   CHECK (EMPTY == s_elem->is_empty);
69   CHECK (NULL != &s_elem->auth_key);
70   CHECK (auth_key.key != s_elem->auth_key.key);
71   CHECK (0 != memcmp (auth_key.key, s_elem->auth_key.key, GNUNET_CRYPTO_HASH_LENGTH));
72   RPS_sampler_elem_destroy (s_elem);
73
74
75   /* Check that input of single peer id
76    * sets valid values */
77   s_elem = RPS_sampler_elem_create ();
78   CHECK (EMPTY == s_elem->is_empty);
79   CHECK (NULL != &s_elem->auth_key);
80   CHECK (auth_key.key != s_elem->auth_key.key);
81   /* This fails only with minimal chance */
82   CHECK (0 != memcmp (auth_key.key, s_elem->auth_key.key, GNUNET_CRYPTO_HASH_LENGTH));
83   auth_key = s_elem->auth_key;
84
85   /* Check also that the hash of the peer id changed
86    * Also fails with minimal probability */
87   hash_code = s_elem->peer_id_hash;
88   RPS_sampler_elem_next (s_elem, &pid0);
89   CHECK (0 == memcmp (&pid0,
90                       &s_elem->peer_id,
91                       sizeof (struct GNUNET_PeerIdentity)));
92   CHECK (0 != memcmp (&hash_code,
93                       &s_elem->peer_id_hash,
94                       sizeof (struct GNUNET_HashCode)));
95   hash_code = s_elem->peer_id_hash;
96
97   /* We can only check that the peer id is one of both inputs */
98   RPS_sampler_elem_next (s_elem, &pid1);
99   CHECK ( (0 == memcmp (&pid0,
100                         &s_elem->peer_id,
101                         sizeof (struct GNUNET_PeerIdentity))) ||
102           (0 == memcmp (&pid1,
103                         &s_elem->peer_id,
104                         sizeof (struct GNUNET_PeerIdentity))) );
105
106   /* Check that hash stayed the same when peer id did not change */
107   if (0 == memcmp (&pid0,
108                    &s_elem->peer_id,
109                    sizeof (struct GNUNET_PeerIdentity)))
110   {
111     CHECK (0 == memcmp (&hash_code,
112                         &s_elem->peer_id_hash,
113                         sizeof (struct GNUNET_HashCode)));
114   }
115   else /* Check that hash changed */
116   {
117     CHECK (0 != memcmp (&hash_code,
118                         &s_elem->peer_id_hash,
119                         sizeof (struct GNUNET_HashCode)));
120   }
121
122   /* Check multiple inputs of same id
123    * hash should not change anymore */
124   hash_code2 = s_elem->peer_id_hash;
125   RPS_sampler_elem_next (s_elem, &pid0);
126   CHECK (0 == memcmp (&hash_code2,
127                       &s_elem->peer_id_hash,
128                       sizeof (struct GNUNET_HashCode)));
129   RPS_sampler_elem_next (s_elem, &pid1);
130   CHECK (0 == memcmp (&hash_code2,
131                       &s_elem->peer_id_hash,
132                       sizeof (struct GNUNET_HashCode)));
133   RPS_sampler_elem_next (s_elem, &pid0);
134   CHECK (0 == memcmp (&hash_code2,
135                       &s_elem->peer_id_hash,
136                       sizeof (struct GNUNET_HashCode)));
137   RPS_sampler_elem_next (s_elem, &pid0);
138   CHECK (0 == memcmp (&hash_code2,
139                       &s_elem->peer_id_hash,
140                       sizeof (struct GNUNET_HashCode)));
141   RPS_sampler_elem_next (s_elem, &pid0);
142   CHECK (0 == memcmp (&hash_code2,
143                       &s_elem->peer_id_hash,
144                       sizeof (struct GNUNET_HashCode)));
145   RPS_sampler_elem_next (s_elem, &pid1);
146   CHECK (0 == memcmp (&hash_code2,
147                       &s_elem->peer_id_hash,
148                       sizeof (struct GNUNET_HashCode)));
149   RPS_sampler_elem_next (s_elem, &pid1);
150   CHECK (0 == memcmp (&hash_code2,
151                       &s_elem->peer_id_hash,
152                       sizeof (struct GNUNET_HashCode)));
153   RPS_sampler_elem_next (s_elem, &pid1);
154   CHECK (0 == memcmp (&hash_code2,
155                       &s_elem->peer_id_hash,
156                       sizeof (struct GNUNET_HashCode)));
157
158   /* Check whether pid stayed the same all the time */
159   if (0 == memcmp (&hash_code,
160                    &hash_code2,
161                    sizeof (struct GNUNET_HashCode)))
162   {
163     CHECK (0 == memcmp (&pid0,
164                         &s_elem->peer_id,
165                         sizeof (struct GNUNET_PeerIdentity)));
166   }
167   else
168   {
169     CHECK (0 == memcmp (&pid1,
170                         &s_elem->peer_id,
171                         sizeof (struct GNUNET_PeerIdentity)));
172   }
173   RPS_sampler_elem_destroy (s_elem);
174
175   /* Check _set() */
176   s_elem = RPS_sampler_elem_create ();
177   CHECK (NULL != s_elem);
178   CHECK (EMPTY == s_elem->is_empty);
179   CHECK (NULL != &s_elem->auth_key);
180   auth_key = s_elem->auth_key;
181   memset (&auth_key2, 0, sizeof (auth_key2));
182   RPS_sampler_elem_set (s_elem, auth_key2);
183   CHECK (0 == memcmp (auth_key2.key,
184                       s_elem->auth_key.key,
185                       GNUNET_CRYPTO_HASH_LENGTH));
186   RPS_sampler_elem_destroy (s_elem);
187
188
189   /* TODO: deterministic tests (use _set() to set auth_key) */
190   return 0;
191 }
192
193
194 int
195 main (int argc, char *argv[])
196 {
197   GNUNET_log_setup ("test_service_rps_peers", 
198                     "WARNING",
199                     NULL);
200   return check ();
201 }
202
203 /* end of test_service_rps_peers.c */