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