aboutsummaryrefslogtreecommitdiff
path: root/src/rps
diff options
context:
space:
mode:
authorJulius Bünger <buenger@mytum.de>2016-09-16 17:49:13 +0000
committerJulius Bünger <buenger@mytum.de>2016-09-16 17:49:13 +0000
commiteec70a836bd83508ae609c7358e3a48228932e46 (patch)
treed27b9a06f23fddfc810a396cbdd032468588783a /src/rps
parent56ba7fb37960428cb758d0dad7e0e1b423fe86f6 (diff)
downloadgnunet-eec70a836bd83508ae609c7358e3a48228932e46.tar.gz
gnunet-eec70a836bd83508ae609c7358e3a48228932e46.zip
rps: added test for sampler element
Diffstat (limited to 'src/rps')
-rw-r--r--src/rps/test_service_rps_sampler_elem.c205
1 files changed, 205 insertions, 0 deletions
diff --git a/src/rps/test_service_rps_sampler_elem.c b/src/rps/test_service_rps_sampler_elem.c
new file mode 100644
index 000000000..6f515d2c2
--- /dev/null
+++ b/src/rps/test_service_rps_sampler_elem.c
@@ -0,0 +1,205 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C)
4
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.
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 General Public License for more details.
14
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.
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
32static int
33check ()
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
196int
197main (int argc, char *argv[])
198{
199 GNUNET_log_setup ("test_service_rps_peers",
200 "WARNING",
201 NULL);
202 return check ();
203}
204
205/* end of test_service_rps_peers.c */