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