error handling
[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__); \
29                   return 1; }
30 #define CHECK(c) { if (! (c)) ABORT (); }
31
32
33 static int
34 check ()
35 {
36   struct GNUNET_PeerIdentity pid0;
37   struct GNUNET_PeerIdentity pid1;
38   struct RPS_SamplerElement *s_elem;
39   struct GNUNET_CRYPTO_AuthKey auth_key;
40   struct GNUNET_CRYPTO_AuthKey auth_key2;
41   struct GNUNET_HashCode hash_code;
42   struct GNUNET_HashCode hash_code2;
43
44   memset (&pid0, 1, sizeof(pid0));
45   memset (&pid1, 0, sizeof(pid1));
46
47   /* Check if creation and destruction of an
48    * (empty) sampler element works */
49   s_elem = RPS_sampler_elem_create ();
50   CHECK (NULL != s_elem);
51   CHECK (EMPTY == s_elem->is_empty);
52   CHECK (NULL != &s_elem->auth_key);
53   auth_key = s_elem->auth_key;
54   RPS_sampler_elem_destroy (s_elem);
55
56
57   /* Check creation of another sampler element
58    * yields another (random) key */
59   s_elem = RPS_sampler_elem_create ();
60   CHECK (NULL != s_elem);
61   CHECK (EMPTY == s_elem->is_empty);
62   CHECK (NULL != &s_elem->auth_key);
63   CHECK (auth_key.key != s_elem->auth_key.key);
64   CHECK (0 != memcmp (auth_key.key, s_elem->auth_key.key,
65                       GNUNET_CRYPTO_HASH_LENGTH));
66   auth_key = s_elem->auth_key;
67
68   /* Check that reinitialisation
69    * yields another (random) key */
70   RPS_sampler_elem_reinit (s_elem);
71   CHECK (NULL != s_elem);
72   CHECK (EMPTY == s_elem->is_empty);
73   CHECK (NULL != &s_elem->auth_key);
74   CHECK (auth_key.key != s_elem->auth_key.key);
75   CHECK (0 != memcmp (auth_key.key, s_elem->auth_key.key,
76                       GNUNET_CRYPTO_HASH_LENGTH));
77   RPS_sampler_elem_destroy (s_elem);
78
79
80   /* Check that input of single peer id
81    * sets valid values */
82   s_elem = RPS_sampler_elem_create ();
83   CHECK (EMPTY == s_elem->is_empty);
84   CHECK (NULL != &s_elem->auth_key);
85   CHECK (auth_key.key != s_elem->auth_key.key);
86   /* This fails only with minimal chance */
87   CHECK (0 != memcmp (auth_key.key, s_elem->auth_key.key,
88                       GNUNET_CRYPTO_HASH_LENGTH));
89   auth_key = s_elem->auth_key;
90
91   /* Check also that the hash of the peer id changed
92    * Also fails with minimal probability */
93   hash_code = s_elem->peer_id_hash;
94   RPS_sampler_elem_next (s_elem, &pid0);
95   CHECK (0 == memcmp (&pid0,
96                       &s_elem->peer_id,
97                       sizeof(struct GNUNET_PeerIdentity)));
98   CHECK (0 != memcmp (&hash_code,
99                       &s_elem->peer_id_hash,
100                       sizeof(struct GNUNET_HashCode)));
101   hash_code = s_elem->peer_id_hash;
102
103   /* We can only check that the peer id is one of both inputs */
104   RPS_sampler_elem_next (s_elem, &pid1);
105   CHECK ((0 == memcmp (&pid0,
106                        &s_elem->peer_id,
107                        sizeof(struct GNUNET_PeerIdentity))) ||
108          (0 == memcmp (&pid1,
109                        &s_elem->peer_id,
110                        sizeof(struct GNUNET_PeerIdentity))));
111
112   /* Check that hash stayed the same when peer id did not change */
113   if (0 == memcmp (&pid0,
114                    &s_elem->peer_id,
115                    sizeof(struct GNUNET_PeerIdentity)))
116   {
117     CHECK (0 == memcmp (&hash_code,
118                         &s_elem->peer_id_hash,
119                         sizeof(struct GNUNET_HashCode)));
120   }
121   else /* Check that hash changed */
122   {
123     CHECK (0 != memcmp (&hash_code,
124                         &s_elem->peer_id_hash,
125                         sizeof(struct GNUNET_HashCode)));
126   }
127
128   /* Check multiple inputs of same id
129   * hash should not change anymore */
130   hash_code2 = s_elem->peer_id_hash;
131   RPS_sampler_elem_next (s_elem, &pid0);
132   CHECK (0 == memcmp (&hash_code2,
133                       &s_elem->peer_id_hash,
134                       sizeof(struct GNUNET_HashCode)));
135   RPS_sampler_elem_next (s_elem, &pid1);
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, &pid0);
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   RPS_sampler_elem_next (s_elem, &pid1);
160   CHECK (0 == memcmp (&hash_code2,
161                       &s_elem->peer_id_hash,
162                       sizeof(struct GNUNET_HashCode)));
163
164   /* Check whether pid stayed the same all the time */
165   if (0 == memcmp (&hash_code,
166                    &hash_code2,
167                    sizeof(struct GNUNET_HashCode)))
168   {
169     CHECK (0 == memcmp (&pid0,
170                         &s_elem->peer_id,
171                         sizeof(struct GNUNET_PeerIdentity)));
172   }
173   else
174   {
175     CHECK (0 == memcmp (&pid1,
176                         &s_elem->peer_id,
177                         sizeof(struct GNUNET_PeerIdentity)));
178   }
179   RPS_sampler_elem_destroy (s_elem);
180
181   /* Check _set() */
182   s_elem = RPS_sampler_elem_create ();
183   CHECK (NULL != s_elem);
184   CHECK (EMPTY == s_elem->is_empty);
185   CHECK (NULL != &s_elem->auth_key);
186   auth_key = s_elem->auth_key;
187   memset (&auth_key2, 0, sizeof(auth_key2));
188   RPS_sampler_elem_set (s_elem, auth_key2);
189   CHECK (0 == memcmp (auth_key2.key,
190                       s_elem->auth_key.key,
191                       GNUNET_CRYPTO_HASH_LENGTH));
192   RPS_sampler_elem_destroy (s_elem);
193
194
195   /* TODO: deterministic tests (use _set() to set auth_key) */
196   return 0;
197 }
198
199
200 int
201 main (int argc, char *argv[])
202 {
203   (void) argc;
204   (void) argv;
205
206   GNUNET_log_setup ("test_service_rps_peers",
207                     "WARNING",
208                     NULL);
209   return check ();
210 }
211
212
213 /* end of test_service_rps_peers.c */