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