2 This file is part of GNUnet.
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 3, or (at your
8 option) any later version.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file rps/test_service_rps_sampler_elem.c
22 * @brief testcase for gnunet-service-rps_sampler_elem.c
25 #include "gnunet_util_lib.h"
26 #include "gnunet-service-rps_sampler_elem.h"
28 #define ABORT() { fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); return 1; }
29 #define CHECK(c) { if (! (c)) ABORT(); }
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;
43 memset (&pid0, 1, sizeof (pid0));
44 memset (&pid1, 0, sizeof (pid1));
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);
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;
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);
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;
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,
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;
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,
103 sizeof (struct GNUNET_PeerIdentity))) ||
106 sizeof (struct GNUNET_PeerIdentity))) );
108 /* Check that hash stayed the same when peer id did not change */
109 if (0 == memcmp (&pid0,
111 sizeof (struct GNUNET_PeerIdentity)))
113 CHECK (0 == memcmp (&hash_code,
114 &s_elem->peer_id_hash,
115 sizeof (struct GNUNET_HashCode)));
117 else /* Check that hash changed */
119 CHECK (0 != memcmp (&hash_code,
120 &s_elem->peer_id_hash,
121 sizeof (struct GNUNET_HashCode)));
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)));
160 /* Check whether pid stayed the same all the time */
161 if (0 == memcmp (&hash_code,
163 sizeof (struct GNUNET_HashCode)))
165 CHECK (0 == memcmp (&pid0,
167 sizeof (struct GNUNET_PeerIdentity)));
171 CHECK (0 == memcmp (&pid1,
173 sizeof (struct GNUNET_PeerIdentity)));
175 RPS_sampler_elem_destroy (s_elem);
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);
191 /* TODO: deterministic tests (use _set() to set auth_key) */
197 main (int argc, char *argv[])
199 GNUNET_log_setup ("test_service_rps_peers",
205 /* end of test_service_rps_peers.c */