test_service_rps_sampler_elem.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C)
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file rps/test_service_rps_sampler_elem.c
  18. * @brief testcase for gnunet-service-rps_sampler_elem.c
  19. */
  20. #include <platform.h>
  21. #include "gnunet_util_lib.h"
  22. #include "gnunet-service-rps_sampler_elem.h"
  23. #define ABORT() { fprintf (stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
  24. return 1; }
  25. #define CHECK(c) { if (! (c)) ABORT (); }
  26. static int
  27. check ()
  28. {
  29. struct GNUNET_PeerIdentity pid0;
  30. struct GNUNET_PeerIdentity pid1;
  31. struct RPS_SamplerElement *s_elem;
  32. struct GNUNET_CRYPTO_AuthKey auth_key;
  33. struct GNUNET_CRYPTO_AuthKey auth_key2;
  34. struct GNUNET_HashCode hash_code;
  35. struct GNUNET_HashCode hash_code2;
  36. memset (&pid0, 1, sizeof(pid0));
  37. memset (&pid1, 0, sizeof(pid1));
  38. /* Check if creation and destruction of an
  39. * (empty) sampler element works */
  40. s_elem = RPS_sampler_elem_create ();
  41. CHECK (NULL != s_elem);
  42. CHECK (EMPTY == s_elem->is_empty);
  43. CHECK (NULL != &s_elem->auth_key);
  44. auth_key = s_elem->auth_key;
  45. RPS_sampler_elem_destroy (s_elem);
  46. /* Check creation of another sampler element
  47. * yields another (random) key */
  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. CHECK (auth_key.key != s_elem->auth_key.key);
  53. CHECK (0 != memcmp (auth_key.key, s_elem->auth_key.key,
  54. GNUNET_CRYPTO_HASH_LENGTH));
  55. auth_key = s_elem->auth_key;
  56. /* Check that reinitialisation
  57. * yields another (random) key */
  58. RPS_sampler_elem_reinit (s_elem);
  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,
  64. GNUNET_CRYPTO_HASH_LENGTH));
  65. RPS_sampler_elem_destroy (s_elem);
  66. /* Check that input of single peer id
  67. * sets valid values */
  68. s_elem = RPS_sampler_elem_create ();
  69. CHECK (EMPTY == s_elem->is_empty);
  70. CHECK (NULL != &s_elem->auth_key);
  71. CHECK (auth_key.key != s_elem->auth_key.key);
  72. /* This fails only with minimal chance */
  73. CHECK (0 != memcmp (auth_key.key, s_elem->auth_key.key,
  74. GNUNET_CRYPTO_HASH_LENGTH));
  75. auth_key = s_elem->auth_key;
  76. /* Check also that the hash of the peer id changed
  77. * Also fails with minimal probability */
  78. hash_code = s_elem->peer_id_hash;
  79. RPS_sampler_elem_next (s_elem, &pid0);
  80. CHECK (0 == memcmp (&pid0,
  81. &s_elem->peer_id,
  82. sizeof(struct GNUNET_PeerIdentity)));
  83. CHECK (0 != memcmp (&hash_code,
  84. &s_elem->peer_id_hash,
  85. sizeof(struct GNUNET_HashCode)));
  86. hash_code = s_elem->peer_id_hash;
  87. /* We can only check that the peer id is one of both inputs */
  88. RPS_sampler_elem_next (s_elem, &pid1);
  89. CHECK ((0 == memcmp (&pid0,
  90. &s_elem->peer_id,
  91. sizeof(struct GNUNET_PeerIdentity))) ||
  92. (0 == memcmp (&pid1,
  93. &s_elem->peer_id,
  94. sizeof(struct GNUNET_PeerIdentity))));
  95. /* Check that hash stayed the same when peer id did not change */
  96. if (0 == memcmp (&pid0,
  97. &s_elem->peer_id,
  98. sizeof(struct GNUNET_PeerIdentity)))
  99. {
  100. CHECK (0 == memcmp (&hash_code,
  101. &s_elem->peer_id_hash,
  102. sizeof(struct GNUNET_HashCode)));
  103. }
  104. else /* Check that hash changed */
  105. {
  106. CHECK (0 != memcmp (&hash_code,
  107. &s_elem->peer_id_hash,
  108. sizeof(struct GNUNET_HashCode)));
  109. }
  110. /* Check multiple inputs of same id
  111. * hash should not change anymore */
  112. hash_code2 = s_elem->peer_id_hash;
  113. RPS_sampler_elem_next (s_elem, &pid0);
  114. CHECK (0 == memcmp (&hash_code2,
  115. &s_elem->peer_id_hash,
  116. sizeof(struct GNUNET_HashCode)));
  117. RPS_sampler_elem_next (s_elem, &pid1);
  118. CHECK (0 == memcmp (&hash_code2,
  119. &s_elem->peer_id_hash,
  120. sizeof(struct GNUNET_HashCode)));
  121. RPS_sampler_elem_next (s_elem, &pid0);
  122. CHECK (0 == memcmp (&hash_code2,
  123. &s_elem->peer_id_hash,
  124. sizeof(struct GNUNET_HashCode)));
  125. RPS_sampler_elem_next (s_elem, &pid0);
  126. CHECK (0 == memcmp (&hash_code2,
  127. &s_elem->peer_id_hash,
  128. sizeof(struct GNUNET_HashCode)));
  129. RPS_sampler_elem_next (s_elem, &pid0);
  130. CHECK (0 == memcmp (&hash_code2,
  131. &s_elem->peer_id_hash,
  132. sizeof(struct GNUNET_HashCode)));
  133. RPS_sampler_elem_next (s_elem, &pid1);
  134. CHECK (0 == memcmp (&hash_code2,
  135. &s_elem->peer_id_hash,
  136. sizeof(struct GNUNET_HashCode)));
  137. RPS_sampler_elem_next (s_elem, &pid1);
  138. CHECK (0 == memcmp (&hash_code2,
  139. &s_elem->peer_id_hash,
  140. sizeof(struct GNUNET_HashCode)));
  141. RPS_sampler_elem_next (s_elem, &pid1);
  142. CHECK (0 == memcmp (&hash_code2,
  143. &s_elem->peer_id_hash,
  144. sizeof(struct GNUNET_HashCode)));
  145. /* Check whether pid stayed the same all the time */
  146. if (0 == memcmp (&hash_code,
  147. &hash_code2,
  148. sizeof(struct GNUNET_HashCode)))
  149. {
  150. CHECK (0 == memcmp (&pid0,
  151. &s_elem->peer_id,
  152. sizeof(struct GNUNET_PeerIdentity)));
  153. }
  154. else
  155. {
  156. CHECK (0 == memcmp (&pid1,
  157. &s_elem->peer_id,
  158. sizeof(struct GNUNET_PeerIdentity)));
  159. }
  160. RPS_sampler_elem_destroy (s_elem);
  161. /* Check _set() */
  162. s_elem = RPS_sampler_elem_create ();
  163. CHECK (NULL != s_elem);
  164. CHECK (EMPTY == s_elem->is_empty);
  165. CHECK (NULL != &s_elem->auth_key);
  166. auth_key = s_elem->auth_key;
  167. memset (&auth_key2, 0, sizeof(auth_key2));
  168. RPS_sampler_elem_set (s_elem, auth_key2);
  169. CHECK (0 == memcmp (auth_key2.key,
  170. s_elem->auth_key.key,
  171. GNUNET_CRYPTO_HASH_LENGTH));
  172. RPS_sampler_elem_destroy (s_elem);
  173. /* TODO: deterministic tests (use _set() to set auth_key) */
  174. return 0;
  175. }
  176. int
  177. main (int argc, char *argv[])
  178. {
  179. (void) argc;
  180. (void) argv;
  181. GNUNET_log_setup ("test_service_rps_peers",
  182. "WARNING",
  183. NULL);
  184. return check ();
  185. }
  186. /* end of test_service_rps_peers.c */